(四)Jira Api对接:缺陷分析和任务分析

时间:2024-02-01 11:35:38

        迭代进行期间或者结束后,在我们的测试日报或者测试报告中需要体现缺陷详细情况,甚至大家工作效率情况。本文就讨论下如何通过jira api获取缺陷信息并进行分析,同时获取需求子任务情况来了解测试和开发的工时。

  具体代码如下

 //定义数据对象
 @Data
public class ReportData {

    String name;

    int bugNum;

    int taskNum;

    double taskTime;

    String bugTate;
}
 /**
     * 获取sprint下的开发任务、测试任务以及缺陷
     *
     * @param springId
     * @return
     */
    public static Map<String, Map<String, ReportData>> getReportData(String springId) {

        Map<String, Map<String, ReportData>> result = new HashMap<>();
        //缺陷比较多时我们进行分页查询,默认一页150条数据
        JSONObject jsonObject = getIssueKey(0, springId);
        if (!JSONNull.getInstance().equals(jsonObject)) {

            //解析查询的数据
            Map<String, ReportData> testerData = new HashMap<>();//测试任务
            Map<String, ReportData> developData = new HashMap<>();//开发任务
            Map<String, ReportData> bugDetail = new HashMap<>();//缺陷等级
            Map<String, ReportData> bugReason = new HashMap<>();//缺陷原因
           //解析数据
            result = getReportData(jsonObject, testerData, developData, bugDetail, bugReason);
            //获取接口返回的缺陷总数
            int total = jsonObject.getInt("total");
            logger.info("spring上共有story" + total + "个");
            if (total > 150) {
               //如果大于150则进行分页查询
                int page = total / 150;
                if (total % 150 > 0) {
                    page = page + 1;//总页码
                }
                logger.info("spring上共有story" + page + "页");
                for (int pageIndex = 1; pageIndex < page; pageIndex++) {

                    //分页获取数据
                    JSONObject object = getIssueKey(pageIndex * 150, springId);
                    if (!JSONNull.getInstance().equals(object)) {

                        //解析数据
                        result = getReportData(object, result.get("testerGeneral"), result.get("developGeneral"), result.get("bugDetail"), result.get("bugReason"));
                    }
                }
            }
        }
        return result;
    }
     /**
     * 获取spring下的issue
     *
     * @param startAt
     * @param springId
     * @return
     */
    private static JSONObject getIssueKey(int startAt, String springId) {
        //调用jira api接口获取sprint下所有issue
        HttpClientResponse issueResponse = httpClient("get", "http://you jira address:port/rest/agile/1.0/sprint/" + springId + "/issue?maxResults=150&startAt=" + startAt, "");
        if (issueResponse != null && "200".equals(issueResponse.getStateCode())
                && issueResponse.getResponseBody() != null) {
           //直接返回查询结果
            JSONObject jsonObject = JSONObject.fromObject(issueResponse.getResponseBody().toString());
            return jsonObject;
        }

        return null;
    }
  
   /**
     * 解析数据 分别解析出测试人员情况,开发人员情况,缺陷等级概况以及缺陷原因概况
     *
     * @param jsonObject
     * @param testerData
     * @param developData
     * @param bugDetail
     * @param bugReason
     * @return
     */
    private static Map<String, Map<String, ReportData>> getReportData(JSONObject jsonObject, Map<String, ReportData> testerData, Map<String, ReportData> developData, Map<String, ReportData> bugDetail, Map<String, ReportData> bugReason) {

        Map<String, Map<String, ReportData>> result = new HashMap<>();
        JSONArray issueArray = jsonObject.getJSONArray("issues");
        if (issueArray != null && issueArray.size() > 0) {
            for (int i = 0; i < issueArray.size(); i++) {

                JSONObject issueObject = issueArray.getJSONObject(i);
                JSONObject fields = issueObject.getJSONObject("fields");
                if (!JSONNull.getInstance().equals(fields)) {
                    //获取issuetype
                    JSONObject issuetype = fields.getJSONObject("issuetype");
                    if (!JSONNull.getInstance().equals(issuetype)) {
                        //获取issue 类别
                        String issuetypeName = issuetype.getString("name");
                        //获取经办人信息
                        JSONObject assignee = fields.getJSONObject("assignee");
                        if (!JSONNull.getInstance().equals(assignee)) {
                            //获取经办人花名
                            String displayName = assignee.getString("displayName");
                            //获取完成任务的预估时间的信息,这个字段是自定义字段
                            double originalEstimate = 0;
                            JSONObject timetracking = fields.getJSONObject("timetracking");
                            if (!JSONNull.getInstance().equals(timetracking) && timetracking.size() > 0) {
                                //获取预估时间
                                originalEstimate = timetracking.getInt("originalEstimateSeconds") / 3600;
                            }                           
                          //根据任务类别进行不同的处理
                            switch (issuetypeName) {
                                case "Story"://需求
                                    break;
                                case "SubTask"://开发子任务
                                   setReportData(developData, displayName, originalEstimate, false);
                                    break;
                                case "测试子任务":测试子任务
                                    setReportData(testerData, displayName, originalEstimate, false);
                                    break;
                                case "缺陷":
                                   //获取解决结果
                                    JSONObject resolution = fields.getJSONObject("resolution");
                                    if (!JSONNull.getInstance().equals(resolution)) {
                                        //过滤掉被否决的bug
                                        if ("被否决".equals(resolution.getString("name"))) {
                                            break;
                                        }
                                    }
                                    setReportData(developData, displayName, 0, true);
                                    //获取报告人信息
                                    JSONObject reporter = fields.getJSONObject("reporter");
                                    if (!JSONNull.getInstance().equals(reporter)) {
                                        //获取报告人花名
                                        String reporterName = reporter.getString("displayName");
                                        setReportData(testerData, reporterName, 0, true);
                                    }
                                    //bug等级进行分类
                                    JSONObject priority = fields.getJSONObject("priority");
                                    setReportData(bugDetail, priority, "name");
                                    //bug原因进行分类
                                    JSONObject reason = fields.getJSONObject("customfield_11522");
                                    setReportData(bugReason, reason, "value");
                                default:
                                    break;
                            }
                        }
                    }
                }
            }
        }

        result.put("testerGeneral", testerData);
        result.put("developGeneral", developData);
        result.put("bugDetail", bugDetail);
        result.put("bugReason", bugReason);
        return result;
    }  
    /**
     * 统一设置报告信息
     *
     * @param reportDataMap
     * @param key
     * @param originalEstimate
     * @param isBug
     */
    private static void setReportData(Map<String, ReportData> reportDataMap, String key, double originalEstimate, boolean isBug) {

        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        if (reportDataMap.containsKey(key)) {
            //如果包含key
            ReportData temp = reportDataMap.get(key);
            if (originalEstimate != 0) {
                //设置任务时间,以小时计算
                temp.setTaskTime(temp.getTaskTime() + originalEstimate);
            }
            if (isBug) {
                //缺陷数量+1
                temp.setBugNum(temp.getBugNum() + 1);
            } else {
                //任务数量+1
                temp.setTaskNum(temp.getTaskNum() + 1);
            }
            //设置缺陷率
            String formatNum = decimalFormat
                    .format((float) temp.getBugNum()
                            / (temp.getTaskTime() == 0 ? 1 : temp.getTaskTime()) * 100);
            temp.setBugTate(formatNum + "%");
        } else {
            ReportData reportData = new ReportData();
            if (originalEstimate != 0) {
                //设置任务时间,以小时计算
                reportData.setTaskTime(originalEstimate);
            }
            if (isBug) {
                //缺陷数量+1
                reportData.setBugNum(1);
            } else {
                //任务数量+1
                reportData.setTaskNum(1);
            }
            //设置缺陷率
            String formatNum = decimalFormat
                    .format((float) reportData.getBugNum()
                            / (reportData.getTaskTime() == 0 ? 1 : reportData.getTaskTime()) * 100);
            reportData.setBugTate(formatNum + "%");
            reportData.setName(key);
            reportDataMap.put(key, reportData);
        }
    }

    /**
     * 统一设置报告信息
     *
     * @param dataMap
     * @param jsonObject
     * @param key
     */
    private static void setReportData(Map<String, ReportData> dataMap, JSONObject jsonObject, String key) {

        if (!JSONNull.getInstance().equals(jsonObject)) {
            //获取key值
            String priorityName = jsonObject.getString(key);
            if (dataMap.containsKey(priorityName)) {
                //数量+1
                ReportData reportData = dataMap.get(priorityName);
                reportData.setBugNum(reportData.getBugNum() + 1);
            } else {
                //数量+1
                ReportData reportData = new ReportData();
                reportData.setBugNum(1);
                reportData.setName(priorityName);
                dataMap.put(priorityName, reportData);
            }
        }
    }
//getReportData(String sprintId) 方法返回结果示例,按照Map读取成自己的格式就可以了
{
  bugReason={
    功能错误=ReportData(name=功能错误,
    bugNum=3,
    taskNum=0,
    taskTime=0.0,
    bugTate=null),
    需求问题=ReportData(name=需求问题,
    bugNum=1,
    taskNum=0,
    taskTime=0.0,
    bugTate=null)
  },
  developGeneral={
    桑落=ReportData(name=桑落,
    bugNum=3,
    taskNum=7,
    taskTime=28.0,
    bugTate=10.71%),
    白衣=ReportData(name=白衣,
    bugNum=0,
    taskNum=8,
    taskTime=18.0,
    bugTate=0.00%),
    清远=ReportData(name=清远,
    bugNum=1,
    taskNum=0,
    taskTime=0.0,
    bugTate=100.00%)
  },
  testerGeneral={
    黄台=ReportData(name=黄台,
    bugNum=4,
    taskNum=3,
    taskTime=24.0,
    bugTate=16.67%)
  },
  bugDetail={
    Medium=ReportData(name=Medium,
    bugNum=4,
    taskNum=0,
    taskTime=0.0,
    bugTate=null)
  }
}
//对结果优化后以图标显示如下图

 

 

 

 

更多文章请关注公众号