mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-13 00:11:22 +00:00
Compare commits
14 Commits
github-api
...
github-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e2c9f86da | ||
|
|
44cc3c8556 | ||
|
|
bf65a746b0 | ||
|
|
4a4a469ca4 | ||
|
|
46b2e5dd32 | ||
|
|
8a7adf876f | ||
|
|
c16ebe4fec | ||
|
|
4c61f79e5c | ||
|
|
646dc17e5d | ||
|
|
7977471458 | ||
|
|
9ac2ad957b | ||
|
|
7fead8171d | ||
|
|
70bc5c59e2 | ||
|
|
06ef6e841d |
25
pom.xml
25
pom.xml
@@ -7,7 +7,7 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>github-api</artifactId>
|
<artifactId>github-api</artifactId>
|
||||||
<version>1.15</version>
|
<version>1.16</version>
|
||||||
<name>GitHub API for Java</name>
|
<name>GitHub API for Java</name>
|
||||||
<url>http://github-api.kohsuke.org/</url>
|
<url>http://github-api.kohsuke.org/</url>
|
||||||
<description>GitHub API for Java</description>
|
<description>GitHub API for Java</description>
|
||||||
@@ -25,6 +25,23 @@
|
|||||||
</site>
|
</site>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.infradna.tool</groupId>
|
||||||
|
<artifactId>bridge-method-injector</artifactId>
|
||||||
|
<version>1.2</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>process</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jvnet.hudson</groupId>
|
<groupId>org.jvnet.hudson</groupId>
|
||||||
@@ -54,6 +71,12 @@
|
|||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>1.4</version>
|
<version>1.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.infradna.tool</groupId>
|
||||||
|
<artifactId>bridge-method-annotation</artifactId>
|
||||||
|
<version>1.4</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<reporting>
|
<reporting>
|
||||||
|
|||||||
28
src/main/java/org/kohsuke/github/GHEvent.java
Normal file
28
src/main/java/org/kohsuke/github/GHEvent.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook event type.
|
||||||
|
*
|
||||||
|
* See http://developer.github.com/v3/events/types/
|
||||||
|
*
|
||||||
|
* @author Kohsuke Kawaguchi
|
||||||
|
*/
|
||||||
|
public enum GHEvent {
|
||||||
|
COMMIT_COMMENT,
|
||||||
|
CREATE,
|
||||||
|
DELETE,
|
||||||
|
DOWNLOAD,
|
||||||
|
FOLLOW,
|
||||||
|
FORK,
|
||||||
|
FORK_APPLY,
|
||||||
|
GIST,
|
||||||
|
GOLLUM,
|
||||||
|
ISSUE_COMMENT,
|
||||||
|
ISSUES,
|
||||||
|
MEMBER,
|
||||||
|
PUBLIC,
|
||||||
|
PULL_REQUEST,
|
||||||
|
PUSH,
|
||||||
|
TEAM_ADD,
|
||||||
|
WATCH
|
||||||
|
}
|
||||||
60
src/main/java/org/kohsuke/github/GHHook.java
Normal file
60
src/main/java/org/kohsuke/github/GHHook.java
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kohsuke Kawaguchi
|
||||||
|
*/
|
||||||
|
public final class GHHook {
|
||||||
|
/**
|
||||||
|
* Repository that the hook belongs to.
|
||||||
|
*/
|
||||||
|
/*package*/ transient GHRepository repository;
|
||||||
|
|
||||||
|
String created_at, updated_at, name;
|
||||||
|
List<String> events;
|
||||||
|
boolean active;
|
||||||
|
Map<String,String> config;
|
||||||
|
int id;
|
||||||
|
|
||||||
|
/*package*/ GHHook wrap(GHRepository owner) {
|
||||||
|
this.repository = owner;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnumSet<GHEvent> getEvents() {
|
||||||
|
EnumSet<GHEvent> s = EnumSet.noneOf(GHEvent.class);
|
||||||
|
for (String e : events)
|
||||||
|
Enum.valueOf(GHEvent.class,e.toUpperCase(Locale.ENGLISH));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isActive() {
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getConfig() {
|
||||||
|
return Collections.unmodifiableMap(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes this hook.
|
||||||
|
*/
|
||||||
|
public void delete() throws IOException {
|
||||||
|
new Poster(repository.root,ApiVersion.V3).withCredential()
|
||||||
|
.to(String.format("/repos/%s/%s/hooks/%d",repository.getOwnerName(),repository.getName(),id),null,"DELETE");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,9 @@ package org.kohsuke.github;
|
|||||||
|
|
||||||
import com.gargoylesoftware.htmlunit.WebClient;
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.AbstractList;
|
import java.util.AbstractList;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -13,7 +13,7 @@ import java.util.Collection;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.kohsuke.github.ApiVersion.V3;
|
import static org.kohsuke.github.ApiVersion.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Kohsuke Kawaguchi
|
* @author Kohsuke Kawaguchi
|
||||||
@@ -43,6 +43,15 @@ public class GHOrganization extends GHPerson {
|
|||||||
return root.retrieveWithAuth("/organizations/"+login+"/teams",JsonTeams.class).toMap(this);
|
return root.retrieveWithAuth("/organizations/"+login+"/teams",JsonTeams.class).toMap(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GHRepository getRepository(String name) throws IOException {
|
||||||
|
try {
|
||||||
|
return root.retrieveWithAuth3("/repos/" + login + '/' + name, GHRepository.class).wrap(root);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Publicizes the membership.
|
* Publicizes the membership.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -14,9 +14,17 @@ import java.util.TreeMap;
|
|||||||
public abstract class GHPerson {
|
public abstract class GHPerson {
|
||||||
/*package almost final*/ GitHub root;
|
/*package almost final*/ GitHub root;
|
||||||
|
|
||||||
protected String gravatar_id,login;
|
// common
|
||||||
|
protected String login,location,blog,email,name,created_at,company;
|
||||||
|
protected int id;
|
||||||
|
protected String gravatar_id; // appears in V3 as well but presumably subsumed by avatar_url?
|
||||||
|
|
||||||
protected int public_gist_count,public_repo_count,following_count,id;
|
// V2
|
||||||
|
protected int public_gist_count,public_repo_count,followers_count,following_count;
|
||||||
|
|
||||||
|
// V3
|
||||||
|
protected String avatar_url,html_url;
|
||||||
|
protected int followers,following,public_repos,public_gists;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the repositories this user owns.
|
* Gets the repositories this user owns.
|
||||||
@@ -24,9 +32,12 @@ public abstract class GHPerson {
|
|||||||
public synchronized Map<String,GHRepository> getRepositories() throws IOException {
|
public synchronized Map<String,GHRepository> getRepositories() throws IOException {
|
||||||
Map<String,GHRepository> repositories = new TreeMap<String, GHRepository>();
|
Map<String,GHRepository> repositories = new TreeMap<String, GHRepository>();
|
||||||
for (int i=1; ; i++) {
|
for (int i=1; ; i++) {
|
||||||
Map<String, GHRepository> map = root.retrieve3("/user/" + login + "/repos?per_page=100&page=" + i, JsonRepositories.class).wrap(root);
|
GHRepository[] array = root.retrieve3("/users/" + login + "/repos?per_page=100&page=" + i, GHRepository[].class);
|
||||||
repositories.putAll(map);
|
for (GHRepository r : array) {
|
||||||
if (map.isEmpty()) break;
|
r.root = root;
|
||||||
|
repositories.put(r.getName(),r);
|
||||||
|
}
|
||||||
|
if (array.length==0) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Collections.unmodifiableMap(repositories);
|
return Collections.unmodifiableMap(repositories);
|
||||||
@@ -52,7 +63,73 @@ public abstract class GHPerson {
|
|||||||
return gravatar_id;
|
return gravatar_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the login ID of this user, like 'kohsuke'
|
||||||
|
*/
|
||||||
public String getLogin() {
|
public String getLogin() {
|
||||||
return login;
|
return login;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the human-readable name of the user, like "Kohsuke Kawaguchi"
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the company name of this user, like "Sun Microsystems, Inc."
|
||||||
|
*/
|
||||||
|
public String getCompany() {
|
||||||
|
return company;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the location of this user, like "Santa Clara, California"
|
||||||
|
*/
|
||||||
|
public String getLocation() {
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCreatedAt() {
|
||||||
|
return created_at;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the blog URL of this user.
|
||||||
|
*/
|
||||||
|
public String getBlog() {
|
||||||
|
return blog;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the e-mail address of the user.
|
||||||
|
*/
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPublicGistCount() {
|
||||||
|
return Math.max(public_gist_count,public_gists);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPublicRepoCount() {
|
||||||
|
return Math.max(public_repo_count,public_repos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFollowingCount() {
|
||||||
|
return Math.max(following_count,following);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What appears to be a GitHub internal unique number that identifies this user.
|
||||||
|
*/
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFollowersCount() {
|
||||||
|
return Math.max(followers_count,followers);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
36
src/main/java/org/kohsuke/github/GHPersonSet.java
Normal file
36
src/main/java/org/kohsuke/github/GHPersonSet.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set of {@link GHPerson} with helper lookup methods.
|
||||||
|
*
|
||||||
|
* @author Kohsuke Kawaguchi
|
||||||
|
*/
|
||||||
|
public final class GHPersonSet<T extends GHPerson> extends HashSet<T> {
|
||||||
|
public GHPersonSet() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHPersonSet(Collection<? extends T> c) {
|
||||||
|
super(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHPersonSet(int initialCapacity, float loadFactor) {
|
||||||
|
super(initialCapacity, loadFactor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHPersonSet(int initialCapacity) {
|
||||||
|
super(initialCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the item by its login.
|
||||||
|
*/
|
||||||
|
public T byLogin(String login) {
|
||||||
|
for (T t : this)
|
||||||
|
if (t.getLogin().equals(login))
|
||||||
|
return t;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,14 +30,15 @@ import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
|
|||||||
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlInput;
|
import com.gargoylesoftware.htmlunit.html.HtmlInput;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||||
|
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.AbstractSet;
|
import java.util.AbstractSet;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -45,6 +46,7 @@ import java.util.HashSet;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import static java.util.Arrays.*;
|
import static java.util.Arrays.*;
|
||||||
@@ -143,11 +145,12 @@ public class GHRepository {
|
|||||||
* Gets the collaborators on this repository.
|
* Gets the collaborators on this repository.
|
||||||
* This set always appear to include the owner.
|
* This set always appear to include the owner.
|
||||||
*/
|
*/
|
||||||
public Set<GHUser> getCollaborators() throws IOException {
|
@WithBridgeMethods(Set.class)
|
||||||
Set<GHUser> r = new HashSet<GHUser>();
|
public GHPersonSet<GHUser> getCollaborators() throws IOException {
|
||||||
|
GHPersonSet<GHUser> r = new GHPersonSet<GHUser>();
|
||||||
for (String u : root.retrieve("/repos/show/"+owner.login+"/"+name+"/collaborators",JsonCollaborators.class).collaborators)
|
for (String u : root.retrieve("/repos/show/"+owner.login+"/"+name+"/collaborators",JsonCollaborators.class).collaborators)
|
||||||
r.add(root.getUser(u));
|
r.add(root.getUser(u));
|
||||||
return Collections.unmodifiableSet(r);
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -297,6 +300,58 @@ public class GHRepository {
|
|||||||
return root.retrieveWithAuth("/pulls/"+owner.login+'/'+name+"/"+state.name().toLowerCase(Locale.ENGLISH),JsonPullRequests.class).wrap(this);
|
return root.retrieveWithAuth("/pulls/"+owner.login+'/'+name+"/"+state.name().toLowerCase(Locale.ENGLISH),JsonPullRequests.class).wrap(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the currently configured hooks.
|
||||||
|
*/
|
||||||
|
public List<GHHook> getHooks() throws IOException {
|
||||||
|
List<GHHook> list = new ArrayList<GHHook>(Arrays.asList(
|
||||||
|
root.retrieveWithAuth3(String.format("/repos/%s/%s/hooks",owner.login,name),GHHook[].class)));
|
||||||
|
for (GHHook h : list)
|
||||||
|
h.wrap(this);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHHook getHook(int id) throws IOException {
|
||||||
|
return root.retrieveWithAuth3(String.format("/repos/%s/%s/hooks/%d",owner.login,name,id),GHHook.class).wrap(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* See https://api.github.com/hooks for possible names and their configuration scheme.
|
||||||
|
* TODO: produce type-safe binding
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* Type of the hook to be created.
|
||||||
|
* @param config
|
||||||
|
* The configuration hash.
|
||||||
|
* @param events
|
||||||
|
* Can be null. Types of events to hook into.
|
||||||
|
*/
|
||||||
|
public GHHook createHook(String name, Map<String,String> config, Collection<GHEvent> events, boolean active) throws IOException {
|
||||||
|
List<String> ea = null;
|
||||||
|
if (events!=null) {
|
||||||
|
ea = new ArrayList<String>();
|
||||||
|
for (GHEvent e : events)
|
||||||
|
ea.add(e.name().toLowerCase(Locale.ENGLISH));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Poster(root,ApiVersion.V3)
|
||||||
|
.withCredential()
|
||||||
|
.with("name",name)
|
||||||
|
.with("active", active)
|
||||||
|
._with("config", config)
|
||||||
|
._with("events",ea)
|
||||||
|
.to(String.format("/repos/%s/%s/hooks",owner.login,this.name),GHHook.class).wrap(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHHook createWebHook(URL url, Collection<GHEvent> events) throws IOException {
|
||||||
|
return createHook("web",Collections.singletonMap("url",url.toExternalForm()),events,true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHHook createWebHook(URL url) throws IOException {
|
||||||
|
return createWebHook(url,null);
|
||||||
|
}
|
||||||
|
|
||||||
// this is no different from getPullRequests(OPEN)
|
// this is no different from getPullRequests(OPEN)
|
||||||
// /**
|
// /**
|
||||||
// * Retrieves all the pull requests.
|
// * Retrieves all the pull requests.
|
||||||
@@ -313,6 +368,9 @@ public class GHRepository {
|
|||||||
/**
|
/**
|
||||||
* Returns a set that represents the post-commit hook URLs.
|
* Returns a set that represents the post-commit hook URLs.
|
||||||
* The returned set is live, and changes made to them are reflected to GitHub.
|
* The returned set is live, and changes made to them are reflected to GitHub.
|
||||||
|
*
|
||||||
|
* @deprecated
|
||||||
|
* Use {@link #getHooks()} and {@link #createHook(String, Map, Collection, boolean)}
|
||||||
*/
|
*/
|
||||||
public Set<URL> getPostCommitHooks() {
|
public Set<URL> getPostCommitHooks() {
|
||||||
return postCommitHooks;
|
return postCommitHooks;
|
||||||
@@ -324,15 +382,11 @@ public class GHRepository {
|
|||||||
private final Set<URL> postCommitHooks = new AbstractSet<URL>() {
|
private final Set<URL> postCommitHooks = new AbstractSet<URL>() {
|
||||||
private List<URL> getPostCommitHooks() {
|
private List<URL> getPostCommitHooks() {
|
||||||
try {
|
try {
|
||||||
verifyMine();
|
|
||||||
|
|
||||||
HtmlForm f = getForm();
|
|
||||||
|
|
||||||
List<URL> r = new ArrayList<URL>();
|
List<URL> r = new ArrayList<URL>();
|
||||||
for (HtmlInput i : f.getInputsByName("urls[]")) {
|
for (GHHook h : getHooks()) {
|
||||||
String v = i.getValueAttribute();
|
if (h.getName().equals("web")) {
|
||||||
if (v.length()==0) continue;
|
r.add(new URL(h.getConfig().get("url")));
|
||||||
r.add(new URL(v));
|
}
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -353,22 +407,7 @@ public class GHRepository {
|
|||||||
@Override
|
@Override
|
||||||
public boolean add(URL url) {
|
public boolean add(URL url) {
|
||||||
try {
|
try {
|
||||||
String u = url.toExternalForm();
|
createWebHook(url);
|
||||||
|
|
||||||
verifyMine();
|
|
||||||
|
|
||||||
HtmlForm f = getForm();
|
|
||||||
|
|
||||||
List<HtmlInput> controls = f.getInputsByName("urls[]");
|
|
||||||
for (HtmlInput i : controls) {
|
|
||||||
String v = i.getValueAttribute();
|
|
||||||
if (v.length()==0) continue;
|
|
||||||
if (v.equals(u))
|
|
||||||
return false; // already there
|
|
||||||
}
|
|
||||||
|
|
||||||
controls.get(controls.size()-1).setValueAttribute(u);
|
|
||||||
f.submit(null);
|
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new GHException("Failed to update post-commit hooks",e);
|
throw new GHException("Failed to update post-commit hooks",e);
|
||||||
@@ -376,37 +415,20 @@ public class GHRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean remove(Object o) {
|
public boolean remove(Object url) {
|
||||||
try {
|
try {
|
||||||
String u = ((URL)o).toExternalForm();
|
String _url = ((URL)url).toExternalForm();
|
||||||
|
for (GHHook h : getHooks()) {
|
||||||
verifyMine();
|
if (h.getName().equals("web") && h.getConfig().get("url").equals(_url)) {
|
||||||
|
h.delete();
|
||||||
HtmlForm f = getForm();
|
|
||||||
|
|
||||||
List<HtmlInput> controls = f.getInputsByName("urls[]");
|
|
||||||
for (HtmlInput i : controls) {
|
|
||||||
String v = i.getValueAttribute();
|
|
||||||
if (v.length()==0) continue;
|
|
||||||
if (v.equals(u)) {
|
|
||||||
i.setValueAttribute("");
|
|
||||||
f.submit(null);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new GHException("Failed to update post-commit hooks",e);
|
throw new GHException("Failed to update post-commit hooks",e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private HtmlForm getForm() throws IOException {
|
|
||||||
WebClient wc = root.createWebClient();
|
|
||||||
HtmlPage pg = (HtmlPage)wc.getPage(getUrl()+"/admin");
|
|
||||||
HtmlForm f = (HtmlForm) pg.getElementById("new_service");
|
|
||||||
return f;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*package*/ GHRepository wrap(GitHub root) {
|
/*package*/ GHRepository wrap(GitHub root) {
|
||||||
|
|||||||
@@ -23,7 +23,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.kohsuke.github;
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import com.infradna.tool.bridge_method_injector.BridgeMethodsAdded;
|
||||||
|
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,77 +37,6 @@ import java.util.Set;
|
|||||||
* @author Kohsuke Kawaguchi
|
* @author Kohsuke Kawaguchi
|
||||||
*/
|
*/
|
||||||
public class GHUser extends GHPerson {
|
public class GHUser extends GHPerson {
|
||||||
private String name,company,location,created_at,blog,email;
|
|
||||||
private int followers_count;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the human-readable name of the user, like "Kohsuke Kawaguchi"
|
|
||||||
*/
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the company name of this user, like "Sun Microsystems, Inc."
|
|
||||||
*/
|
|
||||||
public String getCompany() {
|
|
||||||
return company;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the location of this user, like "Santa Clara, California"
|
|
||||||
*/
|
|
||||||
public String getLocation() {
|
|
||||||
return location;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCreatedAt() {
|
|
||||||
return created_at;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the blog URL of this user.
|
|
||||||
*/
|
|
||||||
public String getBlog() {
|
|
||||||
return blog;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the login ID of this user, like 'kohsuke'
|
|
||||||
*/
|
|
||||||
public String getLogin() {
|
|
||||||
return login;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the e-mail address of the user.
|
|
||||||
*/
|
|
||||||
public String getEmail() {
|
|
||||||
return email;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPublicGistCount() {
|
|
||||||
return public_gist_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPublicRepoCount() {
|
|
||||||
return public_repo_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getFollowingCount() {
|
|
||||||
return following_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* What appears to be a GitHub internal unique number that identifies this user.
|
|
||||||
*/
|
|
||||||
public int getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getFollowersCount() {
|
|
||||||
return followers_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Follow this user.
|
* Follow this user.
|
||||||
@@ -121,17 +55,33 @@ public class GHUser extends GHPerson {
|
|||||||
/**
|
/**
|
||||||
* Lists the users that this user is following
|
* Lists the users that this user is following
|
||||||
*/
|
*/
|
||||||
public Set<GHUser> getFollows() throws IOException {
|
@WithBridgeMethods(Set.class)
|
||||||
|
public GHPersonSet<GHUser> getFollows() throws IOException {
|
||||||
return root.retrieve("/user/show/"+login+"/following",JsonUsers.class).toSet(root);
|
return root.retrieve("/user/show/"+login+"/following",JsonUsers.class).toSet(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists the users who are following this user.
|
* Lists the users who are following this user.
|
||||||
*/
|
*/
|
||||||
public Set<GHUser> getFollowers() throws IOException {
|
@WithBridgeMethods(Set.class)
|
||||||
|
public GHPersonSet<GHUser> getFollowers() throws IOException {
|
||||||
return root.retrieve("/user/show/"+login+"/followers",JsonUsers.class).toSet(root);
|
return root.retrieve("/user/show/"+login+"/followers",JsonUsers.class).toSet(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the organization that this user belongs to publicly.
|
||||||
|
*/
|
||||||
|
@WithBridgeMethods(Set.class)
|
||||||
|
public GHPersonSet<GHOrganization> getOrganizations() throws IOException {
|
||||||
|
GHPersonSet<GHOrganization> orgs = new GHPersonSet<GHOrganization>();
|
||||||
|
Set<String> names = new HashSet<String>();
|
||||||
|
for (GHOrganization o : root.retrieve3("/users/"+login+"/orgs",GHOrganization[].class)) {
|
||||||
|
if (names.add(o.getLogin())) // I've seen some duplicates in the data
|
||||||
|
orgs.add(root.getOrganization(o.getLogin()));
|
||||||
|
}
|
||||||
|
return orgs;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "User:"+login;
|
return "User:"+login;
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ public class GitHub {
|
|||||||
|
|
||||||
private final Map<String,GHUser> users = new HashMap<String, GHUser>();
|
private final Map<String,GHUser> users = new HashMap<String, GHUser>();
|
||||||
private final Map<String,GHOrganization> orgs = new HashMap<String, GHOrganization>();
|
private final Map<String,GHOrganization> orgs = new HashMap<String, GHOrganization>();
|
||||||
private String oauthAccessToken;
|
/*package*/ String oauthAccessToken;
|
||||||
|
|
||||||
private GitHub(String login, String apiToken, String password) {
|
private GitHub(String login, String apiToken, String password) {
|
||||||
this.login = login;
|
this.login = login;
|
||||||
@@ -134,7 +134,7 @@ public class GitHub {
|
|||||||
/*package*/ URL getApiURL(ApiVersion v, String tailApiUrl) throws IOException {
|
/*package*/ URL getApiURL(ApiVersion v, String tailApiUrl) throws IOException {
|
||||||
if (oauthAccessToken != null) {
|
if (oauthAccessToken != null) {
|
||||||
// append the access token
|
// append the access token
|
||||||
tailApiUrl = tailApiUrl + "?access_token=" + oauthAccessToken;
|
tailApiUrl = tailApiUrl + (tailApiUrl.indexOf('?')>=0 ?'&':'?') + "access_token=" + oauthAccessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new URL(v.url+tailApiUrl);
|
return new URL(v.url+tailApiUrl);
|
||||||
@@ -167,7 +167,6 @@ public class GitHub {
|
|||||||
private <T> T _retrieve(String tailApiUrl, Class<T> type, String method, boolean withAuth, ApiVersion v) throws IOException {
|
private <T> T _retrieve(String tailApiUrl, Class<T> type, String method, boolean withAuth, ApiVersion v) throws IOException {
|
||||||
while (true) {// loop while API rate limit is hit
|
while (true) {// loop while API rate limit is hit
|
||||||
|
|
||||||
|
|
||||||
HttpURLConnection uc = (HttpURLConnection) getApiURL(v,tailApiUrl).openConnection();
|
HttpURLConnection uc = (HttpURLConnection) getApiURL(v,tailApiUrl).openConnection();
|
||||||
|
|
||||||
if (withAuth && this.oauthAccessToken == null)
|
if (withAuth && this.oauthAccessToken == null)
|
||||||
@@ -224,23 +223,16 @@ public class GitHub {
|
|||||||
public GHUser getMyself() throws IOException {
|
public GHUser getMyself() throws IOException {
|
||||||
requireCredential();
|
requireCredential();
|
||||||
|
|
||||||
GHUser u = null;
|
|
||||||
|
|
||||||
if (oauthAccessToken != null) {
|
if (oauthAccessToken != null) {
|
||||||
|
GHUser u = retrieveWithAuth("/user/show", JsonUser.class).user;
|
||||||
u = retrieve("/user/show", JsonUser.class).user;
|
|
||||||
|
|
||||||
u.root = this;
|
u.root = this;
|
||||||
users.put(u.getLogin(), u);
|
users.put(u.getLogin(), u);
|
||||||
|
|
||||||
return u;
|
return u;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return getUser(login);
|
return getUser(login);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ import java.util.Set;
|
|||||||
class JsonUsers {
|
class JsonUsers {
|
||||||
public List<String> users;
|
public List<String> users;
|
||||||
|
|
||||||
public Set<GHUser> toSet(GitHub root) throws IOException {
|
public GHPersonSet<GHUser> toSet(GitHub root) throws IOException {
|
||||||
Set<GHUser> r = new HashSet<GHUser>();
|
GHPersonSet<GHUser> r = new GHPersonSet<GHUser>();
|
||||||
for (String u : users)
|
for (String u : users)
|
||||||
r.add(root.getUser(u));
|
r.add(root.getUser(u));
|
||||||
return r;
|
return r;
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ class Poster {
|
|||||||
return _with(key, value);
|
return _with(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Poster _with(String key, Object value) {
|
public Poster _with(String key, Object value) {
|
||||||
if (value!=null) {
|
if (value!=null) {
|
||||||
args.add(new Entry(key,value));
|
args.add(new Entry(key,value));
|
||||||
}
|
}
|
||||||
@@ -125,9 +125,13 @@ class Poster {
|
|||||||
uc.setRequestProperty("Content-type","application/x-www-form-urlencoded");
|
uc.setRequestProperty("Content-type","application/x-www-form-urlencoded");
|
||||||
if (authenticate) {
|
if (authenticate) {
|
||||||
if (v==ApiVersion.V3) {
|
if (v==ApiVersion.V3) {
|
||||||
if (root.password==null)
|
if (root.oauthAccessToken!=null) {
|
||||||
throw new IllegalArgumentException("V3 API doesn't support API token");
|
uc.setRequestProperty("Authorization", "token " + root.oauthAccessToken);
|
||||||
uc.setRequestProperty("Authorization", "Basic " + root.encodedAuthorization);
|
} else {
|
||||||
|
if (root.password==null)
|
||||||
|
throw new IllegalArgumentException("V3 API doesn't support API token");
|
||||||
|
uc.setRequestProperty("Authorization", "Basic " + root.encodedAuthorization);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
uc.setRequestProperty("Authorization", "Basic " + root.encodedAuthorization);
|
uc.setRequestProperty("Authorization", "Basic " + root.encodedAuthorization);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
package org.kohsuke;
|
package org.kohsuke;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
import org.kohsuke.github.GHHook;
|
||||||
import org.kohsuke.github.GHOrganization;
|
import org.kohsuke.github.GHOrganization;
|
||||||
import org.kohsuke.github.GHOrganization.Permission;
|
import org.kohsuke.github.GHOrganization.Permission;
|
||||||
import org.kohsuke.github.GHPullRequest;
|
|
||||||
import org.kohsuke.github.GHRepository;
|
import org.kohsuke.github.GHRepository;
|
||||||
import org.kohsuke.github.GHTeam;
|
import org.kohsuke.github.GHTeam;
|
||||||
import org.kohsuke.github.GHUser;
|
|
||||||
import org.kohsuke.github.GitHub;
|
import org.kohsuke.github.GitHub;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -38,6 +37,22 @@ public class AppTest extends TestCase {
|
|||||||
Set<String> members = gitHub.getOrganization("jenkinsci").getRepository("violations-plugin").getCollaboratorNames();
|
Set<String> members = gitHub.getOrganization("jenkinsci").getRepository("violations-plugin").getCollaboratorNames();
|
||||||
System.out.println(members.contains("kohsuke"));
|
System.out.println(members.contains("kohsuke"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testMemberOrgs() throws Exception {
|
||||||
|
GitHub gitHub = GitHub.connect();
|
||||||
|
Set<GHOrganization> o = gitHub.getUser("kohsuke").getOrganizations();
|
||||||
|
System.out.println(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tryHook() throws Exception {
|
||||||
|
GitHub gitHub = GitHub.connect();
|
||||||
|
GHRepository r = gitHub.getMyself().getRepository("test2");
|
||||||
|
GHHook hook = r.createWebHook(new URL("http://www.google.com/"));
|
||||||
|
System.out.println(hook);
|
||||||
|
|
||||||
|
for (GHHook h : r.getHooks())
|
||||||
|
h.delete();
|
||||||
|
}
|
||||||
|
|
||||||
public void testApp() throws IOException {
|
public void testApp() throws IOException {
|
||||||
GitHub gitHub = GitHub.connect();
|
GitHub gitHub = GitHub.connect();
|
||||||
|
|||||||
Reference in New Issue
Block a user