jira二次开发经验分享

原创
2013/09/25 15:42
阅读数 8W

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

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

部分操作Jira的代码:

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.NullProgressMonitor;
import com.atlassian.jira.rest.client.domain.BasicIssue;
import com.atlassian.jira.rest.client.domain.BasicProject;
import com.atlassian.jira.rest.client.domain.Issue;
import com.atlassian.jira.rest.client.domain.User;
import com.atlassian.jira.rest.client.domain.input.FieldInput;
import com.atlassian.jira.rest.client.domain.input.IssueInput;
import com.atlassian.jira.rest.client.domain.input.IssueInputBuilder;
import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;

public class jira {


 static JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
 static NullProgressMonitor pm = new NullProgressMonitor();
 static String uri="http://localhost:8100";
 static String user="gfjira";
 static String pwd="123456";
 /**
 * @param args
 */
 public static void main(String[] args) {
 // TODO Auto-generated method stub


 }
 public static void getIssue(String issueKey) throws URISyntaxException{
 
 URI jiraServerUri = new URI(uri);
 JiraRestClient restClient = factory.createWithBasicHttpAuthentication(
 jiraServerUri, user, pwd);
 //get issue through issueKey
 Issue issue = restClient.getIssueClient().getIssue(issueKey, pm);
 //grab user info
 User usr = restClient.getUserClient().getUser(user, pm); 
 System.out.println(usr);
 System.out.println(issue);
 }
 
 //
 /*
 * create the issue
 * @param Issue type :	1L:bug; 2L:new requirement; 3L:task; 4L.improvement
 * @param projectName:such as "GTP",the project short name
 * @param Longid:such as 10000
 * @param Summary:topic
 * @param Description:
 */
public static void createIssue(String projectName,String issueType,String description,String summary) throws URISyntaxException{
 //JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
 URI jiraServerUri = new URI(uri);
 JiraRestClient restClient = factory.createWithBasicHttpAuthentication(
 jiraServerUri, user, pwd);
 IssueInputBuilder issueBuilder = new IssueInputBuilder("ProjectKey",
 Long.valueOf(issueType));


 final URI projectUri = new URI(
 "http://localhost:8100/rest/api/2/project/IT");
 BasicProject bporject = new BasicProject(projectUri, projectName, "",
 (long) 10000);
 issueBuilder.setProject(bporject);
 issueBuilder.setDescription(description);
 issueBuilder.setSummary(summary);


 IssueInput issueInput = issueBuilder.build();
 BasicIssue bIssue = restClient.getIssueClient().createIssue(issueInput,
 pm);
 // print the newly created issuekey
 System.out.println(bIssue.getKey());
 }  public static void updateIssueField(String key,String field,String value) throws URISyntaxException{


 URI jiraServerUri = new URI(uri);
 JiraRestClient restClient = factory.createWithBasicHttpAuthentication(
 jiraServerUri, user, pwd);
 //get the issue through issuekey
 Issue issue = restClient.getIssueClient().getIssue(key, pm);
 //update the default field environment,if the field is defined by self,pls use the filed FieldInput fieldinput = new FieldInput("environment", value);
 List<FieldInput> fields = new ArrayList<FieldInput>();
 fields.add(fieldinput);
 restClient.getIssueClient().update(issue, fields, pm);
 }


public static void changeIssueStatus(String issuekey) throws URISyntaxException
 {


 URI jiraServerUri = new URI(uri);
 JiraRestClient restClient = factory
 .createWithBasicHttpAuthentication(jiraServerUri, user,
 pwd);				 
 Issue issue = restClient.getIssueClient().getIssue(issuekey, pm);
//the number 3 is involed below picture,you can focus on the red rectangular
 TransitionInput tinput= new TransitionInput(3);
 restClient.getIssueClient().transition(issue,
 tinput, pm);
 }  }

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

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

TransitionInput tinput= new TransitionInput(2);

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


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



近来有很多朋友都问到编译出错的问题,我把我的依赖包贴出来给大家参考,希望能帮到大家:D

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.8.2</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>3.2.4.RELEASE</version>

</dependency>


<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>3.2.4.RELEASE</version>

</dependency>

            


<dependency>

<groupId>commons-pool</groupId>

<artifactId>commons-pool</artifactId>

<version>20030825.183949</version>

</dependency>

            


<!-- <dependency>

<groupId>commons-dbcp</groupId>

<artifactId>commons-dbcp</artifactId>

<version>20030825.184428</version>

</dependency> -->

                       

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.2.2</version>

</dependency>

                        

            

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.2.0</version>

</dependency>

            

        

<dependency>

    <groupId>org.postgresql</groupId>

    <artifactId>postgresql</artifactId>

    <version>9.2-1003-jdbc4</version>

</dependency>

                                                        

<dependency>

<groupId>commons-dbcp</groupId>

<artifactId>commons-dbcp</artifactId>

<version>1.2.2</version>

</dependency>

                        

            

<dependency>

<groupId>com.atlassian.jira</groupId>

<artifactId>jira-rest-java-client</artifactId>

<version>1.1-m02</version>

</dependency>


<dependency>

<groupId>commons-httpclient</groupId>

<artifactId>commons-httpclient</artifactId>

<version>3.1</version>

</dependency>


<dependency>

<groupId>com.sun.jersey</groupId>

<artifactId>jersey-json</artifactId>

<version>1.5</version>

</dependency>


<dependency>

<groupId>com.sun.jersey</groupId>

<artifactId>jersey-client</artifactId>

<version>1.6-ea06</version>

</dependency>


<dependency>

<groupId>org.codehaus.jettison</groupId>

<artifactId>jettison</artifactId>

<version>1.2</version>

</dependency>



<dependency>

<groupId>com.sun.jersey.contribs</groupId>

<artifactId>jersey-apache-client</artifactId>

<version>1.6</version>

</dependency>





<dependency>

<groupId>joda-time</groupId>

<artifactId>joda-time</artifactId>

<version>1.6</version>

</dependency>





<dependency>

<groupId>com.google.guava</groupId>

<artifactId>guava</artifactId>

<version>11.0</version>

</dependency>


<dependency>

<groupId>org.jboss.resteasy</groupId>

<artifactId>resteasy-jaxrs</artifactId>

<version>2.2.1.GA</version>

</dependency>


<dependency>

<groupId>org.jboss.resteasy</groupId>

<artifactId>resteasy-jackson-provider</artifactId>

<version>2.2.1.GA</version>

</dependency>


<dependency>

<groupId>org.apache.axis2</groupId>

<artifactId>axis2</artifactId>

<version>1.4</version>

</dependency>


<dependency>

<groupId>axis</groupId>

<artifactId>axis-jaxrpc</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>org.apache.axis</groupId>

<artifactId>axis</artifactId>

<version>1.4</version>

</dependency>

<dependency>

<groupId>com.sun.xml.rpc</groupId>

<artifactId>jaxrpc-impl</artifactId>

<version>1.1.3_01</version>

</dependency>

<dependency>

<groupId>commons-discovery</groupId>

<artifactId>commons-discovery</artifactId>

<version>0.2</version>

</dependency>


<dependency>

<groupId>wsdl4j</groupId>

<artifactId>wsdl4j</artifactId>

<version>1.6.2</version>

</dependency>


<dependency>

<!-- jsoup HTML parser library @ http://jsoup.org/ -->

<groupId>org.jsoup</groupId>

<artifactId>jsoup</artifactId>

<version>1.7.2</version>

</dependency>


<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>


<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-core</artifactId>

<version>2.0.11</version>

</dependency>


<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.9</version>

</dependency>

</dependencies>

展开阅读全文
加载中

作者的其它热门文章

打赏
4
58 收藏
分享
打赏
45 评论
58 收藏
4
分享
返回顶部
顶部