Skip to content

Commit

Permalink
Merge branch 'master' into getKeysForUser
Browse files Browse the repository at this point in the history
  • Loading branch information
Arngrimur Bjarnason committed Sep 27, 2019
2 parents a1a9153 + e740f52 commit 55f9c40
Show file tree
Hide file tree
Showing 307 changed files with 517 additions and 28 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/kohsuke/github/GHContentWithLicense.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @see <a href="https://developer.github.com/v3/licenses/#get-a-repositorys-license">documentation</a>
* @see GHRepository#getLicense()
*/
@Preview @Deprecated
class GHContentWithLicense extends GHContent {
GHLicense license;

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/kohsuke/github/GHLicense.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@
import java.util.ArrayList;
import java.util.List;

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

/**
* The GitHub Preview API's license information
* <p>
* WARNING: This uses a PREVIEW API - subject to change.
*
* @author Duncan Dickinson
* @see GitHub#getLicense(String)
* @see GHRepository#getLicense()
* @see <a href="https://developer.github.com/v3/licenses/">https://developer.github.com/v3/licenses/</a>
*/
@Preview @Deprecated
@SuppressWarnings({"UnusedDeclaration"})
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
"NP_UNWRITTEN_FIELD"}, justification = "JSON API")
Expand Down Expand Up @@ -144,7 +140,7 @@ public String getBody() throws IOException {
protected synchronized void populate() throws IOException {
if (description!=null) return; // already populated

root.retrieve().withPreview(DRAX).to(url, this);
root.retrieve().to(url, this);
}

@Override
Expand Down
52 changes: 45 additions & 7 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -51,6 +53,8 @@
import java.util.WeakHashMap;

import static java.util.Arrays.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.kohsuke.github.Previews.*;

/**
Expand Down Expand Up @@ -631,6 +635,29 @@ public void delete() throws IOException {
}
}

/**
* Will archive and this repository as read-only. When a repository is archived, any operation
* that can change its state is forbidden. This applies symmetrically if trying to unarchive it.
*
* <p>When you try to do any operation that modifies a read-only repository, it returns the
* response:
*
* <pre>
* org.kohsuke.github.HttpException: {
* "message":"Repository was archived so is read-only.",
* "documentation_url":"https://developer.github.com/v3/repos/#edit"
* }
* </pre>
*
* @throws IOException In case of any networking error or error from the server.
*/
public void archive() throws IOException {
edit("archived", "true");
// Generall would not update this record,
// but do so here since this will result in any other update actions failing
archived = true;
}

/**
* Sort orders for listing forks
*/
Expand Down Expand Up @@ -1049,12 +1076,10 @@ protected void wrapUp(GHCommitComment[] page) {
/**
* Gets the basic license details for the repository.
* <p>
* This is a preview item and subject to change.
*
* @throws IOException as usual but also if you don't use the preview connector
* @return null if there's no license.
*/
@Preview @Deprecated
public GHLicense getLicense() throws IOException{
GHContentWithLicense lic = getLicenseContent_();
return lic!=null ? lic.license : null;
Expand All @@ -1063,21 +1088,17 @@ public GHLicense getLicense() throws IOException{
/**
* Retrieves the contents of the repository's license file - makes an additional API call
* <p>
* This is a preview item and subject to change.
*
* @return details regarding the license contents, or null if there's no license.
* @throws IOException as usual but also if you don't use the preview connector
*/
@Preview @Deprecated
public GHContent getLicenseContent() throws IOException {
return getLicenseContent_();
}

@Preview @Deprecated
private GHContentWithLicense getLicenseContent_() throws IOException {
try {
return root.retrieve()
.withPreview(DRAX)
.to(getApiTailUrl("license"), GHContentWithLicense.class).wrap(this);
} catch (FileNotFoundException e) {
return null;
Expand Down Expand Up @@ -1376,8 +1397,25 @@ public Map<String,GHBranch> getBranches() throws IOException {
return r;
}

/**
* Replace special characters (e.g. #) with standard values (e.g. %23) so
* GitHub understands what is being requested.
* @param The string to be encoded.
* @return The encoded string.
*/
private String UrlEncode(String value) {
try {
return URLEncoder.encode(value, org.apache.commons.codec.CharEncoding.UTF_8);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(GHRepository.class.getName()).log(Level.SEVERE, null, ex);
}

// Something went wrong - just return original value as is.
return value;
}

public GHBranch getBranch(String name) throws IOException {
return root.retrieve().to(getApiTailUrl("branches/"+name),GHBranch.class).wrap(this);
return root.retrieve().to(getApiTailUrl("branches/"+UrlEncode(name)),GHBranch.class).wrap(this);
}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,10 @@ public GHRepository getRepositoryById(String id) throws IOException {
*
* @return a list of popular open source licenses
*/
@Preview @Deprecated
public PagedIterable<GHLicense> listLicenses() throws IOException {
return new PagedIterable<GHLicense>() {
public PagedIterator<GHLicense> _iterator(int pageSize) {
return new PagedIterator<GHLicense>(retrieve().withPreview(DRAX).asIterator("/licenses", GHLicense[].class, pageSize)) {
return new PagedIterator<GHLicense>(retrieve().asIterator("/licenses", GHLicense[].class, pageSize)) {
@Override
protected void wrapUp(GHLicense[] page) {
for (GHLicense c : page)
Expand Down Expand Up @@ -523,15 +522,12 @@ protected void wrapUp(GHUser[] page) {
/**
* Returns the full details for a license
*
* WARNING: This uses a PREVIEW API.
*
* @param key The license key provided from the API
* @return The license details
* @see GHLicense#getKey()
*/
@Preview @Deprecated
public GHLicense getLicense(String key) throws IOException {
return retrieve().withPreview(DRAX).to("/licenses/" + key, GHLicense.class);
return retrieve().to("/licenses/" + key, GHLicense.class);
}

/**
Expand Down
31 changes: 29 additions & 2 deletions src/main/java/org/kohsuke/github/Previews.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
package org.kohsuke.github;

/**
* Provides the media type strings for GitHub API previews
*
* https://developer.github.com/v3/previews/
*
* @author Kohsuke Kawaguchi
*/
/*package*/ class Previews {

/**
* Require multiple approving reviews
*
* @see <a href="https://developer.github.com/v3/previews/#require-multiple-approving-reviews">GitHub API Previews</a>
*/
static final String LUKE_CAGE = "application/vnd.github.luke-cage-preview+json";
static final String DRAX = "application/vnd.github.drax-preview+json";

/**
* Reactions
*
* @see <a href="https://developer.github.com/v3/previews/#reactions">GitHub API Previews</a>
*/
static final String SQUIRREL_GIRL = "application/vnd.github.squirrel-girl-preview";
static final String CLOAK = "application/vnd.github.cloak-preview";

/**
* Commit Search
*
* @see <a href="https://developer.github.com/v3/previews/#commit-search">GitHub API Previews</a>
*/
static final String CLOAK = "application/vnd.github.cloak-preview+json";

/**
* Require signed commits
*
* @see <a href="https://developer.github.com/v3/previews/#require-signed-commits">GitHub API Previews</a>
*/
static final String ZZZAX = "application/vnd.github.zzzax-preview+json";
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Properties;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assume.assumeFalse;

/**
* @author Liam Newman
Expand All @@ -29,8 +30,10 @@ public abstract class AbstractGitHubApiWireMockTest extends Assert {

private final GitHubBuilder githubBuilder = createGitHubBuilder();

public final static String STUBBED_USER_LOGIN = "placeholder-user";
public final static String STUBBED_USER_PASSWORD = "placeholder-password";
final static String GITHUB_API_TEST_ORG = "github-api-test-org";

final static String STUBBED_USER_LOGIN = "placeholder-user";
final static String STUBBED_USER_PASSWORD = "placeholder-password";

/**
* {@link GitHub} instance for use during test.
Expand Down Expand Up @@ -109,4 +112,9 @@ public void wireMockSetup() throws Exception {
gitHubBeforeAfter = null;
}
}

protected void snapshotNotAllowed() {
assumeFalse("Test contains hand written mappings. Only valid when not taking a snapshot.", githubApi.isTakeSnapshot());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* @author Kohsuke Kawaguchi
*/
public class PullRequestTest extends AbstractGitHubApiWireMockTest {
public class GHPullRequestTest extends AbstractGitHubApiWireMockTest {

@Before
@After
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.kohsuke.github;

import org.junit.Test;

import java.io.IOException;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeFalse;

/**
* @author Liam Newman
*/
public class GHRepositoryTest extends AbstractGitHubApiWireMockTest {

@Test
public void archive() throws Exception {
snapshotNotAllowed();

// Archive is a one-way action in the API.
// We do thi this one
GHRepository repo = getRepository();

assertThat(repo.isArchived(), is(false));

repo.archive();

assertThat(repo.isArchived(), is(true));
assertThat(getRepository().isArchived(), is(true));
}

@Test
public void getBranch_URLEncoded() throws Exception {
GHRepository repo = getRepository();
GHBranch branch = repo.getBranch("test/#UrlEncode");
assertThat(branch.getName(), is("test/#UrlEncode"));
}



protected GHRepository getRepository() throws IOException {
return getRepository(gitHub);
}

private GHRepository getRepository(GitHub gitHub) throws IOException {
return gitHub.getOrganization("github-api-test-org").getRepository("github-api");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class WireMockStatusReporterTest extends AbstractGitHubApiWireMockTest {

@Test
public void user_whenProxying_AuthCorrectlyConfigured() throws Exception {
assumeFalse("Test only valid when not taking a snapshot", githubApi.isTakeSnapshot());
snapshotNotAllowed();
assumeTrue("Test only valid when proxying (-Dtest.github.useProxy to enable)", githubApi.isUseProxy());

assertThat(
Expand All @@ -42,7 +42,7 @@ public void user_whenProxying_AuthCorrectlyConfigured() throws Exception {

@Test
public void user_whenNotProxying_Stubbed() throws Exception {
assumeFalse("Test only valid when not taking a snapshot", githubApi.isTakeSnapshot());
snapshotNotAllowed();
assumeFalse("Test only valid when not proxying", githubApi.isUseProxy());

assertThat(gitHub.isAnonymous(), is(false));
Expand All @@ -59,7 +59,7 @@ public void user_whenNotProxying_Stubbed() throws Exception {

@Test
public void BasicBehaviors_whenNotProxying() throws Exception {
assumeFalse("Test only valid when not taking a snapshot", githubApi.isTakeSnapshot());
snapshotNotAllowed();
assumeFalse("Test only valid when not proxying", githubApi.isUseProxy());

Exception e = null;
Expand Down Expand Up @@ -96,7 +96,7 @@ public void BasicBehaviors_whenNotProxying() throws Exception {

@Test
public void BasicBehaviors_whenProxying() throws Exception {
assumeFalse("Test only valid when not taking a snapshot", githubApi.isTakeSnapshot());
snapshotNotAllowed();
assumeTrue("Test only valid when proxying (-Dtest.github.useProxy to enable)", githubApi.isUseProxy());
Exception e = null;
GHRepository repo = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"login":"github-api-test-org","id":7544739,"node_id":"MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=","url":"https://api.github.com/orgs/github-api-test-org","repos_url":"https://api.github.com/orgs/github-api-test-org/repos","events_url":"https://api.github.com/orgs/github-api-test-org/events","hooks_url":"https://api.github.com/orgs/github-api-test-org/hooks","issues_url":"https://api.github.com/orgs/github-api-test-org/issues","members_url":"https://api.github.com/orgs/github-api-test-org/members{/member}","public_members_url":"https://api.github.com/orgs/github-api-test-org/public_members{/member}","avatar_url":"https://avatars3.githubusercontent.com/u/7544739?v=4","description":null,"is_verified":false,"has_organization_projects":true,"has_repository_projects":true,"public_repos":9,"public_gists":0,"followers":0,"following":0,"html_url":"https://github.com/github-api-test-org","created_at":"2014-05-10T19:39:11Z","updated_at":"2015-04-20T00:42:30Z","type":"Organization","total_private_repos":0,"owned_private_repos":0,"private_gists":0,"disk_usage":132,"collaborators":0,"billing_email":"kk@kohsuke.org","default_repository_permission":"none","members_can_create_repositories":false,"two_factor_requirement_enabled":false,"members_allowed_repository_creation_type":"none","plan":{"name":"free","space":976562499,"private_repos":0,"filled_seats":3,"seats":0}}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"login":"bitwiseman","id":1958953,"node_id":"MDQ6VXNlcjE5NTg5NTM=","avatar_url":"https://avatars3.githubusercontent.com/u/1958953?v=4","gravatar_id":"","url":"https://api.github.com/users/bitwiseman","html_url":"https://github.com/bitwiseman","followers_url":"https://api.github.com/users/bitwiseman/followers","following_url":"https://api.github.com/users/bitwiseman/following{/other_user}","gists_url":"https://api.github.com/users/bitwiseman/gists{/gist_id}","starred_url":"https://api.github.com/users/bitwiseman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitwiseman/subscriptions","organizations_url":"https://api.github.com/users/bitwiseman/orgs","repos_url":"https://api.github.com/users/bitwiseman/repos","events_url":"https://api.github.com/users/bitwiseman/events{/privacy}","received_events_url":"https://api.github.com/users/bitwiseman/received_events","type":"User","site_admin":false,"name":"Liam Newman","company":"Cloudbees, Inc.","blog":"","location":"Seattle, WA, USA","email":"bitwiseman@gmail.com","hireable":null,"bio":"https://twitter.com/bitwiseman","public_repos":166,"public_gists":4,"followers":135,"following":9,"created_at":"2012-07-11T20:38:33Z","updated_at":"2019-09-24T19:32:29Z"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"id" : "cb173af2-c793-444a-acdf-c3850e7afdbe",
"name" : "orgs_github-api-test-org",
"request" : {
"url" : "/orgs/github-api-test-org",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "orgs_github-api-test-org-cb173af2-c793-444a-acdf-c3850e7afdbe.json",
"headers" : {
"Date" : "Wed, 25 Sep 2019 23:35:58 GMT",
"Content-Type" : "application/json; charset=utf-8",
"Server" : "github.com",
"Status" : "200 OK",
"X-RateLimit-Limit" : "5000",
"X-RateLimit-Remaining" : "4989",
"X-RateLimit-Reset" : "1569457884",
"Cache-Control" : "private, max-age=60, s-maxage=60",
"Vary" : [ "Accept, Authorization, Cookie, X-GitHub-OTP", "Accept-Encoding" ],
"ETag" : "W/\"b7989d48e6539c9c76038995b902421b\"",
"Last-Modified" : "Mon, 20 Apr 2015 00:42:30 GMT",
"X-OAuth-Scopes" : "gist, notifications, read:org, read:public_key, read:repo_hook, repo",
"X-Accepted-OAuth-Scopes" : "admin:org, read:org, repo, user, write:org",
"X-GitHub-Media-Type" : "unknown, github.v3",
"Access-Control-Expose-Headers" : "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"Access-Control-Allow-Origin" : "*",
"Strict-Transport-Security" : "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options" : "deny",
"X-Content-Type-Options" : "nosniff",
"X-XSS-Protection" : "1; mode=block",
"Referrer-Policy" : "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy" : "default-src 'none'",
"X-GitHub-Request-Id" : "F855:59E1:1397ED7:171400E:5D8BF9DE"
}
},
"uuid" : "cb173af2-c793-444a-acdf-c3850e7afdbe",
"persistent" : true,
"insertionIndex" : 2
}
Loading

0 comments on commit 55f9c40

Please sign in to comment.