//定义数据对象
@Datapublic 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);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.