Skip to content

Commit c1a4cf2

Browse files
authored
Wrap issue label API (#189)
1 parent 937c0b5 commit c1a4cf2

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Here's a table that matches up the provided `GitHubType`s with their correspondi
4848
| `Branch` | name, e.g. `master` | [repository branches](https://developer.github.com/v3/repos/#get-branch) |
4949
| `Content` | path, e.g. `"src/owners/owners.jl"` | [repository contents](https://developer.github.com/v3/repos/contents/) |
5050
| `Comment` | id, e.g. `162224613` | [commit comments](https://developer.github.com/v3/repos/comments/), [issue comments](https://developer.github.com/v3/issues/comments/), [PR review comments](https://developer.github.com/v3/pulls/comments/) |
51+
| `Label` | name, e.g. `bug` | [issue labels](https://docs.github.com/en/rest/reference/issues#labels)
5152
| `Status` | id, e.g. `366961773` | [commit statuses](https://developer.github.com/v3/repos/statuses/) |
5253
| `PullRequest` | number, e.g. `44` | [pull requests](https://developer.github.com/v3/pulls/) |
5354
| `Issue` | number, e.g. `31` | [issues](https://developer.github.com/v3/issues/) |
@@ -172,6 +173,17 @@ GitHub.jl implements a bunch of methods that make REST requests to GitHub's API.
172173
| `delete_comment(repo, comment, :commit)` | `HTTP.Response` | [delete the commit`comment` from `repo`](https://developer.github.com/v3/repos/comments/#delete-a-commit-comment) |
173174
| `reply_to(repo, review, comment, body)` | `HTTP.Response` | [reply to the `comment` (of `review` in `repo`) creating a new comment with the specified `body`](https://developer.github.com/v3/pulls/comments/#alternative-input) |
174175

176+
#### Labels
177+
178+
179+
| method | return type | documentation |
180+
|------------------------------------------|--------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
181+
| `labels(repo,issue)` | `Vector{Label}` | [list labels from `issue`](https://docs.github.com/en/rest/reference/issues#list-labels-for-an-issue) |
182+
| `add_labels(repo, issue, labels)` | `Vector{Label}` | [add labels to an `issue`](https://docs.github.com/en/rest/reference/issues#add-labels-to-an-issue) |
183+
| `set_labels(repo, issue, labels)` | `Vector{Label}` | [set the labels for an `issue`](https://docs.github.com/en/rest/reference/issues#set-labels-for-an-issue)
184+
| `remove_all_labels(repo, issue)` | `HTTP.Response` | [remove all labels from an `issue`](https://docs.github.com/en/rest/reference/issues#remove-all-labels-from-an-issue)
185+
| `remove_label(repo, issue, label)` | `HTTP.Response` | [remove a label from an `issue`](https://docs.github.com/en/rest/reference/issues#remove-a-label-from-an-issue)
186+
175187
#### Social Activity
176188

177189
| method | return type | documentation |

src/GitHub.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ include("issues/pull_requests.jl")
181181
include("issues/issues.jl")
182182
include("issues/comments.jl")
183183
include("issues/reviews.jl")
184+
include("issues/labels.jl")
184185

185186
# export -------
186187

@@ -217,6 +218,13 @@ export # reviews.jl
217218
reply_to,
218219
dismiss_review
219220

221+
export # labels.jl
222+
Label,
223+
labels,
224+
add_labels,
225+
set_labels,
226+
remove_all_labels,
227+
remove_label
220228

221229
#########
222230
# Gists #

src/issues/labels.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@ghdef mutable struct Label
2+
name::Union{String, Nothing}
3+
default::Union{Bool, Nothing}
4+
id::Union{Int, Nothing}
5+
color::Union{String, Nothing}
6+
node_id::Union{String, Nothing}
7+
url::Union{String, Nothing}
8+
description::Union{String, Nothing}
9+
end
10+
11+
namefield(label::Label) = label.name
12+
13+
Label(name::AbstractString) = Label(Dict("name" => name))
14+
15+
@api_default function labels(api::GitHubAPI, repo, issue; options...)
16+
result = gh_get_json(api, "/repos/$(name(repo))/issues/$(name(issue))/labels"; options...)
17+
return Label.(result)
18+
end
19+
20+
@api_default function add_labels(api::GitHubAPI, repo, issue, labels; options...)
21+
result = gh_post_json(api, "/repos/$(name(repo))/issues/$(name(issue))/labels";
22+
params=Dict("labels" => name.(labels)), options...)
23+
return Label.(result)
24+
end
25+
26+
@api_default function set_labels(api::GitHubAPI, repo, issue, labels; options...)
27+
result = gh_put_json(api, "/repos/$(name(repo))/issues/$(name(issue))/labels";
28+
params=Dict("labels" => name.(labels)), options...)
29+
return Label.(result)
30+
end
31+
32+
@api_default function remove_all_labels(api::GitHubAPI, repo, issue; options...)
33+
return gh_delete(api, "/repos/$(name(repo))/issues/$(name(issue))/labels"; options...)
34+
end
35+
36+
@api_default function remove_label(api::GitHubAPI, repo, issue, label; options...)
37+
return gh_delete(api, "/repos/$(name(repo))/issues/$(name(issue))/labels/$(name(label))"; options...)
38+
end

test/ghtype_tests.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,35 @@ end
305305
test_show(comment_result)
306306
end
307307

308+
@testset "Label" begin
309+
label_json = JSON.parse(
310+
"""
311+
{
312+
"id": 208045946,
313+
"node_id": "MDU6TGFiZWwyMDgwNDU5NDY=",
314+
"url": "https://api.github.com/repos/octocat/Hello-World/labels/bug",
315+
"name": "bug",
316+
"description": "Something isn't working",
317+
"color": "f29513",
318+
"default": true
319+
}
320+
"""
321+
)
322+
323+
label_result = Label(String(label_json["name"]),
324+
Bool(label_json["default"]),
325+
Int(label_json["id"]),
326+
String(label_json["color"]),
327+
String(label_json["node_id"]),
328+
String(label_json["url"]),
329+
String(label_json["description"]))
330+
331+
332+
@test Label(label_json) == label_result
333+
@test name(Label(label_json["name"])) == name(label_result)
334+
test_show(label_result)
335+
end
336+
308337
@testset "Content" begin
309338
content_json = JSON.parse(
310339
"""

0 commit comments

Comments
 (0)