Skip to content

Commit

Permalink
Merge pull request #897 from bonnie-young/add-create-repo-with-templa…
Browse files Browse the repository at this point in the history
…te-support

add create repo with template support
  • Loading branch information
bitwiseman authored Aug 12, 2020
2 parents dcf26d5 + 11bc669 commit ff4324a
Show file tree
Hide file tree
Showing 23 changed files with 1,091 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/main/java/org/kohsuke/github/GHCreateRepositoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.net.URL;

import static org.kohsuke.github.Previews.BAPTISE;

/**
* Creates a repository
*
Expand All @@ -11,7 +13,7 @@
public class GHCreateRepositoryBuilder {
private final GitHub root;
protected final Requester builder;
private final String apiUrlTail;
private String apiUrlTail;

GHCreateRepositoryBuilder(GitHub root, String apiUrlTail, String name) {
this.root = root;
Expand Down Expand Up @@ -200,6 +202,51 @@ public GHCreateRepositoryBuilder team(GHTeam team) {
return this;
}

/**
* Specifies whether the repository is a template.
*
* @param enabled
* true if enabled
* @return a builder to continue with building
*/
@Preview
@Deprecated
public GHCreateRepositoryBuilder templateRepository(boolean enabled) {
this.builder.withPreview(BAPTISE);
this.builder.with("is_template", enabled);
return this;
}

/**
* Specifies the ownership of the repository.
*
* @param owner
* organization or personage
* @return a builder to continue with building
*/
public GHCreateRepositoryBuilder owner(String owner) {
this.builder.with("owner", owner);
return this;
}

/**
* Create repository from template repository.
*
* @param templateOwner
* template repository owner
* @param templateRepo
* template repository
* @return a builder to continue with building
* @see <a href="https://developer.github.com/v3/previews/">GitHub API Previews</a>
*/
@Preview
@Deprecated
public GHCreateRepositoryBuilder fromTemplateRepository(String templateOwner, String templateRepo) {
this.builder.withPreview(BAPTISE);
this.apiUrlTail = "/repos/" + templateOwner + "/" + templateRepo + "/generate";
return this;
}

/**
* Creates a repository with all the parameters.
*
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/kohsuke/github/Previews.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ class Previews {
* @see <a href="https://developer.github.com/v3/previews/#require-signed-commits">GitHub API Previews</a>
*/
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";
}
31 changes: 31 additions & 0 deletions src/test/java/org/kohsuke/github/GHOrganizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class GHOrganizationTest extends AbstractGitHubWireMockTest {

public static final String GITHUB_API_TEST = "github-api-test";
public static final String GITHUB_API_TEMPLATE_TEST = "github-api-template-test";
public static final String TEAM_NAME_CREATE = "create-team-test";

@Before
Expand Down Expand Up @@ -56,6 +57,36 @@ public void testCreateRepositoryWithAutoInitialization() throws IOException {
Assert.assertNotNull(repository.getReadme());
}

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

GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
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"))
.autoInit(true)
.templateRepository(true)
.create();
Assert.assertNotNull(repository);
Assert.assertNotNull(repository.getReadme());
}

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

GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
GHRepository repository = org.createRepository(GITHUB_API_TEST)
.fromTemplateRepository(GITHUB_API_TEST_ORG, GITHUB_API_TEMPLATE_TEST)
.owner(GITHUB_API_TEST_ORG)
.create();

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

@Test
public void testInviteUser() throws IOException {
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"url": "https://api.github.com/orgs/hub4j-test-org",
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
"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,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 9,
"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",
"type": "Organization",
"total_private_repos": 0,
"owned_private_repos": 0,
"private_gists": 0,
"disk_usage": 132,
"collaborators": 0,
"billing_email": "[email protected]",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 0,
"filled_seats": 3,
"seats": 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
"id": 212682270,
"node_id": "MDEwOlJlcG9zaXRvcnkyMTI2ODIyNzA=",
"name": "github-api-template-test",
"full_name": "hub4j-test-org/github-api-template-test",
"private": false,
"owner": {
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"gravatar_id": "",
"url": "http://localhost:51951/users/hub4j-test-org",
"html_url": "https://github.com/hub4j-test-org",
"followers_url": "http://localhost:51951/users/hub4j-test-org/followers",
"following_url": "http://localhost:51951/users/hub4j-test-org/following{/other_user}",
"gists_url": "http://localhost:51951/users/hub4j-test-org/gists{/gist_id}",
"starred_url": "http://localhost:51951/users/hub4j-test-org/starred{/owner}{/repo}",
"subscriptions_url": "http://localhost:51951/users/hub4j-test-org/subscriptions",
"organizations_url": "http://localhost:51951/users/hub4j-test-org/orgs",
"repos_url": "http://localhost:51951/users/hub4j-test-org/repos",
"events_url": "http://localhost:51951/users/hub4j-test-org/events{/privacy}",
"received_events_url": "http://localhost:51951/users/hub4j-test-org/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/hub4j-test-org/github-api-template-test",
"description": "a test repository used to test kohsuke's github-api",
"fork": false,
"url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test",
"forks_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/forks",
"keys_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/keys{/key_id}",
"collaborators_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/collaborators{/collaborator}",
"teams_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/teams",
"hooks_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/hooks",
"issue_events_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/issues/events{/number}",
"events_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/events",
"assignees_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/assignees{/user}",
"branches_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/branches{/branch}",
"tags_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/tags",
"blobs_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/git/blobs{/sha}",
"git_tags_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/git/tags{/sha}",
"git_refs_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/git/refs{/sha}",
"trees_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/git/trees{/sha}",
"statuses_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/statuses/{sha}",
"languages_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/languages",
"stargazers_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/stargazers",
"contributors_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/contributors",
"subscribers_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/subscribers",
"subscription_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/subscription",
"commits_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/commits{/sha}",
"git_commits_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/git/commits{/sha}",
"comments_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/comments{/number}",
"issue_comment_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/issues/comments{/number}",
"contents_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/contents/{+path}",
"compare_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/compare/{base}...{head}",
"merges_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/merges",
"archive_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/{archive_format}{/ref}",
"downloads_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/downloads",
"issues_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/issues{/number}",
"pulls_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/pulls{/number}",
"milestones_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/milestones{/number}",
"notifications_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/notifications{?since,all,participating}",
"labels_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/labels{/name}",
"releases_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/releases{/id}",
"deployments_url": "http://localhost:51951/repos/hub4j-test-org/github-api-template-test/deployments",
"created_at": "2019-10-03T21:18:43Z",
"updated_at": "2019-10-03T21:18:43Z",
"pushed_at": "2019-10-03T21:18:44Z",
"git_url": "git://github.com/hub4j-test-org/github-api-template-test.git",
"ssh_url": "[email protected]:hub4j-test-org/github-api-template-test.git",
"clone_url": "https://github.com/hub4j-test-org/github-api-template-test.git",
"svn_url": "https://github.com/hub4j-test-org/github-api-template-test",
"homepage": "http://github-api.kohsuke.org/",
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 0,
"license": null,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "master",
"permissions": {
"admin": true,
"push": true,
"pull": true
},
"allow_squash_merge": true,
"allow_merge_commit": true,
"allow_rebase_merge": true,
"organization": {
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"gravatar_id": "",
"url": "http://localhost:51951/users/hub4j-test-org",
"html_url": "https://github.com/hub4j-test-org",
"followers_url": "http://localhost:51951/users/hub4j-test-org/followers",
"following_url": "http://localhost:51951/users/hub4j-test-org/following{/other_user}",
"gists_url": "http://localhost:51951/users/hub4j-test-org/gists{/gist_id}",
"starred_url": "http://localhost:51951/users/hub4j-test-org/starred{/owner}{/repo}",
"subscriptions_url": "http://localhost:51951/users/hub4j-test-org/subscriptions",
"organizations_url": "http://localhost:51951/users/hub4j-test-org/orgs",
"repos_url": "http://localhost:51951/users/hub4j-test-org/repos",
"events_url": "http://localhost:51951/users/hub4j-test-org/events{/privacy}",
"received_events_url": "http://localhost:51951/users/hub4j-test-org/received_events",
"type": "Organization",
"site_admin": false
},
"network_count": 0,
"subscribers_count": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"name": "Owners",
"id": 820404,
"node_id": "MDQ6VGVhbTgyMDQwNA==",
"slug": "owners",
"description": null,
"privacy": "secret",
"url": "https://api.github.com/teams/820404",
"html_url": "https://github.com/orgs/hub4j-test-org/teams/owners",
"members_url": "https://api.github.com/teams/820404/members{/member}",
"repositories_url": "https://api.github.com/teams/820404/repos",
"permission": "admin"
},
{
"name": "Core Developers",
"id": 820406,
"node_id": "MDQ6VGVhbTgyMDQwNg==",
"slug": "core-developers",
"description": "A random team",
"privacy": "secret",
"url": "https://api.github.com/teams/820406",
"html_url": "https://github.com/orgs/hub4j-test-org/teams/core-developers",
"members_url": "https://api.github.com/teams/820406/members{/member}",
"repositories_url": "https://api.github.com/teams/820406/repos",
"permission": "pull"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "README.md",
"path": "README.md",
"sha": "aa0e9008d8d8c4745d81d718b5d418f6a5529759",
"size": 70,
"url": "https://api.github.com/repos/hub4j-test-org/github-api-template-test/contents/README.md?ref=master",
"html_url": "https://github.com/hub4j-test-org/github-api-template-test/blob/master/README.md",
"git_url": "https://api.github.com/repos/hub4j-test-org/github-api-template-test/git/blobs/aa0e9008d8d8c4745d81d718b5d418f6a5529759",
"download_url": "https://raw.githubusercontent.com/hub4j-test-org/github-api-template-test/master/README.md",
"type": "file",
"content": "IyBnaXRodWItYXBpLXRlc3QKYSB0ZXN0IHJlcG9zaXRvcnkgdXNlZCB0byB0\nZXN0IGtvaHN1a2UncyBnaXRodWItYXBpCg==\n",
"encoding": "base64",
"_links": {
"self": "https://api.github.com/repos/hub4j-test-org/github-api-template-test/contents/README.md?ref=master",
"git": "https://api.github.com/repos/hub4j-test-org/github-api-template-test/git/blobs/aa0e9008d8d8c4745d81d718b5d418f6a5529759",
"html": "https://github.com/hub4j-test-org/github-api-template-test/blob/master/README.md"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars3.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": "https://twitter.com/bitwiseman",
"public_repos": 167,
"public_gists": 4,
"followers": 136,
"following": 9,
"created_at": "2012-07-11T20:38:33Z",
"updated_at": "2019-09-24T19:32:29Z",
"private_gists": 7,
"total_private_repos": 9,
"owned_private_repos": 0,
"disk_usage": 33697,
"collaborators": 0,
"two_factor_authentication": true,
"plan": {
"name": "free",
"space": 976562499,
"collaborators": 0,
"private_repos": 10000
}
}
Loading

0 comments on commit ff4324a

Please sign in to comment.