mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-11 00:11:25 +00:00
Compare commits
29 Commits
github-api
...
github-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ddbef093b | ||
|
|
4ccdfccdf0 | ||
|
|
3c8aa0c630 | ||
|
|
ba519f996a | ||
|
|
62d9b92e6e | ||
|
|
d2c909584d | ||
|
|
219916eb14 | ||
|
|
0a37ac901f | ||
|
|
4586baea27 | ||
|
|
d83961e85b | ||
|
|
7e35716ff0 | ||
|
|
d0cf1ac605 | ||
|
|
9a19d07ab8 | ||
|
|
922be0b164 | ||
|
|
d0b8e2e37e | ||
|
|
e3d6e08b6a | ||
|
|
16a6623095 | ||
|
|
7bf31ca149 | ||
|
|
030f2360ca | ||
|
|
08eafc4214 | ||
|
|
134ece9385 | ||
|
|
3e4b06e959 | ||
|
|
3e3c6f70ba | ||
|
|
3097378a10 | ||
|
|
53f7a0f78a | ||
|
|
45a6841772 | ||
|
|
80b93a6e33 | ||
|
|
6aaab641be | ||
|
|
4999490c06 |
19
pom.xml
19
pom.xml
@@ -3,11 +3,11 @@
|
||||
<parent>
|
||||
<groupId>org.kohsuke</groupId>
|
||||
<artifactId>pom</artifactId>
|
||||
<version>6</version>
|
||||
<version>8</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>github-api</artifactId>
|
||||
<version>1.53</version>
|
||||
<version>1.56</version>
|
||||
<name>GitHub API for Java</name>
|
||||
<url>http://github-api.kohsuke.org/</url>
|
||||
<description>GitHub API for Java</description>
|
||||
@@ -16,7 +16,7 @@
|
||||
<connection>scm:git:git@github.com/kohsuke/${project.artifactId}.git</connection>
|
||||
<developerConnection>scm:git:ssh://git@github.com/kohsuke/${project.artifactId}.git</developerConnection>
|
||||
<url>http://${project.artifactId}.kohsuke.org/</url>
|
||||
<tag>github-api-1.53</tag>
|
||||
<tag>github-api-1.56</tag>
|
||||
</scm>
|
||||
|
||||
<distributionManagement>
|
||||
@@ -35,7 +35,7 @@
|
||||
<plugin>
|
||||
<groupId>com.infradna.tool</groupId>
|
||||
<artifactId>bridge-method-injector</artifactId>
|
||||
<version>1.8</version>
|
||||
<version>1.12</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
@@ -77,7 +77,7 @@
|
||||
<dependency>
|
||||
<groupId>com.infradna.tool</groupId>
|
||||
<artifactId>bridge-method-annotation</artifactId>
|
||||
<version>1.8</version>
|
||||
<version>1.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.kohsuke.stapler</groupId>
|
||||
@@ -93,8 +93,8 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>1.5.3</version>
|
||||
<artifactId>okhttp-urlconnection</artifactId>
|
||||
<version>2.0.0</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -103,6 +103,11 @@
|
||||
<version>1.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>1.9.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
|
||||
@@ -16,6 +16,7 @@ public class GHCommitStatus {
|
||||
String target_url,description;
|
||||
int id;
|
||||
String url;
|
||||
String context;
|
||||
GHUser creator;
|
||||
|
||||
private GitHub root;
|
||||
@@ -69,4 +70,8 @@ public class GHCommitStatus {
|
||||
public GHUser getCreator() {
|
||||
return creator;
|
||||
}
|
||||
|
||||
public String getContext() {
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.kohsuke.github;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
|
||||
@@ -8,7 +10,9 @@ import javax.xml.bind.DatatypeConverter;
|
||||
* A Content of a repository.
|
||||
*
|
||||
* @author Alexandre COLLIGNON
|
||||
* @see GHRepository#getFileContent(String)
|
||||
*/
|
||||
@SuppressWarnings({"UnusedDeclaration"})
|
||||
public class GHContent {
|
||||
private GHRepository owner;
|
||||
|
||||
@@ -105,6 +109,25 @@ public class GHContent {
|
||||
return "dir".equals(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* List immediate children of this directory.
|
||||
*/
|
||||
public PagedIterable<GHContent> listDirectoryContent() throws IOException {
|
||||
if (!isDirectory())
|
||||
throw new IllegalStateException(path+" is not a directory");
|
||||
|
||||
return new PagedIterable<GHContent>() {
|
||||
public PagedIterator<GHContent> iterator() {
|
||||
return new PagedIterator<GHContent>(owner.root.retrieve().asIterator(url, GHContent[].class)) {
|
||||
@Override
|
||||
protected void wrapUp(GHContent[] page) {
|
||||
GHContent.wrap(page,owner);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public GHContentUpdateResponse update(String newContent, String commitMessage) throws IOException {
|
||||
return update(newContent, commitMessage, null);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ package org.kohsuke.github;
|
||||
* The response that is returned when updating
|
||||
* repository content.
|
||||
**/
|
||||
public final class GHContentUpdateResponse {
|
||||
public class GHContentUpdateResponse {
|
||||
private GHContent content;
|
||||
private GHCommit commit;
|
||||
|
||||
|
||||
46
src/main/java/org/kohsuke/github/GHDeployKey.java
Normal file
46
src/main/java/org/kohsuke/github/GHDeployKey.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package org.kohsuke.github;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||
|
||||
public class GHDeployKey {
|
||||
|
||||
protected String url, key, title;
|
||||
protected boolean verified;
|
||||
protected int id;
|
||||
private GHRepository owner;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public boolean isVerified() {
|
||||
return verified;
|
||||
}
|
||||
|
||||
public GHDeployKey wrap(GHRepository repo) {
|
||||
this.owner = repo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this).append("title",title).append("id",id).append("key",key).toString();
|
||||
}
|
||||
|
||||
public void delete() throws IOException {
|
||||
new Requester(owner.root).method("DELETE").to(String.format("/repos/%s/%s/keys/%d", owner.getOwnerName(), owner.getName(), id));
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import java.util.Map.Entry;
|
||||
* @see GitHub#getGist(String)
|
||||
* @see GitHub#createGist()
|
||||
*/
|
||||
public final class GHGist {
|
||||
public class GHGist {
|
||||
/*package almost final*/ GHUser owner;
|
||||
/*package almost final*/ GitHub root;
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.util.Map;
|
||||
/**
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public final class GHHook {
|
||||
public class GHHook {
|
||||
/**
|
||||
* Repository that the hook belongs to.
|
||||
*/
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.util.HashSet;
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public final class GHPersonSet<T extends GHPerson> extends HashSet<T> {
|
||||
public class GHPersonSet<T extends GHPerson> extends HashSet<T> {
|
||||
public GHPersonSet() {
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ package org.kohsuke.github;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
import java.io.IOException;
|
||||
@@ -321,7 +322,32 @@ public class GHRepository {
|
||||
*/
|
||||
@WithBridgeMethods(Set.class)
|
||||
public GHPersonSet<GHUser> getCollaborators() throws IOException {
|
||||
return new GHPersonSet<GHUser>(GHUser.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root));
|
||||
return new GHPersonSet<GHUser>(listCollaborators().asList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists up the collaborators on this repository.
|
||||
*
|
||||
* @return Users
|
||||
* @throws IOException
|
||||
*/
|
||||
public PagedIterable<GHUser> listCollaborators() throws IOException {
|
||||
return new PagedIterable<GHUser>() {
|
||||
public PagedIterator<GHUser> iterator() {
|
||||
|
||||
return new PagedIterator<GHUser>(root.retrieve().asIterator("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class)) {
|
||||
|
||||
@Override
|
||||
protected void wrapUp(GHUser[] users) {
|
||||
for (GHUser user : users) {
|
||||
user.wrapUp(root);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -416,7 +442,11 @@ public class GHRepository {
|
||||
* Deletes this repository.
|
||||
*/
|
||||
public void delete() throws IOException {
|
||||
new Requester(root).method("DELETE").to("/repos/" + owner.login + "/" + name);
|
||||
try {
|
||||
new Requester(root).method("DELETE").to("/repos/" + owner.login + "/" + name);
|
||||
} catch (FileNotFoundException x) {
|
||||
throw (FileNotFoundException) new FileNotFoundException("Failed to delete " + owner.login + "/" + name + "; might not exist, or you might need the delete_repo scope in your token: http://stackoverflow.com/a/19327004/12916").initCause(x);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -561,7 +591,19 @@ public class GHRepository {
|
||||
public GHRef[] getRefs(String refType) throws IOException {
|
||||
return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refType), GHRef[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrive a ref of the given type for the current GitHub repository.
|
||||
*
|
||||
* @param refName
|
||||
* eg: heads/branch
|
||||
* @return refs matching the request type
|
||||
* @throws IOException
|
||||
* on failure communicating with GitHub, potentially due to an
|
||||
* invalid ref type being requested
|
||||
*/
|
||||
public GHRef getRef(String refName) throws IOException {
|
||||
return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refName), GHRef.class);
|
||||
}
|
||||
/**
|
||||
* Gets a commit object in this repository.
|
||||
*/
|
||||
@@ -646,14 +688,24 @@ public class GHRepository {
|
||||
* Optional parameter that points to the URL that has more details.
|
||||
* @param description
|
||||
* Optional short description.
|
||||
* @param context
|
||||
* Optinal commit status context.
|
||||
*/
|
||||
public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, String targetUrl, String description) throws IOException {
|
||||
public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, String targetUrl, String description, String context) throws IOException {
|
||||
return new Requester(root)
|
||||
.with("state", state.name().toLowerCase(Locale.ENGLISH))
|
||||
.with("target_url", targetUrl)
|
||||
.with("description", description)
|
||||
.with("context", context)
|
||||
.to(String.format("/repos/%s/%s/statuses/%s",owner.login,this.name,sha1),GHCommitStatus.class).wrapUp(root);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see {@link #createCommitStatus(String, GHCommitState,String,String,String) createCommitStatus}
|
||||
*/
|
||||
public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, String targetUrl, String description) throws IOException {
|
||||
return createCommitStatus(sha1, state, targetUrl, description,null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists repository events.
|
||||
@@ -907,6 +959,22 @@ public class GHRepository {
|
||||
return new Requester(root)
|
||||
.with("title", title).with("description", description).method("POST").to(getApiTailUrl("milestones"), GHMilestone.class).wrap(this);
|
||||
}
|
||||
|
||||
public GHDeployKey addDeployKey(String title,String key) throws IOException {
|
||||
return new Requester(root)
|
||||
.with("title", title).with("key", key).method("POST").to(getApiTailUrl("keys"), GHDeployKey.class).wrap(this);
|
||||
|
||||
}
|
||||
|
||||
public List<GHDeployKey> getDeployKeys() throws IOException{
|
||||
List<GHDeployKey> list = new ArrayList<GHDeployKey>(Arrays.asList(
|
||||
root.retrieve().to(String.format("/repos/%s/%s/keys", owner.login, name), GHDeployKey[].class)));
|
||||
for (GHDeployKey h : list)
|
||||
h.wrap(this);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.kohsuke.github.extras;
|
||||
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.OkUrlFactory;
|
||||
import org.kohsuke.github.HttpConnector;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -19,13 +20,13 @@ import java.net.URL;
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class OkHttpConnector implements HttpConnector {
|
||||
private final OkHttpClient client;
|
||||
private final OkUrlFactory urlFactory;
|
||||
|
||||
public OkHttpConnector(OkHttpClient client) {
|
||||
this.client = client;
|
||||
public OkHttpConnector(OkUrlFactory urlFactory) {
|
||||
this.urlFactory = urlFactory;
|
||||
}
|
||||
|
||||
public HttpURLConnection connect(URL url) throws IOException {
|
||||
return client.open(url);
|
||||
return urlFactory.open(url);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.kohsuke.github;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -20,6 +21,9 @@ import org.junit.Test;
|
||||
import org.kohsuke.github.GHCommit.File;
|
||||
import org.kohsuke.github.GHOrganization.Permission;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
@@ -529,6 +533,52 @@ public class AppTest extends AbstractGitHubApiTestBase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRef() throws IOException {
|
||||
GHRef masterRef = gitHub.getRepository("jenkinsci/jenkins").getRef("heads/master");
|
||||
assertEquals("https://api.github.com/repos/jenkinsci/jenkins/git/refs/heads/master", masterRef.getUrl().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directoryListing() throws IOException {
|
||||
List<GHContent> children = gitHub.getRepository("jenkinsci/jenkins").getDirectoryContent("core");
|
||||
for (GHContent c : children) {
|
||||
System.out.println(c.getName());
|
||||
if (c.isDirectory()) {
|
||||
for (GHContent d : c.listDirectoryContent()) {
|
||||
System.out.println(" "+d.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddDeployKey() throws IOException {
|
||||
GHRepository myRepository = Iterables.get(gitHub.getMyself().getRepositories().values(),0);
|
||||
final GHDeployKey newDeployKey = myRepository.addDeployKey("test", "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUt0RAycC5cS42JKh6SecfFZBR1RrF+2hYMctz4mk74/arBE+wFb7fnSHGzdGKX2h5CFOWODifRCJVhB7hlVxodxe+QkQQYAEL/x1WVCJnGgTGQGOrhOMj95V3UE5pQKhsKD608C+u5tSofcWXLToP1/wZ7U4/AHjqYi08OLsWToHCax55TZkvdt2jo0hbIoYU+XI9Q8Uv4ONDN1oabiOdgeKi8+crvHAuvNleiBhWVBzFh8KdfzaH5uNdw7ihhFjEd1vzqACsjCINCjdMfzl6jD9ExuWuE92nZJnucls2cEoNC6k2aPmrZDg9hA32FXVpyseY+bDUWFU6LO2LG6PB kohsuke@atlas");
|
||||
try {
|
||||
assertNotNull(newDeployKey.getId());
|
||||
|
||||
GHDeployKey k = Iterables.find(myRepository.getDeployKeys(), new Predicate<GHDeployKey>() {
|
||||
public boolean apply(GHDeployKey deployKey) {
|
||||
return newDeployKey.getId() == deployKey.getId();
|
||||
}
|
||||
});
|
||||
assertNotNull(k);
|
||||
} finally {
|
||||
newDeployKey.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommitStatusContext() throws IOException {
|
||||
GHRepository myRepository = Iterables.get(gitHub.getMyself().getRepositories().values(), 0);
|
||||
GHRef masterRef = myRepository.getRef("heads/master");
|
||||
GHCommitStatus commitStatus = myRepository.createCommitStatus(masterRef.getObject().getSha(), GHCommitState.SUCCESS, "http://www.example.com", "test", "test/context");
|
||||
assertEquals("test/context", commitStatus.getContext());
|
||||
|
||||
}
|
||||
|
||||
private void kohsuke() {
|
||||
String login = getUser().getLogin();
|
||||
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
|
||||
|
||||
91
src/test/java/org/kohsuke/github/RepositoryTest.java
Normal file
91
src/test/java/org/kohsuke/github/RepositoryTest.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package org.kohsuke.github;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Luciano P. Sabenca (luciano.sabenca [at] movile [com] | lucianosabenca [at] gmail [dot] com
|
||||
*/
|
||||
public class RepositoryTest {
|
||||
|
||||
@Mock
|
||||
GitHub mockGitHub;
|
||||
|
||||
@Mock
|
||||
Iterator<GHUser[]> iterator;
|
||||
|
||||
@Mock
|
||||
GHRepository mockRepository;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listCollaborators() throws Exception {
|
||||
GHUser user1 = new GHUser();
|
||||
user1.login = "login1";
|
||||
|
||||
GHUser user2 = new GHUser();
|
||||
user2.login = "login2";
|
||||
|
||||
|
||||
when(iterator.hasNext()).thenReturn(true, false, true);
|
||||
when(iterator.next()).thenReturn(new GHUser[]{user1}, new GHUser[]{user2});
|
||||
|
||||
Requester requester = Mockito.mock(Requester.class);
|
||||
when(mockGitHub.retrieve()).thenReturn(requester);
|
||||
|
||||
|
||||
when(requester.asIterator("/repos/*/*/collaborators",
|
||||
GHUser[].class)).thenReturn(iterator, iterator);
|
||||
|
||||
|
||||
PagedIterable<GHUser> pagedIterable = Mockito.mock(PagedIterable.class);
|
||||
when(mockRepository.listCollaborators()).thenReturn(pagedIterable);
|
||||
|
||||
PagedIterator<GHUser> userPagedIterator = new PagedIterator<GHUser>(iterator) {
|
||||
@Override
|
||||
protected void wrapUp(GHUser[] page) {
|
||||
|
||||
}
|
||||
};
|
||||
PagedIterator<GHUser> userPagedIterator2 = new PagedIterator<GHUser>(iterator) {
|
||||
@Override
|
||||
protected void wrapUp(GHUser[] page) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
when(pagedIterable.iterator()).thenReturn(userPagedIterator, userPagedIterator2);
|
||||
|
||||
Iterator<GHUser> returnIterator1 = mockRepository.listCollaborators().iterator();
|
||||
|
||||
|
||||
Assert.assertTrue(returnIterator1.hasNext());
|
||||
GHUser user = returnIterator1.next();
|
||||
Assert.assertEquals(user, user1);
|
||||
Assert.assertFalse(returnIterator1.hasNext());
|
||||
|
||||
|
||||
Iterator returnIterator2 = mockRepository.listCollaborators().iterator();
|
||||
|
||||
|
||||
Assert.assertTrue(returnIterator2.hasNext());
|
||||
user = returnIterator1.next();
|
||||
Assert.assertEquals(user, user2);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user