jira二次开发经验分享

最近在折腾Jira的二次开发,说实话,二次开发需要对系统的架构有一定的了解才好下手。虽然Jira这个产品很完善,官方有很详细的文档供开发者参考,但小弟还是花了不少时间研究,开发了近一个月的时间,现在把一些用到的知识和问题整理出来,与大家分享。

这次开发主要是做一个中间件,用来连接jira系统和其他系统从而实现系统之间的相互的通信,如创建一个问题(Issue),对jira里一些问题(即Issue)信息的修改,流程步骤(workflowstep)的改变,添加与更新字段(Field)和自定义字段(customerField)的等等。这里用到了jira-rest-java-client包来实现对jira的访问。

部分操作Jira的代码:

001 import java.net.URI;
002 import java.net.URISyntaxException;
003 import java.util.ArrayList;
004 import java.util.List;
005 import com.atlassian.jira.rest.client.JiraRestClient;
006 import com.atlassian.jira.rest.client.NullProgressMonitor;
007 import com.atlassian.jira.rest.client.domain.BasicIssue;
008 import com.atlassian.jira.rest.client.domain.BasicProject;
009 import com.atlassian.jira.rest.client.domain.Issue;
010 import com.atlassian.jira.rest.client.domain.User;
011 import com.atlassian.jira.rest.client.domain.input.FieldInput;
012 import com.atlassian.jira.rest.client.domain.input.IssueInput;
013 import com.atlassian.jira.rest.client.domain.input.IssueInputBuilder;
014 import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;
015   
016 public class jira {
017   
018   
019  staticJerseyJiraRestClientFactory factory =newJerseyJiraRestClientFactory();
020  staticNullProgressMonitor pm =newNullProgressMonitor();
021  staticString uri="http://localhost:8100";
022  staticString user="gfjira";
023  staticString pwd="123456";
024  /**
025  * @param args
026  */
027  publicstaticvoid main(String[] args) {
028  // TODO Auto-generated method stub
029   
030   
031  }
032  publicstaticvoid getIssue(String issueKey)throws URISyntaxException{
033    
034  URI jiraServerUri =newURI(uri);
035  JiraRestClient restClient = factory.createWithBasicHttpAuthentication(
036  jiraServerUri, user, pwd);
037  //get issue through issueKey
038  Issue issue = restClient.getIssueClient().getIssue(issueKey, pm);
039  //grab user info
040  User usr = restClient.getUserClient().getUser(user, pm); 
041  System.out.println(usr);
042  System.out.println(issue);
043  }
044    
045  //
046  /*
047  * create the issue
048  * @param Issue type :  1L:bug; 2L:new requirement; 3L:task; 4L.improvement
049  * @param projectName:such as "GTP",the project short name
050  * @param Longid:such as 10000
051  * @param Summary:topic
052  * @param Description:
053  */
054 public static void createIssue(String projectName,String issueType,String description,String summary)throwsURISyntaxException{
055  //JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
056  URI jiraServerUri =newURI(uri);
057  JiraRestClient restClient = factory.createWithBasicHttpAuthentication(
058  jiraServerUri, user, pwd);
059  IssueInputBuilder issueBuilder =newIssueInputBuilder("ProjectKey",
060  Long.valueOf(issueType));
061   
062   
063  finalURI projectUri =newURI(
064  "http://localhost:8100/rest/api/2/project/IT");
065  BasicProject bporject =newBasicProject(projectUri, projectName,"",
066  (long)10000);
067  issueBuilder.setProject(bporject);
068  issueBuilder.setDescription(description);
069  issueBuilder.setSummary(summary);
070   
071   
072  IssueInput issueInput = issueBuilder.build();
073  BasicIssue bIssue = restClient.getIssueClient().createIssue(issueInput,
074  pm);
075  // print the newly created issuekey
076  System.out.println(bIssue.getKey());
077  } publicstaticvoid updateIssueField(String key,String field,String value)throwsURISyntaxException{
078   
079   
080  URI jiraServerUri =newURI(uri);
081  JiraRestClient restClient = factory.createWithBasicHttpAuthentication(
082  jiraServerUri, user, pwd);
083  //get the issue through issuekey
084  Issue issue = restClient.getIssueClient().getIssue(key, pm);
085  //update the default field environment,if the field is defined by self,pls use the filed FieldInput fieldinput = new FieldInput("environment", value);
086  List fields =newArrayList();
087  fields.add(fieldinput);
088  restClient.getIssueClient().update(issue, fields, pm);
089  }
090   
091   
092 public static void changeIssueStatus(String issuekey)throwsURISyntaxException
093  {
094   
095   
096  URI jiraServerUri =newURI(uri);
097  JiraRestClient restClient = factory
098  .createWithBasicHttpAuthentication(jiraServerUri, user,
099  pwd);               
100  Issue issue = restClient.getIssueClient().getIssue(issuekey, pm);
101 //the number 3 is involed below picture,you can focus on the red rectangular
102  TransitionInput tinput=newTransitionInput(3);
103  restClient.getIssueClient().transition(issue,
104  tinput, pm);
105  }  }


就上面这段代码都折腾了我好久,特别是jira-rest-java-client的版本问题,最新的版本找不到需要的函数,却在旧的版本(1.1.m02,这个版本很奇葩吧)里有需要的函数,该死的官方没有说清楚,害得我一个一个的测试。这里想说的是changeIssueStatus这个方法,issue的workflow是不可以随便改变的,必须按照流程图的顺序进行改变,其实就是提供一个id值,图中红色的区域可查到具体的id值,比如当前issue的状态是open,要改变成resolved的状态,就要"5"这个值,即

1 TransitionInput tinput= newTransitionInput(5);
同理,要改变成closed,就用"2"这个值
1 TransitionInput tinput= newTransitionInput(2);

既然是二次开发,系统就少不了前端开发,那就要用到javascript了,考虑到Jira系统自身的复杂和以后版本升级带来的问题,所以没有对源码的js进行修改,而是利用了jira里提供的一个customer field,他可以让我们往里面添加html,css,javascript,而且是基于Jquery的,虽然称不上完美,但是还勉强能实现需求的功能。

暂时整理出这么多,希望各位大侠能多喷多给意见:D 

你可能感兴趣的:(JIRA的使用)