团队的成员碰到一个问题,使用JIRA建立多个有关联性的任务时,过于耗时。所以花了3天时间,间断地看了一下Jira官方文档和网上的资料。下面总结一下Jira API的使用。
Jira develop 文档中提到了其对rest风格api的支持,同时也支持SOAP的调用。我们采用 Jira rest API的方式进行调用,链接中有各种调用方式,可以细细看一下。
首先思考一下我们要做什么,可以归纳为以下几点:
- 一个Jira的访问接口
- 一个Jira访问参数的封装
- 一个Jira返回结果的解析器
- 一套业务需求的配置方案
针对前面3点,在GitHub找到了一个share的
jira-client。其提供maven支持,对于repository无法下载的同学请自动查找maven repository解决方案或者直接下jar本地生成pom。
先来看下jira-client的整体结构
熟悉jira的童鞋肯定会发现,这不就是jira元素的对象化吗。我们之后就会用到这些类。下面来逐点分析。
jira-client通过一个http request来实现与jira的连接,creds是授权认证。
提供json参数封装
解析器
这样就满足了我们前面的3点,目前测试jira-client连接正常。完整的连接demo,虚提供url,username,password参数。
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的支持。
第二部分简单介绍一下jqGird。jqGrid网上有些资料可循,不过多介绍,这里主要给出一个custom的demo。
$("#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
});
}
});
这里主要是为了给多个人创建任务,提供了一个简单的多选支持框,效果图明天补上。