Skip to content

Commit

Permalink
Merge pull request #1 from kohsuke/master
Browse files Browse the repository at this point in the history
catchup
  • Loading branch information
jeffnelson committed Dec 14, 2016
2 parents d91388a + 0731f63 commit 9d15cd4
Show file tree
Hide file tree
Showing 84 changed files with 7,196 additions and 178 deletions.
25 changes: 23 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</parent>

<artifactId>github-api</artifactId>
<version>1.73-SNAPSHOT</version>
<version>1.82-SNAPSHOT</version>
<name>GitHub API for Java</name>
<url>http://github-api.kohsuke.org/</url>
<description>GitHub API for Java</description>
Expand All @@ -34,6 +34,27 @@

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.15</version>
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java15</artifactId>
<version>1.0</version>
</signature>
</configuration>
<executions>
<execution>
<id>ensure-java-1.5-class-library</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.infradna.tool</groupId>
<artifactId>bridge-method-injector</artifactId>
Expand Down Expand Up @@ -114,7 +135,7 @@
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp-urlconnection</artifactId>
<version>2.0.0</version>
<version>2.7.5</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/org/kohsuke/github/AbuseLimitHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.kohsuke.github;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;

/**
* Pluggable strategy to determine what to do when the API abuse limit is hit.
*
* @author Kohsuke Kawaguchi
* @see GitHubBuilder#withAbuseLimitHandler(AbuseLimitHandler)
* @see <a href="https://developer.github.com/v3/#abuse-rate-limits">documentation</a>
* @see RateLimitHandler
*/
public abstract class AbuseLimitHandler {
/**
* Called when the library encounters HTTP error indicating that the API abuse limit is reached.
*
* <p>
* Any exception thrown from this method will cause the request to fail, and the caller of github-api
* will receive an exception. If this method returns normally, another request will be attempted.
* For that to make sense, the implementation needs to wait for some time.
*
* @see <a href="https://developer.github.com/v3/#abuse-rate-limits">API documentation from GitHub</a>
* @param e
* Exception from Java I/O layer. If you decide to fail the processing, you can throw
* this exception (or wrap this exception into another exception and throw it.)
* @param uc
* Connection that resulted in an error. Useful for accessing other response headers.
*/
public abstract void onError(IOException e, HttpURLConnection uc) throws IOException;

/**
* Wait until the API abuse "wait time" is passed.
*/
public static final AbuseLimitHandler WAIT = new AbuseLimitHandler() {
@Override
public void onError(IOException e, HttpURLConnection uc) throws IOException {
try {
Thread.sleep(parseWaitTime(uc));
} catch (InterruptedException _) {
throw (InterruptedIOException)new InterruptedIOException().initCause(e);
}
}

private long parseWaitTime(HttpURLConnection uc) {
String v = uc.getHeaderField("Retry-After");
if (v==null) return 60 * 1000; // can't tell, return 1 min

return Math.max(1000, Long.parseLong(v)*1000);
}
};

/**
* Fail immediately.
*/
public static final AbuseLimitHandler FAIL = new AbuseLimitHandler() {
@Override
public void onError(IOException e, HttpURLConnection uc) throws IOException {
throw (IOException)new IOException("Abust limit reached").initCause(e);
}
};
}
21 changes: 21 additions & 0 deletions src/main/java/org/kohsuke/github/BranchProtection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.ArrayList;
import java.util.List;

/**
* @author Kohsuke Kawaguchi
* @see GHBranch#disableProtection()
*/
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD"}, justification = "JSON API")
class BranchProtection {
boolean enabled;
RequiredStatusChecks requiredStatusChecks;

static class RequiredStatusChecks {
EnforcementLevel enforcement_level;
List<String> contexts = new ArrayList<String>();
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/kohsuke/github/EnforcementLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.kohsuke.github;

import java.util.Locale;

/**
* @author Kohsuke Kawaguchi
*/
public enum EnforcementLevel {
OFF, NON_ADMINS, EVERYONE;

public String toString() {
return name().toLowerCase(Locale.ENGLISH);
}
}
18 changes: 18 additions & 0 deletions src/main/java/org/kohsuke/github/GHAuthorization.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ public class GHAuthorization extends GHObject {
private GitHub root;
private List<String> scopes;
private String token;
private String token_last_eight;
private String hashed_token;
private App app;
private String note;
private String note_url;
private String fingerprint;
//TODO add some user class for https://developer.github.com/v3/oauth_authorizations/#check-an-authorization ?
//private GHUser user;

public GitHub getRoot() {
return root;
Expand All @@ -52,6 +57,14 @@ public String getToken() {
return token;
}

public String getTokenLastEight() {
return token_last_eight;
}

public String getHashedToken() {
return hashed_token;
}

public URL getAppUrl() {
return GitHub.parseURL(app.url);
}
Expand Down Expand Up @@ -82,6 +95,10 @@ public URL getNoteUrl() {
return GitHub.parseURL(note_url);
}

public String getFingerprint() {
return fingerprint;
}

/*package*/ GHAuthorization wrap(GitHub root) {
this.root = root;
return this;
Expand All @@ -92,5 +109,6 @@ public URL getNoteUrl() {
private static class App {
private String url;
private String name;
// private String client_id; not yet used
}
}
47 changes: 46 additions & 1 deletion src/main/java/org/kohsuke/github/GHBranch.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.kohsuke.github.BranchProtection.RequiredStatusChecks;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

import static org.kohsuke.github.Previews.LOKI;

/**
* A branch in a repository.
*
* @author Yusuke Kokubo
*/
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
"NP_UNWRITTEN_FIELD"}, justification = "JSON API")
"NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD"}, justification = "JSON API")
public class GHBranch {
private GitHub root;
private GHRepository owner;
Expand Down Expand Up @@ -44,6 +51,44 @@ public String getName() {
public String getSHA1() {
return commit.sha;
}

/**
* Disables branch protection and allows anyone with push access to push changes.
*/
@Preview @Deprecated
public void disableProtection() throws IOException {
BranchProtection bp = new BranchProtection();
bp.enabled = false;
setProtection(bp);
}

/**
* Enables branch protection to control what commit statuses are required to push.
*
* @see GHCommitStatus#getContext()
*/
@Preview @Deprecated
public void enableProtection(EnforcementLevel level, Collection<String> contexts) throws IOException {
BranchProtection bp = new BranchProtection();
bp.enabled = true;
bp.requiredStatusChecks = new RequiredStatusChecks();
bp.requiredStatusChecks.enforcement_level = level;
bp.requiredStatusChecks.contexts.addAll(contexts);
setProtection(bp);
}

@Preview @Deprecated
public void enableProtection(EnforcementLevel level, String... contexts) throws IOException {
enableProtection(level, Arrays.asList(contexts));
}

private void setProtection(BranchProtection bp) throws IOException {
new Requester(root).method("PATCH").withPreview(LOKI)._with("protection",bp).to(getApiRoute());
}

String getApiRoute() {
return owner.getApiTailUrl("/branches/"+name);
}

@Override
public String toString() {
Expand Down
45 changes: 41 additions & 4 deletions src/main/java/org/kohsuke/github/GHCommit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.IOException;
import java.net.URL;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
Expand Down Expand Up @@ -42,11 +42,19 @@ public GitUser getAuthor() {
return author;
}

public Date getAuthoredDate() {
return GitHub.parseDate(author.date);
}

@WithBridgeMethods(value = GHAuthor.class, castRequired = true)
public GitUser getCommitter() {
return committer;
}

public Date getCommitDate() {
return GitHub.parseDate(committer.date);
}

/**
* Commit message.
*/
Expand All @@ -63,6 +71,7 @@ public int getCommentCount() {
* @deprecated Use {@link GitUser} instead.
*/
public static class GHAuthor extends GitUser {
private String date;
}

public static class Stats {
Expand Down Expand Up @@ -102,7 +111,7 @@ public int getLinesDeleted() {
}

/**
* "modified", "added", or "deleted"
* "modified", "added", or "removed"
*/
public String getStatus() {
return status;
Expand Down Expand Up @@ -171,14 +180,16 @@ static class User {
String login;
}

String url,sha;
String url,html_url,sha;
List<File> files;
Stats stats;
List<Parent> parents;
User author,committer;


public ShortInfo getCommitShortInfo() {
public ShortInfo getCommitShortInfo() throws IOException {
if (commit==null)
populate();
return commit;
}

Expand Down Expand Up @@ -213,6 +224,13 @@ public int getLinesDeleted() throws IOException {
return stats.deletions;
}

/**
* URL of this commit like "https://github.com/kohsuke/sandbox-ant/commit/8ae38db0ea5837313ab5f39d43a6f73de3bd9000"
*/
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
}

/**
* [0-9a-f]{40} SHA1 checksum.
*/
Expand Down Expand Up @@ -263,10 +281,29 @@ public GHUser getAuthor() throws IOException {
return resolveUser(author);
}

/**
* Gets the date the change was authored on.
* @return the date the change was authored on.
* @throws IOException if the information was not already fetched and an attempt at fetching the information failed.
*/
public Date getAuthoredDate() throws IOException {
return getCommitShortInfo().getAuthoredDate();
}

public GHUser getCommitter() throws IOException {
return resolveUser(committer);
}

/**
* Gets the date the change was committed on.
*
* @return the date the change was committed on.
* @throws IOException if the information was not already fetched and an attempt at fetching the information failed.
*/
public Date getCommitDate() throws IOException {
return getCommitShortInfo().getCommitDate();
}

private GHUser resolveUser(User author) throws IOException {
if (author==null || author.login==null) return null;
return owner.root.getUser(author.login);
Expand Down
Loading

0 comments on commit 9d15cd4

Please sign in to comment.