DEV Community

Cover image for Automate Jira with Golang
Mathias Jiya
Mathias Jiya

Posted on

6 1

Automate Jira with Golang

In today's article, I would be showing you how to automate Jira with Golang.
Jira Software is part of a family of products designed to help teams of all types manage work. Originally, Jira was designed as a bug and issue tracker. But today, Jira has evolved into a powerful work management tool for all kinds of use cases, from requirements and test case management to agile software development.
In this article we would be using a go-jira library. To get started, you would need to head over to jira atlas to generate your API token. Ensure to copy your token when you see the copy button after creating as you can't view it afterwards.
For the authentication, we would need:

  • your jira token
  • your jira username
  • your jira cloud url

which would be stored in a .env file as

JIRA_TOKEN=<jira token can be generated here: https://id.atlassian.com/manage-profile/security/api-tokens>
JIRA_USER=<your email address>
JIRA_URL=<jira url can be gotten from the url (https://username.atlassian.net/jira/software/projects/<projectname>/boards) by extracting this (https://name.atlassian.net) only >
Enter fullscreen mode Exit fullscreen mode

Now lets get to the fun part where we get to write some code :)


package main

import (
    "fmt"
    "os"
    "github.com/joho/godotenv"
    jira "github.com/andygrunwald/go-jira"
)

func init() {
    if envErr := godotenv.Load(".env"); envErr != nil {
        fmt.Println(".env file missing")
    }
}

func jiraClient() *jira.Client {
    jt := jira.BasicAuthTransport{
        Username: os.Getenv("JIRA_USER"),
        Password: os.Getenv("JIRA_TOKEN"),
    }

    client, err := jira.NewClient(jt.Client(), os.Getenv("JIRA_URL"))
    if err != nil {
        fmt.Println(err)
    }

    me, _, err := client.User.GetSelf()
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(me)

    return client
}
Enter fullscreen mode Exit fullscreen mode

To create new issue on jira

  • Note that the Create an issue example on go-jira repo would be a bit different from the example I would be giving below and that is due to a bug in the package right now. You would get an error if you try to use the example code from the go-jira repo to create an issue. The way round that is what gave birth to the example below to create an issue on jira
func CreateNewIssue() {

    client := jiraClient()
    i := jira.Issue{
        Fields: &jira.IssueFields{
            Description: "your issue description",
            Type: jira.IssueType{
                Name: "you issue type e.g Bug, Task e.t.c",
            },
            Project: jira.Project{
                Key: "your project key- this can be found under the project settings",
            },
            Summary: "your issue summary",
        },
    }
    issue, _, err := client.Issue.Create(&i)
    if err != nil {
        fmt.Println(err)
    }

    //update the assignee on the issue just created
    _, assignErr := client.Issue.UpdateAssignee(issue.ID, &jira.User{
        AccountID: "the assignee Id",
//to get the assignee id, on your dashboard click on people.
//From the dropdown menu click on search people and teams and then click on the user you wish to assign a task to
//and then you should see this in ur url https://name.atlassian.net/jira/people/62e45a423780798663d14427.
//The id <62e45a423780798663d14427> after people endpoint is the assignee id    
})

    if assignErr != nil {
        fmt.Println(assignErr)
    }

    fmt.Println(issue)
}
Enter fullscreen mode Exit fullscreen mode

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (3)

Collapse
 
mocart2 profile image
mocart2

Hi! Can you help me please? How i can assignee user to issue by username ? In this example, you use assignee id, i have only username, like supermike

Collapse
 
mocart2 profile image
mocart2 • Edited

i found solution!

_, assignErr := jiraClient.Issue.UpdateAssignee(ctx, i.ID, &jira.User{Name: "supermike"})
Enter fullscreen mode Exit fullscreen mode
Collapse
 
iqquee profile image
Mathias Jiya

I glad you found a solution to that aleady. Now because of a "bug" in the go-jira package, you will get an error if you try to add an assigned user while creating a new issue(just as it has been explained in the article).
The way around that is to create an issue without assigning a user and then you update the issue you just created by added the user then it will work just fine.

PulumiUP 2025 image

PulumiUP 2025: Cloud Innovation Starts Here

Get inspired by experts at PulumiUP. Discover the latest in platform engineering, IaC, and DevOps. Keynote, demos, panel, and Q&A with Pulumi engineers.

Register Now

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay