Skip to content

Commit

Permalink
Merge pull request #923 from bitwiseman/feature/is-template
Browse files Browse the repository at this point in the history
Added GHRepository.isTemplate() method
  • Loading branch information
bitwiseman authored Aug 13, 2020
2 parents ff4324a + 906238a commit 94df5fc
Show file tree
Hide file tree
Showing 24 changed files with 667 additions and 184 deletions.
5 changes: 1 addition & 4 deletions src/main/java/org/kohsuke/github/GHPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ public List<GHRepository> next() {
*/
public GHRepository getRepository(String name) throws IOException {
try {
return root.createRequest()
.withUrlPath("/repos/" + login + '/' + name)
.fetch(GHRepository.class)
.wrap(root);
return GHRepository.read(root, login, name);
} catch (FileNotFoundException e) {
return null;
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/kohsuke/github/GHProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ public GHObject getOwner() throws IOException {
} else if (owner_url.contains("/users/")) {
owner = root.createRequest().withUrlPath(getOwnerUrl().getPath()).fetch(GHUser.class).wrapUp(root);
} else if (owner_url.contains("/repos/")) {
owner = root.createRequest()
.withUrlPath(getOwnerUrl().getPath())
.fetch(GHRepository.class)
.wrap(root);
String[] pathElements = getOwnerUrl().getPath().split("/");
owner = GHRepository.read(root, pathElements[1], pathElements[2]);
}
} catch (FileNotFoundException e) {
return null;
Expand Down
32 changes: 30 additions & 2 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public class GHRepository extends GHObject {

private GHRepository source, parent;

private Boolean isTemplate;

static GHRepository read(GitHub root, String owner, String name) throws IOException {
return root.createRequest().withUrlPath("/repos/" + owner + '/' + name).fetch(GHRepository.class).wrap(root);
}

/**
* Create deployment gh deployment builder.
*
Expand Down Expand Up @@ -692,6 +698,28 @@ public boolean isPrivate() {
return _private;
}

/**
* Is template boolean.
*
* @return the boolean
*/
@Deprecated
@Preview
public boolean isTemplate() {
// isTemplate is still in preview, we do not want to retrieve it unless needed.
if (isTemplate == null) {
try {
populate();
} catch (IOException e) {
// Convert this to a runtime exception to avoid messy method signature
throw new GHException("Could not populate the template setting of the repository", e);
}
// if this somehow is not populated, set it to false;
isTemplate = Boolean.TRUE.equals(isTemplate);
}
return isTemplate;
}

/**
* Has downloads boolean.
*
Expand Down Expand Up @@ -2855,10 +2883,10 @@ void populate() throws IOException {
// There is bug in Push event payloads that returns the wrong url.
// All other occurrences of "url" take the form "https://api.github.com/...".
// For Push event repository records, they take the form "https://github.com/{fullName}".
root.createRequest().setRawUrlPath(url.toString()).fetchInto(this).wrap(root);
root.createRequest().withPreview(BAPTISE).setRawUrlPath(url.toString()).fetchInto(this).wrap(root);
} catch (HttpException e) {
if (e.getCause() instanceof JsonParseException) {
root.createRequest().withUrlPath("/repos/" + full_name).fetchInto(this).wrap(root);
root.createRequest().withPreview(BAPTISE).withUrlPath("/repos/" + full_name).fetchInto(this).wrap(root);
} else {
throw e;
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,7 @@ public GHRepository getRepository(String name) throws IOException {
if (tokens.length < 2) {
throw new IllegalArgumentException("Repository name must be in format owner/repo");
}
return createRequest().withUrlPath("/repos/" + tokens[0] + '/' + tokens[1])
.fetch(GHRepository.class)
.wrap(this);
return GHRepository.read(this, tokens[0], tokens[1]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public <T> GitHubResponse<T> sendRequest(GitHubRequest request, @CheckForNull Gi
// Setting "Cache-Control" to "no-cache" stops the cache from supplying
// "If-Modified-Since" or "If-None-Match" values.
// This makes GitHub give us current data (not incorrectly cached data)
request = request.toBuilder().withHeader("Cache-Control", "no-cache").build();
request = request.toBuilder().setHeader("Cache-Control", "no-cache").build();
continue;
}
if (!(isRateLimitResponse(responseInfo) || isAbuseLimitResponse(responseInfo))) {
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/kohsuke/github/GitHubRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,11 @@ public B withApiUrl(String url) {
* the name
* @param value
* the value
* @return the request builder
*/
public void setHeader(String name, String value) {
public B setHeader(String name, String value) {
headers.put(name, value);
return (B) this;
}

/**
Expand All @@ -399,8 +401,11 @@ public void setHeader(String name, String value) {
* @return the request builder
*/
public B withHeader(String name, String value) {
setHeader(name, value);
return (B) this;
String oldValue = headers.get(name);
if (!StringUtils.isBlank(oldValue)) {
value = oldValue + ", " + value;
}
return setHeader(name, value);
}

/**
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/org/kohsuke/github/Previews.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class Previews {
*/
static final String ANTIOPE = "application/vnd.github.antiope-preview+json";

/**
* Create repository from template repository
*
* @see <a href="https://developer.github.com/v3/previews/#create-and-use-repository-templates">GitHub API
* Previews</a>
*/
static final String BAPTISE = "application/vnd.github.baptiste-preview+json";

/**
* Commit Search
*
Expand Down Expand Up @@ -58,6 +66,14 @@ class Previews {
*/
static final String MERCY = "application/vnd.github.mercy-preview+json";

/**
* New visibility parameter for the Repositories API
*
* @see <a href="https://developer.github.com/v3/previews/#new-visibility-parameter-for-the-repositories-api">GitHub
* API Previews</a>
*/
static final String NEBULA = "application/vnd.github.nebula-preview+json";

/**
* Draft pull requests
*
Expand All @@ -79,11 +95,4 @@ class Previews {
*/
static final String ZZZAX = "application/vnd.github.zzzax-preview+json";

/**
* Create repository from template repository
*
* @see <a href="https://developer.github.com/v3/previews/#create-and-use-repository-templates">GitHub API
* Previews</a>
*/
static final String BAPTISE = "application/vnd.github.baptiste-preview+json";
}
28 changes: 26 additions & 2 deletions src/test/java/org/kohsuke/github/GHOrganizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.io.IOException;
import java.util.List;

import static org.hamcrest.Matchers.*;

public class GHOrganizationTest extends AbstractGitHubWireMockTest {

public static final String GITHUB_API_TEST = "github-api-test";
Expand Down Expand Up @@ -59,18 +61,39 @@ public void testCreateRepositoryWithAutoInitialization() throws IOException {

@Test
public void testCreateRepositoryWithParameterIsTemplate() throws IOException {
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEMPLATE_TEST);

GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
GHTeam team = org.getTeamByName("Core Developers");

int requestCount = mockGitHub.getRequestCount();
GHRepository repository = org.createRepository(GITHUB_API_TEMPLATE_TEST)
.description("a test template repository used to test kohsuke's github-api")
.homepage("http://github-api.kohsuke.org/")
.team(org.getTeamByName("Core Developers"))
.team(team)
.autoInit(true)
.templateRepository(true)
.create();
Assert.assertNotNull(repository);
assertThat(mockGitHub.getRequestCount(), equalTo(requestCount + 1));

Assert.assertNotNull(repository.getReadme());
assertThat(mockGitHub.getRequestCount(), equalTo(requestCount + 2));

// isTemplate() does not call populate() from create
assertThat(repository.isTemplate(), equalTo(true));
assertThat(mockGitHub.getRequestCount(), equalTo(requestCount + 2));

repository = org.getRepository(GITHUB_API_TEMPLATE_TEST);

// first isTemplate() calls populate()
assertThat(repository.isTemplate(), equalTo(true));
assertThat(mockGitHub.getRequestCount(), equalTo(requestCount + 4));

// second isTemplate() does not call populate()
assertThat(repository.isTemplate(), equalTo(true));
assertThat(mockGitHub.getRequestCount(), equalTo(requestCount + 4));

}

@Test
Expand All @@ -85,6 +108,7 @@ public void testCreateRepositoryWithTemplate() throws IOException {

Assert.assertNotNull(repository);
Assert.assertNotNull(repository.getReadme());

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"url": "https://api.github.com/licenses/mit",
"node_id": "MDc6TGljZW5zZTEz"
},
"is_template": false,
"forks": 0,
"open_issues": 5,
"watchers": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"method": "GET",
"headers": {
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
"equalTo": "application/vnd.github.baptiste-preview+json"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"description": null,
"description": "Hub4j Test Org Description (this could be null or blank too)",
"name": "Hub4j Test Org Name (this could be null or blank too)",
"company": null,
"blog": "https://hub4j.url.io/could/be/null",
"location": "Hub4j Test Org Location (this could be null or blank too)",
"email": "[email protected]",
"twitter_username": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 9,
"public_repos": 12,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/hub4j-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2015-04-20T00:42:30Z",
"updated_at": "2020-06-04T05:56:10Z",
"type": "Organization",
"total_private_repos": 0,
"owned_private_repos": 0,
"private_gists": 0,
"disk_usage": 132,
"disk_usage": 148,
"collaborators": 0,
"billing_email": "[email protected]",
"default_repository_permission": "none",
Expand All @@ -34,8 +40,8 @@
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 0,
"filled_seats": 3,
"seats": 0
"private_repos": 10000,
"filled_seats": 18,
"seats": 3
}
}
Loading

0 comments on commit 94df5fc

Please sign in to comment.