JIRA、Jira client和jqGrid實踐

團隊的成員碰到一個問題,使用JIRA建立多個有關聯性的任務時,過於耗時。所以花了3天時間,間斷地看了一下Jira官方文檔和網上的資料。下面總結一下Jira API的使用。

Jira develop 文檔中提到了其對rest風格api的支持,同時也支持SOAP的調用。我們採用 Jira rest API的方式進行調用,鏈接中有各種調用方式,可以細細看一下。


首先思考一下我們要做什麼,可以歸納爲以下幾點:

  1. 一個Jira的訪問接口
  2. 一個Jira訪問參數的封裝
  3. 一個Jira返回結果的解析器
  4. 一套業務需求的配置方案
針對前面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
			});
		}
	});
這裏主要是爲了給多個人創建任務,提供了一個簡單的多選支持框,效果圖明天補上。




發佈了52 篇原創文章 · 獲贊 3 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章