Compare commits

..

83 Commits

Author SHA1 Message Date
Kohsuke Kawaguchi
47409a9a99 [maven-release-plugin] prepare release github-api-1.89 2017-09-09 13:28:31 -07:00
Kohsuke Kawaguchi
60bfea2d3b Bug fix 2017-09-09 13:22:16 -07:00
Kohsuke Kawaguchi
d3ed8eaed5 Merge pull request #339 2017-09-09 13:07:18 -07:00
Kohsuke Kawaguchi
692dccf110 Massaging the change a bit 2017-09-09 13:03:18 -07:00
Kohsuke Kawaguchi
92caf98683 Reverting java1.6 change which I assume is accidental.
Not that I really care about Java5 but I think that change should
be done separatel & intentionally
2017-09-09 12:57:52 -07:00
Kohsuke Kawaguchi
6178d38895 connector usage is unsynchronized 2017-09-09 12:47:18 -07:00
Kohsuke Kawaguchi
40fb38a9ba Window focus problem 2017-09-09 12:45:58 -07:00
Kohsuke Kawaguchi
20e68d53fd Merge pull request #283 2017-09-09 12:45:02 -07:00
Kohsuke Kawaguchi
2d3557e049 Improved the intern logic
if the user record does not exist yet, there's no need to fetch that
eagerly, as they are fetched on demand via the populate() method.
2017-09-09 12:44:12 -07:00
Kohsuke Kawaguchi
d8f4bc7395 Added updater
This solves #331 differently
2017-09-09 12:31:10 -07:00
Kohsuke Kawaguchi
353f9bb809 pointless null check since the with method already does it 2017-09-09 12:23:40 -07:00
Kohsuke Kawaguchi
ccfe3ad4f7 Merge pull request #333 2017-09-09 12:17:39 -07:00
Kohsuke Kawaguchi
9012820c03 Massage the signature a bit.
AFAICT sha and merge_method are not mutually exclusive.
2017-09-09 12:17:21 -07:00
Kohsuke Kawaguchi
fe2af19e42 Unused constant 2017-09-09 12:15:21 -07:00
Kohsuke Kawaguchi
f721e053f1 Added convenience connector for OkHttp3
Note that the existing one needs to be kept for compatibility with OkHttp2
2017-09-09 12:11:36 -07:00
Kohsuke Kawaguchi
e6ad9feb84 Keeping Findbugs happy 2017-09-09 12:05:39 -07:00
Kohsuke Kawaguchi
635350c40e Additional naming consistency change 2017-09-09 12:02:44 -07:00
Kohsuke Kawaguchi
17edd33703 Reorganized imports following #337 2017-09-09 12:00:23 -07:00
Kohsuke Kawaguchi
b0f2a871c6 Merge pull request #337 2017-09-09 11:58:13 -07:00
Kohsuke Kawaguchi
8928a8a1dc Content type should be JSON by default when sending JSON.
This solves #350 a little differently.
2017-09-09 11:51:55 -07:00
Kohsuke Kawaguchi
2b6f37a6cc Merge pull request #361 2017-09-09 11:48:33 -07:00
Kohsuke Kawaguchi
f3a3b87861 Defined entry points 2017-09-09 11:48:25 -07:00
Kohsuke Kawaguchi
9cf6ee78d4 Pointless string conversion 2017-09-09 11:44:09 -07:00
Kohsuke Kawaguchi
bbc2f3962f Proper access control modifier 2017-09-09 11:44:02 -07:00
Kohsuke Kawaguchi
be49eb22d2 Merge pull request #368 2017-09-09 11:40:41 -07:00
Kohsuke Kawaguchi
fb47067215 Naming changes to emphasize that these are just traffic info 2017-09-09 11:40:28 -07:00
Kohsuke Kawaguchi
2c80ef178d Capture commonality between total and daily 2017-09-09 11:39:12 -07:00
Kohsuke Kawaguchi
9af8112148 Tightening up access control and use primitive type 2017-09-09 11:37:18 -07:00
Kohsuke Kawaguchi
57c36f437a [maven-release-plugin] prepare for next development iteration 2017-09-09 08:19:29 -07:00
Kohsuke Kawaguchi
5ed8a34566 [maven-release-plugin] prepare release github-api-1.88 2017-09-09 08:19:20 -07:00
Kohsuke Kawaguchi
ea8df9bd61 javadoc fix 2017-09-09 08:13:51 -07:00
Kohsuke Kawaguchi
b0c51e03b7 [maven-release-plugin] prepare for next development iteration 2017-09-09 08:06:27 -07:00
Kohsuke Kawaguchi
336924ef23 [maven-release-plugin] prepare release github-api-1.87 2017-09-09 08:06:18 -07:00
Kohsuke Kawaguchi
ad28ca4a90 Use string constant like other previews 2017-09-09 07:59:31 -07:00
Kohsuke Kawaguchi
aebbe86cfc Keeping findbugs happy 2017-09-09 07:57:49 -07:00
Kohsuke Kawaguchi
df9faf4943 Auto-retry flaky tests 2017-09-08 16:09:41 -07:00
Kohsuke Kawaguchi
3e295b6be4 Merge branch 'master' of github.com:kohsuke/github-api 2017-09-08 15:52:31 -07:00
Kohsuke Kawaguchi
8dd6dbf995 getRef never returns null 2017-09-08 15:52:11 -07:00
Kohsuke Kawaguchi
612139f2ff Merge pull request #375 from stephenc/tag-object-support
Add basic support for tag objects
2017-09-08 15:49:33 -07:00
Kohsuke Kawaguchi
240bcabb76 Merge pull request #351 2017-09-08 14:16:45 -07:00
Kohsuke Kawaguchi
cda27d5963 This field should be still final 2017-09-08 14:16:09 -07:00
Kohsuke Kawaguchi
2f8c3997f7 Restored signature of the previous enableProtection() method 2017-09-08 14:13:21 -07:00
Kohsuke Kawaguchi
46b89a48db Merge Pull request #369 2017-09-08 14:06:22 -07:00
Kohsuke Kawaguchi
5c9cbee2f9 Merge pull request #332 from sebkur/fix_javadoc
Fix a bug in the Javadocs (due to copy and paste)
2017-09-08 10:39:58 -07:00
Kohsuke Kawaguchi
ee8973c239 Merge pull request #338 from sebkur/ignore-eclipse-files
Ignore eclipse files
2017-09-08 10:39:39 -07:00
Kohsuke Kawaguchi
2e2813f363 Merge pull request #343 from kamontat/feature/latest-release
add latest release
2017-09-08 10:39:13 -07:00
Kohsuke Kawaguchi
7ceca0769f Merge pull request #363 from PauloMigAlmeida/master
Add missing event types used by repository webhooks
2017-09-08 10:36:33 -07:00
Kohsuke Kawaguchi
4d277cc61f Merge pull request #352 from stephenc/pr-reviews
Add support for PR reviews preview
2017-09-08 10:35:58 -07:00
Kohsuke Kawaguchi
e67fbb4621 Merge pull request #362 from KostyaSha/pingHook
Add ping hook method
2017-09-08 10:34:45 -07:00
Kohsuke Kawaguchi
29b5357ceb Merge pull request #358 from jglick/no-preview-JENKINS-36240
[JENKINS-36240] /repos/:owner/:repo/collaborators/:username/permission no longer requires korra preview
2017-09-08 10:33:31 -07:00
Stephen Connolly
9dabec107b Add basic support for tag objects 2017-09-01 12:52:15 +01:00
Matt Mitchell
f2a2ad90b7 Switch to a concurrent hash map 2017-08-29 11:30:25 -07:00
Matt Mitchell
cfe4c0c510 Merge remote-tracking branch 'upstream/master' into synchro-api 2017-08-29 10:38:54 -07:00
Jae Gangemi
23cd51a6da - improved branch protection support 2017-08-08 16:17:58 -06:00
Jae Gangemi
7396395f90 - updated ignore entries for eclipse/moc os 2017-08-08 14:31:11 -06:00
adw1n
a1819bf232 Added getClones method to GHRepository (https://developer.github.com/v3/repos/traffic/#clones). 2017-08-01 06:38:28 +02:00
adw1n
8accf07d46 Changed timestamp (GHRepositoryViews.DayViews.timestamp) field type from String to Date. 2017-08-01 04:02:10 +02:00
adw1n
6dcbace572 Added getViews method to GHRepository.
getViews implements https://developer.github.com/v3/repos/traffic/#views
2017-08-01 03:38:46 +02:00
Greg Gianforcaro
e90c86ec2f Remove Preview status, merge_method is now out of preview 2017-07-18 11:36:38 -04:00
Greg Gianforcaro
971ae1fa4d Merge branch 'master' into pr-merge-method 2017-07-18 11:33:20 -04:00
Paulo Miguel Almeida
4abe87036c Add missing event types used by repository webhooks 2017-07-14 04:23:08 +00:00
Kanstantsin Shautsou
8d1b44db97 Add ping hook method 2017-07-10 02:39:03 +03:00
Serban Iordache
b537f9925b issue #360: Add support for committing multiple files 2017-07-07 16:21:11 +02:00
Kohsuke Kawaguchi
f2fe8eaf86 [maven-release-plugin] prepare for next development iteration 2017-07-02 17:08:35 -07:00
Kohsuke Kawaguchi
46715cac08 [maven-release-plugin] prepare release github-api-1.86 2017-07-02 17:08:24 -07:00
Kohsuke Kawaguchi
0f21eba57f Merge pull request #359 from jglick/SocketTimeoutException-JENKINS-45142
[JENKINS-45142] Retry connections after getting SocketTimeoutException
2017-06-29 17:13:31 -04:00
Jesse Glick
cb7620395a [JENKINS-45142] Retry connections after getting SocketTimeoutException. 2017-06-28 17:09:54 -04:00
Jesse Glick
3a40af8871 [JENKINS-36240] /repos/:owner/:repo/collaborators/:username/permission no longer requires korra preview. 2017-06-12 10:28:25 -04:00
mdeverdelhan
c9b5074bc4 Fix the wrapping of retrieved commits (owner/repository) 2017-05-11 12:32:34 +02:00
Stephen Connolly
44d4d0d767 Add support for PR reviews preview 2017-03-30 12:17:06 +01:00
mdeverdelhan
64af13f40d Add the Commit search API (still in preview) 2017-03-30 12:55:55 +02:00
Kohsuke Kawaguchi
e9b59c6bef [maven-release-plugin] prepare for next development iteration 2017-02-28 21:06:14 -08:00
kamontat
5554332b5b add return null if latest release not found 2017-02-20 10:20:53 +07:00
kamontat
1cffea892b add test 2017-02-20 10:20:08 +07:00
kamontat
fd859815b0 fixed indent, rename to getLastestRelease 2017-02-20 09:36:28 +07:00
kamontat
166e26d101 add latest release 2017-02-19 23:57:44 +07:00
Kanstantsin Shautsou
be081eec3f Inject responce headers in GHObject and Exceptions.
GH has specific to GET/POST headers required for analysing in case of error.

Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com>
2017-02-10 04:11:20 +03:00
Kanstantsin Shautsou
55b00a87f6 Set 1.6 level. I'm not so old.
Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com>
2017-02-10 03:28:06 +03:00
Sebastian Kürten
429b26cee8 Ignore eclipse files 2017-02-09 18:18:08 +01:00
Sebastian Kürten
fafe6b0ff7 Remove unused imports
Especially also remove the unsued import of
javax.xml.bind.DatatypeConverter from GHContent which is non-public API
as of Java 8
2017-02-09 18:15:20 +01:00
Greg Gianforcaro
5b156006fb Add 'Preview' support for MergeMethod on GHPullRequest
- Add 'polaris' preview
- Add MergeMethod Enum
- Add merge method to GHPullRequest which takes a MergeMethod
2017-01-27 23:36:54 -05:00
Sebastian Kürten
75f0c08ca4 Fix a bug in the Javadocs (due to copy and paste) 2017-01-23 12:21:13 +01:00
Matt Mitchell
9f3f644b83 Add some level of synchronization to the root of the API
This adds some synchronization to the maps at the root of the API to avoid duplicated calls to the actual GH REST API.  Specifically this is targeted around the two maps, orgs and users.  This fix makes the GHPRB jenkins plugin behave much better when there are lots of projects that could build for a specific repo (even if only a few are actually triggered)

There are also a few fixes around GHUser and GHPullRequest
* GHPullRequest was checking a field that may be null (merged_by) when determining whether to fetch details.  An unmerged PR would make a bunch of Github API calls for each property accessed.
* Where GHUser was returned in various objects, we weren't going through the caching mechanism at the root, so calls to APIs on GHUSer often resulted in new REST calls.  Instead, return from the cache wherever possible.
2016-06-08 10:43:40 -07:00
74 changed files with 2155 additions and 184 deletions

4
.gitignore vendored
View File

@@ -3,3 +3,7 @@ target
*.iml
*.ipr
*.iws
.classpath
.project
.settings/
.DS_Store

22
pom.xml
View File

@@ -7,7 +7,7 @@
</parent>
<artifactId>github-api</artifactId>
<version>1.85</version>
<version>1.89</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.85</tag>
<tag>github-api-1.89</tag>
</scm>
<distributionManagement>
@@ -34,6 +34,12 @@
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<rerunFailingTestsCount>2</rerunFailingTestsCount>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
@@ -105,6 +111,12 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
@@ -138,6 +150,12 @@
<version>2.7.5</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp-urlconnection</artifactId>
<version>3.4.0</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>wordnet-random-name</artifactId>

View File

@@ -1,21 +0,0 @@
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>();
}
}

View File

@@ -3,8 +3,11 @@ package org.kohsuke.github;
import java.util.Locale;
/**
* This was added during preview API period but it has changed since then.
*
* @author Kohsuke Kawaguchi
*/
@Deprecated
public enum EnforcementLevel {
OFF, NON_ADMINS, EVERYONE;

View File

@@ -1,9 +1,9 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.URL;
import java.util.Collection;
import java.util.Date;
import java.util.List;
/**

View File

@@ -0,0 +1,49 @@
package org.kohsuke.github;
import org.apache.commons.codec.binary.Base64;
import java.io.IOException;
/**
* Builder pattern for creating a new blob.
* Based on https://developer.github.com/v3/git/blobs/#create-a-blob
*/
public class GHBlobBuilder {
private final GHRepository repo;
private final Requester req;
GHBlobBuilder(GHRepository repo) {
this.repo = repo;
req = new Requester(repo.root);
}
/**
* Configures a blob with the specified text {@code content}.
*/
public GHBlobBuilder textContent(String content) {
req.with("content", content);
req.with("encoding", "utf-8");
return this;
}
/**
* Configures a blob with the specified binary {@code content}.
*/
public GHBlobBuilder binaryContent(byte[] content) {
String base64Content = Base64.encodeBase64String(content);
req.with("content", base64Content);
req.with("encoding", "base64");
return this;
}
private String getApiTail() {
return String.format("/repos/%s/%s/git/blobs", repo.getOwnerName(), repo.getName());
}
/**
* Creates a blob based on the parameters specified thus far.
*/
public GHBlob create() throws IOException {
return req.method("POST").to(getApiTail(), GHBlob.class);
}
}

View File

@@ -1,24 +1,20 @@
package org.kohsuke.github;
import static org.kohsuke.github.Previews.LOKI;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import org.kohsuke.github.BranchProtection.RequiredStatusChecks;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import static org.kohsuke.github.Previews.*;
/**
* A branch in a repository.
*
*
* @author Yusuke Kokubo
*/
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
"NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD"}, justification = "JSON API")
public class GHBranch {
private GitHub root;
@@ -33,7 +29,7 @@ public class GHBranch {
public static class Commit {
String sha;
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
String url;
}
@@ -69,6 +65,10 @@ public class GHBranch {
return GitHub.parseURL(protection_url);
}
@Preview @Deprecated
public GHBranchProtection getProtection() throws IOException {
return root.retrieve().withPreview(LOKI).to(protection_url, GHBranchProtection.class);
}
/**
* The commit that this branch currently points to.
@@ -82,9 +82,7 @@ public class GHBranch {
*/
@Preview @Deprecated
public void disableProtection() throws IOException {
BranchProtection bp = new BranchProtection();
bp.enabled = false;
setProtection(bp);
new Requester(root).method("DELETE").withPreview(LOKI).to(protection_url);
}
/**
@@ -93,28 +91,31 @@ public class GHBranch {
* @see GHCommitStatus#getContext()
*/
@Preview @Deprecated
public GHBranchProtectionBuilder enableProtection() {
return new GHBranchProtectionBuilder(this);
}
// backward compatibility with previous signature
@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());
switch (level) {
case OFF:
disableProtection();
break;
case NON_ADMINS:
case EVERYONE:
enableProtection()
.addRequiredChecks(contexts)
.includeAdmins(level==EnforcementLevel.EVERYONE)
.enable();
break;
}
}
String getApiRoute() {
return owner.getApiTailUrl("/branches/"+name);
}
@Override
public String toString() {
final String url = owner != null ? owner.getUrl().toString() : "unknown";

View File

@@ -0,0 +1,151 @@
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Collection;
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
"URF_UNREAD_FIELD" }, justification = "JSON API")
public class GHBranchProtection {
@JsonProperty("enforce_admins")
private EnforceAdmins enforceAdmins;
@JsonProperty("required_pull_request_reviews")
private RequiredReviews requiredReviews;
@JsonProperty("required_status_checks")
private RequiredStatusChecks requiredStatusChecks;
@JsonProperty
private Restrictions restrictions;
@JsonProperty
private String url;
public EnforceAdmins getEnforceAdmins() {
return enforceAdmins;
}
public RequiredReviews getRequiredReviews() {
return requiredReviews;
}
public RequiredStatusChecks getRequiredStatusChecks() {
return requiredStatusChecks;
}
public Restrictions getRestrictions() {
return restrictions;
}
public String getUrl() {
return url;
}
public static class EnforceAdmins {
@JsonProperty
private boolean enabled;
@JsonProperty
private String url;
public String getUrl() {
return url;
}
public boolean isEnabled() {
return enabled;
}
}
public static class RequiredReviews {
@JsonProperty("dismissal_restrictions")
private Restrictions dismissalRestriction;
@JsonProperty("dismiss_stale_reviews")
private boolean dismissStaleReviews;
@JsonProperty("require_code_owner_reviews")
private boolean requireCodeOwnerReviews;
@JsonProperty
private String url;
public Restrictions getDismissalRestrictions() {
return dismissalRestriction;
}
public String getUrl() {
return url;
}
public boolean isDismissStaleReviews() {
return dismissStaleReviews;
}
public boolean isRequireCodeOwnerReviews() {
return requireCodeOwnerReviews;
}
}
public static class RequiredStatusChecks {
@JsonProperty
private Collection<String> contexts;
@JsonProperty
private boolean strict;
@JsonProperty
private String url;
public Collection<String> getContexts() {
return contexts;
}
public String getUrl() {
return url;
}
public boolean isRequiresBranchUpToDate() {
return strict;
}
}
public static class Restrictions {
@JsonProperty
private Collection<GHTeam> teams;
@JsonProperty("teams_url")
private String teamsUrl;
@JsonProperty
private String url;
@JsonProperty
private Collection<GHUser> users;
@JsonProperty("users_url")
private String usersUrl;
public Collection<GHTeam> getTeams() {
return teams;
}
public String getTeamsUrl() {
return teamsUrl;
}
public String getUrl() {
return url;
}
public Collection<GHUser> getUsers() {
return users;
}
public String getUsersUrl() {
return usersUrl;
}
}
}

View File

@@ -0,0 +1,203 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.kohsuke.github.Previews.*;
/**
* Builder to configure the branch protection settings.
*
* @see GHBranch#enableProtection()
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
"URF_UNREAD_FIELD" }, justification = "JSON API")
public class GHBranchProtectionBuilder {
private final GHBranch branch;
private boolean enforceAdmins;
private Map<String, Object> prReviews;
private Restrictions restrictions;
private StatusChecks statusChecks;
GHBranchProtectionBuilder(GHBranch branch) {
this.branch = branch;
}
public GHBranchProtectionBuilder addRequiredChecks(Collection<String> checks) {
getStatusChecks().contexts.addAll(checks);
return this;
}
public GHBranchProtectionBuilder addRequiredChecks(String... checks) {
addRequiredChecks(Arrays.asList(checks));
return this;
}
public GHBranchProtectionBuilder dismissStaleReviews() {
getPrReviews().put("dismiss_stale_reviews", true);
return this;
}
public GHBranchProtection enable() throws IOException {
return requester().method("PUT")
.withNullable("required_status_checks", statusChecks)
.withNullable("required_pull_request_reviews", prReviews)
.withNullable("restrictions", restrictions)
.withNullable("enforce_admins", enforceAdmins)
.to(branch.getProtectionUrl().toString(), GHBranchProtection.class);
}
public GHBranchProtectionBuilder includeAdmins() {
return includeAdmins(true);
}
public GHBranchProtectionBuilder includeAdmins(boolean v) {
enforceAdmins = v;
return this;
}
public GHBranchProtectionBuilder requireBranchIsUpToDate() {
return requireBranchIsUpToDate(true);
}
public GHBranchProtectionBuilder requireBranchIsUpToDate(boolean v) {
getStatusChecks().strict = v;
return this;
}
public GHBranchProtectionBuilder requireCodeOwnReviews() {
return requireCodeOwnReviews(true);
}
public GHBranchProtectionBuilder requireCodeOwnReviews(boolean v) {
getPrReviews().put("require_code_owner_reviews", v);
return this;
}
public GHBranchProtectionBuilder requireReviews() {
getPrReviews();
return this;
}
public GHBranchProtectionBuilder restrictPushAccess() {
getRestrictions();
return this;
}
public GHBranchProtectionBuilder teamPushAccess(Collection<GHTeam> teams) {
for (GHTeam team : teams) {
teamPushAccess(team);
}
return this;
}
public GHBranchProtectionBuilder teamPushAccess(GHTeam... teams) {
for (GHTeam team : teams) {
getRestrictions().teams.add(team.getSlug());
}
return this;
}
public GHBranchProtectionBuilder teamReviewDismissals(Collection<GHTeam> teams) {
for (GHTeam team : teams) {
teamReviewDismissals(team);
}
return this;
}
public GHBranchProtectionBuilder teamReviewDismissals(GHTeam... teams) {
for (GHTeam team : teams) {
addReviewRestriction(team.getSlug(), true);
}
return this;
}
public GHBranchProtectionBuilder userPushAccess(Collection<GHUser> users) {
for (GHUser user : users) {
userPushAccess(user);
}
return this;
}
public GHBranchProtectionBuilder userPushAccess(GHUser... users) {
for (GHUser user : users) {
getRestrictions().users.add(user.getLogin());
}
return this;
}
public GHBranchProtectionBuilder userReviewDismissals(Collection<GHUser> users) {
for (GHUser team : users) {
userReviewDismissals(team);
}
return this;
}
public GHBranchProtectionBuilder userReviewDismissals(GHUser... users) {
for (GHUser user : users) {
addReviewRestriction(user.getLogin(), false);
}
return this;
}
private void addReviewRestriction(String restriction, boolean isTeam) {
getPrReviews();
if (!prReviews.containsKey("dismissal_restrictions")) {
prReviews.put("dismissal_restrictions", new Restrictions());
}
Restrictions restrictions = (Restrictions) prReviews.get("dismissal_restrictions");
if (isTeam) {
restrictions.teams.add(restriction);
} else {
restrictions.users.add(restriction);
}
}
private Map<String, Object> getPrReviews() {
if (prReviews == null) {
prReviews = new HashMap<String, Object>();
}
return prReviews;
}
private Restrictions getRestrictions() {
if (restrictions == null) {
restrictions = new Restrictions();
}
return restrictions;
}
private StatusChecks getStatusChecks() {
if (statusChecks == null) {
statusChecks = new StatusChecks();
}
return statusChecks;
}
private Requester requester() {
return new Requester(branch.getRoot()).withPreview(LOKI);
}
private static class Restrictions {
private Set<String> teams = new HashSet<String>();
private Set<String> users = new HashSet<String>();
}
private static class StatusChecks {
final List<String> contexts = new ArrayList<String>();
boolean strict;
}
}

View File

@@ -2,6 +2,7 @@ package org.kohsuke.github;
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;

View File

@@ -0,0 +1,92 @@
package org.kohsuke.github;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
/**
* Builder pattern for creating a new commit.
* Based on https://developer.github.com/v3/git/commits/#create-a-commit
*/
public class GHCommitBuilder {
private final GHRepository repo;
private final Requester req;
private final List<String> parents = new ArrayList<String>();
private static final class UserInfo {
private final String name;
private final String email;
private final String date;
private UserInfo(String name, String email, Date date) {
this.name = name;
this.email = email;
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(tz);
this.date = df.format((date != null) ? date : new Date());
}
}
GHCommitBuilder(GHRepository repo) {
this.repo = repo;
req = new Requester(repo.root);
}
/**
* @param message the commit message
*/
public GHCommitBuilder message(String message) {
req.with("message", message);
return this;
}
/**
* @param tree the SHA of the tree object this commit points to
*/
public GHCommitBuilder tree(String tree) {
req.with("tree", tree);
return this;
}
/**
* @param parent the SHA of a parent commit.
*/
public GHCommitBuilder parent(String parent) {
parents.add(parent);
return this;
}
/**
* Configures the author of this commit.
*/
public GHCommitBuilder author(String name, String email, Date date) {
req._with("author", new UserInfo(name, email, date));
return this;
}
/**
* Configures the committer of this commit.
*/
public GHCommitBuilder committer(String name, String email, Date date) {
req._with("committer", new UserInfo(name, email, date));
return this;
}
private String getApiTail() {
return String.format("/repos/%s/%s/git/commits", repo.getOwnerName(), repo.getName());
}
/**
* Creates a blob based on the parameters specified thus far.
*/
public GHCommit create() throws IOException {
req._with("parents", parents);
return req.method("POST").to(getApiTail(), GHCommit.class).wrapUp(repo);
}
}

View File

@@ -1,11 +1,11 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import static org.kohsuke.github.Previews.SQUIRREL_GIRL;
import static org.kohsuke.github.Previews.*;
/**
* A comment attached to a commit (or a specific line in a specific file of a commit.)

View File

@@ -39,7 +39,8 @@ public class GHCommitPointer {
* This points to the user who owns
* the {@link #getRepository()}.
*/
public GHUser getUser() {
public GHUser getUser() throws IOException {
if (user != null) return user.root.intern(user);
return user;
}

View File

@@ -0,0 +1,137 @@
package org.kohsuke.github;
import org.apache.commons.lang.StringUtils;
import java.io.IOException;
/**
* Search commits.
*
* @author Marc de Verdelhan
* @see GitHub#searchCommits()
*/
@Preview @Deprecated
public class GHCommitSearchBuilder extends GHSearchBuilder<GHCommit> {
/*package*/ GHCommitSearchBuilder(GitHub root) {
super(root,CommitSearchResult.class);
req.withPreview(Previews.CLOAK);
}
/**
* Search terms.
*/
public GHCommitSearchBuilder q(String term) {
super.q(term);
return this;
}
public GHCommitSearchBuilder author(String v) {
return q("author:"+v);
}
public GHCommitSearchBuilder committer(String v) {
return q("committer:"+v);
}
public GHCommitSearchBuilder authorName(String v) {
return q("author-name:"+v);
}
public GHCommitSearchBuilder committerName(String v) {
return q("committer-name:"+v);
}
public GHCommitSearchBuilder authorEmail(String v) {
return q("author-email:"+v);
}
public GHCommitSearchBuilder committerEmail(String v) {
return q("committer-email:"+v);
}
public GHCommitSearchBuilder authorDate(String v) {
return q("author-date:"+v);
}
public GHCommitSearchBuilder committerDate(String v) {
return q("committer-date:"+v);
}
public GHCommitSearchBuilder merge(boolean merge) {
return q("merge:"+Boolean.valueOf(merge).toString().toLowerCase());
}
public GHCommitSearchBuilder hash(String v) {
return q("hash:"+v);
}
public GHCommitSearchBuilder parent(String v) {
return q("parent:"+v);
}
public GHCommitSearchBuilder tree(String v) {
return q("tree:"+v);
}
public GHCommitSearchBuilder is(String v) {
return q("is:"+v);
}
public GHCommitSearchBuilder user(String v) {
return q("user:"+v);
}
public GHCommitSearchBuilder org(String v) {
return q("org:"+v);
}
public GHCommitSearchBuilder repo(String v) {
return q("repo:"+v);
}
public GHCommitSearchBuilder order(GHDirection v) {
req.with("order",v);
return this;
}
public GHCommitSearchBuilder sort(Sort sort) {
req.with("sort",sort);
return this;
}
public enum Sort { AUTHOR_DATE, COMMITTER_DATE }
private static class CommitSearchResult extends SearchResult<GHCommit> {
private GHCommit[] items;
@Override
/*package*/ GHCommit[] getItems(GitHub root) {
for (GHCommit commit : items) {
String repoName = getRepoName(commit.url);
try {
GHRepository repo = root.getRepository(repoName);
commit.wrapUp(repo);
} catch (IOException ioe) {}
}
return items;
}
}
/**
* @param commitUrl a commit URL
* @return the repo name ("username/reponame")
*/
private static String getRepoName(String commitUrl) {
if (StringUtils.isBlank(commitUrl)) {
return null;
}
int indexOfUsername = (GitHub.GITHUB_URL + "/repos/").length();
String[] tokens = commitUrl.substring(indexOfUsername).split("/", 3);
return tokens[0] + '/' + tokens[1];
}
@Override
protected String getApiUrl() {
return "/search/commits";
}
}

View File

@@ -1,5 +1,6 @@
package org.kohsuke.github;
import java.io.IOException;
import java.net.URL;
/**
@@ -45,8 +46,8 @@ public class GHCommitStatus extends GHObject {
return description;
}
public GHUser getCreator() {
return creator;
public GHUser getCreator() throws IOException {
return root.intern(creator);
}
public String getContext() {

View File

@@ -7,8 +7,6 @@ import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.bind.DatatypeConverter;
/**
* A Content of a repository.
*

View File

@@ -1,9 +1,9 @@
package org.kohsuke.github;
import java.io.IOException;
import org.apache.commons.lang.builder.ToStringBuilder;
import java.io.IOException;
public class GHDeployKey {
protected String url, key, title;

View File

@@ -1,6 +1,6 @@
package org.kohsuke.github;
import java.io.IOException;
import java.net.URL;
public class GHDeployment extends GHObject {
@@ -41,8 +41,8 @@ public class GHDeployment extends GHObject {
public String getEnvironment() {
return environment;
}
public GHUser getCreator() {
return creator;
public GHUser getCreator() throws IOException {
return root.intern(creator);
}
public String getRef() {
return ref;

View File

@@ -1,7 +1,6 @@
package org.kohsuke.github;
import java.io.IOException;
import java.util.Locale;
public class GHDeploymentStatusBuilder {
private final Requester builder;

View File

@@ -21,17 +21,30 @@ public enum GHEvent {
FORK_APPLY,
GIST,
GOLLUM,
INSTALLATION,
INSTALLATION_REPOSITORIES,
ISSUE_COMMENT,
ISSUES,
LABEL,
MARKETPLACE_PURCHASE,
MEMBER,
MEMBERSHIP,
MILESTONE,
ORGANIZATION,
ORG_BLOCK,
PAGE_BUILD,
PROJECT_CARD,
PROJECT_COLUMN,
PROJECT,
PUBLIC,
PULL_REQUEST,
PULL_REQUEST_REVIEW,
PULL_REQUEST_REVIEW_COMMENT,
PUSH,
RELEASE,
REPOSITORY, // only valid for org hooks
STATUS,
TEAM,
TEAM_ADD,
WATCH,
PING,

View File

@@ -1,11 +1,11 @@
package org.kohsuke.github;
import java.io.IOException;
import java.util.Date;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.util.Date;
/**
* Represents an event.
*

View File

@@ -3,6 +3,7 @@ package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.Reader;
import java.util.List;
@@ -77,7 +78,7 @@ public abstract class GHEventPayload {
throw new IllegalStateException("Expected pull_request payload, but got something else. Maybe we've got another type of event?");
if (repository!=null) {
repository.wrap(root);
pull_request.wrap(repository);
pull_request.wrapUp(repository);
} else {
pull_request.wrapUp(root);
}

View File

@@ -0,0 +1,34 @@
package org.kohsuke.github;
import javax.annotation.CheckForNull;
import java.io.FileNotFoundException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
/**
* Request/responce contains useful metadata.
* Custom exception allows store info for next diagnostics.
*
* @author Kanstantsin Shautsou
*/
public class GHFileNotFoundException extends FileNotFoundException {
protected Map<String, List<String>> responseHeaderFields;
public GHFileNotFoundException() {
}
public GHFileNotFoundException(String s) {
super(s);
}
@CheckForNull
public Map<String, List<String>> getResponseHeaderFields() {
return responseHeaderFields;
}
GHFileNotFoundException withResponseHeaderFields(HttpURLConnection urlConnection) {
this.responseHeaderFields = urlConnection.getHeaderFields();
return this;
}
}

View File

@@ -38,8 +38,8 @@ public class GHGist extends GHObject {
/**
* User that owns this Gist.
*/
public GHUser getOwner() {
return owner;
public GHUser getOwner() throws IOException {
return root.intern(owner);
}
public String getForksUrl() {

View File

@@ -1,6 +1,7 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
@@ -41,6 +42,13 @@ public abstract class GHHook extends GHObject {
return Collections.unmodifiableMap(config);
}
/**
* @see <a href="https://developer.github.com/v3/repos/hooks/#ping-a-hook">Ping hook</a>
*/
public void ping() throws IOException {
new Requester(getRoot()).method("POST").to(getApiRoute() + "/pings");
}
/**
* Deletes this hook.
*/

View File

@@ -5,7 +5,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**

View File

@@ -0,0 +1,34 @@
package org.kohsuke.github;
import javax.annotation.CheckForNull;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
/**
* Request/responce contains useful metadata.
* Custom exception allows store info for next diagnostics.
*
* @author Kanstantsin Shautsou
*/
public class GHIOException extends IOException {
protected Map<String, List<String>> responseHeaderFields;
public GHIOException() {
}
public GHIOException(String message) {
super(message);
}
@CheckForNull
public Map<String, List<String>> getResponseHeaderFields() {
return responseHeaderFields;
}
GHIOException withResponseHeaderFields(HttpURLConnection urlConnection) {
this.responseHeaderFields = urlConnection.getHeaderFields();
return this;
}
}

View File

@@ -288,8 +288,8 @@ public class GHIssue extends GHObject implements Reactable{
return "/repos/"+owner.getOwnerName()+"/"+owner.getName()+"/issues/"+number;
}
public GHUser getAssignee() {
return assignee;
public GHUser getAssignee() throws IOException {
return root.intern(assignee);
}
public List<GHUser> getAssignees() {
@@ -299,8 +299,8 @@ public class GHIssue extends GHObject implements Reactable{
/**
* User who submitted the issue.
*/
public GHUser getUser() {
return user;
public GHUser getUser() throws IOException {
return root.intern(user);
}
/**
@@ -311,12 +311,16 @@ public class GHIssue extends GHObject implements Reactable{
* even for an issue that's already closed. See
* https://github.com/kohsuke/github-api/issues/60.
*/
public GHUser getClosedBy() {
public GHUser getClosedBy() throws IOException {
if(!"closed".equals(state)) return null;
if(closed_by != null) return closed_by;
//TODO closed_by = owner.getIssue(number).getClosed_by();
return closed_by;
//TODO
/*
if (closed_by==null) {
closed_by = owner.getIssue(number).getClosed_by();
}
*/
return root.intern(closed_by);
}
public int getCommentsCount(){

View File

@@ -26,7 +26,7 @@ package org.kohsuke.github;
import java.io.IOException;
import java.net.URL;
import static org.kohsuke.github.Previews.SQUIRREL_GIRL;
import static org.kohsuke.github.Previews.*;
/**
* Comment to the issue

View File

@@ -1,7 +1,5 @@
package org.kohsuke.github;
import java.util.Locale;
/**
* Search issues.
*

View File

@@ -32,7 +32,7 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static org.kohsuke.github.Previews.DRAX;
import static org.kohsuke.github.Previews.*;
/**
* The GitHub Preview API's license information

View File

@@ -27,8 +27,8 @@ public class GHMilestone extends GHObject {
return owner;
}
public GHUser getCreator() {
return creator;
public GHUser getCreator() throws IOException {
return root.intern(creator);
}
public Date getDueOn() {

View File

@@ -6,7 +6,6 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

View File

@@ -3,14 +3,15 @@ package org.kohsuke.github;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang.reflect.FieldUtils;
import javax.annotation.CheckForNull;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Most (all?) domain objects in GitHub seems to have these 4 properties.
@@ -18,6 +19,11 @@ import java.util.Date;
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
"NP_UNWRITTEN_FIELD"}, justification = "JSON API")
public abstract class GHObject {
/**
* Capture response HTTP headers on the state object.
*/
protected Map<String, List<String>> responseHeaderFields;
protected String url;
protected int id;
protected String created_at;
@@ -26,6 +32,21 @@ public abstract class GHObject {
/*package*/ GHObject() {
}
/**
* Returns the HTTP response headers given along with the state of this object.
*
* <p>
* Some of the HTTP headers have nothing to do with the object, for example "Cache-Control"
* and others are different depending on how this object was retrieved.
*
* This method was added as a kind of hack to allow the caller to retrieve OAuth scopes and such.
* Use with caution. The method might be removed in the future.
*/
@CheckForNull @Deprecated
public Map<String, List<String>> getResponseHeaderFields() {
return responseHeaderFields;
}
/**
* When was this resource created?
*/

View File

@@ -23,10 +23,16 @@
*/
package org.kohsuke.github;
import javax.annotation.CheckForNull;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import static org.kohsuke.github.Previews.*;
/**
* A pull request.
@@ -200,7 +206,7 @@ public class GHPullRequest extends GHIssue {
* Depending on the original API call where this object is created, it may not contain everything.
*/
private void populate() throws IOException {
if (merged_by!=null) return; // already populated
if (mergeable_state!=null) return; // already populated
if (root.isOffline()) {
return; // cannot populate, will have to live with what we have
}
@@ -208,7 +214,7 @@ public class GHPullRequest extends GHIssue {
}
/**
* Retrieves all the commits associated to this pull request.
* Retrieves all the files associated to this pull request.
*/
public PagedIterable<GHPullRequestFileDetail> listFiles() {
return new PagedIterable<GHPullRequestFileDetail>() {
@@ -223,6 +229,27 @@ public class GHPullRequest extends GHIssue {
};
}
/**
* Retrieves all the reviews associated to this pull request.
*/
public PagedIterable<GHPullRequestReview> listReviews() {
return new PagedIterable<GHPullRequestReview>() {
public PagedIterator<GHPullRequestReview> _iterator(int pageSize) {
return new PagedIterator<GHPullRequestReview>(root.retrieve()
.withPreview(BLACK_CAT)
.asIterator(String.format("%s/reviews", getApiRoute()),
GHPullRequestReview[].class, pageSize)) {
@Override
protected void wrapUp(GHPullRequestReview[] page) {
for (GHPullRequestReview r: page) {
r.wrapUp(GHPullRequest.this);
}
}
};
}
};
}
/**
* Obtains all the review comments associated with this pull request.
*/
@@ -259,6 +286,34 @@ public class GHPullRequest extends GHIssue {
};
}
@Preview
@Deprecated
public GHPullRequestReview createReview(String body, @CheckForNull GHPullRequestReviewState event,
GHPullRequestReviewComment... comments)
throws IOException {
return createReview(body, event, Arrays.asList(comments));
}
@Preview
@Deprecated
public GHPullRequestReview createReview(String body, @CheckForNull GHPullRequestReviewState event,
List<GHPullRequestReviewComment> comments)
throws IOException {
// if (event == null) {
// event = GHPullRequestReviewState.PENDING;
// }
List<DraftReviewComment> draftComments = new ArrayList<DraftReviewComment>(comments.size());
for (GHPullRequestReviewComment c : comments) {
draftComments.add(new DraftReviewComment(c.getBody(), c.getPath(), c.getPosition()));
}
return new Requester(root).method("POST")
.with("body", body)
//.with("event", event.name())
._with("comments", draftComments)
.withPreview(BLACK_CAT)
.to(getApiRoute() + "/reviews", GHPullRequestReview.class).wrapUp(this);
}
public GHPullRequestReviewComment createReviewComment(String body, String sha, String path, int position) throws IOException {
return new Requester(root).method("POST")
.with("body", body)
@@ -291,13 +346,57 @@ public class GHPullRequest extends GHIssue {
* SHA that pull request head must match to allow merge.
*/
public void merge(String msg, String sha) throws IOException {
new Requester(root).method("PUT").with("commit_message",msg).with("sha",sha).to(getApiRoute()+"/merge");
merge(msg, sha, null);
}
/**
* Merge this pull request, using the specified merge method.
*
* The equivalent of the big green "Merge pull request" button.
*
* @param msg
* Commit message. If null, the default one will be used.
* @param method
* SHA that pull request head must match to allow merge.
*/
public void merge(String msg, String sha, MergeMethod method) throws IOException {
new Requester(root).method("PUT")
.with("commit_message",msg)
.with("sha",sha)
.with("merge_method",method)
.to(getApiRoute()+"/merge");
}
public enum MergeMethod{ MERGE, SQUASH, REBASE }
private void fetchIssue() throws IOException {
if (!fetchedIssueDetails) {
new Requester(root).to(getIssuesApiRoute(), this);
fetchedIssueDetails = true;
}
}
private static class DraftReviewComment {
private String body;
private String path;
private int position;
public DraftReviewComment(String body, String path, int position) {
this.body = body;
this.path = path;
this.position = position;
}
public String getBody() {
return body;
}
public String getPath() {
return path;
}
public int getPosition() {
return position;
}
}
}

View File

@@ -0,0 +1,149 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.kohsuke.github;
import java.io.IOException;
import java.net.URL;
import static org.kohsuke.github.Previews.*;
/**
* Review to the pull request
*
* @see GHPullRequest#listReviews()
* @see GHPullRequest#createReview(String, GHPullRequestReviewState, GHPullRequestReviewComment...)
*/
public class GHPullRequestReview extends GHObject {
GHPullRequest owner;
private String body;
private GHUser user;
private String commit_id;
private GHPullRequestReviewState state;
/*package*/ GHPullRequestReview wrapUp(GHPullRequest owner) {
this.owner = owner;
return this;
}
/**
* Gets the pull request to which this review is associated.
*/
public GHPullRequest getParent() {
return owner;
}
/**
* The comment itself.
*/
public String getBody() {
return body;
}
/**
* Gets the user who posted this review.
*/
public GHUser getUser() throws IOException {
return owner.root.getUser(user.getLogin());
}
public String getCommitId() {
return commit_id;
}
public GHPullRequestReviewState getState() {
return state;
}
@Override
public URL getHtmlUrl() {
return null;
}
protected String getApiRoute() {
return owner.getApiRoute()+"/reviews/"+id;
}
/**
* Updates the comment.
*/
@Preview
@Deprecated
public void submit(String body, GHPullRequestReviewState event) throws IOException {
new Requester(owner.root).method("POST")
.with("body", body)
.with("event", event.action())
.withPreview("application/vnd.github.black-cat-preview+json")
.to(getApiRoute()+"/events",this);
this.body = body;
this.state = event;
}
/**
* Deletes this review.
*/
@Preview
@Deprecated
public void delete() throws IOException {
new Requester(owner.root).method("DELETE")
.withPreview(BLACK_CAT)
.to(getApiRoute());
}
/**
* Dismisses this review.
*/
@Preview
@Deprecated
public void dismiss(String message) throws IOException {
new Requester(owner.root).method("PUT")
.with("message", message)
.withPreview(BLACK_CAT)
.to(getApiRoute()+"/dismissals");
state = GHPullRequestReviewState.DISMISSED;
}
/**
* Obtains all the review comments associated with this pull request review.
*/
@Preview
@Deprecated
public PagedIterable<GHPullRequestReviewComment> listReviewComments() throws IOException {
return new PagedIterable<GHPullRequestReviewComment>() {
public PagedIterator<GHPullRequestReviewComment> _iterator(int pageSize) {
return new PagedIterator<GHPullRequestReviewComment>(
owner.root.retrieve()
.withPreview(BLACK_CAT)
.asIterator(getApiRoute() + "/comments",
GHPullRequestReviewComment[].class, pageSize)) {
protected void wrapUp(GHPullRequestReviewComment[] page) {
for (GHPullRequestReviewComment c : page)
c.wrapUp(owner);
}
};
}
};
}
}

View File

@@ -44,6 +44,14 @@ public class GHPullRequestReviewComment extends GHObject implements Reactable {
private int position;
private int originalPosition;
public static GHPullRequestReviewComment draft(String body, String path, int position) {
GHPullRequestReviewComment result = new GHPullRequestReviewComment();
result.body = body;
result.path = path;
result.position = position;
return result;
}
/*package*/ GHPullRequestReviewComment wrapUp(GHPullRequest owner) {
this.owner = owner;
return this;

View File

@@ -0,0 +1,19 @@
package org.kohsuke.github;
public enum GHPullRequestReviewState {
PENDING(null),
APPROVED("APPROVE"),
REQUEST_CHANGES("REQUEST_CHANGES"),
COMMENTED("COMMENT"),
DISMISSED(null);
private final String _action;
GHPullRequestReviewState(String action) {
_action = action;
}
public String action() {
return _action;
}
}

View File

@@ -1,6 +1,7 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Date;
/**

View File

@@ -3,7 +3,7 @@ package org.kohsuke.github;
import java.io.IOException;
import java.net.URL;
import static org.kohsuke.github.Previews.SQUIRREL_GIRL;
import static org.kohsuke.github.Previews.*;
/**
* Reaction to issue, comment, PR, and so on.

View File

@@ -1,6 +1,7 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URL;

View File

@@ -8,7 +8,7 @@ import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static java.lang.String.format;
import static java.lang.String.*;
/**
* Release in a github repository.
@@ -45,10 +45,12 @@ public class GHRelease extends GHObject {
return draft;
}
/**
* @deprecated
* Use {@link #update()}
*/
public GHRelease setDraft(boolean draft) throws IOException {
edit("draft", draft);
this.draft = draft;
return this;
return update().draft(draft).update();
}
public URL getHtmlUrl() {
@@ -149,10 +151,10 @@ public class GHRelease extends GHObject {
}
/**
* Edit this release.
* Updates this release via a builder.
*/
private void edit(String key, Object value) throws IOException {
new Requester(root)._with(key, value).method("PATCH").to(owner.getApiTailUrl("releases/"+id));
public GHReleaseUpdater update() {
return new GHReleaseUpdater(this);
}
private String getApiTailUrl(String end) {

View File

@@ -21,9 +21,7 @@ public class GHReleaseBuilder {
* @param body The release notes body.
*/
public GHReleaseBuilder body(String body) {
if (body != null) {
builder.with("body", body);
}
builder.with("body", body);
return this;
}
@@ -35,9 +33,7 @@ public class GHReleaseBuilder {
* already exists.
*/
public GHReleaseBuilder commitish(String commitish) {
if (commitish != null) {
builder.with("target_commitish", commitish);
}
builder.with("target_commitish", commitish);
return this;
}
@@ -56,9 +52,7 @@ public class GHReleaseBuilder {
* @param name the name of the release
*/
public GHReleaseBuilder name(String name) {
if (name != null) {
builder.with("name", name);
}
builder.with("name", name);
return this;
}

View File

@@ -0,0 +1,81 @@
package org.kohsuke.github;
import java.io.IOException;
/**
* Modifies {@link GHRelease}.
*
* @author Kohsuke Kawaguchi
* @see GHRelease#update()
*/
public class GHReleaseUpdater {
private final GHRelease base;
private final Requester builder;
GHReleaseUpdater(GHRelease base) {
this.base = base;
this.builder = new Requester(base.root);
}
public GHReleaseUpdater tag(String tag) {
builder.with("tag_name",tag);
return this;
}
/**
* @param body The release notes body.
*/
public GHReleaseUpdater body(String body) {
builder.with("body", body);
return this;
}
/**
* Specifies the commitish value that determines where the Git tag is created from. Can be any branch or
* commit SHA.
*
* @param commitish Defaults to the repositorys default branch (usually "master"). Unused if the Git tag
* already exists.
*/
public GHReleaseUpdater commitish(String commitish) {
builder.with("target_commitish", commitish);
return this;
}
/**
* Optional.
*
* @param draft {@code true} to create a draft (unpublished) release, {@code false} to create a published one.
* Default is {@code false}.
*/
public GHReleaseUpdater draft(boolean draft) {
builder.with("draft", draft);
return this;
}
/**
* @param name the name of the release
*/
public GHReleaseUpdater name(String name) {
builder.with("name", name);
return this;
}
/**
* Optional
*
* @param prerelease {@code true} to identify the release as a prerelease. {@code false} to identify the release
* as a full release. Default is {@code false}.
*/
public GHReleaseUpdater prerelease(boolean prerelease) {
builder.with("prerelease", prerelease);
return this;
}
public GHRelease update() throws IOException {
return builder
.method("PATCH")
.to(base.owner.getApiTailUrl("releases/"+base.id), GHRelease.class).wrap(base.owner);
}
}

View File

@@ -60,7 +60,7 @@ import static org.kohsuke.github.Previews.*;
* @author Kohsuke Kawaguchi
*/
@SuppressWarnings({"UnusedDeclaration"})
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
"NP_UNWRITTEN_FIELD"}, justification = "JSON API")
public class GHRepository extends GHObject {
/*package almost final*/ GitHub root;
@@ -298,6 +298,14 @@ public class GHRepository extends GHObject {
public List<GHRelease> getReleases() throws IOException {
return listReleases().asList();
}
public GHRelease getLatestRelease() throws IOException {
try {
return root.retrieve().to(getApiTailUrl("releases/latest"), GHRelease.class).wrap(this);
} catch (FileNotFoundException e) {
return null; // no latest release
}
}
public PagedIterable<GHRelease> listReleases() throws IOException {
return new PagedIterable<GHRelease>() {
@@ -434,8 +442,8 @@ public class GHRepository extends GHObject {
public int getSize() {
return size;
}
/**
* Gets the collaborators on this repository.
* This set always appear to include the owner.
@@ -486,9 +494,8 @@ public class GHRepository extends GHObject {
* @throws FileNotFoundException under some conditions (e.g., private repo you can see but are not an admin of); treat as unknown
* @throws HttpException with a 403 under other conditions (e.g., public repo you have no special rights to); treat as unknown
*/
@Deprecated @Preview
public GHPermissionType getPermission(String user) throws IOException {
GHPermission perm = root.retrieve().withPreview(KORRA).to(getApiTailUrl("collaborators/" + user + "/permission"), GHPermission.class);
GHPermission perm = root.retrieve().to(getApiTailUrl("collaborators/" + user + "/permission"), GHPermission.class);
perm.wrapUp(root);
return perm.getPermissionType();
}
@@ -498,7 +505,6 @@ public class GHRepository extends GHObject {
* @throws FileNotFoundException under some conditions (e.g., private repo you can see but are not an admin of); treat as unknown
* @throws HttpException with a 403 under other conditions (e.g., public repo you have no special rights to); treat as unknown
*/
@Deprecated @Preview
public GHPermissionType getPermission(GHUser u) throws IOException {
return getPermission(u.getLogin());
}
@@ -784,6 +790,26 @@ public class GHRepository extends GHObject {
return GHRef.wrap(root.retrieve().to(String.format("/repos/%s/%s/git/refs", getOwnerName(), name), GHRef[].class), root);
}
/**
* Retrieves all refs for the github repository.
*
* @return paged iterable of all refs
* @throws IOException on failure communicating with GitHub, potentially due to an invalid ref type being requested
*/
public PagedIterable<GHRef> listRefs() throws IOException {
final String url = String.format("/repos/%s/%s/git/refs", getOwnerName(), name);
return new PagedIterable<GHRef>() {
public PagedIterator<GHRef> _iterator(int pageSize) {
return new PagedIterator<GHRef>(root.retrieve().asIterator(url, GHRef[].class, pageSize)) {
protected void wrapUp(GHRef[] page) {
// no-op
}
};
}
};
}
/**
* Retrieves all refs of the given type for the current GitHub repository.
* @param refType the type of reg to search for e.g. <tt>tags</tt> or <tt>commits</tt>
@@ -793,6 +819,27 @@ public class GHRepository extends GHObject {
public GHRef[] getRefs(String refType) throws IOException {
return GHRef.wrap(root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", getOwnerName(), name, refType), GHRef[].class),root);
}
/**
* Retrieves all refs of the given type for the current GitHub repository.
*
* @param refType the type of reg to search for e.g. <tt>tags</tt> or <tt>commits</tt>
* @return paged iterable of all refs of the specified type
* @throws IOException on failure communicating with GitHub, potentially due to an invalid ref type being requested
*/
public PagedIterable<GHRef> listRefs(String refType) throws IOException {
final String url = String.format("/repos/%s/%s/git/refs/%s", getOwnerName(), name, refType);
return new PagedIterable<GHRef>() {
public PagedIterator<GHRef> _iterator(int pageSize) {
return new PagedIterator<GHRef>(root.retrieve().asIterator(url, GHRef[].class, pageSize)) {
protected void wrapUp(GHRef[] page) {
// no-op
}
};
}
};
}
/**
* Retrive a ref of the given type for the current GitHub repository.
*
@@ -810,6 +857,18 @@ public class GHRepository extends GHObject {
refName = refName.replaceAll("#", "%23");
return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", getOwnerName(), name, refName), GHRef.class).wrap(root);
}
/**
* Returns the <strong>annotated</strong> tag object. Only valid if the {@link GHRef#getObject()} has a
* {@link GHRef.GHObject#getType()} of {@code tag}.
*
* @param sha the sha of the tag object
* @return the annotated tag object
*/
public GHTagObject getTagObject(String sha) throws IOException {
return root.retrieve().to(getApiTailUrl("git/tags/" + sha), GHTagObject.class).wrap(this);
}
/**
* Retrive a tree of the given type for the current GitHub repository.
*
@@ -824,6 +883,10 @@ public class GHRepository extends GHObject {
return root.retrieve().to(url, GHTree.class).wrap(this);
}
public GHTreeBuilder createTree() {
return new GHTreeBuilder(this);
}
/**
* Retrieves the tree for the current GitHub repository, recursively as described in here:
* https://developer.github.com/v3/git/trees/#get-a-tree-recursively
@@ -853,6 +916,10 @@ public class GHRepository extends GHObject {
return root.retrieve().to(target, GHBlob.class);
}
public GHBlobBuilder createBlob() {
return new GHBlobBuilder(this);
}
/**
* Reads the content of a blob as a stream for better efficiency.
*
@@ -876,6 +943,10 @@ public class GHRepository extends GHObject {
return c;
}
public GHCommitBuilder createCommit() {
return new GHCommitBuilder(this);
}
/**
* Lists all the commits.
*/
@@ -1146,7 +1217,7 @@ public class GHRepository extends GHObject {
* @deprecated
* Use {@link #getHooks()} and {@link #createHook(String, Map, Collection, boolean)}
*/
@SuppressFBWarnings(value = "DMI_COLLECTION_OF_URLS",
@SuppressFBWarnings(value = "DMI_COLLECTION_OF_URLS",
justification = "It causes a performance degradation, but we have already exposed it to the API")
public Set<URL> getPostCommitHooks() {
return postCommitHooks;
@@ -1155,7 +1226,7 @@ public class GHRepository extends GHObject {
/**
* Live set view of the post-commit hook.
*/
@SuppressFBWarnings(value = "DMI_COLLECTION_OF_URLS",
@SuppressFBWarnings(value = "DMI_COLLECTION_OF_URLS",
justification = "It causes a performance degradation, but we have already exposed it to the API")
@SkipFromToString
private final Set<URL> postCommitHooks = new AbstractSet<URL>() {
@@ -1223,7 +1294,7 @@ public class GHRepository extends GHObject {
*/
public Map<String,GHBranch> getBranches() throws IOException {
Map<String,GHBranch> r = new TreeMap<String,GHBranch>();
for (GHBranch p : root.retrieve().to(getApiTailUrl("branches"), GHBranch[].class)) {
for (GHBranch p : root.retrieve().withPreview(LOKI).to(getApiTailUrl("branches"), GHBranch[].class)) {
p.wrap(this);
r.put(p.getName(),p);
}
@@ -1231,7 +1302,7 @@ public class GHRepository extends GHObject {
}
public GHBranch getBranch(String name) throws IOException {
return root.retrieve().to(getApiTailUrl("branches/"+name),GHBranch.class).wrap(this);
return root.retrieve().withPreview(LOKI).to(getApiTailUrl("branches/"+name),GHBranch.class).wrap(this);
}
/**
@@ -1453,7 +1524,7 @@ public class GHRepository extends GHObject {
public boolean equals(Object obj) {
// We ignore contributions in the calculation
return super.equals(obj);
}
}
}
/**
@@ -1481,6 +1552,19 @@ public class GHRepository extends GHObject {
return new GHNotificationStream(root,getApiTailUrl("/notifications"));
}
/**
* <a href="https://developer.github.com/v3/repos/traffic/#views">https://developer.github.com/v3/repos/traffic/#views</a>
*/
public GHRepositoryViewTraffic getViewTraffic() throws IOException{
return root.retrieve().to(getApiTailUrl("/traffic/views"), GHRepositoryViewTraffic.class);
}
/**
* <a href="https://developer.github.com/v3/repos/traffic/#clones">https://developer.github.com/v3/repos/traffic/#clones</a>
*/
public GHRepositoryCloneTraffic getCloneTraffic() throws IOException{
return root.retrieve().to(getApiTailUrl("/traffic/clones"), GHRepositoryCloneTraffic.class);
}
@Override
public int hashCode() {

View File

@@ -0,0 +1,37 @@
package org.kohsuke.github;
import java.util.List;
/**
* Repository clone statistics.
*
* @see GHRepository#getCloneTraffic()
*/
public class GHRepositoryCloneTraffic extends GHRepositoryTraffic {
private List<DailyInfo> clones;
/*package*/ GHRepositoryCloneTraffic() {
}
/*package*/ GHRepositoryCloneTraffic(Integer count, Integer uniques, List<DailyInfo> clones) {
super(count, uniques);
this.clones = clones;
}
public List<DailyInfo> getClones() {
return clones;
}
public List<DailyInfo> getDailyInfo() {
return getClones();
}
public static class DailyInfo extends GHRepositoryTraffic.DailyInfo {
/*package*/ DailyInfo() {
}
/*package*/ DailyInfo(String timestamp, int count, int uniques) {
super(timestamp, count, uniques);
}
}
}

View File

@@ -1,7 +1,5 @@
package org.kohsuke.github;
import java.util.Locale;
/**
* Search repositories.
*

View File

@@ -0,0 +1,54 @@
package org.kohsuke.github;
import java.util.Date;
import java.util.List;
public abstract class GHRepositoryTraffic implements TrafficInfo {
private int count;
private int uniques;
/*package*/ GHRepositoryTraffic() {
}
/*package*/ GHRepositoryTraffic(int count, int uniques) {
this.count = count;
this.uniques = uniques;
}
public int getCount() {
return count;
}
public int getUniques() {
return uniques;
}
public abstract List<? extends DailyInfo> getDailyInfo();
public static abstract class DailyInfo implements TrafficInfo {
private String timestamp;
private int count;
private int uniques;
public Date getTimestamp() {
return GitHub.parseDate(timestamp);
}
public int getCount() {
return count;
}
public int getUniques() {
return uniques;
}
/*package*/ DailyInfo() {
}
/*package*/ DailyInfo(String timestamp, Integer count, Integer uniques) {
this.timestamp = timestamp;
this.count = count;
this.uniques = uniques;
}
}
}

View File

@@ -0,0 +1,37 @@
package org.kohsuke.github;
import java.util.List;
/**
* Repository view statistics.
*
* @see GHRepository#getViewTraffic()
*/
public class GHRepositoryViewTraffic extends GHRepositoryTraffic {
private List<DailyInfo> views;
/*package*/ GHRepositoryViewTraffic() {
}
/*package*/ GHRepositoryViewTraffic(int count, int uniques, List<DailyInfo> views) {
super(count, uniques);
this.views = views;
}
public List<DailyInfo> getViews() {
return views;
}
public List<DailyInfo> getDailyInfo() {
return getViews();
}
public static class DailyInfo extends GHRepositoryTraffic.DailyInfo {
/*package*/ DailyInfo() {
}
/*package*/ DailyInfo(String timestamp, int count, int uniques) {
super(timestamp, count, uniques);
}
}
}

View File

@@ -0,0 +1,60 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Represents an annotated tag in a {@link GHRepository}
*
* @see GHRepository#getTagObject(String)
*/
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
"NP_UNWRITTEN_FIELD"}, justification = "JSON API")
public class GHTagObject {
private GHRepository owner;
private GitHub root;
private String tag;
private String sha;
private String url;
private String message;
private GitUser tagger;
private GHRef.GHObject object;
/*package*/ GHTagObject wrap(GHRepository owner) {
this.owner = owner;
this.root = owner.root;
return this;
}
public GHRepository getOwner() {
return owner;
}
public GitHub getRoot() {
return root;
}
public String getTag() {
return tag;
}
public String getSha() {
return sha;
}
public String getUrl() {
return url;
}
public String getMessage() {
return message;
}
public GitUser getTagger() {
return tagger;
}
public GHRef.GHObject getObject() {
return object;
}
}

View File

@@ -1,6 +1,7 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

View File

@@ -0,0 +1,90 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Builder pattern for creating a new tree.
* Based on https://developer.github.com/v3/git/trees/#create-a-tree
*/
public class GHTreeBuilder {
private final GHRepository repo;
private final Requester req;
private final List<TreeEntry> treeEntries = new ArrayList<TreeEntry>();
@SuppressFBWarnings("URF_UNREAD_FIELD")
private static final class TreeEntry {
private final String path;
private final String mode;
private final String type;
private String sha;
private String content;
private TreeEntry(String path, String mode, String type) {
this.path = path;
this.mode = mode;
this.type = type;
}
}
GHTreeBuilder(GHRepository repo) {
this.repo = repo;
req = new Requester(repo.root);
}
/**
* @param baseTree the SHA of tree you want to update with new data
*/
public GHTreeBuilder baseTree(String baseTree) {
req.with("base_tree", baseTree);
return this;
}
/**
* Adds a new entry to the tree.
* Exactly one of the parameters {@code sha} and {@code content} must be non-null.
*/
public GHTreeBuilder entry(String path, String mode, String type, String sha, String content) {
TreeEntry entry = new TreeEntry(path, mode, type);
entry.sha = sha;
entry.content = content;
treeEntries.add(entry);
return this;
}
/**
* Specialized version of {@link #entry(String, String, String, String, String)} for adding an existing blob referred by its SHA.
*/
public GHTreeBuilder shaEntry(String path, String sha, boolean executable) {
TreeEntry entry = new TreeEntry(path, executable ? "100755" : "100644", "blob");
entry.sha = sha;
treeEntries.add(entry);
return this;
}
/**
* Specialized version of {@link #entry(String, String, String, String, String)} for adding a text file with the specified {@code content}.
*/
public GHTreeBuilder textEntry(String path, String content, boolean executable) {
TreeEntry entry = new TreeEntry(path, executable ? "100755" : "100644", "blob");
entry.content = content;
treeEntries.add(entry);
return this;
}
private String getApiTail() {
return String.format("/repos/%s/%s/git/trees", repo.getOwnerName(), repo.getName());
}
/**
* Creates a tree based on the parameters specified thus far.
*/
public GHTree create() throws IOException {
req._with("tree", treeEntries);
return req.method("POST").to(getApiTail(), GHTree.class).wrap(repo);
}
}

View File

@@ -1,7 +1,5 @@
package org.kohsuke.github;
import java.util.Locale;
/**
* Search users.
*

View File

@@ -27,6 +27,12 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker.Std;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -47,18 +53,14 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
import static java.util.logging.Level.FINE;
import static org.kohsuke.github.Previews.DRAX;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.*;
import static java.net.HttpURLConnection.*;
import static java.util.logging.Level.*;
import static org.kohsuke.github.Previews.*;
/**
* Root of the GitHub API.
@@ -79,9 +81,10 @@ public class GitHub {
*/
/*package*/ final String encodedAuthorization;
private final Map<String,GHUser> users = new Hashtable<String, GHUser>();
private final Map<String,GHOrganization> orgs = new Hashtable<String, GHOrganization>();
private final ConcurrentMap<String,GHUser> users;
private final ConcurrentMap<String,GHOrganization> orgs;
// Cache of myself object.
private GHMyself myself;
private final String apiUrl;
/*package*/ final RateLimitHandler rateLimitHandler;
@@ -146,6 +149,8 @@ public class GitHub {
}
}
users = new ConcurrentHashMap<String, GHUser>();
orgs = new ConcurrentHashMap<String, GHOrganization>();
this.rateLimitHandler = rateLimitHandler;
this.abuseLimitHandler = abuseLimitHandler;
@@ -357,13 +362,15 @@ public class GitHub {
@WithBridgeMethods(GHUser.class)
public GHMyself getMyself() throws IOException {
requireCredential();
synchronized (this) {
if (this.myself != null) return myself;
GHMyself u = retrieve().to("/user", GHMyself.class);
GHMyself u = retrieve().to("/user", GHMyself.class);
u.root = this;
users.put(u.getLogin(), u);
return u;
u.root = this;
this.myself = u;
return u;
}
}
/**
@@ -379,7 +386,7 @@ public class GitHub {
return u;
}
/**
* clears all cached data in order for external changes (modifications and del
*/
@@ -641,6 +648,18 @@ public class GitHub {
}
}
/*package*/ GHUser intern(GHUser user) throws IOException {
if (user==null) return user;
// if we already have this user in our map, use it
GHUser u = users.get(user.getLogin());
if (u!=null) return u;
// if not, remember this new user
users.putIfAbsent(user.getLogin(),user);
return user;
}
private static class GHApiInfo {
private String rate_limit_url;
@@ -719,6 +738,14 @@ public class GitHub {
}
}
/**
* Search commits.
*/
@Preview @Deprecated
public GHCommitSearchBuilder searchCommits() {
return new GHCommitSearchBuilder(this);
}
/**
* Search issues.
*/

View File

@@ -1,6 +1,7 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Date;
/**

View File

@@ -5,7 +5,6 @@ import org.kohsuke.github.extras.ImpatientHttpConnector;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
/**
* Pluggability for customizing HTTP request behaviors or using altogether different library.

View File

@@ -1,11 +1,10 @@
package org.kohsuke.github;
import javax.annotation.CheckForNull;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.annotation.CheckForNull;
/**
* {@link IOException} for http exceptions because {@link HttpURLConnection} throws un-discerned
* {@link IOException} and it can help to know the http response code to decide how to handle an

View File

@@ -1,6 +1,5 @@
package org.kohsuke.github;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

View File

@@ -2,7 +2,6 @@ package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.util.Iterator;
/**

View File

@@ -7,5 +7,6 @@ package org.kohsuke.github;
static final String LOKI = "application/vnd.github.loki-preview+json";
static final String DRAX = "application/vnd.github.drax-preview+json";
static final String SQUIRREL_GIRL = "application/vnd.github.squirrel-girl-preview";
static final String KORRA = "application/vnd.github.korra-preview";
static final String CLOAK = "application/vnd.github.cloak-preview";
static final String BLACK_CAT = "application/vnd.github.black-cat-preview+json";
}

View File

@@ -25,6 +25,11 @@ package org.kohsuke.github;
import com.fasterxml.jackson.databind.JsonMappingException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import javax.annotation.CheckForNull;
import javax.annotation.WillClose;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -36,6 +41,7 @@ import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
@@ -48,17 +54,16 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import javax.annotation.WillClose;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import static java.util.Arrays.asList;
import static java.util.Arrays.*;
import static java.util.logging.Level.*;
import static org.kohsuke.github.GitHub.MAPPER;
import static org.apache.commons.lang.StringUtils.*;
import static org.kohsuke.github.GitHub.*;
/**
* A builder pattern for making HTTP call and parsing its output.
@@ -74,7 +79,7 @@ class Requester {
* Request method.
*/
private String method = "POST";
private String contentType = "application/x-www-form-urlencoded";
private String contentType = null;
private InputStream body;
/**
@@ -168,6 +173,11 @@ class Requester {
return this;
}
public Requester withNullable(String key, Object value) {
args.add(new Entry(key, value));
return this;
}
public Requester _with(String key, Object value) {
if (value!=null) {
args.add(new Entry(key,value));
@@ -268,7 +278,7 @@ class Requester {
if (nextLinkMatcher.find()) {
final String link = nextLinkMatcher.group(1);
T nextResult = _to(link, type, instance);
setResponseHeaders(nextResult);
final int resultLength = Array.getLength(result);
final int nextResultLength = Array.getLength(nextResult);
T concatResult = (T) Array.newInstance(type.getComponentType(), resultLength + nextResultLength);
@@ -278,7 +288,7 @@ class Requester {
}
}
}
return result;
return setResponseHeaders(result);
} catch (IOException e) {
handleApiError(e);
} finally {
@@ -312,7 +322,7 @@ class Requester {
setupConnection(root.getApiURL(tailApiUrl));
buildRequest();
try {
return wrapStream(uc.getInputStream());
} catch (IOException e) {
@@ -385,18 +395,19 @@ class Requester {
private void buildRequest() throws IOException {
if (isMethodWithBody()) {
uc.setDoOutput(true);
uc.setRequestProperty("Content-type", contentType);
if (body == null) {
uc.setRequestProperty("Content-type", defaultString(contentType,"application/json"));
Map json = new HashMap();
for (Entry e : args) {
json.put(e.key, e.value);
}
MAPPER.writeValue(uc.getOutputStream(), json);
} else {
uc.setRequestProperty("Content-type", defaultString(contentType,"application/x-www-form-urlencoded"));
try {
byte[] bytes = new byte[32768];
int read = 0;
int read;
while ((read = body.read(bytes)) != -1) {
uc.getOutputStream().write(bytes, 0, read);
}
@@ -578,7 +589,12 @@ class Requester {
throw new IllegalStateException("Failed to set the request method to "+method);
}
@CheckForNull
private <T> T parse(Class<T> type, T instance) throws IOException {
return parse(type, instance, 2);
}
private <T> T parse(Class<T> type, T instance, int timeouts) throws IOException {
InputStreamReader r = null;
int responseCode = -1;
String responseMessage = null;
@@ -597,24 +613,44 @@ class Requester {
String data = IOUtils.toString(r);
if (type!=null)
try {
return MAPPER.readValue(data,type);
return setResponseHeaders(MAPPER.readValue(data, type));
} catch (JsonMappingException e) {
throw (IOException)new IOException("Failed to deserialize " +data).initCause(e);
}
if (instance!=null)
return MAPPER.readerForUpdating(instance).<T>readValue(data);
if (instance!=null) {
return setResponseHeaders(MAPPER.readerForUpdating(instance).<T>readValue(data));
}
return null;
} catch (FileNotFoundException e) {
// java.net.URLConnection handles 404 exception has FileNotFoundException, don't wrap exception in HttpException
// to preserve backward compatibility
throw e;
} catch (IOException e) {
if (e instanceof SocketTimeoutException && timeouts > 0) {
LOGGER.log(INFO, "timed out accessing " + uc.getURL() + "; will try " + timeouts + " more time(s)", e);
return parse(type, instance, timeouts - 1);
}
throw new HttpException(responseCode, responseMessage, uc.getURL(), e);
} finally {
IOUtils.closeQuietly(r);
}
}
private <T> T setResponseHeaders(T readValue) {
if (readValue instanceof GHObject[]) {
for (GHObject ghObject : (GHObject[]) readValue) {
setResponseHeaders(ghObject);
}
} else if (readValue instanceof GHObject) {
setResponseHeaders((GHObject) readValue);
}
return readValue;
}
private void setResponseHeaders(GHObject readValue) {
readValue.responseHeaderFields = uc.getHeaderFields();
}
/**
* Handles the "Content-Encoding" header.
*/
@@ -647,13 +683,13 @@ class Requester {
String error = IOUtils.toString(es, "UTF-8");
if (e instanceof FileNotFoundException) {
// pass through 404 Not Found to allow the caller to handle it intelligently
e = (IOException) new FileNotFoundException(error).initCause(e);
e = (IOException) new GHFileNotFoundException(error).withResponseHeaderFields(uc).initCause(e);
} else if (e instanceof HttpException) {
HttpException http = (HttpException) e;
e = new HttpException(error, http.getResponseCode(), http.getResponseMessage(),
http.getUrl(), e);
} else {
e = (IOException) new IOException(error).initCause(e);
e = (IOException) new GHIOException(error).withResponseHeaderFields(uc).initCause(e);
}
} finally {
IOUtils.closeQuietly(es);

View File

@@ -0,0 +1,16 @@
package org.kohsuke.github;
/**
* @author Kohsuke Kawaguchi
*/
public interface TrafficInfo {
/**
* Total count of hits.
*/
int getCount();
/**
* Unique visitors.
*/
int getUniques();
}

View File

@@ -0,0 +1,32 @@
package org.kohsuke.github.extras;
import okhttp3.OkHttpClient;
import okhttp3.OkUrlFactory;
import org.kohsuke.github.HttpConnector;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* {@link HttpConnector} for {@link OkHttpClient}.
*
* Unlike {@link #DEFAULT}, OkHttp does response caching.
* Making a conditional request against GitHubAPI and receiving a 304
* response does not count against the rate limit.
* See http://developer.github.com/v3/#conditional-requests
*
* @author Roberto Tyley
* @author Kohsuke Kawaguchi
*/
public class OkHttp3Connector implements HttpConnector {
private final OkUrlFactory urlFactory;
public OkHttp3Connector(OkUrlFactory urlFactory) {
this.urlFactory = urlFactory;
}
public HttpURLConnection connect(URL url) throws IOException {
return urlFactory.open(url);
}
}

View File

@@ -1,10 +1,7 @@
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHRepository.Contributor;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;
import java.util.Collection;
/**
* @author Kohsuke Kawaguchi
*/

View File

@@ -1,5 +1,8 @@
package org.kohsuke.github;
import java.io.FileInputStream;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
@@ -19,9 +22,17 @@ public abstract class AbstractGitHubApiTestBase extends Assert {
public void setUp() throws Exception {
File f = new File(System.getProperty("user.home"), ".github.kohsuke2");
if (f.exists()) {
Properties props = new Properties();
FileInputStream in = null;
try {
in = new FileInputStream(f);
props.load(in);
} finally {
IOUtils.closeQuietly(in);
}
// use the non-standard credential preferentially, so that developers of this library do not have
// to clutter their event stream.
gitHub = GitHubBuilder.fromPropertyFile(f.getPath()).withRateLimitHandler(RateLimitHandler.FAIL).build();
gitHub = GitHubBuilder.fromProperties(props).withRateLimitHandler(RateLimitHandler.FAIL).build();
} else {
gitHub = GitHubBuilder.fromCredentials().withRateLimitHandler(RateLimitHandler.FAIL).build();
}

View File

@@ -5,8 +5,6 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;
import org.hamcrest.CoreMatchers;
import org.junit.Assume;
import org.junit.Test;
import org.kohsuke.github.GHCommit.File;
import org.kohsuke.github.GHOrganization.Permission;
@@ -16,7 +14,6 @@ import java.io.InputStream;
import java.net.URL;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
import static org.hamcrest.CoreMatchers.*;
@@ -685,6 +682,15 @@ public class AppTest extends AbstractGitHubApiTestBase {
assertFalse(all.isEmpty());
}
@Test
public void testCommitSearch() throws IOException {
PagedSearchIterable<GHCommit> r = gitHub.searchCommits().author("kohsuke").list();
assertTrue(r.getTotalCount() > 0);
GHCommit firstCommit = r.iterator().next();
assertTrue(firstCommit.getFiles().size() > 0);
}
@Test
public void testIssueSearch() throws IOException {
PagedSearchIterable<GHIssue> r = gitHub.searchIssues().mentions("kohsuke").isOpen().list();

View File

@@ -1,7 +1,6 @@
package org.kohsuke.github;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import org.junit.Test;
import java.io.IOException;

View File

@@ -0,0 +1,82 @@
package org.kohsuke.github;
import org.junit.Before;
import org.junit.Test;
import org.kohsuke.github.GHBranchProtection.EnforceAdmins;
import org.kohsuke.github.GHBranchProtection.RequiredReviews;
import org.kohsuke.github.GHBranchProtection.RequiredStatusChecks;
import java.io.FileNotFoundException;
public class GHBranchProtectionTest extends AbstractGitHubApiTestBase {
private static final String BRANCH = "bp-test";
private static final String BRANCH_REF = "heads/" + BRANCH;
private GHBranch branch;
private GHRepository repo;
@Before
@Override
public void setUp() throws Exception {
super.setUp();
repo = gitHub.getRepository("github-api-test-org/GHContentIntegrationTest").fork();
try {
repo.getRef(BRANCH_REF);
} catch (FileNotFoundException e) {
repo.createRef("refs/" + BRANCH_REF, repo.getBranch("master").getSHA1());
}
branch = repo.getBranch(BRANCH);
if (branch.isProtected()) {
branch.disableProtection();
}
branch = repo.getBranch(BRANCH);
assertFalse(branch.isProtected());
}
@Test
public void testEnableBranchProtections() throws Exception {
// team/user restrictions require an organization repo to test against
GHBranchProtection protection = branch.enableProtection()
.addRequiredChecks("test-status-check")
.requireBranchIsUpToDate()
.requireCodeOwnReviews()
.dismissStaleReviews()
.includeAdmins()
.enable();
RequiredStatusChecks statusChecks = protection.getRequiredStatusChecks();
assertNotNull(statusChecks);
assertTrue(statusChecks.isRequiresBranchUpToDate());
assertTrue(statusChecks.getContexts().contains("test-status-check"));
RequiredReviews requiredReviews = protection.getRequiredReviews();
assertNotNull(requiredReviews);
assertTrue(requiredReviews.isDismissStaleReviews());
assertTrue(requiredReviews.isRequireCodeOwnerReviews());
EnforceAdmins enforceAdmins = protection.getEnforceAdmins();
assertNotNull(enforceAdmins);
assertTrue(enforceAdmins.isEnabled());
}
@Test
public void testEnableProtectionOnly() throws Exception {
branch.enableProtection().enable();
assertTrue(repo.getBranch(BRANCH).isProtected());
}
@Test
public void testEnableRequireReviewsOnly() throws Exception {
GHBranchProtection protection = branch.enableProtection()
.requireReviews()
.enable();
assertNotNull(protection.getRequiredReviews());
}
}

View File

@@ -20,13 +20,6 @@ public class GHContentIntegrationTest extends AbstractGitHubApiTestBase {
repo = gitHub.getRepository("github-api-test-org/GHContentIntegrationTest").fork();
}
@Test
public void testBranchProtection() throws Exception {
GHBranch b = repo.getBranch("master");
b.enableProtection(EnforcementLevel.NON_ADMINS, "foo/bar");
b.disableProtection();
}
@Test
public void testGetFileContent() throws Exception {
GHContent content = repo.getFileContent("ghcontent-ro/a-file-with-content");

View File

@@ -0,0 +1,78 @@
package org.kohsuke.github;
import org.apache.commons.lang.StringUtils;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasValue;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;
/**
* @author Kanstantsin Shautsou
*/
public class GHHookTest {
@Ignore
@Test
public void exposeResponceHeaders() throws Exception {
String user1Login = "KostyaSha-auto";
String user1Pass = "secret";
String clientId = "90140219451";
String clientSecret = "1451245425";
String orgRepo = "KostyaSha-org/test";
// some login based user that has access to application
final GitHub gitHub = GitHub.connectUsingPassword(user1Login, user1Pass);
gitHub.getMyself();
// we request read
final List<String> scopes = Arrays.asList("repo", "read:org", "user:email", "read:repo_hook");
// application creates token with scopes
final GHAuthorization auth = gitHub.createOrGetAuth(clientId, clientSecret, scopes, "", "");
String token = auth.getToken();
if (StringUtils.isEmpty(token)) {
gitHub.deleteAuth(auth.getId());
token = gitHub.createOrGetAuth(clientId, clientSecret, scopes, "", "").getToken();
}
/// now create connection using token
final GitHub gitHub2 = GitHub.connectUsingOAuth(token);
// some repo in organisation
final GHRepository repository = gitHub2.getRepository(orgRepo);
// doesn't fail because we have read access
final List<GHHook> hooks = repository.getHooks();
try {
// fails because application isn't approved in organisation and you can find it only after doing real call
final GHHook hook = repository.createHook(
"my-hook",
singletonMap("url", "http://localhost"),
singletonList(GHEvent.PUSH),
true
);
} catch (IOException ex) {
assertThat(ex, instanceOf(GHFileNotFoundException.class));
final GHFileNotFoundException ghFileNotFoundException = (GHFileNotFoundException) ex;
final Map<String, List<String>> responseHeaderFields = ghFileNotFoundException.getResponseHeaderFields();
assertThat(responseHeaderFields, hasKey("X-Accepted-OAuth-Scopes"));
assertThat(responseHeaderFields.get("X-Accepted-OAuth-Scopes"),
hasItem("admin:repo_hook, public_repo, repo, write:repo_hook")
);
}
}
}

View File

@@ -8,7 +8,6 @@ import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.notNullValue;

View File

@@ -7,6 +7,9 @@ import java.io.IOException;
import java.util.Collection;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
/**
* @author Kohsuke Kawaguchi
*/
@@ -26,6 +29,33 @@ public class PullRequestTest extends AbstractGitHubApiTestBase {
p.comment("Some comment");
}
@Test
public void testPullRequestReviews() throws Exception {
String name = rnd.next();
GHPullRequest p = getRepository().createPullRequest(name, "stable", "master", "## test");
GHPullRequestReview draftReview = p.createReview("Some draft review", null,
GHPullRequestReviewComment.draft("Some niggle", "changelog.html", 1)
);
assertThat(draftReview.getState(), is(GHPullRequestReviewState.PENDING));
assertThat(draftReview.getBody(), is("Some draft review"));
assertThat(draftReview.getCommitId(), notNullValue());
List<GHPullRequestReview> reviews = p.listReviews().asList();
assertThat(reviews.size(), is(1));
GHPullRequestReview review = reviews.get(0);
assertThat(review.getState(), is(GHPullRequestReviewState.PENDING));
assertThat(review.getBody(), is("Some draft review"));
assertThat(review.getCommitId(), notNullValue());
review.submit("Some review comment", GHPullRequestReviewState.COMMENTED);
List<GHPullRequestReviewComment> comments = review.listReviewComments().asList();
assertEquals(1, comments.size());
GHPullRequestReviewComment comment = comments.get(0);
assertEquals("Some niggle", comment.getBody());
review = p.createReview("Some new review", null,
GHPullRequestReviewComment.draft("Some niggle", "changelog.html", 1)
);
review.delete();
}
@Test
public void testPullRequestReviewComments() throws Exception {
String name = rnd.next();
@@ -69,6 +99,19 @@ public class PullRequestTest extends AbstractGitHubApiTestBase {
fail();
}
@Test
public void testSquashMerge() throws Exception {
String name = rnd.next();
GHRef masterRef = getRepository().getRef("heads/master");
GHRef branchRef = getRepository().createRef("refs/heads/" + name, masterRef.getObject().getSha());
getRepository().createContent(name, name, name, name);
Thread.sleep(1000);
GHPullRequest p = getRepository().createPullRequest(name, name, "master", "## test squash");
Thread.sleep(1000);
p.merge("squash merge", null, GHPullRequest.MergeMethod.SQUASH);
branchRef.delete();
}
@Test
// Requires push access to the test repo to pass
public void setLabels() throws Exception {

View File

@@ -67,6 +67,32 @@ public class RepositoryTest extends AbstractGitHubApiTestBase {
}
}
}
@Test
public void LatestRepositoryExist() {
try {
// add the repository that have latest release
GHRelease release = gitHub.getRepository("kamontat/CheckIDNumber").getLatestRelease();
assertEquals("v3.0", release.getTagName());
} catch (IOException e) {
e.printStackTrace();
fail();
}
}
@Test
public void LatestRepositoryNotExist() {
try {
// add the repository that `NOT` have latest release
GHRelease release = gitHub.getRepository("kamontat/Java8Example").getLatestRelease();
assertNull(release);
} catch (IOException e) {
e.printStackTrace();
fail();
}
}
private GHRepository getRepository() throws IOException {
return gitHub.getOrganization("github-api-test-org").getRepository("jenkins");

View File

@@ -0,0 +1,167 @@
package org.kohsuke.github;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.kohsuke.github.GHRepositoryTraffic.DailyInfo;
import org.mockito.Mockito;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;
public class RepositoryTrafficTest {
final private String login = "kohsuke", repositoryName = "github-api";
@SuppressWarnings("unchecked")
private <T extends GHRepositoryTraffic> void checkResponse(T expected, T actual){
Assert.assertEquals(expected.getCount(), actual.getCount());
Assert.assertEquals(expected.getUniques(), actual.getUniques());
List<? extends DailyInfo> expectedList = expected.getDailyInfo();
List<? extends DailyInfo> actualList = actual.getDailyInfo();
Iterator<? extends DailyInfo> expectedIt;
Iterator<? extends DailyInfo> actualIt;
Assert.assertEquals(expectedList.size(), actualList.size());
expectedIt = expectedList.iterator();
actualIt = actualList.iterator();
while(expectedIt.hasNext() && actualIt.hasNext()) {
DailyInfo expectedDailyInfo = expectedIt.next();
DailyInfo actualDailyInfo = actualIt.next();
Assert.assertEquals(expectedDailyInfo.getCount(), actualDailyInfo.getCount());
Assert.assertEquals(expectedDailyInfo.getUniques(), actualDailyInfo.getUniques());
Assert.assertEquals(expectedDailyInfo.getTimestamp(), actualDailyInfo.getTimestamp());
}
}
private <T extends GHRepositoryTraffic> void testTraffic(T expectedResult) throws IOException{
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
ObjectMapper mapper = new ObjectMapper().setDateFormat(dateFormat);
String mockedResponse = mapper.writeValueAsString(expectedResult);
GitHub gitHub = GitHub.connect(login, null);
GitHub gitHubSpy = Mockito.spy(gitHub);
GHRepository repo = gitHubSpy.getUser(login).getRepository(repositoryName);
// accessing traffic info requires push access to the repo
// since we don't have that, let the mocking begin...
HttpConnector connectorSpy = Mockito.spy(gitHubSpy.getConnector());
Mockito.doReturn(connectorSpy).when(gitHubSpy).getConnector();
// also known as the "uc" in the Requester class
HttpURLConnection mockHttpURLConnection = Mockito.mock(HttpURLConnection.class);
// needed for Requester.setRequestMethod
Mockito.doReturn("GET").when(mockHttpURLConnection).getRequestMethod();
// this covers calls on "uc" in Requester.setupConnection and Requester.buildRequest
URL trafficURL = new URL(
"https://api.github.com/repos/"+login+"/"+repositoryName+"/traffic/" +
((expectedResult instanceof GHRepositoryViewTraffic) ? "views" : "clones")
);
Mockito.doReturn(mockHttpURLConnection).when(connectorSpy).connect(Mockito.eq(trafficURL));
// make Requester.parse work
Mockito.doReturn(200).when(mockHttpURLConnection).getResponseCode();
Mockito.doReturn("OK").when(mockHttpURLConnection).getResponseMessage();
InputStream stubInputStream = IOUtils.toInputStream(mockedResponse, "UTF-8");
Mockito.doReturn(stubInputStream).when(mockHttpURLConnection).getInputStream();
if(expectedResult instanceof GHRepositoryViewTraffic){
GHRepositoryViewTraffic views = repo.getViewTraffic();
checkResponse(expectedResult, views);
}
else if(expectedResult instanceof GHRepositoryCloneTraffic) {
GHRepositoryCloneTraffic clones = repo.getCloneTraffic();
checkResponse(expectedResult, clones);
}
}
@Test
public void testGetViews() throws IOException{
GHRepositoryViewTraffic expectedResult = new GHRepositoryViewTraffic(
21523359,
65534,
Arrays.asList(
new GHRepositoryViewTraffic.DailyInfo("2016-10-10T00:00:00Z", 3, 2),
new GHRepositoryViewTraffic.DailyInfo("2016-10-11T00:00:00Z", 9, 4),
new GHRepositoryViewTraffic.DailyInfo("2016-10-12T00:00:00Z", 27, 8),
new GHRepositoryViewTraffic.DailyInfo("2016-10-13T00:00:00Z", 81, 16),
new GHRepositoryViewTraffic.DailyInfo("2016-10-14T00:00:00Z", 243, 32),
new GHRepositoryViewTraffic.DailyInfo("2016-10-15T00:00:00Z", 729, 64),
new GHRepositoryViewTraffic.DailyInfo("2016-10-16T00:00:00Z", 2187, 128),
new GHRepositoryViewTraffic.DailyInfo("2016-10-17T00:00:00Z", 6561, 256),
new GHRepositoryViewTraffic.DailyInfo("2016-10-18T00:00:00Z", 19683, 512),
new GHRepositoryViewTraffic.DailyInfo("2016-10-19T00:00:00Z", 59049, 1024),
new GHRepositoryViewTraffic.DailyInfo("2016-10-20T00:00:00Z", 177147, 2048),
new GHRepositoryViewTraffic.DailyInfo("2016-10-21T00:00:00Z", 531441, 4096),
new GHRepositoryViewTraffic.DailyInfo("2016-10-22T00:00:00Z", 1594323, 8192),
new GHRepositoryViewTraffic.DailyInfo("2016-10-23T00:00:00Z", 4782969, 16384),
new GHRepositoryViewTraffic.DailyInfo("2016-10-24T00:00:00Z", 14348907, 32768)
)
);
testTraffic(expectedResult);
}
@Test
public void testGetClones() throws IOException{
GHRepositoryCloneTraffic expectedResult = new GHRepositoryCloneTraffic(
1500,
455,
Arrays.asList(
new GHRepositoryCloneTraffic.DailyInfo("2016-10-10T00:00:00Z", 10,3),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-11T00:00:00Z", 20,6),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-12T00:00:00Z", 30,5),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-13T00:00:00Z", 40,7),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-14T00:00:00Z", 50,11),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-15T00:00:00Z", 60,12),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-16T00:00:00Z", 70,19),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-17T00:00:00Z", 170,111),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-18T00:00:00Z", 180,70),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-19T00:00:00Z", 190,10),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-20T00:00:00Z", 200,18),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-21T00:00:00Z", 210,8),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-22T00:00:00Z", 220,168),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-23T00:00:00Z", 5,2),
new GHRepositoryCloneTraffic.DailyInfo("2016-10-24T00:00:00Z", 45,5)
)
);
testTraffic(expectedResult);
}
@Test
public void testGetTrafficStatsAccessFailureDueToInsufficientPermissions() throws IOException {
String errorMsg = "Exception should be thrown, since we don't have permission to access repo traffic info.";
GitHub gitHub = GitHub.connect(login, null);
GHRepository repo = gitHub.getUser(login).getRepository(repositoryName);
try {
repo.getViewTraffic();
Assert.fail(errorMsg);
}
catch (HttpException ex){
}
try {
repo.getCloneTraffic();
Assert.fail(errorMsg);
}
catch (HttpException ex){
}
}
}