Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 578fe08

Browse filesBrowse files
authored
Merge pull request hub4j#1074 from nvahren/repository-visibility
Add support for repository visibility
2 parents 83aa9d0 + 2553a79 commit 578fe08
Copy full SHA for 578fe08

File tree

Expand file treeCollapse file tree

36 files changed

+2507
-7
lines changed
Filter options
Expand file treeCollapse file tree

36 files changed

+2507
-7
lines changed

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

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHRepository.java
+67-1Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3232
import org.apache.commons.lang3.StringUtils;
3333
import org.kohsuke.github.function.InputStreamFunction;
34+
import org.kohsuke.github.internal.EnumUtils;
3435

3536
import java.io.FileNotFoundException;
3637
import java.io.IOException;
@@ -49,6 +50,7 @@
4950
import java.util.Iterator;
5051
import java.util.LinkedHashSet;
5152
import java.util.List;
53+
import java.util.Locale;
5254
import java.util.Map;
5355
import java.util.Set;
5456
import java.util.TreeMap;
@@ -64,6 +66,7 @@
6466
import static org.kohsuke.github.internal.Previews.FLASH;
6567
import static org.kohsuke.github.internal.Previews.INERTIA;
6668
import static org.kohsuke.github.internal.Previews.MERCY;
69+
import static org.kohsuke.github.internal.Previews.NEBULA;
6770
import static org.kohsuke.github.internal.Previews.SHADOW_CAT;
6871

6972
/**
@@ -104,6 +107,8 @@ public class GHRepository extends GHObject {
104107
@JsonProperty("private")
105108
private boolean _private;
106109

110+
private String visibility;
111+
107112
private int forks_count, stargazers_count, watchers_count, size, open_issues_count, subscribers_count;
108113

109114
private String pushed_at;
@@ -710,6 +715,41 @@ public boolean isPrivate() {
710715
return _private;
711716
}
712717

718+
/**
719+
* Visibility of a repository.
720+
*/
721+
public enum Visibility {
722+
PUBLIC, INTERNAL, PRIVATE, UNKNOWN;
723+
724+
public static Visibility from(String value) {
725+
return EnumUtils.getNullableEnumOrDefault(Visibility.class, value, Visibility.UNKNOWN);
726+
}
727+
728+
@Override
729+
public String toString() {
730+
return name().toLowerCase(Locale.ROOT);
731+
}
732+
}
733+
734+
/**
735+
* Gets the visibility of the repository.
736+
*
737+
* @return the visibility
738+
*/
739+
@Deprecated
740+
@Preview(NEBULA)
741+
public Visibility getVisibility() {
742+
if (visibility == null) {
743+
try {
744+
populate();
745+
} catch (final IOException e) {
746+
// Convert this to a runtime exception to avoid messy method signature
747+
throw new GHException("Could not populate the visibility of the repository", e);
748+
}
749+
}
750+
return Visibility.from(visibility);
751+
}
752+
713753
/**
714754
* Is template boolean.
715755
*
@@ -1202,6 +1242,26 @@ public void setPrivate(boolean value) throws IOException {
12021242
set().private_(value);
12031243
}
12041244

1245+
/**
1246+
* Sets visibility.
1247+
*
1248+
* @param value
1249+
* the value
1250+
* @throws IOException
1251+
* the io exception
1252+
*/
1253+
@Deprecated
1254+
@Preview(NEBULA)
1255+
public void setVisibility(final Visibility value) throws IOException {
1256+
root.createRequest()
1257+
.method("PATCH")
1258+
.withPreview(NEBULA)
1259+
.with("name", name)
1260+
.with("visibility", value)
1261+
.withUrlPath(getApiTailUrl(""))
1262+
.send();
1263+
}
1264+
12051265
/**
12061266
* Allow squash merge.
12071267
*
@@ -3122,11 +3182,17 @@ void populate() throws IOException {
31223182
// There is bug in Push event payloads that returns the wrong url.
31233183
// All other occurrences of "url" take the form "https://api.github.com/...".
31243184
// 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);
3185+
root.createRequest()
3186+
.withPreview(BAPTISTE)
3187+
.withPreview(NEBULA)
3188+
.setRawUrlPath(url.toString())
3189+
.fetchInto(this)
3190+
.wrap(root);
31263191
} catch (HttpException e) {
31273192
if (e.getCause() instanceof JsonParseException) {
31283193
root.createRequest()
31293194
.withPreview(BAPTISTE)
3195+
.withPreview(NEBULA)
31303196
.withUrlPath("/repos/" + full_name)
31313197
.fetchInto(this)
31323198
.wrap(root);

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

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHRepositoryBuilder.java
+17Lines 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.Visibility;
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 Visibility 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/EnumTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/EnumTest.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void touchEnums() {
7070

7171
assertThat(GHRepository.CollaboratorAffiliation.values().length, equalTo(3));
7272
assertThat(GHRepository.ForkSort.values().length, equalTo(3));
73+
assertThat(GHRepository.Visibility.values().length, equalTo(4));
7374

7475
assertThat(GHRepositorySearchBuilder.Sort.values().length, equalTo(3));
7576

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

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/GHRepositoryTest.java
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.apache.commons.io.IOUtils;
55
import org.junit.Test;
66
import org.kohsuke.github.GHCheckRun.Conclusion;
7+
import org.kohsuke.github.GHRepository.Visibility;
78

89
import java.io.ByteArrayInputStream;
910
import java.io.FileNotFoundException;
@@ -249,6 +250,30 @@ public void testUpdateRepository() throws Exception {
249250
assertThat(redux.getDescription(), equalTo(updatedDescription));
250251
}
251252

253+
@Test
254+
public void testGetRepositoryWithVisibility() throws IOException {
255+
snapshotNotAllowed();
256+
final String repoName = "test-repo-visibility";
257+
final GHRepository repo = getTempRepository(repoName);
258+
assertEquals(Visibility.PUBLIC, repo.getVisibility());
259+
260+
repo.setVisibility(Visibility.INTERNAL);
261+
assertEquals(Visibility.INTERNAL,
262+
gitHub.getRepository(repo.getOwnerName() + "/" + repo.getName()).getVisibility());
263+
264+
repo.setVisibility(Visibility.PRIVATE);
265+
assertEquals(Visibility.PRIVATE,
266+
gitHub.getRepository(repo.getOwnerName() + "/" + repo.getName()).getVisibility());
267+
268+
repo.setVisibility(Visibility.PUBLIC);
269+
assertEquals(Visibility.PUBLIC,
270+
gitHub.getRepository(repo.getOwnerName() + "/" + repo.getName()).getVisibility());
271+
272+
// deliberately bogus response in snapshot
273+
assertEquals(Visibility.UNKNOWN,
274+
gitHub.getRepository(repo.getOwnerName() + "/" + repo.getName()).getVisibility());
275+
}
276+
252277
@Test
253278
public void listContributors() throws IOException {
254279
GHRepository r = gitHub.getOrganization("hub4j").getRepository("github-api");

‎src/test/resources/org/kohsuke/github/GHEventPayloadTest/wiremock/pushToFork/__files/repos_hub4j-test-org_github-api-2.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/GHEventPayloadTest/wiremock/pushToFork/__files/repos_hub4j-test-org_github-api-2.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"url": "https://api.github.com/licenses/mit",
9494
"node_id": "MDc6TGljZW5zZTEz"
9595
},
96+
"visibility": "public",
9697
"is_template": false,
9798
"forks": 0,
9899
"open_issues": 5,
@@ -330,4 +331,4 @@
330331
},
331332
"network_count": 478,
332333
"subscribers_count": 0
333-
}
334+
}

‎src/test/resources/org/kohsuke/github/GHEventPayloadTest/wiremock/pushToFork/mappings/repos_hub4j-test-org_github-api-2.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/GHEventPayloadTest/wiremock/pushToFork/mappings/repos_hub4j-test-org_github-api-2.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"method": "GET",
77
"headers": {
88
"Accept": {
9-
"equalTo": "application/vnd.github.baptiste-preview+json"
9+
"equalTo": "application/vnd.github.baptiste-preview+json, application/vnd.github.nebula-preview+json"
1010
}
1111
}
1212
},
@@ -44,4 +44,4 @@
4444
"uuid": "90aa0017-3f50-4829-bda4-6531fbcfba60",
4545
"persistent": true,
4646
"insertionIndex": 2
47-
}
47+
}

‎src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testCreateRepositoryWithParameterIsTemplate/__files/repos_hub4j-test-org_github-api-template-test-7.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testCreateRepositoryWithParameterIsTemplate/__files/repos_hub4j-test-org_github-api-template-test-7.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"disabled": false,
8888
"open_issues_count": 0,
8989
"license": null,
90+
"visibility": "public",
9091
"is_template": true,
9192
"forks": 0,
9293
"open_issues": 0,
@@ -125,4 +126,4 @@
125126
},
126127
"network_count": 0,
127128
"subscribers_count": 8
128-
}
129+
}

‎src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testCreateRepositoryWithParameterIsTemplate/mappings/repos_hub4j-test-org_github-api-template-test-7.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testCreateRepositoryWithParameterIsTemplate/mappings/repos_hub4j-test-org_github-api-template-test-7.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"method": "GET",
77
"headers": {
88
"Accept": {
9-
"equalTo": "application/vnd.github.baptiste-preview+json"
9+
"equalTo": "application/vnd.github.baptiste-preview+json, application/vnd.github.nebula-preview+json"
1010
}
1111
}
1212
},
@@ -44,4 +44,4 @@
4444
"uuid": "51d54e86-a714-457b-88d6-5c045631a074",
4545
"persistent": true,
4646
"insertionIndex": 7
47-
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"id": 354063893,
3+
"node_id": "MDEwOlJlcG9zaXRvcnkzNTQwNjM4OTM=",
4+
"name": "test-repo-visibility",
5+
"full_name": "hub4j-test-org/test-repo-visibility",
6+
"private": true,
7+
"owner": {
8+
"login": "hub4j-test-org",
9+
"id": 7544739,
10+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
11+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/hub4j-test-org",
14+
"html_url": "https://github.com/hub4j-test-org",
15+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
16+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
20+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
21+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
22+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
24+
"type": "Organization",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/hub4j-test-org/test-repo-visibility",
28+
"description": "A test repository for testing the github-api project: test-repo-visibility",
29+
"fork": false,
30+
"url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility",
31+
"forks_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/forks",
32+
"keys_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/teams",
35+
"hooks_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/hooks",
36+
"issue_events_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/events",
38+
"assignees_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/tags",
41+
"blobs_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/languages",
47+
"stargazers_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/stargazers",
48+
"contributors_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/contributors",
49+
"subscribers_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/subscribers",
50+
"subscription_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/subscription",
51+
"commits_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/merges",
58+
"archive_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/downloads",
60+
"issues_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/hub4j-test-org/test-repo-visibility/deployments",
67+
"created_at": "2021-04-02T15:48:41Z",
68+
"updated_at": "2021-04-02T15:48:48Z",
69+
"pushed_at": "2021-04-02T15:48:42Z",
70+
"git_url": "git://github.com/hub4j-test-org/test-repo-visibility.git",
71+
"ssh_url": "git@github.com:hub4j-test-org/test-repo-visibility.git",
72+
"clone_url": "https://github.com/hub4j-test-org/test-repo-visibility.git",
73+
"svn_url": "https://github.com/hub4j-test-org/test-repo-visibility",
74+
"homepage": "http://github-api.kohsuke.org/",
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": "public",
91+
"forks": 0,
92+
"open_issues": 0,
93+
"watchers": 0,
94+
"default_branch": "main",
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+
"organization": {
105+
"login": "hub4j-test-org",
106+
"id": 7544739,
107+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
108+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
109+
"gravatar_id": "",
110+
"url": "https://api.github.com/users/hub4j-test-org",
111+
"html_url": "https://github.com/hub4j-test-org",
112+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
113+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
114+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
115+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
116+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
117+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
118+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
119+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
120+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
121+
"type": "Organization",
122+
"site_admin": false
123+
},
124+
"network_count": 0,
125+
"subscribers_count": 11
126+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.