如何使用 java rest api 在 jira 中创建问题

Posted

技术标签:

【中文标题】如何使用 java rest api 在 jira 中创建问题【英文标题】:How to create an issue in jira using java rest api 【发布时间】:2015-11-25 07:45:33 【问题描述】:

我几乎检查了每个链接,但找不到合适的解决方案。 我需要使用 java rest api 在 JIRA 中创建一个问题。 代码:-

  import java.io.IOException;
  import java.net.URI;
  import java.net.URISyntaxException;
  import org.codehaus.jettison.json.JSONException;
  import com.atlassian.jira.rest.client.api.JiraRestClient;
  import com.atlassian.jira.rest.client.api.domain.BasicProject;
  import com.atlassian.jira.rest.client.api.domain.Issue;
  import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
  import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactor;


public class TestingRestAPI 
    private static URI jiraServerUri =
 URI.create("https://vgsvdgteam.jira.com/rest/api/2/project");
public static void main(String[] args) throws URISyntaxException,
JSONException, IOException 
  final AsynchronousJiraRestClientFactory factory = new 
                                        AsynchronousJiraRestClientFactory();
      final JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, "liasia","cooki123!!");
           try 
            final int buildNumber = restClient.getMetadataClient().getServerInfo().claim().getBuildNumber();
             System.out.println("---------------------------------"+buildNumber);
            // first let's get and print all visible projects (only jira4.3+)
            System.out.println("Print all print all visible projects:");
            if (buildNumber >= ServerVersionConstants.BN_JIRA_6) 
                final Iterable<BasicProject> allProjects = restClient.getProjectClient().getAllProjects().claim();
                for (BasicProject project : allProjects) 
                    System.out.println(project);
                
            
            // then print one issue details
            System.out.println("Print issue TST-63239");
            final Issue issue = restClient.getIssueClient().getIssue("TST-63239").claim();
            System.out.println(issue);
        
        finally 
            restClient.close();
        
    

在 pom.xml 中添加了依赖项:-

<dependency>
            <groupId>com.atlassian.jira</groupId>
            <artifactId>jira-rest-java-client-api</artifactId>
            <version>2.0.0-m25</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.atlassian.jira</groupId>
            <artifactId>jira-rest-java-client-core</artifactId>
            <version>2.0.0-m25</version>
            <scope>provided</scope>
        </dependency>

异常获取:-

 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" RestClientExceptionstatusCode=Optional.of(404), errorCollections=[ErrorCollectionstatus=404, errors=, errorMessages=[]]
    at com.atlassian.jira.rest.client.internal.async.DelegatingPromise.claim(DelegatingPromise.java:47)
    at com.citrix.restjira.jiratest.TestingRestAPI.main(TestingRestAPI.java:25)
Caused by: RestClientExceptionstatusCode=Optional.of(404), errorCollections=[ErrorCollectionstatus=404, errors=, errorMessages=[]]
    at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:166)
    at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:160)
    at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:48)
    at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:12)
    at com.atlassian.util.concurrent.Promises$Of$3.apply(Promises.java:285)
    at com.atlassian.util.concurrent.Promises$2.onSuccess(Promises.java:162)
    at com.google.common.util.concurrent.Futures$7.run(Futures.java:1072)

检查异常后,Serverinfo 类的 claim() 函数似乎有问题。 还是网址有问题? 一些可以帮助我克服这个问题或任何有用的链接?

【问题讨论】:

【参考方案1】:

我将与您分享我在 Jira 中使用 IssueInputBuilder 类创建问题的代码:

    JiraRestClient restClient;

    public boolean createIssue(IssueJiraDTO issueDTO) throws IOException

            final IssueInputBuilder builder = new IssueInputBuilder(PROJECT_KEY, issueDTO.getIssueType(), issueDTO.getSummary());
            final ComponentRestClient componentClient = restClient.getComponentClient();

            try 

                openClient();

                //completamos los datos
                Component component = componentClient.getComponent(new URI(issueDTO.getComponent())).claim();
                builder.setComponents(component);
                builder.setFieldValue(CODCESCEFIELDID, issueDTO.getCodcesce());
                builder.setFieldValue(LINKCAFIELD, issueDTO.getLinkCA().trim());
                builder.setFieldValue(SECURITYFIELD, ComplexIssueInputFieldValue.with("id",  issueDTO.getSecurityLevel()));
                builder.setPriorityId(issueDTO.getPriority());
                builder.setDescription(issueDTO.getDescription());
                final IssueInput input = builder.build();

                // create issue
                final IssueRestClient client = restClient.getIssueClient();
                final BasicIssue issue = client.createIssue(input).claim();
                logger.info("Incidencia creada correctamente: "+ issue.getId());

            catch(Exception e)
                logger.error("Error al crear la Issue en Jira: " + issueDTO.getCodcesce(), e);
             finally 
                closeClient();
            

            return true;
        

openClient 基本上是这样做的:

restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(new URI(JIRA_URL), JIRA_ADMIN_USERNAME, JIRA_ADMIN_PASSWORD);

希望对你有所帮助。

【讨论】:

谢谢...但是这里定义的restClient和openClient方法是什么? 谢谢..非常有帮助。你能分享一些关于 IssueJiraDTO issueDTO 的事情吗? `IssueJiraDTO 只是一个自定义 bean,其中包含要插入问题的数据。如果有用,你可以接受我的回答@Littlebird :D 我尝试了您的代码并进行了一些修改,但仍然得到相同的异常:( 404 表示未找到,所以可能你的 url 错误,或者https://vgsvdgteam.jira.com 中的服务已关闭

以上是关于如何使用 java rest api 在 jira 中创建问题的主要内容,如果未能解决你的问题,请参考以下文章

JIRA REST java client API实际应用

如何使用 JIRA REST API 将 JIRA Agile 问题分配给当前用户的当前 sprint?

如何使用 JIRA -REST-API for python 访问下一页

如何通过rest api向jira问题添加标志

使用 OAuth 和 Node.JS 对 JIRA REST API 进行身份验证

JIRA rest api 来获取活动流