Skip to content

Commit 5b69a29

Browse files
committed
Merge pull request hub4j#324
2 parents 198fede + d1c900a commit 5b69a29

6 files changed

Lines changed: 127 additions & 3 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
import java.util.Locale;
28+
29+
/**
30+
* Permission for a user in a repository.
31+
* @see <a href="https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level">API</a>
32+
*/
33+
/*package*/ class GHPermission {
34+
35+
private String permission;
36+
private GHUser user;
37+
38+
/**
39+
* @return one of {@code admin}, {@code write}, {@code read}, or {@code none}
40+
*/
41+
public String getPermission() {
42+
return permission;
43+
}
44+
45+
public GHPermissionType getPermissionType() {
46+
return Enum.valueOf(GHPermissionType.class, permission.toUpperCase(Locale.ENGLISH));
47+
}
48+
49+
public GHUser getUser() {
50+
return user;
51+
}
52+
53+
void wrapUp(GitHub root) {
54+
if (user != null) {
55+
user.root = root;
56+
}
57+
}
58+
59+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* @author Kohsuke Kawaguchi
5+
*/
6+
public enum GHPermissionType {
7+
ADMIN,
8+
WRITE,
9+
READ,
10+
NONE
11+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,29 @@ 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+
@Deprecated @Preview
490+
public GHPermissionType getPermission(String user) throws IOException {
491+
GHPermission perm = root.retrieve().withPreview(KORRA).to(getApiTailUrl("collaborators/" + user + "/permission"), GHPermission.class);
492+
perm.wrapUp(root);
493+
return perm.getPermissionType();
494+
}
495+
496+
/**
497+
* Obtain permission for a given user in this repository.
498+
* @throws FileNotFoundException under some conditions (e.g., private repo you can see but are not an admin of); treat as unknown
499+
* @throws HttpException with a 403 under other conditions (e.g., public repo you have no special rights to); treat as unknown
500+
*/
501+
@Deprecated @Preview
502+
public GHPermissionType getPermission(GHUser u) throws IOException {
503+
return getPermission(u.getLogin());
504+
}
505+
483506
/**
484507
* If this repository belongs to an organization, return a set of teams.
485508
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
static final String LOKI = "application/vnd.github.loki-preview+json";
88
static final String DRAX = "application/vnd.github.drax-preview+json";
99
static final String SQUIRREL_GIRL = "application/vnd.github.squirrel-girl-preview";
10+
static final String KORRA = "application/vnd.github.korra-preview";
1011
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,16 @@ private InputStream wrapStream(InputStream in) throws IOException {
660660
InputStream es = wrapStream(uc.getErrorStream());
661661
try {
662662
if (es!=null) {
663+
String error = IOUtils.toString(es, "UTF-8");
663664
if (e instanceof FileNotFoundException) {
664665
// pass through 404 Not Found to allow the caller to handle it intelligently
665-
throw (IOException) new FileNotFoundException(IOUtils.toString(es, "UTF-8")).initCause(e);
666-
} else
667-
throw (IOException) new IOException(IOUtils.toString(es, "UTF-8")).initCause(e);
666+
throw (IOException) new FileNotFoundException(error).initCause(e);
667+
} else if (e instanceof HttpException) {
668+
HttpException http = (HttpException) e;
669+
throw (IOException) new HttpException(error, http.getResponseCode(), http.getResponseMessage(), http.getUrl(), e);
670+
} else {
671+
throw (IOException) new IOException(error).initCause(e);
672+
}
668673
} else
669674
throw e;
670675
} 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)