-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissue_collaboration.go
More file actions
128 lines (113 loc) · 4.05 KB
/
Copy pathissue_collaboration.go
File metadata and controls
128 lines (113 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// CreateIssueWithAssignee creates an issue with an optional assignee.
// Accepts HTTP 200 or 201 and does not retry.
func CreateIssueWithAssignee(client *Client, owner, repo, title, body, assignee string) (*Issue, error) {
requestBody := map[string]string{
"title": title,
"body": body,
}
if assignee != "" {
requestBody["assignee"] = assignee
}
jsonBody, err := json.Marshal(requestBody)
if err != nil {
return nil, fmt.Errorf("marshal issue create body: %w", err)
}
path := fmt.Sprintf("/repos/%s/%s/issues", url.PathEscape(owner), url.PathEscape(repo))
var issue Issue
err = client.doJSONRequest(http.MethodPost, path, bytes.NewReader(jsonBody),
"application/json", "application/json",
RequestPolicy{AllowedStatuses: []int{http.StatusOK, http.StatusCreated}, CanRetry: false},
&issue)
if err != nil {
return nil, fmt.Errorf("create issue with assignee: %w", err)
}
return &issue, nil
}
// EditIssueAssignee edits an issue's assignee via multipart PATCH.
// When clearAssignee is true, assignee is set to empty string to clear the assignee.
// Uses the existing PatchForm which enforces exact HTTP 200.
func EditIssueAssignee(client *Client, owner, repo, number, title string, body *string, assignee string, clearAssignee bool) error {
path := fmt.Sprintf("/repos/%s/issues/%s", url.PathEscape(owner), url.PathEscape(number))
fields := map[string]string{
"repo": repo,
"title": title,
}
if body != nil {
fields["body"] = *body
}
if clearAssignee {
fields["assignee"] = ""
} else if assignee != "" {
fields["assignee"] = assignee
}
if err := client.PatchForm(path, fields, nil); err != nil {
return fmt.Errorf("edit issue assignee: %w", err)
}
return nil
}
// ListIssueLinkedPullRequests returns pull requests linked to an issue.
// Uses exact HTTP 200.
func ListIssueLinkedPullRequests(client *Client, owner, repo, number string) ([]IssueLinkedPullRequest, error) {
path := fmt.Sprintf("/repos/%s/%s/issues/%s/pull_requests",
url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(number))
var prs []IssueLinkedPullRequest
err := client.doJSONRequest(http.MethodGet, path, nil,
"", "application/json",
RequestPolicy{AllowedStatuses: []int{http.StatusOK}, CanRetry: true},
&prs)
if err != nil {
return nil, fmt.Errorf("list issue linked pull requests: %w", err)
}
if prs == nil {
prs = []IssueLinkedPullRequest{}
}
return prs, nil
}
// ListIssueRelatedBranches returns the related branches for an issue as a string array.
// Uses exact HTTP 200.
func ListIssueRelatedBranches(client *Client, owner, repo, number string) ([]string, error) {
path := fmt.Sprintf("/repos/%s/%s/issues/%s/related_branches",
url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(number))
var branches []string
err := client.doJSONRequest(http.MethodGet, path, nil,
"", "application/json",
RequestPolicy{AllowedStatuses: []int{http.StatusOK}, CanRetry: true},
&branches)
if err != nil {
return nil, fmt.Errorf("list issue related branches: %w", err)
}
if branches == nil {
branches = []string{}
}
return branches, nil
}
// UpdateIssueRelatedBranches replaces the related branch set for an issue.
// Uses exact HTTP 200 and no retry (whole-list replacement).
func UpdateIssueRelatedBranches(client *Client, owner, repo, number string, branches []string) error {
path := fmt.Sprintf("/repos/%s/%s/issues/%s/related_branches",
url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(number))
if branches == nil {
branches = []string{}
}
reqBody := RelatedBranchesRequest{BranchNames: branches}
jsonBody, err := json.Marshal(reqBody)
if err != nil {
return fmt.Errorf("marshal related branches body: %w", err)
}
err = client.doJSONRequest(http.MethodPut, path, bytes.NewReader(jsonBody),
"application/json", "application/json",
RequestPolicy{AllowedStatuses: []int{http.StatusOK}, CanRetry: false},
nil)
if err != nil {
return fmt.Errorf("update issue related branches: the remote state may have changed; re-list and retry: %w", err)
}
return nil
}