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 >
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
}
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)
}
Top comments (3)
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
i found solution!
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.