Skip to content

Commit 5001541

Browse files
authored
Add Support for Retrieving Template Repository Information for a Repo… (#1817)
* Add Support for Retrieving Template Repository Information for a Repository Spotless apply Applied spotless * doc: Improvement javadoc * Resolve comments on pull-request
1 parent 183ee8c commit 5001541

File tree

34 files changed

+2546
-1
lines changed

34 files changed

+2546
-1
lines changed

src/main/java/org/kohsuke/github/GHCreateRepositoryBuilder.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.kohsuke.github;
22

33
import java.io.IOException;
4+
import java.util.Objects;
45

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

@@ -131,6 +132,23 @@ public GHCreateRepositoryBuilder fromTemplateRepository(String templateOwner, St
131132
return this;
132133
}
133134

135+
/**
136+
* Create repository from template repository.
137+
*
138+
* @param templateRepository
139+
* the template repository as a GHRepository
140+
* @return a builder to continue with building
141+
* @see <a href="https://developer.github.com/v3/previews/">GitHub API Previews</a>
142+
*/
143+
@Preview(BAPTISTE)
144+
public GHCreateRepositoryBuilder fromTemplateRepository(GHRepository templateRepository) {
145+
Objects.requireNonNull(templateRepository, "templateRepository cannot be null");
146+
if (!templateRepository.isTemplate()) {
147+
throw new IllegalArgumentException("The provided repository is not a template repository.");
148+
}
149+
return fromTemplateRepository(templateRepository.getOwnerName(), templateRepository.getName());
150+
}
151+
134152
/**
135153
* Creates a repository with all the parameters.
136154
*

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public class GHRepository extends GHObject {
120120

121121
private String default_branch, language;
122122

123+
private GHRepository template_repository;
124+
123125
private Map<String, GHCommit> commits = Collections.synchronizedMap(new WeakHashMap<>());
124126

125127
@SkipFromToString
@@ -948,6 +950,16 @@ public String getMasterBranch() {
948950
return default_branch;
949951
}
950952

953+
/**
954+
* Get Repository template was the repository created from.
955+
*
956+
* @return the repository template
957+
*/
958+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
959+
public GHRepository getTemplateRepository() {
960+
return (GHRepository) template_repository;
961+
}
962+
951963
/**
952964
* Gets size.
953965
*

src/test/java/org/kohsuke/github/GHContentIntegrationTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.List;
1616

1717
import static org.hamcrest.Matchers.*;
18-
import static org.hamcrest.Matchers.equalTo;
1918

2019
// TODO: Auto-generated Javadoc
2120
/**
@@ -75,6 +74,20 @@ public void testGetRepository() throws Exception {
7574
assertThat(testRepo.getName(), equalTo(repo.getName()));
7675
}
7776

77+
/**
78+
* Test get repository created from a template repository
79+
*
80+
* @throws Exception
81+
* the exception
82+
*/
83+
@Test
84+
public void testGetRepositoryWithTemplateRepositoryInfo() throws Exception {
85+
GHRepository testRepo = gitHub.getRepositoryById(repo.getId());
86+
assertThat(testRepo.getTemplateRepository(), notNullValue());
87+
assertThat(testRepo.getTemplateRepository().getOwnerName(), equalTo("octocat"));
88+
assertThat(testRepo.getTemplateRepository().isTemplate(), equalTo(true));
89+
}
90+
7891
/**
7992
* Test get file content.
8093
*

src/test/java/org/kohsuke/github/GHOrganizationTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.stream.Collectors;
1212

1313
import static org.hamcrest.Matchers.*;
14+
import static org.junit.Assert.assertThrows;
1415

1516
// TODO: Auto-generated Javadoc
1617
/**
@@ -154,6 +155,66 @@ public void testCreateRepositoryWithTemplate() throws IOException {
154155

155156
}
156157

158+
/**
159+
* Test create repository with template.
160+
*
161+
* @throws IOException
162+
* Signals that an I/O exception has occurred.
163+
*/
164+
@Test
165+
public void testCreateRepositoryWithTemplateAndGHRepository() throws IOException {
166+
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);
167+
168+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
169+
GHRepository templateRepository = org.getRepository(GITHUB_API_TEMPLATE_TEST);
170+
171+
GHRepository repository = org.createRepository(GITHUB_API_TEST)
172+
.fromTemplateRepository(templateRepository)
173+
.owner(GITHUB_API_TEST_ORG)
174+
.create();
175+
176+
assertThat(repository, notNullValue());
177+
assertThat(repository.getReadme(), notNullValue());
178+
179+
}
180+
181+
/**
182+
* Test create repository with template repository null.
183+
*
184+
* @throws IOException
185+
* Signals that an I/O exception has occurred.
186+
*/
187+
@Test
188+
public void testCreateRepositoryFromTemplateRepositoryNull() throws IOException {
189+
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);
190+
191+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
192+
assertThrows(NullPointerException.class, () -> {
193+
org.createRepository(GITHUB_API_TEST).fromTemplateRepository(null).owner(GITHUB_API_TEST_ORG).create();
194+
});
195+
}
196+
197+
/**
198+
* Test create repository when repository template is not a template.
199+
*
200+
* @throws IOException
201+
* Signals that an I/O exception has occurred.
202+
*/
203+
@Test
204+
public void testCreateRepositoryWhenRepositoryTemplateIsNotATemplate() throws IOException {
205+
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);
206+
207+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
208+
GHRepository templateRepository = org.getRepository(GITHUB_API_TEMPLATE_TEST);
209+
210+
assertThrows(IllegalArgumentException.class, () -> {
211+
org.createRepository(GITHUB_API_TEST)
212+
.fromTemplateRepository(templateRepository)
213+
.owner(GITHUB_API_TEST_ORG)
214+
.create();
215+
});
216+
}
217+
157218
/**
158219
* Test invite user.
159220
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"login": "bitwiseman",
3+
"id": 1958953,
4+
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/1958953?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/bitwiseman",
8+
"html_url": "https://github.com/bitwiseman",
9+
"followers_url": "https://api.github.com/users/bitwiseman/followers",
10+
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
14+
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
15+
"repos_url": "https://api.github.com/users/bitwiseman/repos",
16+
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Liam Newman",
21+
"company": "Cloudbees, Inc.",
22+
"blog": "",
23+
"location": "Seattle, WA, USA",
24+
"email": "[email protected]",
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": "bitwiseman",
28+
"public_repos": 202,
29+
"public_gists": 8,
30+
"followers": 179,
31+
"following": 11,
32+
"created_at": "2012-07-11T20:38:33Z",
33+
"updated_at": "2021-02-25T18:01:06Z",
34+
"private_gists": 19,
35+
"total_private_repos": 18,
36+
"owned_private_repos": 0,
37+
"disk_usage": 33700,
38+
"collaborators": 0,
39+
"two_factor_authentication": true,
40+
"plan": {
41+
"name": "free",
42+
"space": 976562499,
43+
"collaborators": 0,
44+
"private_repos": 10000
45+
}
46+
}

0 commit comments

Comments
 (0)