團隊的成員碰到一個問題,使用JIRA建立多個有關聯性的任務時,過於耗時。所以花了3天時間,間斷地看了一下Jira官方文檔和網上的資料。下面總結一下Jira API的使用。
Jira develop 文檔中提到了其對rest風格api的支持,同時也支持SOAP的調用。我們採用 Jira rest API的方式進行調用,鏈接中有各種調用方式,可以細細看一下。
首先思考一下我們要做什麼,可以歸納爲以下幾點:
- 一個Jira的訪問接口
- 一個Jira訪問參數的封裝
- 一個Jira返回結果的解析器
- 一套業務需求的配置方案
private JiraClient getJiraClient() {
if (null == this.jiraClient) {
Properties prop = PropertiesReader.loadProperties();
String url = prop.getProperty("jira.connection.url");
String username = prop.getProperty("jira.connection.username");
String password = prop.getProperty("jira.connection.password");
jiraClient = new JiraClient(url, new BasicCredentials(username, password));
}
return jiraClient;
}
連接後就可以讀取jira中的項目信息了,比如獲取issue信息Issue issue = this.getJiraClient().getIssue(key.trim());
獲取project信息this.getJiraClient().getProjects();
還有此次工具中的重點,創建issue Issue parentIssue = this.getJiraClient().getIssue(vo.getParentIssueKey());
//2. create develop task
if (null != vo.getDevelopers() && vo.getDevelopers().length != 0) {
for (String dev : vo.getDevelopers()) {
Issue newIssue = this.getJiraClient().createIssue(project, issueType)
.field(Field.SUMMARY, devMap.get(dev) + parentIssue.getSummary())
.field(Field.PRIORITY, Field.valueById("3"))
.field(Field.ASSIGNEE, dev)
.field(Field.FIX_VERSIONS, versions)
.execute();
parentIssue.link(newIssue.getKey(), "包含");
newIssues.add(newIssue);
}
}
到此,一個簡單的流程就結束了,並不複雜。當然要感謝jira-client的支持。
$("#jira_table").jqGrid({
datatype: "local",
height: 500,
weight: 900,
colNames: ['Jira#','Developers', 'Qas'],
colModel : [
{
name : 'issueKey',
index : 'issueKey',
width : 100,
editable : true,
edittype : 'text'
},
{
name : 'developers',
index : 'developers',
width : 400,
editable : true,
edittype : 'custom',
editoptions : {
custom_element : function(value,
options) {
var comp = "<div id=\""
+ options.id
+ "\" style=\"white-space:normal;\" >"
+ "<input type=\"checkbox\" name=\"user\" value=\"1\"/>1"
+ "<input type=\"checkbox\" name=\"user\" value=\"2\" />2"
+ "<input type=\"checkbox\" name=\"user\" value=\"3\" />3"
+ "<input type=\"checkbox\" name=\"user\" value=\"4\" />4"
+ "<input type=\"checkbox\" name=\"user\" value=\"5\" />5"
+ "</div>";
return comp;
},
custom_value : function(elem,
operation, value) {
if (operation === 'get') {
var id = elem.attr("id");
var qas = "";
$("#" + id + " input").each(function() {
if($(this).is(":checked")){
qas = qas + $(this).val()+ ",";
}
});
qas = crudHelper.formatSliptStr(qas,",");
elem.val(qas);
return elem.val();
} else if (operation === 'set') {
var id = elem.attr("id");
var qas = "";
$("#" + id + " input").each(function() {
if($(this).is(":checked")){
qas = qas + $(this).val()+ ",";
}
});
qas = crudHelper.formatSliptStr(qas,",");
elem.val(qas);
}
}
}
},
{
name : 'qas',
index : 'qas',
width : 400,
editable : true,
edittype : 'custom',
editoptions : {
custom_element : function(value,
options) {
var comp = "<div id=\""
+ options.id
+ "\" style=\"white-space:normal;\" >"
+ "<input type=\"checkbox\" name=\"user\" value=\"6\"/>6"
+ "<input type=\"checkbox\" name=\"user\" value=\"7\" />7</div>";
return comp;
},
custom_value : function(elem,
operation, value) {
if (operation === 'get') {
var id = elem.attr("id");
var qas = "";
$("#" + id + " input").each(function() {
if($(this).is(":checked")){
qas = qas + $(this).val()+ ",";
}
});
elem.val(qas);
qas = crudHelper.formatSliptStr(qas,",");
return elem.val();
} else if (operation === 'set') {
var id = elem.attr("id");
var qas = "";
$("#" + id + " input").each(function() {
if($(this).is(":checked")){
qas = qas + $(this).val()+ ",";
}
});
qas = crudHelper.formatSliptStr(qas,",");
elem.val(qas);
}
}
}
}
],
multiselect: true,
caption: "創建Jira任務",
ondblClickRow: function(id){
$('#jira_table').jqGrid('editRow', id, {
keys : true,
url : 'clientArray',
restoreAfterError : true
});
}
});
這裏主要是爲了給多個人創建任務,提供了一個簡單的多選支持框,效果圖明天補上。