-->

JIRA creating issue using java

2019-07-26 22:25发布

问题:

What I'm trying to do is creating a new issue on JIRA over Java. Actually I'm on internship and didn't work with APIs before. Here is the code I found while studying JIRA's documents. Not sure if paramaters are wrong.

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();
        }
    }
}

I've studied for 2 days and all I got is that error. Any help would be appreciated.

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

Edit:

 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));
        }
    }

Edit 2

[INFO] Scanning for projects... [INFO]
[INFO] Building jrjc 1.0-SNAPSHOT [INFO] [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jrjc --- [WARNING] Using platform encoding (Cp1254 actually) to copy filtered resources, i.e. build is platformdependent! > [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ jrjc [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding Cp1254, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Madara\workspace\jrjc-master\target\classes [ERROR] COMPILATION ERROR [ERROR] /C:/Users/Madara/workspace/jrjc-master/src/main/java/JRC.java:[17,81] lambda expressions are not supported in -source 1.5 (use -source 8 or higher to enable lambda expressions) [INFO] 1 error [INFO] ------------------------------------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] [INFO] Total time: 0.970 s [INFO] Finished at: 2016-07-04T19:37:26+03:00 [INFO] Final Memory: 11M/245M [INFO] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project jrjc: Compilation failure [ERROR] /C:/Users/Madara/workspace/jrjc-master/src/main/java/JRC.java:[17,81] lambda expressions are not supported in -source 1.5 [ERROR] (use -source 8 or higher to enable lambda expressions) [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

回答1:

Where did you get the info about the API? The docs on atlassian are outdated.

First of all your pom.xml should look like this:

<?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>

Try changing your pom first to see if this fixes it. You can see a fully working sample here: https://github.com/somaiah/jrjc



回答2:

I was able to create and get the bug in Jira using the following Java code:

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 dependencies: jersey: 1.9 junit: 4.8.2 jira-rest-java-client-*: 3.0.0 guava: 14.0-rc1 commons-logging: 4.0.6