Skip to content

Commit 73d2e1d

Browse files
nv035674vahrennd
authored andcommitted
Add support for repository visibility
1 parent 83aa9d0 commit 73d2e1d

19 files changed

+1077
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>org.kohsuke</groupId>
44
<artifactId>github-api</artifactId>
5-
<version>1.127-SNAPSHOT</version>
5+
<version>1.127-VISIBILITY-SNAPSHOT</version>
66
<name>GitHub API for Java</name>
77
<url>https://github-api.kohsuke.org/</url>
88
<description>GitHub API for Java</description>

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public class GHRepository extends GHObject {
104104
@JsonProperty("private")
105105
private boolean _private;
106106

107+
private GHVisibility visibility;
108+
107109
private int forks_count, stargazers_count, watchers_count, size, open_issues_count, subscribers_count;
108110

109111
private String pushed_at;
@@ -710,6 +712,32 @@ public boolean isPrivate() {
710712
return _private;
711713
}
712714

715+
/**
716+
* Visibility of a repository
717+
*/
718+
public enum GHVisibility {
719+
PUBLIC, INTERNAL, PRIVATE
720+
}
721+
722+
/**
723+
* Gets the visibility of the repository.
724+
*
725+
* @return the visibility
726+
*/
727+
@Deprecated
728+
@Preview(NEBULA)
729+
public GHVisibility getVisibility() {
730+
if (visibility == null) {
731+
try {
732+
populate();
733+
} catch (final IOException e) {
734+
// Convert this to a runtime exception to avoid messy method signature
735+
throw new GHException("Could not populate the visibility of the repository", e);
736+
}
737+
}
738+
return visibility;
739+
}
740+
713741
/**
714742
* Is template boolean.
715743
*
@@ -1202,6 +1230,26 @@ public void setPrivate(boolean value) throws IOException {
12021230
set().private_(value);
12031231
}
12041232

1233+
/**
1234+
* Sets visibility.
1235+
*
1236+
* @param value
1237+
* the value
1238+
* @throws IOException
1239+
* the io exception
1240+
*/
1241+
@Deprecated
1242+
@Preview(NEBULA)
1243+
public void setVisibility(final GHVisibility value) throws IOException {
1244+
root.createRequest()
1245+
.method("PATCH")
1246+
.withPreview(NEBULA)
1247+
.with("name", name)
1248+
.with("visibility", value)
1249+
.withUrlPath(getApiTailUrl(""))
1250+
.send();
1251+
}
1252+
12051253
/**
12061254
* Allow squash merge.
12071255
*
@@ -3122,11 +3170,17 @@ void populate() throws IOException {
31223170
// There is bug in Push event payloads that returns the wrong url.
31233171
// All other occurrences of "url" take the form "https://api.github.com/...".
31243172
// For Push event repository records, they take the form "https://github.com/{fullName}".
3125-
root.createRequest().withPreview(BAPTISTE).setRawUrlPath(url.toString()).fetchInto(this).wrap(root);
3173+
root.createRequest()
3174+
.withPreview(BAPTISTE)
3175+
.withPreview(NEBULA)
3176+
.setRawUrlPath(url.toString())
3177+
.fetchInto(this)
3178+
.wrap(root);
31263179
} catch (HttpException e) {
31273180
if (e.getCause() instanceof JsonParseException) {
31283181
root.createRequest()
31293182
.withPreview(BAPTISTE)
3183+
.withPreview(NEBULA)
31303184
.withUrlPath("/repos/" + full_name)
31313185
.fetchInto(this)
31323186
.wrap(root);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.kohsuke.github;
22

3+
import org.kohsuke.github.GHRepository.GHVisibility;
4+
35
import java.io.IOException;
46
import java.net.URL;
57

68
import static org.kohsuke.github.internal.Previews.BAPTISTE;
9+
import static org.kohsuke.github.internal.Previews.NEBULA;
710

811
abstract class GHRepositoryBuilder<S> extends AbstractBuilder<GHRepository, S> {
912

@@ -146,6 +149,20 @@ public S private_(boolean enabled) throws IOException {
146149
return with("private", enabled);
147150
}
148151

152+
/**
153+
* Sets the repository visibility
154+
*
155+
* @param visibility
156+
* visibility of repository
157+
* @return a builder to continue with building
158+
* @throws IOException
159+
* In case of any networking error or error from the server.
160+
*/
161+
public S visibility(final GHVisibility visibility) throws IOException {
162+
requester.withPreview(NEBULA);
163+
return with("visibility", visibility);
164+
}
165+
149166
/**
150167
* Enables issue tracker
151168
*

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,23 @@ public void testUpdateRepository() throws Exception {
249249
assertThat(redux.getDescription(), equalTo(updatedDescription));
250250
}
251251

252+
@Test
253+
public void testGetRepositoryWithVisibility() throws IOException {
254+
kohsuke();
255+
final GHUser myself = gitHub.getMyself();
256+
final String repoName = "test-repo-visibility";
257+
final GHRepository repo = gitHub.createRepository(repoName)
258+
.visibility(GHRepository.GHVisibility.PUBLIC)
259+
.create();
260+
try {
261+
assertEquals(GHRepository.GHVisibility.PUBLIC, repo.getVisibility());
262+
repo.setVisibility(GHRepository.GHVisibility.PRIVATE);
263+
assertEquals(GHRepository.GHVisibility.PRIVATE, myself.getRepository(repoName).getVisibility());
264+
} finally {
265+
repo.delete();
266+
}
267+
}
268+
252269
@Test
253270
public void listContributors() throws IOException {
254271
GHRepository r = gitHub.getOrganization("hub4j").getRepository("github-api");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"id": 353724658,
3+
"node_id": "MDEwOlJlcG9zaXRvcnkzNTM3MjQ2NTg=",
4+
"name": "test-repo-visibility",
5+
"full_name": "nvahren/test-repo-visibility",
6+
"private": true,
7+
"owner": {
8+
"login": "nvahren",
9+
"id": 8679583,
10+
"node_id": "MDQ6VXNlcjg2Nzk1ODM=",
11+
"avatar_url": "https://avatars.githubusercontent.com/u/8679583?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/nvahren",
14+
"html_url": "https://github.com/nvahren",
15+
"followers_url": "https://api.github.com/users/nvahren/followers",
16+
"following_url": "https://api.github.com/users/nvahren/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/nvahren/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/nvahren/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/nvahren/subscriptions",
20+
"organizations_url": "https://api.github.com/users/nvahren/orgs",
21+
"repos_url": "https://api.github.com/users/nvahren/repos",
22+
"events_url": "https://api.github.com/users/nvahren/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/nvahren/received_events",
24+
"type": "User",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/nvahren/test-repo-visibility",
28+
"description": null,
29+
"fork": false,
30+
"url": "https://api.github.com/repos/nvahren/test-repo-visibility",
31+
"forks_url": "https://api.github.com/repos/nvahren/test-repo-visibility/forks",
32+
"keys_url": "https://api.github.com/repos/nvahren/test-repo-visibility/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/nvahren/test-repo-visibility/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/nvahren/test-repo-visibility/teams",
35+
"hooks_url": "https://api.github.com/repos/nvahren/test-repo-visibility/hooks",
36+
"issue_events_url": "https://api.github.com/repos/nvahren/test-repo-visibility/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/nvahren/test-repo-visibility/events",
38+
"assignees_url": "https://api.github.com/repos/nvahren/test-repo-visibility/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/nvahren/test-repo-visibility/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/nvahren/test-repo-visibility/tags",
41+
"blobs_url": "https://api.github.com/repos/nvahren/test-repo-visibility/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/nvahren/test-repo-visibility/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/nvahren/test-repo-visibility/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/nvahren/test-repo-visibility/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/nvahren/test-repo-visibility/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/nvahren/test-repo-visibility/languages",
47+
"stargazers_url": "https://api.github.com/repos/nvahren/test-repo-visibility/stargazers",
48+
"contributors_url": "https://api.github.com/repos/nvahren/test-repo-visibility/contributors",
49+
"subscribers_url": "https://api.github.com/repos/nvahren/test-repo-visibility/subscribers",
50+
"subscription_url": "https://api.github.com/repos/nvahren/test-repo-visibility/subscription",
51+
"commits_url": "https://api.github.com/repos/nvahren/test-repo-visibility/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/nvahren/test-repo-visibility/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/nvahren/test-repo-visibility/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/nvahren/test-repo-visibility/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/nvahren/test-repo-visibility/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/nvahren/test-repo-visibility/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/nvahren/test-repo-visibility/merges",
58+
"archive_url": "https://api.github.com/repos/nvahren/test-repo-visibility/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/nvahren/test-repo-visibility/downloads",
60+
"issues_url": "https://api.github.com/repos/nvahren/test-repo-visibility/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/nvahren/test-repo-visibility/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/nvahren/test-repo-visibility/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/nvahren/test-repo-visibility/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/nvahren/test-repo-visibility/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/nvahren/test-repo-visibility/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/nvahren/test-repo-visibility/deployments",
67+
"created_at": "2021-04-01T14:20:18Z",
68+
"updated_at": "2021-04-01T14:20:19Z",
69+
"pushed_at": "2021-04-01T14:20:19Z",
70+
"git_url": "git://github.com/nvahren/test-repo-visibility.git",
71+
"ssh_url": "git@github.com:nvahren/test-repo-visibility.git",
72+
"clone_url": "https://github.com/nvahren/test-repo-visibility.git",
73+
"svn_url": "https://github.com/nvahren/test-repo-visibility",
74+
"homepage": null,
75+
"size": 0,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": null,
79+
"has_issues": true,
80+
"has_projects": true,
81+
"has_downloads": true,
82+
"has_wiki": true,
83+
"has_pages": false,
84+
"forks_count": 0,
85+
"mirror_url": null,
86+
"archived": false,
87+
"disabled": false,
88+
"open_issues_count": 0,
89+
"license": null,
90+
"visibility": "private",
91+
"forks": 0,
92+
"open_issues": 0,
93+
"watchers": 0,
94+
"default_branch": "master",
95+
"permissions": {
96+
"admin": true,
97+
"push": true,
98+
"pull": true
99+
},
100+
"allow_squash_merge": true,
101+
"allow_merge_commit": true,
102+
"allow_rebase_merge": true,
103+
"delete_branch_on_merge": false,
104+
"network_count": 0,
105+
"subscribers_count": 1
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"id": 353724658,
3+
"node_id": "MDEwOlJlcG9zaXRvcnkzNTM3MjQ2NTg=",
4+
"name": "test-repo-visibility",
5+
"full_name": "nvahren/test-repo-visibility",
6+
"private": true,
7+
"owner": {
8+
"login": "nvahren",
9+
"id": 8679583,
10+
"node_id": "MDQ6VXNlcjg2Nzk1ODM=",
11+
"avatar_url": "https://avatars.githubusercontent.com/u/8679583?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/nvahren",
14+
"html_url": "https://github.com/nvahren",
15+
"followers_url": "https://api.github.com/users/nvahren/followers",
16+
"following_url": "https://api.github.com/users/nvahren/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/nvahren/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/nvahren/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/nvahren/subscriptions",
20+
"organizations_url": "https://api.github.com/users/nvahren/orgs",
21+
"repos_url": "https://api.github.com/users/nvahren/repos",
22+
"events_url": "https://api.github.com/users/nvahren/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/nvahren/received_events",
24+
"type": "User",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/nvahren/test-repo-visibility",
28+
"description": null,
29+
"fork": false,
30+
"url": "https://api.github.com/repos/nvahren/test-repo-visibility",
31+
"forks_url": "https://api.github.com/repos/nvahren/test-repo-visibility/forks",
32+
"keys_url": "https://api.github.com/repos/nvahren/test-repo-visibility/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/nvahren/test-repo-visibility/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/nvahren/test-repo-visibility/teams",
35+
"hooks_url": "https://api.github.com/repos/nvahren/test-repo-visibility/hooks",
36+
"issue_events_url": "https://api.github.com/repos/nvahren/test-repo-visibility/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/nvahren/test-repo-visibility/events",
38+
"assignees_url": "https://api.github.com/repos/nvahren/test-repo-visibility/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/nvahren/test-repo-visibility/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/nvahren/test-repo-visibility/tags",
41+
"blobs_url": "https://api.github.com/repos/nvahren/test-repo-visibility/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/nvahren/test-repo-visibility/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/nvahren/test-repo-visibility/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/nvahren/test-repo-visibility/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/nvahren/test-repo-visibility/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/nvahren/test-repo-visibility/languages",
47+
"stargazers_url": "https://api.github.com/repos/nvahren/test-repo-visibility/stargazers",
48+
"contributors_url": "https://api.github.com/repos/nvahren/test-repo-visibility/contributors",
49+
"subscribers_url": "https://api.github.com/repos/nvahren/test-repo-visibility/subscribers",
50+
"subscription_url": "https://api.github.com/repos/nvahren/test-repo-visibility/subscription",
51+
"commits_url": "https://api.github.com/repos/nvahren/test-repo-visibility/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/nvahren/test-repo-visibility/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/nvahren/test-repo-visibility/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/nvahren/test-repo-visibility/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/nvahren/test-repo-visibility/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/nvahren/test-repo-visibility/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/nvahren/test-repo-visibility/merges",
58+
"archive_url": "https://api.github.com/repos/nvahren/test-repo-visibility/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/nvahren/test-repo-visibility/downloads",
60+
"issues_url": "https://api.github.com/repos/nvahren/test-repo-visibility/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/nvahren/test-repo-visibility/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/nvahren/test-repo-visibility/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/nvahren/test-repo-visibility/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/nvahren/test-repo-visibility/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/nvahren/test-repo-visibility/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/nvahren/test-repo-visibility/deployments",
67+
"created_at": "2021-04-01T14:20:18Z",
68+
"updated_at": "2021-04-01T14:20:19Z",
69+
"pushed_at": "2021-04-01T14:20:19Z",
70+
"git_url": "git://github.com/nvahren/test-repo-visibility.git",
71+
"ssh_url": "git@github.com:nvahren/test-repo-visibility.git",
72+
"clone_url": "https://github.com/nvahren/test-repo-visibility.git",
73+
"svn_url": "https://github.com/nvahren/test-repo-visibility",
74+
"homepage": null,
75+
"size": 0,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": null,
79+
"has_issues": true,
80+
"has_projects": true,
81+
"has_downloads": true,
82+
"has_wiki": true,
83+
"has_pages": false,
84+
"forks_count": 0,
85+
"mirror_url": null,
86+
"archived": false,
87+
"disabled": false,
88+
"open_issues_count": 0,
89+
"license": null,
90+
"forks": 0,
91+
"open_issues": 0,
92+
"watchers": 0,
93+
"default_branch": "master",
94+
"permissions": {
95+
"admin": true,
96+
"push": true,
97+
"pull": true
98+
},
99+
"temp_clone_token": "ACCHBH3NKBO4HPYLKKIJMLTAMXL5A",
100+
"allow_squash_merge": true,
101+
"allow_merge_commit": true,
102+
"allow_rebase_merge": true,
103+
"delete_branch_on_merge": false,
104+
"network_count": 0,
105+
"subscribers_count": 1
106+
}

0 commit comments

Comments
 (0)