forked from owenthereal/ccat
/
issues.go
234 lines (185 loc) · 5.71 KB
/
issues.go
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package sourcegraph
import (
"fmt"
"github.com/jingweno/ccat/Godeps/_workspace/src/github.com/sourcegraph/go-github/github"
"strconv"
"strings"
"github.com/jingweno/ccat/Godeps/_workspace/src/sourcegraph.com/sourcegraph/go-sourcegraph/router"
)
// IssuesService communicates with the issue-related endpoints in the
// Sourcegraph API.
type IssuesService interface {
// Get fetches a issue.
Get(issue IssueSpec, opt *IssueGetOptions) (*Issue, Response, error)
// List issues for a repository.
ListByRepo(repo RepoSpec, opt *IssueListOptions) ([]*Issue, Response, error)
// ListComments lists comments on a issue.
ListComments(issue IssueSpec, opt *IssueListCommentsOptions) ([]*IssueComment, Response, error)
// CreateComment creates a comment on an issue.
CreateComment(issue IssueSpec, comment *IssueComment) (*IssueComment, Response, error)
// EditComment updates a comment on an issue.
EditComment(issue IssueSpec, comment *IssueComment) (*IssueComment, Response, error)
// DeleteComment deletes a comment on an issue.
DeleteComment(issue IssueSpec, commentID int) (Response, error)
}
// issuesService implements IssuesService.
type issuesService struct {
client *Client
}
var _ IssuesService = &issuesService{}
// IssueSpec specifies a issue.
type IssueSpec struct {
Repo RepoSpec
Number int // Sequence number of the issue
}
func (s IssueSpec) RouteVars() map[string]string {
return map[string]string{"RepoSpec": s.Repo.URI, "Issue": strconv.Itoa(s.Number)}
}
func UnmarshalIssueSpec(routeVars map[string]string) (IssueSpec, error) {
issueNumber, err := strconv.Atoi(routeVars["Issue"])
if err != nil {
return IssueSpec{}, err
}
repoURI := routeVars["RepoSpec"]
if repoURI == "" {
return IssueSpec{}, fmt.Errorf("RepoSpec was empty")
}
return IssueSpec{
Repo: RepoSpec{URI: repoURI},
Number: issueNumber,
}, nil
}
// Issue is a issue returned by the Sourcegraph API.
type Issue struct {
github.Issue
}
// Spec returns the IssueSpec that specifies r.
func (r *Issue) Spec() IssueSpec {
// Extract the URI from the HTMLURL field.
uri := strings.Join(strings.Split(strings.TrimPrefix(*r.HTMLURL, "https://"), "/")[0:3], "/")
return IssueSpec{
Repo: RepoSpec{URI: uri},
Number: *r.Number,
}
}
type IssueGetOptions struct{}
func (s *issuesService) Get(issue IssueSpec, opt *IssueGetOptions) (*Issue, Response, error) {
url, err := s.client.URL(router.RepoIssue, issue.RouteVars(), opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, nil, err
}
var issue_ *Issue
resp, err := s.client.Do(req, &issue_)
if err != nil {
return nil, resp, err
}
return issue_, resp, nil
}
type IssueListOptions struct {
State string `url:",omitempty"` // "open", "closed", or "all"
ListOptions
}
func (s *issuesService) ListByRepo(repo RepoSpec, opt *IssueListOptions) ([]*Issue, Response, error) {
url, err := s.client.URL(router.RepoIssues, repo.RouteVars(), opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, nil, err
}
var issues []*Issue
resp, err := s.client.Do(req, &issues)
if err != nil {
return nil, resp, err
}
return issues, resp, nil
}
type IssueListCommentsOptions struct {
ListOptions
}
type IssueComment struct {
RenderedBody string // the body rendered to HTML (from raw markdown)
Checklist *Checklist
github.IssueComment
}
type IssueCommentSpec struct {
Issue IssueSpec
Comment int
}
func (s IssueCommentSpec) RouteVars() map[string]string {
rv := s.Issue.RouteVars()
rv["CommentID"] = strconv.Itoa(s.Comment)
return rv
}
func (s *issuesService) ListComments(issue IssueSpec, opt *IssueListCommentsOptions) ([]*IssueComment, Response, error) {
url, err := s.client.URL(router.RepoIssueComments, issue.RouteVars(), opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, nil, err
}
var comments []*IssueComment
resp, err := s.client.Do(req, &comments)
if err != nil {
return nil, resp, err
}
return comments, resp, nil
}
func (s *issuesService) CreateComment(issue IssueSpec, comment *IssueComment) (*IssueComment, Response, error) {
url, err := s.client.URL(router.RepoIssueCommentsCreate, issue.RouteVars(), nil)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("POST", url.String(), comment)
if err != nil {
return nil, nil, err
}
var createdComment IssueComment
resp, err := s.client.Do(req, &createdComment)
if err != nil {
return nil, nil, err
}
return &createdComment, resp, nil
}
func (s *issuesService) EditComment(issue IssueSpec, comment *IssueComment) (*IssueComment, Response, error) {
if comment.ID == nil {
return nil, nil, fmt.Errorf("comment ID not specified")
}
url, err := s.client.URL(router.RepoIssueCommentsEdit, IssueCommentSpec{Issue: issue, Comment: *comment.ID}.RouteVars(), nil)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("PATCH", url.String(), comment)
if err != nil {
return nil, nil, err
}
var updatedComment IssueComment
resp, err := s.client.Do(req, &updatedComment)
if err != nil {
return nil, nil, err
}
return &updatedComment, resp, nil
}
func (s *issuesService) DeleteComment(issue IssueSpec, commentID int) (Response, error) {
url, err := s.client.URL(router.RepoIssueCommentsDelete, IssueCommentSpec{Issue: issue, Comment: commentID}.RouteVars(), nil)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("DELETE", url.String(), nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return nil, err
}
return resp, nil
}
var _ IssuesService = &MockIssuesService{}