Skip to content

Commit 11bc669

Browse files
committed
update code for create repository from template
Signed-off-by: Yang Ting <bonnie.young@maxwit.com>
1 parent bb4d441 commit 11bc669

File tree

13 files changed

+53
-81
lines changed

13 files changed

+53
-81
lines changed

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

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class GHCreateRepositoryBuilder {
1414
private final GitHub root;
1515
protected final Requester builder;
16-
private final String apiUrlTail;
16+
private String apiUrlTail;
1717

1818
GHCreateRepositoryBuilder(GitHub root, String apiUrlTail, String name) {
1919
this.root = root;
@@ -22,22 +22,6 @@ public class GHCreateRepositoryBuilder {
2222
this.builder.with("name", name);
2323
}
2424

25-
GHCreateRepositoryBuilder(GitHub root, String apiUrlTail, String name, Boolean isTemplate) {
26-
this.root = root;
27-
this.apiUrlTail = apiUrlTail;
28-
this.builder = root.createRequest();
29-
this.builder.with("name", name);
30-
this.builder.with("is_template", isTemplate);
31-
}
32-
33-
GHCreateRepositoryBuilder(GitHub root, String apiUrlTail, String name, String owner) {
34-
this.root = root;
35-
this.apiUrlTail = apiUrlTail;
36-
this.builder = root.createRequest();
37-
this.builder.with("name", name);
38-
this.builder.with("owner", owner);
39-
}
40-
4125
/**
4226
* Description for repository
4327
*
@@ -219,25 +203,59 @@ public GHCreateRepositoryBuilder team(GHTeam team) {
219203
}
220204

221205
/**
222-
* Creates a repository with all the parameters.
206+
* Specifies whether the repository is a template.
223207
*
224-
* @return the gh repository
225-
* @throws IOException
226-
* if repsitory cannot be created
208+
* @param enabled
209+
* true if enabled
210+
* @return a builder to continue with building
227211
*/
228-
public GHRepository create() throws IOException {
229-
return builder.method("POST").withUrlPath(apiUrlTail).fetch(GHRepository.class).wrap(root);
212+
@Preview
213+
@Deprecated
214+
public GHCreateRepositoryBuilder templateRepository(boolean enabled) {
215+
this.builder.withPreview(BAPTISE);
216+
this.builder.with("is_template", enabled);
217+
return this;
218+
}
219+
220+
/**
221+
* Specifies the ownership of the repository.
222+
*
223+
* @param owner
224+
* organization or personage
225+
* @return a builder to continue with building
226+
*/
227+
public GHCreateRepositoryBuilder owner(String owner) {
228+
this.builder.with("owner", owner);
229+
return this;
230230
}
231231

232232
/**
233-
* Creates a repository with all the parameters, and with Preview BAPTISE for template repo.
233+
* Create repository from template repository.
234+
*
235+
* @param templateOwner
236+
* template repository owner
237+
* @param templateRepo
238+
* template repository
239+
* @return a builder to continue with building
240+
* @see <a href="https://developer.github.com/v3/previews/">GitHub API Previews</a>
241+
*/
242+
@Preview
243+
@Deprecated
244+
public GHCreateRepositoryBuilder fromTemplateRepository(String templateOwner, String templateRepo) {
245+
this.builder.withPreview(BAPTISE);
246+
this.apiUrlTail = "/repos/" + templateOwner + "/" + templateRepo + "/generate";
247+
return this;
248+
}
249+
250+
/**
251+
* Creates a repository with all the parameters.
234252
*
235253
* @return the gh repository
236254
* @throws IOException
237255
* if repsitory cannot be created
238256
*/
239-
public GHRepository createWithTemplate() throws IOException {
240-
return builder.method("POST").withPreview(BAPTISE).withUrlPath(apiUrlTail).fetch(GHRepository.class).wrap(root);
257+
public GHRepository create() throws IOException {
258+
return builder.method("POST").withUrlPath(apiUrlTail).fetch(GHRepository.class).wrap(root);
241259
}
242260

243261
}

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

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -100,50 +100,6 @@ public GHCreateRepositoryBuilder createRepository(String name) {
100100
return new GHCreateRepositoryBuilder(root, "/orgs/" + login + "/repos", name);
101101
}
102102

103-
/**
104-
* Starts a builder that creates a new repository.
105-
*
106-
* <p>
107-
* You use the returned builder to set various properties, then call {@link GHCreateRepositoryBuilder#create()} to
108-
* finally create a repository.
109-
*
110-
* @param name
111-
* the name
112-
* @param isTemplate
113-
* the is_template
114-
* @return the gh create repository builder
115-
*/
116-
public GHCreateRepositoryBuilder createRepositoryWithParametersIsTemplate(String name, Boolean isTemplate) {
117-
return new GHCreateRepositoryBuilder(root, "/orgs/" + login + "/repos", name, isTemplate);
118-
}
119-
120-
/**
121-
* Starts a builder that creates a new repository.
122-
*
123-
* <p>
124-
* You use the returned builder to set various properties, then call
125-
* {@link GHCreateRepositoryBuilder#createWithTemplate()} to finally create a repository.
126-
*
127-
* @param templateOwner
128-
* the owner of template repository
129-
* @param templateRepo
130-
* the template repository
131-
* @param name
132-
* the name of repository to be created
133-
* @param owner
134-
* the organization of repository to be created
135-
* @return the gh create repository builder
136-
*/
137-
public GHCreateRepositoryBuilder createRepositoryWithTemplate(String templateRepo,
138-
String templateOwner,
139-
String name,
140-
String owner) {
141-
return new GHCreateRepositoryBuilder(root,
142-
"/repos/" + templateOwner + "/" + templateRepo + "/generate",
143-
name,
144-
owner);
145-
}
146-
147103
/**
148104
* Teams by their names.
149105
*

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ public void testCreateRepositoryWithAutoInitialization() throws IOException {
5858
}
5959

6060
@Test
61-
public void testCreateRepositoryWithParametersIsTemplate() throws IOException {
62-
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEMPLATE_TEST);
61+
public void testCreateRepositoryWithParameterIsTemplate() throws IOException {
62+
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);
6363

6464
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
65-
GHRepository repository = org.createRepositoryWithParametersIsTemplate(GITHUB_API_TEMPLATE_TEST, true)
65+
GHRepository repository = org.createRepository(GITHUB_API_TEMPLATE_TEST)
6666
.description("a test template repository used to test kohsuke's github-api")
6767
.homepage("http://github-api.kohsuke.org/")
6868
.team(org.getTeamByName("Core Developers"))
6969
.autoInit(true)
70+
.templateRepository(true)
7071
.create();
71-
7272
Assert.assertNotNull(repository);
7373
Assert.assertNotNull(repository.getReadme());
7474
}
@@ -78,12 +78,10 @@ public void testCreateRepositoryWithTemplate() throws IOException {
7878
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);
7979

8080
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
81-
GHRepository repository = org
82-
.createRepositoryWithTemplate(GITHUB_API_TEMPLATE_TEST,
83-
GITHUB_API_TEST_ORG,
84-
GITHUB_API_TEST,
85-
GITHUB_API_TEST_ORG)
86-
.createWithTemplate();
81+
GHRepository repository = org.createRepository(GITHUB_API_TEST)
82+
.fromTemplateRepository(GITHUB_API_TEST_ORG, GITHUB_API_TEMPLATE_TEST)
83+
.owner(GITHUB_API_TEST_ORG)
84+
.create();
8785

8886
Assert.assertNotNull(repository);
8987
Assert.assertNotNull(repository.getReadme());
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"headers": {
1515
"Accept": {
16-
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
16+
"equalTo": "application/vnd.github.baptiste-preview+json"
1717
}
1818
}
1919
},

0 commit comments

Comments
 (0)