简体   繁体   中英

How to create the version and get the details of any version of specific project using JIRA REST CLIENT JAVA in groovy?

I am trying to create the version in JIRA for specific project.Below is my code.I am able to connect the JIRA successfully through JIRA REST CLIENT JAVA java libraries but now want to achieve the get the information of any version,createversion few more actions.

import com.atlassian.jira.rest.client.api.JiraRestClient
import com.atlassian.jira.rest.client.api.JiraRestClientFactory
//import com.atlassian.jira.rest.client.api.domain.User
import com.atlassian.jira.rest.client.api.domain.Version
//import com.atlassian.jira.rest.client.api.domain.input.VersionInput
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory
import com.atlassian.util.concurrent.Promise

class Jira {
  private static final String JIRA_URL = "https://jira.test.com"
  private static final String JIRA_ADMIN_USERNAME = "ABCDE"
  private static final String JIRA_ADMIN_PASSWORD = "xxxxxx"

  static void main(String[] args) throws Exception
  {
    // Construct the JRJC client
    System.out.println(String.format("Logging in to %s with username '%s' and password '%s'", JIRA_URL, JIRA_ADMIN_USERNAME, JIRA_ADMIN_PASSWORD))
    JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory()
    URI uri = new URI(JIRA_URL)
    JiraRestClient client = factory.createWithBasicHttpAuthentication(uri, JIRA_ADMIN_USERNAME, JIRA_ADMIN_PASSWORD)
   // client.withCloseable {
    //  it.projectClient.getProject("ABCD").claim().versions.each { println it }
   // }

    // Invoke the JRJC Client
    //Promise<User> promise = client.getUserClient().getUser(JIRA_ADMIN_USERNAME)
    //User user = promise.claim()

    Promise<Version> promise = client.getVersionRestClient().getVersion(1234)
    //Version version = promise.claim()

    // Print the result
    System.out.println(String.format("Your user's email address is: %s\r\n", user.getEmailAddress()))
  }
}

I am able to do some task like to get the email address of any userid.I am trying this in groovy

package com.temp.jira

import com.atlassian.jira.rest.client.api.JiraRestClient
import com.atlassian.jira.rest.client.api.JiraRestClientFactory
import com.atlassian.jira.rest.client.api.domain.BasicProject
import com.atlassian.jira.rest.client.api.domain.Issue
import com.atlassian.jira.rest.client.api.domain.SearchResult
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory
import com.atlassian.util.concurrent.Promise
/**
 * Created on 7/21/2017.
 */
class Test {

    private static final String JIRA_URL = "https://jira.test.com"
    private static final String JIRA_ADMIN_USERNAME = "ABCDEF"
    private static final String JIRA_ADMIN_PASSWORD = "*****"
    private static final String JIRA_PROJECT = "ABCD"

    static void main(String[] args) throws Exception
    {
        // Construct the JRJC client
        System.out.println(String.format("Logging in to %s with username '%s' and password '%s'", JIRA_URL, JIRA_ADMIN_USERNAME, JIRA_ADMIN_PASSWORD))
        JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory()
        URI uri = new URI(JIRA_URL)
        JiraRestClient client = factory.createWithBasicHttpAuthentication(uri, JIRA_ADMIN_USERNAME, JIRA_ADMIN_PASSWORD)


        for (BasicProject project : client.getProjectClient().getProject(JIRA_PROJECT).claim()) {
            System.out.println(project.getKey() + ": " + project.getName())

        }

       Promise<SearchResult> searchJqlPromise = client.getSearchClient().searchJql("project = $JIRA_PROJECT AND status in (Closed, Completed, Resolved) ORDER BY assignee, resolutiondate");

       for (Issue issue : searchJqlPromise.claim().getIssues()) {
          System.out.println(issue.getId())
       }

         // Done
        System.out.println("Example complete. Now exiting.")
        System.exit(0)
    }
}

Stick to the jira rest client api, since you have a typed access provided from the creator o fJira.

Excerpt from my working Groovy code:

/**
 * Retrieves the latest unreleased version for the given project.
 * @param projectId Given project
 * @return latest unreleased version
 */
private Version getVersion(String projectId)
{
    def project = restClient.projectClient.getProject(projectId).claim()
    def unreleasedVersions = project
        .getVersions()
        .findAll { version ->
            version.released == false
        }

    if (unreleasedVersions.size() != 1) {
        throw new RuntimeException('There are zero or more unreleased versions.')
    }

    unreleasedVersions.get(0)
}

/**
 * Creates a new, undeployed version for the given project.
 * @param projectId Given project
 */
private void createVersion(String projectId)
{
    def versionInputBuilder = new VersionInputBuilder(projectId)
    versionInputBuilder.released = false
    versionInputBuilder.name = incrementVersion(capturedVersion.name)
    versionInputBuilder.description = 'Bugfixing'
    versionInputBuilder.startDate = DateTime.now()

    def promise = restClient.versionRestClient.createVersion(versionInputBuilder.build())
    trackCompletion(promise)
}

What I don't have in my code is get a certain version, but your commented code should work when you change the data type of the version to string.

I created the script in below way and able to create,update and delete the version

import com.atlassian.jira.rest.client.api.JiraRestClient
import com.atlassian.jira.rest.client.api.VersionRestClient
import com.atlassian.jira.rest.client.api.domain.Version
import com.atlassian.jira.rest.client.api.domain.input.VersionInput
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory
import com.atlassian.util.concurrent.Promise
import org.codehaus.jettison.json.JSONException
import org.joda.time.DateTime
import java.util.concurrent.ExecutionException
class VersionClient {
    private static final String JIRA_URL = "https://jira.test.com"
    private static final String JIRA_ADMIN_USERNAME = "ABCDEF"
    private static final String JIRA_ADMIN_PASSWORD = "******"
    private static final String JIRA_PROJECT = "TEST"
     static void main(String[] args)
             throws URISyntaxException, InterruptedException, ExecutionException, JSONException {
        // Initialize REST Client
        final AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory()
        final URI uri = new URI("$JIRA_URL")
        final JiraRestClient jiraRestClient = factory.createWithBasicHttpAuthentication(uri, "$JIRA_ADMIN_USERNAME", "$JIRA_ADMIN_PASSWORD")
        // Get specific client instances
        VersionRestClient versionClient = jiraRestClient.getVersionRestClient()
        // Create Version
        VersionInput versionInput = new VersionInput("$JIRA_PROJECT", "TEST 1.2.8", "Test Version", new DateTime(), false, false)
        Promise<Version> version = versionClient.createVersion(versionInput)
        Version versionObj = version.get()
        System.out.println("Created Version:" + versionObj.getName())
         // Invoke the JRJC Client
         Promise<Version> promise = versionClient.getVersion(versionObj.getSelf())
         Version versionid = promise.claim()
         // Print the result
         System.out.println(String.format("Version id is: %s\r\n", versionid.getId() + " and URI:" + versionid.getSelf()))


        // Release Version using Update
         versionClient.updateVersion(versionid.getSelf(),new VersionInput("$JIRA_PROJECT", "TEST 1.2.8", "Test Version", new DateTime(), false, true))

        // Delete the Version
         versionClient.removeVersion(versionid.getSelf(), null, null).claim()
         System.out.println("Deleted Version")

 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM