Skip to content

Commit 0ab9ee3

Browse files
authored
Add license endpoints and type (#177)
* Add license endpoints and type
1 parent 8f41505 commit 0ab9ee3

File tree

7 files changed

+144
-2
lines changed

7 files changed

+144
-2
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ GitHub.jl implements a bunch of methods that make REST requests to GitHub's API.
237237
| `update_check_run(repo, id::Int; params=...)` | `CheckRun` | [Update the check run with the given `id`](https://developer.github.com/v3/checks/runs/#update-a-check-run) |
238238

239239

240+
#### Licenses
241+
| method | return type | documentation |
242+
| --------------------------------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
243+
| `licenses(; params=...)` | `Vector{License}` | [Get all commonly used licenses](https://docs.github.com/en/free-pro-team@latest/rest/reference/licenses#get-all-commonly-used-licenses) |
244+
| `license(license_id; params=...)` | `License` | [Get a license](https://docs.github.com/en/free-pro-team@latest/rest/reference/licenses#get-a-license) |
245+
| `repo_license(repo; params=...)` | `Content` | [Get the license for a respository](https://docs.github.com/en/free-pro-team@latest/rest/reference/licenses#get-the-license-for-a-repository) |
246+
240247
#### Miscellaneous
241248

242249
| method | return type | documentation |

src/GitHub.jl

+16
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ export # teams.jl
7979
Team,
8080
members
8181

82+
############
83+
# Licenses #
84+
############
85+
86+
# include -------
87+
88+
include("licenses/license.jl")
89+
90+
# export -------
91+
92+
export # license.jl
93+
License,
94+
licenses,
95+
license,
96+
repo_license
97+
8298
################
8399
# Repositories #
84100
################

src/licenses/license.jl

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
################
2+
# License Type #
3+
################
4+
5+
@ghdef mutable struct License
6+
key::Union{String, Nothing}
7+
name::Union{String, Nothing}
8+
spdx_id::Union{String, Nothing}
9+
url::Union{HTTP.URI, Nothing}
10+
html_url::Union{HTTP.URI, Nothing}
11+
description::Union{String, Nothing}
12+
permissions::Union{Vector{String}, Nothing}
13+
conditions::Union{Vector{String}, Nothing}
14+
limitations::Union{Vector{String}, Nothing}
15+
body::Union{String, Nothing}
16+
featured::Union{Bool, Nothing}
17+
end
18+
19+
License(spdx_id::AbstractString) = License(Dict("spdx_id" => spdx_id))
20+
21+
namefield(license::License) = license.spdx_id
22+
23+
###############
24+
# API Methods #
25+
###############
26+
27+
@api_default function licenses(api::GitHubAPI; options...)
28+
results, page_data = gh_get_paged_json(api, "/licenses"; options...)
29+
results = [License(result) for result in results]
30+
return results, page_data
31+
end
32+
33+
@api_default function license(api::GitHubAPI, license_id; options...)
34+
result = gh_get_json(api, "/licenses/$license_id"; options...)
35+
return License(result)
36+
end
37+
38+
@api_default function repo_license(api::GitHubAPI, repo_obj; options...)
39+
result = gh_get_json(api, "/repos/$(name(repo_obj))/license"; options...)
40+
return Content(result)
41+
end

src/repositories/contents.jl

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
html_url::Union{HTTP.URI, Nothing}
1717
download_url::Union{HTTP.URI, Nothing}
1818
size::Union{Int, Nothing}
19+
license::Union{License, Nothing}
1920
end
2021

2122
Content(path::AbstractString) = Content(Dict("path" => path))

src/repositories/repositories.jl

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
has_wiki::Union{Bool, Nothing}
3131
has_downloads::Union{Bool, Nothing}
3232
has_pages::Union{Bool, Nothing}
33+
license::Union{License, Nothing}
3334
private::Union{Bool, Nothing}
3435
fork::Union{Bool, Nothing}
3536
permissions::Union{Dict, Nothing}

test/ghtype_tests.jl

+52-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ end
129129
nothing,
130130
nothing,
131131
nothing,
132+
nothing,
132133
Bool(repo_json["private"]),
133134
nothing,
134135
repo_json["permissions"]
@@ -330,7 +331,8 @@ end
330331
nothing,
331332
nothing,
332333
nothing,
333-
content_json["size"]
334+
content_json["size"],
335+
nothing
334336
)
335337

336338
@test Content(content_json) == content_result
@@ -1300,3 +1302,52 @@ end
13001302

13011303

13021304
end
1305+
1306+
@testset "License" begin
1307+
license_json = JSON.parse(
1308+
"""
1309+
{
1310+
"key": "mit",
1311+
"name": "MIT License",
1312+
"spdx_id": "MIT",
1313+
"url": "https://api.github.com/licenses/mit",
1314+
"node_id": "MDc6TGljZW5zZW1pdA==",
1315+
"html_url": "http://choosealicense.com/licenses/mit/",
1316+
"description": "A permissive license that is short and to the point. It lets people do anything with your code with proper attribution and without warranty.",
1317+
"implementation": "Create a text file (typically named LICENSE or LICENSE.txt) in the root of your source code and copy the text of the license into the file. Replace [year] with the current year and [fullname] with the name (or names) of the copyright holders.",
1318+
"permissions": [
1319+
"commercial-use",
1320+
"modifications",
1321+
"distribution",
1322+
"sublicense",
1323+
"private-use"
1324+
],
1325+
"conditions": [
1326+
"include-copyright"
1327+
],
1328+
"limitations": [
1329+
"no-liability"
1330+
],
1331+
"body": "\\n\\nThe MIT License (MIT)\\n\\nCopyright (c) [year] [fullname]\\n\\nPermission is hereby granted, free of charge, to any person obtaining a copy\\nof this software and associated documentation files (the \\"Software\\"), to deal\\nin the Software without restriction, including without limitation the rights\\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\\ncopies of the Software, and to permit persons to whom the Software is\\nfurnished to do so, subject to the following conditions:\\n\\nThe above copyright notice and this permission notice shall be included in all\\ncopies or substantial portions of the Software.\\n\\nTHE SOFTWARE IS PROVIDED \\"AS IS\\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\\nSOFTWARE.\\n",
1332+
"featured": true
1333+
}
1334+
"""
1335+
)
1336+
1337+
license_result = License(
1338+
license_json["key"],
1339+
license_json["name"],
1340+
license_json["spdx_id"],
1341+
HTTP.URI(license_json["url"]),
1342+
HTTP.URI(license_json["html_url"]),
1343+
license_json["description"],
1344+
license_json["permissions"],
1345+
license_json["conditions"],
1346+
license_json["limitations"],
1347+
license_json["body"],
1348+
license_json["featured"]
1349+
)
1350+
1351+
@test License(license_json) == license_result
1352+
@test name(License(license_json["spdx_id"])) == name(license_result)
1353+
end

test/read_only_api_tests.jl

+26-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ end
5656

5757
@testset "Repositories" begin
5858
# test GitHub.repo
59-
@test name(repo(ghjl; auth = auth)) == name(ghjl)
59+
repo_obj = repo(ghjl; auth = auth)
60+
@test name(repo_obj) == name(ghjl)
61+
@test typeof(repo_obj.license) == License
62+
@test name(repo_obj.license) == "MIT"
6063

6164
# test GitHub.forks
6265
@test length(first(forks(ghjl; auth = auth))) > 0
@@ -221,3 +224,25 @@ end
221224
@test GitHub.api_uri(public_gh, "/rate_limit") == HTTP.URI("https://api.github.com/rate_limit")
222225
@test GitHub.api_uri(enterprise_gh, "/rate_limit") == HTTP.URI("https://git.company.com/api/v3/rate_limit")
223226
end
227+
228+
@testset "Licenses" begin
229+
# test GitHub.licenses
230+
licenses_obj, page_data = licenses(; page_limit = 1, auth = auth)
231+
@test typeof(licenses_obj) == Vector{License}
232+
@test length(licenses_obj) != 0
233+
234+
# test GitHub.license
235+
license_obj = license("MIT"; auth = auth)
236+
@test typeof(license_obj) == License
237+
@test name(license_obj) == "MIT"
238+
@test license_obj.name == "MIT License"
239+
@test startswith(license_obj.body, "MIT License\n\nCopyright (c) [year] [fullname]\n\nPermission is hereby granted,")
240+
241+
# test GitHub.repo_license
242+
repo_license_obj = repo_license(ghjl; auth = auth)
243+
@test typeof(repo_license_obj) == Content
244+
@test name(repo_license_obj.license) == "MIT"
245+
@test name(repo_license_obj) == "LICENSE.md"
246+
@test repo_license_obj.path == "LICENSE.md"
247+
@test repo_license_obj.typ == "file"
248+
end

0 commit comments

Comments
 (0)