-
Notifications
You must be signed in to change notification settings - Fork 74
/
Issues.scala
348 lines (330 loc) · 11.9 KB
/
Issues.scala
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright 2016-2019 47 Degrees, LLC. <http://www.47deg.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package github4s.api
import github4s.GithubResponses.GHResponse
import github4s.{GithubApiUrls, HttpClient, HttpRequestBuilderExtension}
import github4s.free.domain._
import github4s.free.interpreters.Capture
import github4s.util.URLEncoder
import io.circe.syntax._
import io.circe.generic.auto._
/** Factory to encapsulate calls related to Issues operations */
class Issues[C, M[_]](
implicit urls: GithubApiUrls,
C: Capture[M],
httpClientImpl: HttpRequestBuilderExtension[C, M]) {
val httpClient = new HttpClient[C, M]
/**
* List issues for a repository
*
* Note: In the past, pull requests and issues were more closely aligned than they are now.
* As far as the API is concerned, every pull request is an issue, but not every issue is a
* pull request.
*
* This endpoint may also return pull requests in the response. If an issue is a pull request,
* the object will include a `pull_request` key.
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @return a GHResponse with the issue list.
*/
def list(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String): M[GHResponse[List[Issue]]] =
httpClient.get[List[Issue]](accessToken, s"repos/$owner/$repo/issues", headers)
/**
* Get a single issue of a repository
*
* Note: In the past, pull requests and issues were more closely aligned than they are now.
* As far as the API is concerned, every pull request is an issue, but not every issue is a
* pull request.
*
* This endpoint may also return pull requests in the response. If an issue is a pull request,
* the object will include a `pull_request` key.
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @param number Issue number
* @return a GHResponse with the issue list.
*/
def get(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
number: Int): M[GHResponse[Issue]] =
httpClient.get[Issue](accessToken, s"repos/$owner/$repo/issues/$number", headers)
/**
* Search for issues
*
* Note: In the past, pull requests and issues were more closely aligned than they are now.
* As far as the API is concerned, every pull request is an issue, but not every issue is a
* pull request.
*
* This endpoint may also return pull requests in the response. If an issue is a pull request,
* the object will include a `pull_request` key.
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param query the query string for the search
* @param searchParams list of search params
* @return a GHResponse with the result of the search.
*/
def search(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
query: String,
searchParams: List[SearchParam]): M[GHResponse[SearchIssuesResult]] = {
val queryString = s"${URLEncoder.encode(query)}+${searchParams.map(_.value).mkString("+")}"
httpClient
.get[SearchIssuesResult](accessToken, "search/issues", headers, Map("q" -> queryString))
}
/**
* Create an issue
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @param title The title of the issue.
* @param body The contents of the issue.
* @param milestone The number of the milestone to associate this issue with.
* @param labels Labels to associate with this issue.
* @param assignees Logins for Users to assign to this issue.
*/
def create(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
title: String,
body: String,
milestone: Option[Int],
labels: List[String],
assignees: List[String]): M[GHResponse[Issue]] =
httpClient.post[Issue](
accessToken,
s"repos/$owner/$repo/issues",
headers,
data = NewIssueRequest(title, body, milestone, labels, assignees).asJson.noSpaces)
/**
* Edit an issue
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @param issue number
* @param state State of the issue. Either open or closed.
* @param title The title of the issue.
* @param body The contents of the issue.
* @param milestone The number of the milestone to associate this issue with.
* @param labels Labels to associate with this issue.
* Pass one or more Labels to replace the set of Labels on this Issue.
* Send an empty list to clear all Labels from the Issue.
* @param assignees Logins for Users to assign to this issue.
* Pass one or more user logins to replace the set of assignees on this Issue.
* Send an empty list to clear all assignees from the Issue.
*/
def edit(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
issue: Int,
state: String,
title: String,
body: String,
milestone: Option[Int],
labels: List[String],
assignees: List[String]): M[GHResponse[Issue]] =
httpClient.patch[Issue](
accessToken,
s"repos/$owner/$repo/issues/$issue",
headers,
data = EditIssueRequest(state, title, body, milestone, labels, assignees).asJson.noSpaces)
/**
* List comments to an Issue
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @param number Issue number
* @return a GHResponse with the comment list of the Issue.
*/
def listComments(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
number: Int): M[GHResponse[List[Comment]]] =
httpClient.get[List[Comment]](accessToken, s"repos/$owner/$repo/issues/$number/comments", headers)
/**
* Create a comment
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @param number Issue number
* @param body Comment body
* @return a GHResponse with the created Comment
*/
def createComment(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
number: Int,
body: String): M[GHResponse[Comment]] =
httpClient.post[Comment](
accessToken,
s"repos/$owner/$repo/issues/$number/comments",
headers,
CommentData(body).asJson.noSpaces)
/**
* Edit a comment
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @param id Comment id
* @param body Comment body
* @return a GHResponse with the edited Comment
*/
def editComment(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
id: Int,
body: String): M[GHResponse[Comment]] =
httpClient
.patch[Comment](
accessToken,
s"repos/$owner/$repo/issues/comments/$id",
headers,
CommentData(body).asJson.noSpaces)
/**
* Delete a comment
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @param id Comment id
* @return a unit GHResponse
*/
def deleteComment(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
id: Int): M[GHResponse[Unit]] =
httpClient.delete(accessToken, s"repos/$owner/$repo/issues/comments/$id", headers)
/**
* List the labels assigned to an Issue
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @param number Issue number
* @return a GHResponse with the list of labels for the Issue.
*/
def listLabels(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
number: Int): M[GHResponse[List[Label]]] =
httpClient.get[List[Label]](accessToken, s"repos/$owner/$repo/issues/$number/labels", headers)
/**
* Add the specified labels to an Issue
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @param number Issue number
* @param labels the list of labels to add to the issue
* @return a GHResponse with the list of labels added to the Issue.
*/
def addLabels(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
number: Int,
labels: List[String]): M[GHResponse[List[Label]]] =
httpClient.post[List[Label]](
accessToken,
s"repos/$owner/$repo/issues/$number/labels",
headers,
labels.asJson.noSpaces)
/**
* Remove the specified label from an Issue
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner of the repo
* @param repo name of the repo
* @param number Issue number
* @param label the name of the label to remove from the issue
* @return a GHResponse with the list of labels removed from the Issue.
*/
def removeLabel(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
number: Int,
label: String): M[GHResponse[List[Label]]] =
httpClient.deleteWithResponse[List[Label]](
accessToken,
s"repos/$owner/$repo/issues/$number/labels/$label",
headers)
/**
* List available assignees for issues
*
* @param accessToken to identify the authenticated user
* @param headers optional user headers to include in the request
* @param owner repo owner
* @param repo repo name
* @param pagination Limit and Offset for pagination
* @return a GHResponse with the list of available assignees for issues in specified repository
*/
def listAvailableAssignees(
accessToken: Option[String] = None,
headers: Map[String, String] = Map(),
owner: String,
repo: String,
pagination: Option[Pagination] = None): M[GHResponse[List[User]]] =
httpClient.get[List[User]](
accessToken,
s"repos/$owner/$repo/assignees",
headers,
pagination = pagination
)
}