go-jira

repository·main·Indexed 21 days ago

https://github.com/andygrunwald/go-jira

A Go client library for interacting with Atlassian Jira Cloud and self-hosted (Server/Data Center) instances. It provides structured access to issues, transitions, and other Jira resources, while allowing low-level access to any REST API endpoint. The library supports various authentication schemes including Basic Auth, API Tokens, Bearer tokens (PATs), session cookies, and OAuth. Version 2.0.0 introduces separate packages for Cloud and On-Premise APIs to accommodate their divergence.

Tokens
5K
Snippets
15
Records
25
Agent score
80%

What's inside go-jira

  1. Use the go-jira Cloud client for Atlassian Jira

    main
    The cloud package provides an API client library specifically designed for interacting with cloud-hosted Jira instances provided by Atlassian. For general library information, features, and installation instructions, refer to the root README.md.
  2. How authentication works in go-jira

    main

    The go-jira library does not handle authentication directly. Instead, authentication is managed within an http.Client. You create an http.Client (or an http.RoundTripper like jira.BasicAuthTransport) and pass it to jira.NewClient.

    Supported schemes include:

    • Basic Auth / API Tokens: Used for Jira Cloud (using an API token instead of a password) or self-hosted Jira.
    • Bearer (PATs): Used for self-hosted Jira (v8.14+) via Personal Access Tokens.
    • Session Cookie: For cookie-based authentication.
    • OAuth: For connecting via OAuth.
  3. Choose between Jira Cloud and On-Premise packages

    main

    In v2.0.0, you must select the package that matches your Jira deployment type. They are no longer unified.

    • Jira Cloud: Use if your Jira URL ends with .atlassian.net. Import github.com/andygrunwald/go-jira/v2/cloud.
    • Jira On-Premise: Use if your Jira is hosted on your own domain or servers. Import github.com/andygrunwald/go-jira/v2/onpremise.

    Note: The APIs and authentication fields (like APIToken vs Password) differ between these packages.

  4. Choose the correct package for Jira Cloud or On-Premise

    main

    v2.0.0 splits the library into two distinct packages because Jira Cloud and On-Premise (Server/Data Center) APIs have diverged. You must import the package that matches your deployment type:

    Your Jira InstanceImport Path
    Jira Cloud (*.atlassian.net)github.com/andygrunwald/go-jira/v2/cloud
    Jira Server (self-hosted)github.com/andygrunwald/go-jira/v2/onpremise
    Jira Data Center (self-hosted)github.com/andygrunwald/go-jira/v2/onpremise
  5. Migrate from v1.x to v2.0.0: Module and Import Paths

    main

    In v2.0.0, the module path follows Go versioning conventions by adding /v2. You must update your go.mod file and your import statements. Because Cloud and On-Premise APIs differ significantly, they are now in separate packages. Using the alias jira in your imports can help minimize code changes for existing method calls.

    Update go.mod: Change github.com/andygrunwald/go-jira to github.com/andygrunwald/go-jira/v2.

    Update Imports:

    • For Jira Cloud: import jira "github.com/andygrunwald/go-jira/v2/cloud"
    • For Jira On-Premise: import jira "github.com/andygrunwald/go-jira/v2/onpremise"
    - require github.com/andygrunwald/go-jira v1.17.0
    + require github.com/andygrunwald/go-jira/v2 v2.0.0
    // v2.0.0 - Jira Cloud
    import jira "github.com/andygrunwald/go-jira/v2/cloud"
    
    // v2.0.0 - Jira On-Premise (Server/Data Center)
    import jira "github.com/andygrunwald/go-jira/v2/onpremise"
  6. Migrate Service Methods in v2.0.0

    main

    Many service methods have been consolidated or renamed in v2.0.0. A common pattern is that methods with *WithOptions variants have been merged into a single method that accepts an optional parameters object (pass nil for default options).

    Key Service Changes:

    ServiceOld Method(s)New Method (v2.0.0)
    BoardGetAllSprints / GetAllSprintsWithOptionsGetAllSprints(ctx, id, opts)
    GroupGet / GetWithOptionsGet(ctx, name, opts)
    Group (Cloud)Add / RemoveAddUserByGroupName / RemoveUserByGroupName
    IssueUpdate / UpdateWithOptionsUpdate(ctx, issue, opts)
    Issue (Cloud)GetCreateMeta / GetCreateMetaWithOptionsGetCreateMeta(ctx, opts)
    ProjectGetList / ListWithOptionsGetAll(ctx, opts)
    User (Cloud)GetSelfGetCurrentUser(ctx)
    Component (Cloud)CreateComponentOptions (Type)ComponentCreateOptions (Type)
  7. Configure Authentication for Jira Cloud in v2.0.0

    main

    Jira Cloud authentication is streamlined and requires using an API token via BasicAuthTransport.

    Key Changes:

    • Use github.com/andygrunwald/go-jira/v2/cloud.
    • BasicAuthTransport.Password has been renamed to BasicAuthTransport.APIToken.
    • BearerAuthTransport, PATAuthTransport, and CookieAuthTransport are no longer supported for Cloud.
    import jira "github.com/andygrunwald/go-jira/v2/cloud"
    
    tp := jira.BasicAuthTransport{
        Username: "user@example.com",
        APIToken: "your-api-token",  // Note: Field renamed from Password to APIToken
    }
    client, err := jira.NewClient("https://example.atlassian.net/", tp.Client())
  8. Configure Authentication for Jira On-Premise in v2.0.0

    main

    Jira On-Premise (Server/Data Center) supports multiple authentication methods via different transports. Use github.com/andygrunwald/go-jira/v2/onpremise.

    Available Transports:

    • BasicAuthTransport: Username/password authentication.
    • BearerAuthTransport: OAuth 2.0 or Personal Access Tokens (Jira 8.14+).
    • CookieAuthTransport: Session-based authentication.
    • PersonalAccessTokenAuthTransport: PAT authentication.
    • JWTAuthTransport: JWT authentication for add-ons.
    import jira "github.com/andygrunwald/go-jira/v2/onpremise"
    
    // Basic Auth
    tp := jira.BasicAuthTransport{
        Username: "admin",
        Password: "secret",
    }
    client, err := jira.NewClient("https://jira.example.com/", tp.Client())
    
    // Using Personal Access Tokens (Jira 8.14+)
    tp := jira.BearerAuthTransport{
        Token: "your-personal-access-token",
    }
    client, err := jira.NewClient("https://jira.example.com/", tp.Client())
  9. Migrate from go-jira v1.x to v2.0.0

    main

    Migrating to v2.0.0 involves significant breaking changes due to the split between Jira Cloud and On-Premise APIs.

    Quick Checklist

    • Update go.mod to use github.com/andygrunwald/go-jira/v2.
    • Change imports to use either the /v2/cloud or /v2/onpremise subpackages.
    • Swap NewClient arguments: use NewClient(baseURL, httpClient) (previously NewClient(httpClient, baseURL)).
    • Add context.Context as the first argument to all service method calls.
    • Remove WithContext suffix from method names (e.g., GetWithContext becomes Get).
    • Update authentication transport field names (Cloud: Password -> APIToken).

    Prerequisites

    • Go Version: Requires Go 1.21 or later.
  10. Migrate to Context-Required API Methods in v2.0.0

    main

    In v2.0.0, all API methods (both Request methods and Service methods) require a context.Context as their first argument. The previous *WithContext suffix methods have been removed.

    Migration Patterns:

    • For a quick migration, use context.Background() or context.TODO().
    • For production code, use a context with a timeout (e.g., context.WithTimeout).
    // Quick migration
    issue, resp, err := client.Issue.Get(context.Background(), "KEY-123", nil)
    
    // Better: use a proper context with timeout
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()
    issue, resp, err := client.Issue.Get(ctx, "KEY-123", nil)