Automate Jira with Golang
Published: 2022-08-01 14:54:33

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](https://github.com/andygrunwald/go-jira) library. To get started, you would need to head over to [jira atlas](https://id.atlassian.com/manage-profile/security/api-tokens) 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 

```ssh
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 :)

```go

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](https://github.com/andygrunwald/go-jira#create-an-issue) 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
```go
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)
}
```