Skip to content

Commit ccb42d3

Browse files
committed
[JENKINS-36240] Added GHRepository.getPermission(String).
1 parent 0731f63 commit ccb42d3

4 files changed

Lines changed: 98 additions & 3 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2016 CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.kohsuke.github;
26+
27+
/**
28+
* Permission for a user in a repository.
29+
* @see <a href="https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level">API</a>
30+
*/
31+
public class GHPermission {
32+
33+
private String permission;
34+
private GHUser user;
35+
36+
/**
37+
* @return one of {@code admin}, {@code write}, {@code read}, or {@code none}
38+
*/
39+
public String getPermission() {
40+
return permission;
41+
}
42+
43+
public GHUser getUser() {
44+
return user;
45+
}
46+
47+
void wrapUp(GitHub root) {
48+
if (user != null) {
49+
user.root = root;
50+
}
51+
}
52+
53+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,18 @@ public Set<String> getCollaboratorNames() throws IOException {
480480
return r;
481481
}
482482

483+
/**
484+
* Obtain permission for a given user in this repository.
485+
* @param user a {@link GHUser#getLogin}
486+
* @throws FileNotFoundException under some conditions (e.g., private repo you can see but are not an admin of); treat as unknown
487+
* @throws HttpException with a 403 under other conditions (e.g., public repo you have no special rights to); treat as unknown
488+
*/
489+
public GHPermission getPermission(String user) throws IOException {
490+
GHPermission perm = root.retrieve().withHeader("Accept", "application/vnd.github.korra-preview").to(getApiTailUrl("collaborators/" + user + "/permission"), GHPermission.class);
491+
perm.wrapUp(root);
492+
return perm;
493+
}
494+
483495
/**
484496
* If this repository belongs to an organization, return a set of teams.
485497
*/

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,16 @@ private InputStream wrapStream(InputStream in) throws IOException {
598598
InputStream es = wrapStream(uc.getErrorStream());
599599
try {
600600
if (es!=null) {
601+
String error = IOUtils.toString(es, "UTF-8");
601602
if (e instanceof FileNotFoundException) {
602603
// pass through 404 Not Found to allow the caller to handle it intelligently
603-
throw (IOException) new FileNotFoundException(IOUtils.toString(es, "UTF-8")).initCause(e);
604-
} else
605-
throw (IOException) new IOException(IOUtils.toString(es, "UTF-8")).initCause(e);
604+
throw (IOException) new FileNotFoundException(error).initCause(e);
605+
} else if (e instanceof HttpException) {
606+
HttpException http = (HttpException) e;
607+
throw (IOException) new HttpException(error, http.getResponseCode(), http.getResponseMessage(), http.getUrl(), e);
608+
} else {
609+
throw (IOException) new IOException(error).initCause(e);
610+
}
606611
} else
607612
throw e;
608613
} finally {

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

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

3+
import java.io.FileNotFoundException;
34
import org.junit.Test;
45
import org.kohsuke.github.GHRepository.Contributor;
56

67
import java.io.IOException;
8+
import org.junit.Ignore;
79

810
/**
911
* @author Kohsuke Kawaguchi
@@ -40,6 +42,29 @@ public void listContributors() throws IOException {
4042
assertTrue(kohsuke);
4143
}
4244

45+
@Ignore("depends on who runs this test whether it can pass or not")
46+
@Test
47+
public void getPermission() throws Exception {
48+
GHRepository r = gitHub.getOrganization("cloudbeers").getRepository("yolo");
49+
assertEquals("admin", r.getPermission("jglick").getPermission());
50+
assertEquals("read", r.getPermission("dude").getPermission());
51+
r = gitHub.getOrganization("cloudbees").getRepository("private-repo-not-writable-by-me");
52+
try {
53+
r.getPermission("jglick");
54+
fail();
55+
} catch (FileNotFoundException x) {
56+
x.printStackTrace(); // good
57+
}
58+
r = gitHub.getOrganization("apache").getRepository("groovy");
59+
try {
60+
r.getPermission("jglick");
61+
fail();
62+
} catch (HttpException x) {
63+
x.printStackTrace(); // good
64+
assertEquals(403, x.getResponseCode());
65+
}
66+
}
67+
4368
private GHRepository getRepository() throws IOException {
4469
return gitHub.getOrganization("github-api-test-org").getRepository("jenkins");
4570
}

0 commit comments

Comments
 (0)