Skip to content

Commit

Permalink
Add Support for Retrieving Template Repository Information for a Repo… (
Browse files Browse the repository at this point in the history
#1817)

* Add Support for Retrieving Template Repository Information for a Repository

Spotless apply

Applied spotless

* doc: Improvement javadoc

* Resolve comments on pull-request
  • Loading branch information
jbenaventem authored Mar 18, 2024
1 parent 183ee8c commit 5001541
Show file tree
Hide file tree
Showing 34 changed files with 2,546 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main/java/org/kohsuke/github/GHCreateRepositoryBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kohsuke.github;

import java.io.IOException;
import java.util.Objects;

import static org.kohsuke.github.internal.Previews.BAPTISTE;

Expand Down Expand Up @@ -131,6 +132,23 @@ public GHCreateRepositoryBuilder fromTemplateRepository(String templateOwner, St
return this;
}

/**
* Create repository from template repository.
*
* @param templateRepository
* the template repository as a GHRepository
* @return a builder to continue with building
* @see <a href="https://developer.github.com/v3/previews/">GitHub API Previews</a>
*/
@Preview(BAPTISTE)
public GHCreateRepositoryBuilder fromTemplateRepository(GHRepository templateRepository) {
Objects.requireNonNull(templateRepository, "templateRepository cannot be null");
if (!templateRepository.isTemplate()) {
throw new IllegalArgumentException("The provided repository is not a template repository.");
}
return fromTemplateRepository(templateRepository.getOwnerName(), templateRepository.getName());
}

/**
* Creates a repository with all the parameters.
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public class GHRepository extends GHObject {

private String default_branch, language;

private GHRepository template_repository;

private Map<String, GHCommit> commits = Collections.synchronizedMap(new WeakHashMap<>());

@SkipFromToString
Expand Down Expand Up @@ -948,6 +950,16 @@ public String getMasterBranch() {
return default_branch;
}

/**
* Get Repository template was the repository created from.
*
* @return the repository template
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
public GHRepository getTemplateRepository() {
return (GHRepository) template_repository;
}

/**
* Gets size.
*
Expand Down
15 changes: 14 additions & 1 deletion src/test/java/org/kohsuke/github/GHContentIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.List;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;

// TODO: Auto-generated Javadoc
/**
Expand Down Expand Up @@ -75,6 +74,20 @@ public void testGetRepository() throws Exception {
assertThat(testRepo.getName(), equalTo(repo.getName()));
}

/**
* Test get repository created from a template repository
*
* @throws Exception
* the exception
*/
@Test
public void testGetRepositoryWithTemplateRepositoryInfo() throws Exception {
GHRepository testRepo = gitHub.getRepositoryById(repo.getId());
assertThat(testRepo.getTemplateRepository(), notNullValue());
assertThat(testRepo.getTemplateRepository().getOwnerName(), equalTo("octocat"));
assertThat(testRepo.getTemplateRepository().isTemplate(), equalTo(true));
}

/**
* Test get file content.
*
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/org/kohsuke/github/GHOrganizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.stream.Collectors;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThrows;

// TODO: Auto-generated Javadoc
/**
Expand Down Expand Up @@ -154,6 +155,66 @@ public void testCreateRepositoryWithTemplate() throws IOException {

}

/**
* Test create repository with template.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void testCreateRepositoryWithTemplateAndGHRepository() throws IOException {
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);

GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
GHRepository templateRepository = org.getRepository(GITHUB_API_TEMPLATE_TEST);

GHRepository repository = org.createRepository(GITHUB_API_TEST)
.fromTemplateRepository(templateRepository)
.owner(GITHUB_API_TEST_ORG)
.create();

assertThat(repository, notNullValue());
assertThat(repository.getReadme(), notNullValue());

}

/**
* Test create repository with template repository null.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void testCreateRepositoryFromTemplateRepositoryNull() throws IOException {
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);

GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
assertThrows(NullPointerException.class, () -> {
org.createRepository(GITHUB_API_TEST).fromTemplateRepository(null).owner(GITHUB_API_TEST_ORG).create();
});
}

/**
* Test create repository when repository template is not a template.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void testCreateRepositoryWhenRepositoryTemplateIsNotATemplate() throws IOException {
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);

GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
GHRepository templateRepository = org.getRepository(GITHUB_API_TEMPLATE_TEST);

assertThrows(IllegalArgumentException.class, () -> {
org.createRepository(GITHUB_API_TEST)
.fromTemplateRepository(templateRepository)
.owner(GITHUB_API_TEST_ORG)
.create();
});
}

/**
* Test invite user.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars.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": "[email protected]",
"hireable": null,
"bio": null,
"twitter_username": "bitwiseman",
"public_repos": 202,
"public_gists": 8,
"followers": 179,
"following": 11,
"created_at": "2012-07-11T20:38:33Z",
"updated_at": "2021-02-25T18:01:06Z",
"private_gists": 19,
"total_private_repos": 18,
"owned_private_repos": 0,
"disk_usage": 33700,
"collaborators": 0,
"two_factor_authentication": true,
"plan": {
"name": "free",
"space": 976562499,
"collaborators": 0,
"private_repos": 10000
}
}
Loading

0 comments on commit 5001541

Please sign in to comment.