Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
skinlayers opened this issue Feb 1, 2023 · 1 comment
Open

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

skinlayers opened this issue Feb 1, 2023 · 1 comment

Comments

@skinlayers
Copy link

skinlayers commented Feb 1, 2023

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 skinlayers changed the title Add examples for Issue.Update operations Add examples for Issue.Update & Issue.UpdateIssue operations Feb 1, 2023
@skinlayers
Copy link
Author

skinlayers commented Feb 1, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant