andygrunwald / go-jira

Go client library for Atlassian Jira
https://pkg.go.dev/github.com/andygrunwald/go-jira?tab=doc
MIT License
1.47k stars 469 forks source link

Add examples for Issue.Update & Issue.UpdateIssue operations #612

Open skinlayers opened 1 year ago

skinlayers commented 1 year ago

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 commented 1 year ago

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)
}