Skip to content

Commit ff4324a

Browse files
authored
Merge pull request #897 from bonnie-young/add-create-repo-with-template-support
add create repo with template support
2 parents dcf26d5 + 11bc669 commit ff4324a

23 files changed

+1091
-1
lines changed

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.IOException;
44
import java.net.URL;
55

6+
import static org.kohsuke.github.Previews.BAPTISE;
7+
68
/**
79
* Creates a repository
810
*
@@ -11,7 +13,7 @@
1113
public class GHCreateRepositoryBuilder {
1214
private final GitHub root;
1315
protected final Requester builder;
14-
private final String apiUrlTail;
16+
private String apiUrlTail;
1517

1618
GHCreateRepositoryBuilder(GitHub root, String apiUrlTail, String name) {
1719
this.root = root;
@@ -200,6 +202,51 @@ public GHCreateRepositoryBuilder team(GHTeam team) {
200202
return this;
201203
}
202204

205+
/**
206+
* Specifies whether the repository is a template.
207+
*
208+
* @param enabled
209+
* true if enabled
210+
* @return a builder to continue with building
211+
*/
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;
230+
}
231+
232+
/**
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+
203250
/**
204251
* Creates a repository with all the parameters.
205252
*

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ class Previews {
7878
* @see <a href="https://developer.github.com/v3/previews/#require-signed-commits">GitHub API Previews</a>
7979
*/
8080
static final String ZZZAX = "application/vnd.github.zzzax-preview+json";
81+
82+
/**
83+
* Create repository from template repository
84+
*
85+
* @see <a href="https://developer.github.com/v3/previews/#create-and-use-repository-templates">GitHub API
86+
* Previews</a>
87+
*/
88+
static final String BAPTISE = "application/vnd.github.baptiste-preview+json";
8189
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public class GHOrganizationTest extends AbstractGitHubWireMockTest {
1313

1414
public static final String GITHUB_API_TEST = "github-api-test";
15+
public static final String GITHUB_API_TEMPLATE_TEST = "github-api-template-test";
1516
public static final String TEAM_NAME_CREATE = "create-team-test";
1617

1718
@Before
@@ -56,6 +57,36 @@ public void testCreateRepositoryWithAutoInitialization() throws IOException {
5657
Assert.assertNotNull(repository.getReadme());
5758
}
5859

60+
@Test
61+
public void testCreateRepositoryWithParameterIsTemplate() throws IOException {
62+
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);
63+
64+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
65+
GHRepository repository = org.createRepository(GITHUB_API_TEMPLATE_TEST)
66+
.description("a test template repository used to test kohsuke's github-api")
67+
.homepage("http://github-api.kohsuke.org/")
68+
.team(org.getTeamByName("Core Developers"))
69+
.autoInit(true)
70+
.templateRepository(true)
71+
.create();
72+
Assert.assertNotNull(repository);
73+
Assert.assertNotNull(repository.getReadme());
74+
}
75+
76+
@Test
77+
public void testCreateRepositoryWithTemplate() throws IOException {
78+
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);
79+
80+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
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();
85+
86+
Assert.assertNotNull(repository);
87+
Assert.assertNotNull(repository.getReadme());
88+
}
89+
5990
@Test
6091
public void testInviteUser() throws IOException {
6192
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"login": "hub4j-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/hub4j-test-org",
6+
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
13+
"description": null,
14+
"is_verified": false,
15+
"has_organization_projects": true,
16+
"has_repository_projects": true,
17+
"public_repos": 9,
18+
"public_gists": 0,
19+
"followers": 0,
20+
"following": 0,
21+
"html_url": "https://github.com/hub4j-test-org",
22+
"created_at": "2014-05-10T19:39:11Z",
23+
"updated_at": "2015-04-20T00:42:30Z",
24+
"type": "Organization",
25+
"total_private_repos": 0,
26+
"owned_private_repos": 0,
27+
"private_gists": 0,
28+
"disk_usage": 132,
29+
"collaborators": 0,
30+
"billing_email": "kk@kohsuke.org",
31+
"default_repository_permission": "none",
32+
"members_can_create_repositories": false,
33+
"two_factor_requirement_enabled": false,
34+
"plan": {
35+
"name": "free",
36+
"space": 976562499,
37+
"private_repos": 0,
38+
"filled_seats": 3,
39+
"seats": 0
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{
2+
"id": 212682270,
3+
"node_id": "MDEwOlJlcG9zaXRvcnkyMTI2ODIyNzA=",
4+
"name": "github-api-template-test",
5+
"full_name": "hub4j-test-org/github-api-template-test",
6+
"private": false,
7+
"owner": {
8+
"login": "hub4j-test-org",
9+
"id": 7544739,
10+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
11+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
12+
"gravatar_id": "",
13+
"url": "http://localhost:51951/users/hub4j-test-org",
14+
"html_url": "https://github.com/hub4j-test-org",
15+
"followers_url": "http://localhost:51951/users/hub4j-test-org/followers",
16+
"following_url": "http://localhost:51951/users/hub4j-test-org/following{/other_user}",
17+
"gists_url": "http://localhost:51951/users/hub4j-test-org/gists{/gist_id}",
18+
"starred_url": "http://localhost:51951/users/hub4j-test-org/starred{/owner}{/repo}",
19+
"subscriptions_url": "http://localhost:51951/users/hub4j-test-org/subscriptions",
20+
"organizations_url": "http://localhost:51951/users/hub4j-test-org/orgs",
21+
"repos_url": "http://localhost:51951/users/hub4j-test-org/repos",
22+
"events_url": "http://localhost:51951/users/hub4j-test-org/events{/privacy}",
23+
"received_events_url": "http://localhost:51951/users/hub4j-test-org/received_events",
24+
"type": "Organization",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/hub4j-test-org/github-api-template-test",
28+
"description": "a test repository used to test kohsuke's github-api",
29+
"fork": false,
30+
"url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test",
31+
"forks_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/forks",
32+
"keys_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/keys{/key_id}",
33+
"collaborators_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/collaborators{/collaborator}",
34+
"teams_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/teams",
35+
"hooks_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/hooks",
36+
"issue_events_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/issues/events{/number}",
37+
"events_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/events",
38+
"assignees_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/assignees{/user}",
39+
"branches_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/branches{/branch}",
40+
"tags_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/tags",
41+
"blobs_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/git/blobs{/sha}",
42+
"git_tags_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/git/tags{/sha}",
43+
"git_refs_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/git/refs{/sha}",
44+
"trees_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/git/trees{/sha}",
45+
"statuses_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/statuses/{sha}",
46+
"languages_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/languages",
47+
"stargazers_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/stargazers",
48+
"contributors_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/contributors",
49+
"subscribers_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/subscribers",
50+
"subscription_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/subscription",
51+
"commits_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/commits{/sha}",
52+
"git_commits_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/git/commits{/sha}",
53+
"comments_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/comments{/number}",
54+
"issue_comment_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/issues/comments{/number}",
55+
"contents_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/contents/{+path}",
56+
"compare_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/compare/{base}...{head}",
57+
"merges_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/merges",
58+
"archive_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/{archive_format}{/ref}",
59+
"downloads_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/downloads",
60+
"issues_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/issues{/number}",
61+
"pulls_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/pulls{/number}",
62+
"milestones_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/milestones{/number}",
63+
"notifications_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/notifications{?since,all,participating}",
64+
"labels_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/labels{/name}",
65+
"releases_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/releases{/id}",
66+
"deployments_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/deployments",
67+
"created_at": "2019-10-03T21:18:43Z",
68+
"updated_at": "2019-10-03T21:18:43Z",
69+
"pushed_at": "2019-10-03T21:18:44Z",
70+
"git_url": "git://github.com/hub4j-test-org/github-api-template-test.git",
71+
"ssh_url": "git@github.com:hub4j-test-org/github-api-template-test.git",
72+
"clone_url": "https://github.com/hub4j-test-org/github-api-template-test.git",
73+
"svn_url": "https://github.com/hub4j-test-org/github-api-template-test",
74+
"homepage": "http://github-api.kohsuke.org/",
75+
"size": 0,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": null,
79+
"has_issues": true,
80+
"has_projects": true,
81+
"has_downloads": true,
82+
"has_wiki": true,
83+
"has_pages": false,
84+
"forks_count": 0,
85+
"mirror_url": null,
86+
"archived": false,
87+
"disabled": false,
88+
"open_issues_count": 0,
89+
"license": null,
90+
"forks": 0,
91+
"open_issues": 0,
92+
"watchers": 0,
93+
"default_branch": "master",
94+
"permissions": {
95+
"admin": true,
96+
"push": true,
97+
"pull": true
98+
},
99+
"allow_squash_merge": true,
100+
"allow_merge_commit": true,
101+
"allow_rebase_merge": true,
102+
"organization": {
103+
"login": "hub4j-test-org",
104+
"id": 7544739,
105+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
106+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
107+
"gravatar_id": "",
108+
"url": "http://localhost:51951/users/hub4j-test-org",
109+
"html_url": "https://github.com/hub4j-test-org",
110+
"followers_url": "http://localhost:51951/users/hub4j-test-org/followers",
111+
"following_url": "http://localhost:51951/users/hub4j-test-org/following{/other_user}",
112+
"gists_url": "http://localhost:51951/users/hub4j-test-org/gists{/gist_id}",
113+
"starred_url": "http://localhost:51951/users/hub4j-test-org/starred{/owner}{/repo}",
114+
"subscriptions_url": "http://localhost:51951/users/hub4j-test-org/subscriptions",
115+
"organizations_url": "http://localhost:51951/users/hub4j-test-org/orgs",
116+
"repos_url": "http://localhost:51951/users/hub4j-test-org/repos",
117+
"events_url": "http://localhost:51951/users/hub4j-test-org/events{/privacy}",
118+
"received_events_url": "http://localhost:51951/users/hub4j-test-org/received_events",
119+
"type": "Organization",
120+
"site_admin": false
121+
},
122+
"network_count": 0,
123+
"subscribers_count": 2
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[
2+
{
3+
"name": "Owners",
4+
"id": 820404,
5+
"node_id": "MDQ6VGVhbTgyMDQwNA==",
6+
"slug": "owners",
7+
"description": null,
8+
"privacy": "secret",
9+
"url": "https://api.github.com/teams/820404",
10+
"html_url": "https://github.com/orgs/hub4j-test-org/teams/owners",
11+
"members_url": "https://api.github.com/teams/820404/members{/member}",
12+
"repositories_url": "https://api.github.com/teams/820404/repos",
13+
"permission": "admin"
14+
},
15+
{
16+
"name": "Core Developers",
17+
"id": 820406,
18+
"node_id": "MDQ6VGVhbTgyMDQwNg==",
19+
"slug": "core-developers",
20+
"description": "A random team",
21+
"privacy": "secret",
22+
"url": "https://api.github.com/teams/820406",
23+
"html_url": "https://github.com/orgs/hub4j-test-org/teams/core-developers",
24+
"members_url": "https://api.github.com/teams/820406/members{/member}",
25+
"repositories_url": "https://api.github.com/teams/820406/repos",
26+
"permission": "pull"
27+
}
28+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "README.md",
3+
"path": "README.md",
4+
"sha": "aa0e9008d8d8c4745d81d718b5d418f6a5529759",
5+
"size": 70,
6+
"url": "https://api.github.com/repos/hub4j-test-org/github-api-template-test/contents/README.md?ref=master",
7+
"html_url": "https://github.com/hub4j-test-org/github-api-template-test/blob/master/README.md",
8+
"git_url": "https://api.github.com/repos/hub4j-test-org/github-api-template-test/git/blobs/aa0e9008d8d8c4745d81d718b5d418f6a5529759",
9+
"download_url": "https://raw.githubusercontent.com/hub4j-test-org/github-api-template-test/master/README.md",
10+
"type": "file",
11+
"content": "IyBnaXRodWItYXBpLXRlc3QKYSB0ZXN0IHJlcG9zaXRvcnkgdXNlZCB0byB0\nZXN0IGtvaHN1a2UncyBnaXRodWItYXBpCg==\n",
12+
"encoding": "base64",
13+
"_links": {
14+
"self": "https://api.github.com/repos/hub4j-test-org/github-api-template-test/contents/README.md?ref=master",
15+
"git": "https://api.github.com/repos/hub4j-test-org/github-api-template-test/git/blobs/aa0e9008d8d8c4745d81d718b5d418f6a5529759",
16+
"html": "https://github.com/hub4j-test-org/github-api-template-test/blob/master/README.md"
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"login": "bitwiseman",
3+
"id": 1958953,
4+
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
5+
"avatar_url": "https://avatars3.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": "bitwiseman@gmail.com",
25+
"hireable": null,
26+
"bio": "https://twitter.com/bitwiseman",
27+
"public_repos": 167,
28+
"public_gists": 4,
29+
"followers": 136,
30+
"following": 9,
31+
"created_at": "2012-07-11T20:38:33Z",
32+
"updated_at": "2019-09-24T19:32:29Z",
33+
"private_gists": 7,
34+
"total_private_repos": 9,
35+
"owned_private_repos": 0,
36+
"disk_usage": 33697,
37+
"collaborators": 0,
38+
"two_factor_authentication": true,
39+
"plan": {
40+
"name": "free",
41+
"space": 976562499,
42+
"collaborators": 0,
43+
"private_repos": 10000
44+
}
45+
}

0 commit comments

Comments
 (0)