go-jira icon indicating copy to clipboard operation
go-jira copied to clipboard

Add examples for Issue.Update & Issue.UpdateIssue operations

Open skinlayers opened this issue 2 years ago • 1 comments

What would you like to be added?

Examples to cover updating the value of fields in an existing issue e.g. Priority or custom fields.

Why is this needed?

Current examples cover basic Create, Get, and Delete operations, but the only "Update" example is for Transitions. Searching through Github issues have given hints, but no clear examples. Its is also unclear when to use Issue.Update vs Issue.UpdateIssue vs Transition + payload. It appears that I'm not the only one struggling with this.

Anything else we need to know?

I'm using go-jira v1.16.0 with Jira Server v9.4.1

skinlayers avatar Feb 01 '23 02:02 skinlayers

Example code to update the Priority of an existing issue using the Priority's Name:

package main

import (
	"fmt"
	"os"

	jira "github.com/andygrunwald/go-jira"
)

func main() {
	testIssueID := "GOPHER-123"
	priorityName := "P0"

	tp := jira.BasicAuthTransport{
		Username: os.Getenv("JIRA_USER"),
		Password: os.Getenv("JIRA_TOKEN"),
	}

	jiraClient, err := jira.NewClient(tp.Client(), os.Getenv("JIRA_URL"))
	if err != nil {
		fmt.Printf("\nerror: %v\n", err)
		return
	}

	issue, _, _ := jiraClient.Issue.Get(testIssueID, nil)
	currentPriority := issue.Fields.Priority.Name
	fmt.Printf("Current priority: %s\n", currentPriority)

	update := &jira.Issue{
		Key: testIssueID,
		Fields: &jira.IssueFields{
			Priority: &jira.Priority{
				Name: priorityName,
			},
		},
	}

	issue, _, err = jiraClient.Issue.Update(update)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("Issue %s has been updated with the following priority:\n", issue.Key)
	fmt.Printf("Name: %s\n", issue.Fields.Priority.Name)
}

Example code to update the Priority of an existing issue by looking up the Priority's ID:

package main

import (
	"fmt"
	"os"

	jira "github.com/andygrunwald/go-jira"
)

func main() {
	testIssueID := "GOPHER-123"
	priorityName := "P2"

	tp := jira.BasicAuthTransport{
		Username: os.Getenv("JIRA_USER"),
		Password: os.Getenv("JIRA_TOKEN"),
	}

	jiraClient, err := jira.NewClient(tp.Client(), os.Getenv("JIRA_URL"))
	if err != nil {
		fmt.Printf("\nerror: %v\n", err)
		return
	}

	issue, _, _ := jiraClient.Issue.Get(testIssueID, nil)
	currentPriority := issue.Fields.Priority.Name
	fmt.Printf("Current priority: %s\n", currentPriority)

	var priorityID string
	possiblePriorities, _, _ := jiraClient.Priority.GetList()
	for _, v := range possiblePriorities {
		if v.Name == priorityName {
			priorityID = v.ID
			break
		}
	}

	update := &jira.Issue{
		Key: testIssueID,
		Fields: &jira.IssueFields{
			Priority: &jira.Priority{
				ID:   priorityID,
			},
		},
	}

	issue, _, err = jiraClient.Issue.Update(update)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("Issue %s has been updated with the following priority:\n", issue.Key)
	fmt.Printf("ID: %s\n", issue.Fields.Priority.ID)
}

skinlayers avatar Feb 01 '23 08:02 skinlayers