JIRA使用java创建问题

Posted

技术标签:

【中文标题】JIRA使用java创建问题【英文标题】:JIRA creating issue using java 【发布时间】:2016-11-03 13:53:02 【问题描述】:

我想做的是在 JIRA over Java 上创建一个新问题。实际上我正在实习,之前没有使用过 API。这是我在研究 JIRA 文档时发现的代码。不确定参数是否错误。

public class ExampleCreateIssuesAsynchronous 

    private static URI jiraServerUri = URI.create("https://stajtest.atlassian.net/");

    public static void main(String[] args) throws IOException 
        final AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
        final JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, "internship2016****", "***************");

        try 
            final List<Promise<BasicIssue>> promises = Lists.newArrayList();
            final IssueRestClient issueClient = restClient.getIssueClient();

            System.out.println("Sending issue creation requests...");
            for (int i = 0; i < 100; i++) 
                final String summary = "NewIssue#" + i;
                final IssueInput newIssue = new IssueInputBuilder("TST", 1L, summary).build();
                System.out.println("\tCreating: " + summary);
                promises.add(issueClient.createIssue(newIssue));
            
            System.out.println("Collecting responses...");
            final Iterable<BasicIssue> createdIssues = transform(promises, new Function<Promise<BasicIssue>, BasicIssue>() 
                @Override
                public BasicIssue apply(Promise<BasicIssue> promise) 
                    return promise.claim();
                
            );
            System.out.println("Created issues:\n" + Joiner.on("\n").join(createdIssues));
         finally 
            restClient.close();
        
    

我研究了 2 天,得到的只是那个错误。任何帮助将不胜感激。

Exception in thread "main" java.lang.NoClassDefFoundError: com/atlassian/sal/api/executor/ThreadLocalContextManager
    at com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory.create(AsynchronousJiraRestClientFactory.java:35)
    at com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory.createWithBasicHttpAuthentication(AsynchronousJiraRestClientFactory.java:42)
    at ExampleCreateIssuesAsynchronous.main(ExampleCreateIssuesAsynchronous.java:25)
Caused by: java.lang.ClassNotFoundException: com.atlassian.sal.api.executor.ThreadLocalContextManager
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more

编辑:

 import java.net.URI;
    import java.util.Optional;

    import com.atlassian.jira.rest.client.api.JiraRestClient;
    import com.atlassian.jira.rest.client.api.domain.Issue;
    import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
    import com.atlassian.util.concurrent.Promise;

    public class JRC
    
        public Issue getIssue(String issueKey) throws Exception
        
            final URI jiraServerUri = new URI("stajtest.atlassian.net");
            final JiraRestClient restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(jiraServerUri, "stajtest***", "********");
            @SuppressWarnings("rawtypes")
            Promise issuePromise = restClient.getIssueClient().getIssue(issueKey);
            return Optional.ofNullable((Issue) issuePromise.claim()).orElseThrow(() -> new Exception("No such issue"));
        
    

    import static org.hamcrest.CoreMatchers.is;
    import static org.junit.Assert.assertThat;

    import org.junit.Test;

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

public class JRCTest
    
        private static final String jiraKey = "DEN-24";
        @Test
        public void testGetIssue() throws Exception 
            Issue issue = new JRC().getIssue(jiraKey);
            assertThat(issue.getKey(), is(jiraKey));
        
    

编辑 2

[INFO] 正在扫描项目... [INFO] [信息] 构建 jrjc 1.0-SNAPSHOT [信息] [信息] [信息] --- maven-resources-plugin:2.6:resources (default-resources) @jrjc --- [警告] 使用平台编码 (实际上是 Cp1254)复制过滤后的资源,即构建依赖于平台! > [INFO] 复制 0 资源 [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @jrjc [INFO] 检测到更改 - 重新编译模块! [警告] 文件编码尚未设置,使用平台编码 Cp1254,即构建依赖于平台! [INFO] 将 1 个源文件编译为 C:\Users\Madara\workspace\jrjc-master\target\classes [错误] 编译错误 [错误] /C:/Users/Madara/workspace/jrjc-master/src/main/java/JRC.java:[17,81] -source 1.5 不支持 lambda 表达式(使用 -source 8 或更高以启用 lambda 表达式)[INFO] 1 错误 [INFO] -------------------------------------------------- ------------ [信息] ------------------------------------ ------------------------------------------------ [信息] 构建失败 [信息] [INFO] 总时间:0.970 s [INFO] 完成时间: 2016-07-04T19:37:26+03:00 [INFO] 最终内存:11M/245M [INFO] [错误] 未能执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.1:compile 项目 jrjc 上的(默认编译):编译失败 [错误] /C:/Users/Madara/workspace/jrjc-master/src/main/java/JRC.java:[17,81] -source 1.5 [ERROR] 不支持 lambda 表达式(使用 -source 8 或更高版本以启用 lambda 表达式) [ERROR] -> [Help 1] [ERROR] [ERROR] 查看完整的堆栈跟踪 错误,重新运行 带有 -e 开关的 Maven。 [错误] 使用 -X 开关重新运行 Maven 启用完整的调试日志记录。 [错误] [错误] 更多信息 关于错误和可能的解决方案,请阅读以下内容 文章:[错误] [帮助 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

【问题讨论】:

你是如何摆脱这个错误的?线程“主”java.lang.NoClassDefFoundError 中的异常:com/atlassian/sal/api/executor/ThreadLocalContextManager 我面临同样的问题,我将不胜感激。 仅供参考:我只是忘了添加存储库。 【参考方案1】:

您从哪里获得有关 API 的信息? atlassian 上的文档已过时。

首先你的 pom.xml 应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.company</groupId>
<artifactId>jrjc</artifactId>
<version>1.0-SNAPSHOT</version>


<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </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.9</version>
    </dependency>
    <dependency>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-rest-java-client-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-rest-java-client-core</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>14.0-rc1</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>atlassian-public</id>
        <url>https://m2proxy.atlassian.com/repository/public</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
        </snapshots>
        <releases>
            <enabled>true</enabled>
            <checksumPolicy>warn</checksumPolicy>
        </releases>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>atlassian-public</id>
        <url>https://m2proxy.atlassian.com/repository/public</url>
        <releases>
            <enabled>true</enabled>
            <checksumPolicy>warn</checksumPolicy>
        </releases>
        <snapshots>
            <checksumPolicy>warn</checksumPolicy>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

尝试先更改您的 pom,看看是否可以解决问题。您可以在此处查看完整的示例:https://github.com/somaiah/jrjc

【讨论】:

我意识到文档已经过时为时已晚。我试过你的样品,但它对我也不起作用。我应该为 jiraServerUri 发送什么? “stajtest.atlassian.net”有效吗? 您使用哪个 url 从浏览器访问 jira?如果它是“stajtest.atlassian.net”,那么这就是 jiraServerUri。如果你使用https,那么它应该是“stajtest.atlassian.net” 发生了 2 个错误。 测试出错:testGetIssue(JRCTest): 主机名可能不为空 并且 未能执行目标 org.apache.maven.plugins:maven-surefire-plugin :2.12.4:test (default-test) on project jrjc-master: There are test failures. 可能是第一个导致第二个。可以请你点亮我吗? 您可以编辑您的问题以添加 JRC 类和 JRCTest 类吗?从“主机名可能不为空”,我认为你搞砸了 JRC.java 中的“新 URI”行。顺便说一句,我刚刚将 jiraServerUri 更改为 new URI("stajtest.atlassian.net") 并且可以连接。当然我得到了 401,但那是因为我的登录凭据不正确。 如您所愿,我已经编辑了我的问题并且没有隐藏登录信息,因此您可以根据需要尝试一下。别担心,这是一次性帐户。【参考方案2】:

我能够使用以下 Java 代码在 Jira 中创建和获取错误:

public Issue getIssue(String issueKey) throws Exception

    final URI jiraServerUri = new URI("https://yourJiraURI");

    final JiraRestClient restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(jiraServerUri, "yourUsername", "yourPassword");
    System.out.println("\n connecting... ");

    Promise issuePromise = restClient.getIssueClient().getIssue(issueKey);
    System.out.println("issuPromise... ");

    return (Issue) issuePromise.claim();


public Issue createIssue(String project, Long key, String summary, String description) throws Exception

    final URI jiraServerUri = new URI("https://yourJiraURI");

    System.out.println("\n connecting to create a bug... ");

    JiraRestClientFactory restClientFactory = new AsynchronousJiraRestClientFactory();
    JiraRestClient restClient = restClientFactory.createWithBasicHttpAuthentication(jiraServerUri, "yourUsername", "yourPassword");
    IssueRestClient issueClient = restClient.getIssueClient();

    IssueInputBuilder issueBuilder = new IssueInputBuilder(project, key, summary);
    issueBuilder.setDescription(description);   

    IssueType it = new IssueType(jiraServerUri, key, summary, false, "Testing the Issue creation", null);

    issueBuilder.setIssueType(it);
    IssueInput issueInput = issueBuilder.build();

    Promise<BasicIssue> promise = restClient.getIssueClient().createIssue(issueInput);
    BasicIssue basicIssue = promise.claim();
    Promise<Issue> promiseJavaIssue = restClient.getIssueClient().getIssue(basicIssue.getKey());

    Issue issue = promiseJavaIssue.claim();
    System.out.println(String.format("New issue created is: %s\r\n", issue.getSummary()));        


    return issue;

POM.xml 依赖: 球衣:1.9 六月:4.8.2 jira-rest-java-client-*: 3.0.0 番石榴:14.0-rc1 公共日志记录:4.0.6

【讨论】:

谢谢!我花了几个小时试图让它与IssueInput.createWithFields 一起工作,但它一直在生成错误的 JSON。您的 IssueInputBuilder 使用相同的数据开箱即用。可惜它不允许将issuetype 作为字符串传递。

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

Jira OAuth - 无法创建应用程序链接

jira项目创建与使用

如何使用 JIRA Cloud REST API 创建对 jira 问题的内部评论

如何在 JIRA 中自动创建问题?

创建问题时自动创建 JIRA 子任务

使用 ruby​​ 创建 OKTA JIRA 登录