当前位置: 首页 > 工具软件 > JIRA-Client > 使用案例 >

jira rest java api_JIRA Rest JAVA Client API实现问题管理及自定义字段(原创)

慕璞
2023-12-01

package jiraTEST;

import java.net.URI;

import java.net.URISyntaxException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.concurrent.ExecutionException;

import org.apache.commons.codec.binary.Base32;

import org.joda.time.DateTime;

import com.atlassian.jira.rest.client.IssueRestClient;

import com.atlassian.jira.rest.client.JiraRestClient;

import com.atlassian.jira.rest.client.SearchRestClient;

import com.atlassian.jira.rest.client.domain.BasicIssue;

import com.atlassian.jira.rest.client.domain.BasicProject;

import com.atlassian.jira.rest.client.domain.BasicUser;

import com.atlassian.jira.rest.client.domain.Comment;

import com.atlassian.jira.rest.client.domain.Field;

import com.atlassian.jira.rest.client.domain.Issue;

import com.atlassian.jira.rest.client.domain.Project;

import com.atlassian.jira.rest.client.domain.SearchResult;

import com.atlassian.jira.rest.client.domain.Transition;

import com.atlassian.jira.rest.client.domain.input.ComplexIssueInputFieldValue;

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.async.AsynchronousJiraRestClientFactory;

import com.atlassian.util.concurrent.Promise;

import com.google.common.collect.Lists;public classCvteJiraDemo {public static String BaseURL = "http://jira-test:8080/";public static String User = "admin";public static String Password = "admin";private static URI jiraServerUri =URI

.create("http://jira-test:8080/rest/api/2/");private static boolean quiet = false;private static final long BUG_TYPE_ID = 1L; //JIRA magic value

private static final long TASK_TYPE_ID = 3L; //JIRA magic value

private static final DateTime DUE_DATE = newDateTime();private static final String PRIORITY = "Trivial";private static final String DESCRIPTION = "description";public static voidmain(String[] args) throws InterruptedException,

ExecutionException {

final AsynchronousJiraRestClientFactory factory= newAsynchronousJiraRestClientFactory();

URI jiraServerUri;try{

jiraServerUri= newURI(BaseURL);

final JiraRestClient restClient=(JiraRestClient) factory

.createWithBasicHttpAuthentication(jiraServerUri, User,

Password);

getAllProjects(restClient);

getProject(restClient,"DEMO");

getIssue(restClient,"FEEDBACK-14");

getIssueFields(restClient,"FEEDBACK-27");

addIssue(restClient,"FEEDBACK", "AAAAB");

addIssueComplex(restClient,"FEEDBACK",DUE_DATE.toString());

}catch(URISyntaxException e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{

}

}private static voidprintln(Object o) {if (!quiet) {

System.out.println(o);

}

}private static voidparseArgs(String[] argsArray) throws URISyntaxException {

final List args =Lists.newArrayList(argsArray);if (args.contains("-q")) {

quiet= true;

args.remove(args.indexOf("-q"));

}if (!args.isEmpty()) {

jiraServerUri= new URI(args.get(0));

}

}private staticTransition getTransitionByName(

Iterabletransitions, String transitionName) {for(Transition transition : transitions) {if(transition.getName().equals(transitionName)) {returntransition;

}

}return null;

}//获得全部项目信息

private static voidgetAllProjects(final JiraRestClient restClient)

throws InterruptedException, ExecutionException {try{

Promise> list =restClient

.getProjectClient().getAllProjects();

Iterable a = list.get();

Iterator it =a.iterator();while(it.hasNext()) {

System.out.println(it.next());

}

}finally{

}

}//获得单一项目信息

private static voidgetProject(final JiraRestClient restClient,

String porjectKEY) throws InterruptedException, ExecutionException {try{

Project project=restClient.getProjectClient()

.getProject(porjectKEY).get();

System.out.println(project);

}finally{

}

}//获得单一问题信息

private static voidgetIssue(final JiraRestClient restClient,

String issueKEY) throws InterruptedException, ExecutionException {try{

Promise list =restClient.getIssueClient()

.getIssue(issueKEY);

Issue issue= list.get();

System.out.println(issue);

}finally{

}

}//建立问题

public staticBasicIssue createIssue(final JiraRestClient jiraRestClient,

IssueInput newIssue) {

BasicIssue basicIssue=jiraRestClient.getIssueClient()

.createIssue(newIssue).claim();returnbasicIssue;

}//添加备注到问题

public static voidaddCommentToIssue(final JiraRestClient jiraRestClient,Issue issue, String comment) {

IssueRestClient issueClient=jiraRestClient.getIssueClient();

issueClient.addComment(issue.getCommentsUri(), Comment.valueOf(comment)).claim();

}//删除问题,目前找不到对应API

public static voiddeleteIssue(final JiraRestClient jiraRestClient, Issue issue) {

IssueRestClient issueClient=jiraRestClient.getIssueClient();//issueClient.deleteIssue(issue.getKey(), false).claim();

}//经过标题获取问题

public staticIterable findIssuesByLabel(final JiraRestClient jiraRestClient, String label) {

SearchRestClient searchClient=jiraRestClient.getSearchClient();

String jql= "labels%3D"+label;

com.atlassian.jira.rest.client.domain.SearchResult results=((SearchRestClient) jiraRestClient).searchJql(jql).claim();returnresults.getIssues();

}//经过KEY获取问题

public staticIssue findIssueByIssueKey(final JiraRestClient jiraRestClient, String issueKey) {

SearchRestClient searchClient=jiraRestClient.getSearchClient();

String jql= "issuekey = \"" + issueKey + "\"";

SearchResult results=searchClient.searchJql(jql).claim();return(Issue) results.getIssues().iterator().next();

}//建立问题 :仅有简单问题名称

private static voidaddIssue(final JiraRestClient restClient,

String porjectKEY, String issueName) throws InterruptedException,

ExecutionException {try{

IssueInputBuilder builder= newIssueInputBuilder(porjectKEY,

TASK_TYPE_ID, issueName);

builder.setDescription("issue description");

final IssueInput input=builder.build();try{//create issue

final IssueRestClient client =restClient.getIssueClient();

final BasicIssue issue=client.createIssue(input).claim();

final Issue actual=client.getIssue(issue.getKey()).claim();

System.out.println(actual);

}finally{if (restClient != null) {//restClient.close();

}

}

}finally{

}

}//建立问题 :包含自定义字段

private static voidaddIssueComplex(final JiraRestClient restClient,

String porjectKEY, String issueName) throws InterruptedException,

ExecutionException {try{

IssueInputBuilder builder= newIssueInputBuilder(porjectKEY,

TASK_TYPE_ID, issueName);

builder.setDescription("issue description");//builder.setFieldValue("priority", ComplexIssueInputFieldValue.with("name", PRIORITY));//单行文本

builder.setFieldValue("customfield_10042", "单行文本测试");//单选字段

builder.setFieldValue("customfield_10043", ComplexIssueInputFieldValue.with("value", "通常"));//数值自定义字段

builder.setFieldValue("customfield_10044", 100.08);//用户选择自定义字段

builder.setFieldValue("customfield_10045", ComplexIssueInputFieldValue.with("name", "admin"));//用户选择自定义字段(多选)

Map user1 = new HashMap();

user1.put("name", "admin");

Map user2 = new HashMap();

user2.put("name", "wangxn");

ArrayList peoples= newArrayList();

peoples.add(user1);

peoples.add(user2);

builder.setFieldValue("customfield_10047", peoples);//设定父问题

Map parent = new HashMap();

parent.put("key", "FEEDBACK-25");

FieldInput parentField= new FieldInput("parent", newComplexIssueInputFieldValue(parent));

builder.setFieldInput(parentField);

final IssueInput input=builder.build();try{

final IssueRestClient client=restClient.getIssueClient();

final BasicIssue issue=client.createIssue(input).claim();

final Issue actual=client.getIssue(issue.getKey()).claim();

System.out.println(actual);

}finally{if (restClient != null) {//restClient.close();

}

}

}finally{

}

}//获取问题的全部字段

private static voidgetIssueFields(final JiraRestClient restClient,

String issueKEY) throws InterruptedException, ExecutionException {try{

Promise list =restClient.getIssueClient()

.getIssue(issueKEY);

Issue issue= list.get();

Iterable fields =issue.getFields();

Iterator it =fields.iterator();while(it.hasNext()) {

System.out.println(it.next());

}

}finally{

}

}

}

 类似资料: