Skip to content

Commit

Permalink
IssueService: add WithOptions function variants for UpdateIssue (#13
Browse files Browse the repository at this point in the history
)

This enables options to be set for the "edit issue" API endpoint via
query parameters in the `UpdateIssue` family of functions, similarly to
how it currently works for the `Update` family of functions.
  • Loading branch information
chrisnovakovic authored Feb 23, 2024
1 parent 3870a80 commit 7e804f8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
32 changes: 25 additions & 7 deletions issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,26 +890,44 @@ func (s *IssueService) Update(issue *Issue) (*Issue, *Response, error) {
return s.UpdateWithContext(context.Background(), issue)
}

// UpdateIssueWithContext updates an issue from a JSON representation. The issue is found by key.
// UpdateIssueWithOptionsWithContext updates an issue from a JSON representation,
// while also specifying query params. The issue is found by key.
//
// https://docs.atlassian.com/jira/REST/7.4.0/#api/2/issue-editIssue
// Jira API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-editIssue
// Caller must close resp.Body
func (s *IssueService) UpdateIssueWithContext(ctx context.Context, jiraID string, data map[string]interface{}) (*Response, error) {
func (s *IssueService) UpdateIssueWithOptionsWithContext(ctx context.Context, jiraID string, data map[string]interface{}, opts *UpdateQueryOptions) (*Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/2/issue/%v", jiraID)
req, err := s.client.NewRequestWithContext(ctx, "PUT", apiEndpoint, data)
url, err := addOptions(apiEndpoint, opts)
if err != nil {
return nil, err
}
req, err := s.client.NewRequestWithContext(ctx, "PUT", url, data)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return resp, err
jerr := NewJiraError(resp, err)
return resp, jerr
}

// This is just to follow the rest of the API's convention of returning an issue.
// Returning the same pointer here is pointless, so we return a copy instead.
return resp, nil
}

// UpdateIssueWithOptions wraps UpdateIssueWithOptionsWithContext using the background context.
// Caller must close resp.Body
func (s *IssueService) UpdateIssueWithOptions(jiraID string, data map[string]interface{}, opts *UpdateQueryOptions) (*Response, error) {
return s.UpdateIssueWithOptionsWithContext(context.Background(), jiraID, data, opts)
}

// UpdateIssueWithContext updates an issue from a JSON representation. The issue is found by key.
//
// Jira API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-editIssue
// Caller must close resp.Body
func (s *IssueService) UpdateIssueWithContext(ctx context.Context, jiraID string, data map[string]interface{}) (*Response, error) {
return s.UpdateIssueWithOptionsWithContext(ctx, jiraID, data, nil)
}

// UpdateIssue wraps UpdateIssueWithContext using the background context.
// Caller must close resp.Body
func (s *IssueService) UpdateIssue(jiraID string, data map[string]interface{}) (*Response, error) {
Expand Down
23 changes: 23 additions & 0 deletions issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,29 @@ func TestIssueService_Update(t *testing.T) {
}
}

func TestIssueService_UpdateIssueWithOptions(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/rest/api/2/issue/PROJ-9001", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testRequestURL(t, r, "/rest/api/2/issue/PROJ-9001?notifyUsers=false")

w.WriteHeader(http.StatusNoContent)
})
jID := "PROJ-9001"
i := make(map[string]interface{})
fields := make(map[string]interface{})
i["fields"] = fields
opts := &UpdateQueryOptions{NotifyUsers: Bool(false)}
resp, err := testClient.Issue.UpdateIssueWithOptions(jID, i, opts)
if resp == nil {
t.Error("Expected resp. resp is nil")
}
if err != nil {
t.Errorf("Error given: %s", err)
}
}

func TestIssueService_UpdateIssue(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 7e804f8

Please sign in to comment.