Skip to content

Commit 5b53fba

Browse files
authored
Merge pull request hub4j#581 from martinvanzijl/issue_546_list_repository_topics
Add method to list repository topics
2 parents 6fb3b01 + e3c9cec commit 5b53fba

File tree

8 files changed

+530
-1
lines changed

8 files changed

+530
-1
lines changed

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,4 +1744,29 @@ public PagedIterable<GHIssueEvent> listIssueEvents() throws IOException {
17441744
public GHIssueEvent getIssueEvent(long id) throws IOException {
17451745
return root.retrieve().to(getApiTailUrl("issues/events/" + id), GHIssueEvent.class).wrapUp(root);
17461746
}
1747+
1748+
// Only used within listTopics().
1749+
private static class Topics {
1750+
public List<String> names;
1751+
}
1752+
1753+
/**
1754+
* Return the topics for this repository.
1755+
* See https://developer.github.com/v3/repos/#list-all-topics-for-a-repository
1756+
*/
1757+
public List<String> listTopics() throws IOException {
1758+
Topics topics = root.retrieve().withPreview(MERCY).to(getApiTailUrl("topics"), Topics.class);
1759+
return topics.names;
1760+
}
1761+
1762+
/**
1763+
* Set the topics for this repository.
1764+
* See https://developer.github.com/v3/repos/#replace-all-topics-for-a-repository
1765+
*/
1766+
// This currently returns a "404" error (as of 30 Oct 2019).
1767+
// public void setTopics(List<String> topics) throws IOException {
1768+
// Requester requester = new Requester(root);
1769+
// requester.with("names", topics);
1770+
// requester.method("PUT").withPreview(MERCY).to(getApiTailUrl("topics"));
1771+
// }
17471772
}

src/main/java/org/kohsuke/github/Previews.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
*/
4545
static final String MACHINE_MAN = "application/vnd.github.machine-man-preview+json";
4646

47+
/**
48+
* View a list of repository topics in calls that return repository results
49+
*
50+
* @see <a href="https://developer.github.com/v3/previews/#repository-topics">GitHub API Previews</a>
51+
*/
52+
static final String MERCY = "application/vnd.github.mercy-preview+json";
53+
4754
/**
4855
* Draft pull requests
4956
*

src/test/java/org/kohsuke/github/GHRepositoryTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,5 +269,9 @@ public void setMergeOptions() throws IOException {
269269
assertFalse(r.isAllowSquashMerge());
270270
}
271271

272-
272+
@Test
273+
public void testListTopics() throws Exception {
274+
List<String> topics = getRepository(gitHub).listTopics();
275+
assertTrue(topics.contains("api-test-dummy"));
276+
}
273277
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"login": "github-api-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/github-api-test-org",
6+
"repos_url": "https://api.github.com/orgs/github-api-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/github-api-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/github-api-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/github-api-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/github-api-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/github-api-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
13+
"description": null,
14+
"is_verified": false,
15+
"has_organization_projects": true,
16+
"has_repository_projects": true,
17+
"public_repos": 10,
18+
"public_gists": 0,
19+
"followers": 0,
20+
"following": 0,
21+
"html_url": "https://github.com/github-api-test-org",
22+
"created_at": "2014-05-10T19:39:11Z",
23+
"updated_at": "2015-04-20T00:42:30Z",
24+
"type": "Organization",
25+
"total_private_repos": 0,
26+
"owned_private_repos": 0,
27+
"private_gists": 0,
28+
"disk_usage": 132,
29+
"collaborators": 0,
30+
"billing_email": "kk@kohsuke.org",
31+
"default_repository_permission": "none",
32+
"members_can_create_repositories": false,
33+
"two_factor_requirement_enabled": false,
34+
"plan": {
35+
"name": "free",
36+
"space": 976562499,
37+
"private_repos": 0,
38+
"filled_seats": 7,
39+
"seats": 0
40+
}
41+
}

src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/testListTopics/__files/repos_github-api-test-org_github-api-50587270-e68d-42f4-9c04-f149d67a82eb.json

Lines changed: 330 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"id": "e696114b-6f4f-4fcb-9ea5-9cecf257f3d7",
3+
"name": "orgs_github-api-test-org",
4+
"request": {
5+
"url": "/orgs/github-api-test-org",
6+
"method": "GET"
7+
},
8+
"response": {
9+
"status": 200,
10+
"bodyFileName": "orgs_github-api-test-org-e696114b-6f4f-4fcb-9ea5-9cecf257f3d7.json",
11+
"headers": {
12+
"Date": "Wed, 30 Oct 2019 02:00:30 GMT",
13+
"Content-Type": "application/json; charset=utf-8",
14+
"Server": "GitHub.com",
15+
"Status": "200 OK",
16+
"X-RateLimit-Limit": "5000",
17+
"X-RateLimit-Remaining": "4966",
18+
"X-RateLimit-Reset": "1572404135",
19+
"Cache-Control": "private, max-age=60, s-maxage=60",
20+
"Vary": [
21+
"Accept, Authorization, Cookie, X-GitHub-OTP",
22+
"Accept-Encoding"
23+
],
24+
"ETag": "W/\"4342e0e7004c1a50986c767513fe857d\"",
25+
"Last-Modified": "Mon, 20 Apr 2015 00:42:30 GMT",
26+
"X-GitHub-Media-Type": "unknown, github.v3",
27+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
28+
"Access-Control-Allow-Origin": "*",
29+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
30+
"X-Frame-Options": "deny",
31+
"X-Content-Type-Options": "nosniff",
32+
"X-XSS-Protection": "1; mode=block",
33+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
34+
"Content-Security-Policy": "default-src 'none'",
35+
"X-GitHub-Request-Id": "D003:4D00:DEB42:1009B2:5DB8EEBE"
36+
}
37+
},
38+
"uuid": "e696114b-6f4f-4fcb-9ea5-9cecf257f3d7",
39+
"persistent": true,
40+
"insertionIndex": 1
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"id": "50587270-e68d-42f4-9c04-f149d67a82eb",
3+
"name": "repos_github-api-test-org_github-api",
4+
"request": {
5+
"url": "/repos/github-api-test-org/github-api",
6+
"method": "GET"
7+
},
8+
"response": {
9+
"status": 200,
10+
"bodyFileName": "repos_github-api-test-org_github-api-50587270-e68d-42f4-9c04-f149d67a82eb.json",
11+
"headers": {
12+
"Date": "Wed, 30 Oct 2019 02:00:31 GMT",
13+
"Content-Type": "application/json; charset=utf-8",
14+
"Server": "GitHub.com",
15+
"Status": "200 OK",
16+
"X-RateLimit-Limit": "5000",
17+
"X-RateLimit-Remaining": "4965",
18+
"X-RateLimit-Reset": "1572404135",
19+
"Cache-Control": "private, max-age=60, s-maxage=60",
20+
"Vary": [
21+
"Accept, Authorization, Cookie, X-GitHub-OTP",
22+
"Accept-Encoding"
23+
],
24+
"ETag": "W/\"a65593320a04d932942764427a19b681\"",
25+
"Last-Modified": "Wed, 30 Oct 2019 01:54:39 GMT",
26+
"X-GitHub-Media-Type": "unknown, github.v3",
27+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
28+
"Access-Control-Allow-Origin": "*",
29+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
30+
"X-Frame-Options": "deny",
31+
"X-Content-Type-Options": "nosniff",
32+
"X-XSS-Protection": "1; mode=block",
33+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
34+
"Content-Security-Policy": "default-src 'none'",
35+
"X-GitHub-Request-Id": "D003:4D00:DEB51:1009C2:5DB8EEBE"
36+
}
37+
},
38+
"uuid": "50587270-e68d-42f4-9c04-f149d67a82eb",
39+
"persistent": true,
40+
"insertionIndex": 2
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"id": "66254b56-38b1-4be6-b7ad-836a387c2418",
3+
"name": "repos_github-api-test-org_github-api_topics",
4+
"request": {
5+
"url": "/repos/github-api-test-org/github-api/topics",
6+
"method": "GET"
7+
},
8+
"response": {
9+
"status": 200,
10+
"body": "{\"names\":[\"api-test-dummy\"]}",
11+
"headers": {
12+
"Date": "Wed, 30 Oct 2019 02:00:31 GMT",
13+
"Content-Type": "application/json; charset=utf-8",
14+
"Server": "GitHub.com",
15+
"Status": "200 OK",
16+
"X-RateLimit-Limit": "5000",
17+
"X-RateLimit-Remaining": "4964",
18+
"X-RateLimit-Reset": "1572404135",
19+
"Cache-Control": "private, max-age=60, s-maxage=60",
20+
"Vary": [
21+
"Accept, Authorization, Cookie, X-GitHub-OTP",
22+
"Accept-Encoding"
23+
],
24+
"ETag": "W/\"06e6d1ba832cc532a224bbf6bdf2fa46\"",
25+
"X-GitHub-Media-Type": "github.mercy-preview; format=json",
26+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
27+
"Access-Control-Allow-Origin": "*",
28+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
29+
"X-Frame-Options": "deny",
30+
"X-Content-Type-Options": "nosniff",
31+
"X-XSS-Protection": "1; mode=block",
32+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
33+
"Content-Security-Policy": "default-src 'none'",
34+
"X-GitHub-Request-Id": "D003:4D00:DEB61:1009D8:5DB8EEBF"
35+
}
36+
},
37+
"uuid": "66254b56-38b1-4be6-b7ad-836a387c2418",
38+
"persistent": true,
39+
"insertionIndex": 3
40+
}

0 commit comments

Comments
 (0)