Skip to content

Commit 998bda9

Browse files
committed
Add createTag() method.
Fixes hub4j#500.
1 parent a115f34 commit 998bda9

12 files changed

Lines changed: 747 additions & 0 deletions

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,4 +2653,30 @@ public void setTopics(List<String> topics) throws IOException {
26532653
.withUrlPath(getApiTailUrl("topics"))
26542654
.send();
26552655
}
2656+
2657+
/**
2658+
* Create a tag. See https://developer.github.com/v3/git/tags/#create-a-tag-object
2659+
*
2660+
* @param tag
2661+
* The tag's name.
2662+
* @param message
2663+
* The tag message.
2664+
* @param object
2665+
* The SHA of the git object this is tagging.
2666+
* @param type
2667+
* The type of the object we're tagging: "commit", "tree" or "blob".
2668+
* @return The newly created tag.
2669+
* @throws java.io.IOException
2670+
*/
2671+
public GHTagObject createTag(String tag, String message, String object, String type) throws IOException {
2672+
return root.createRequest()
2673+
.method("POST")
2674+
.with("tag", tag)
2675+
.with("message", message)
2676+
.with("object", object)
2677+
.with("type", type)
2678+
.withUrlPath(getApiTailUrl("git/tags"))
2679+
.fetch(GHTagObject.class)
2680+
.wrap(this);
2681+
}
26562682
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,33 @@ public void getPostCommitHooks() throws Exception {
344344
assertThat(postcommitHooks.size(), equalTo(0));
345345
}
346346

347+
public void cleanUpTags() throws Exception {
348+
GHRepository repo = getRepository(gitHubBeforeAfter);
349+
350+
GHRef ref = repo.getRef("tags/create_tag_test");
351+
if (ref != null) {
352+
ref.delete();
353+
}
354+
}
355+
356+
@Test
357+
public void testCreateTag() throws Exception {
358+
cleanUpTags();
359+
360+
GHRepository repo = getRepository(gitHub);
361+
362+
String commitSha = "dfe47235cfdcaa12292dab3b1a84ca53a1ceadaf";
363+
String tagName = "create_tag_test";
364+
String tagMessage = "Test Tag";
365+
String tagType = "commit";
366+
367+
GHTagObject tag = repo.createTag(tagName, tagMessage, commitSha, tagType);
368+
assertEquals(tagName, tag.getTag());
369+
assertEquals(tagMessage, tag.getMessage());
370+
assertEquals(commitSha, tag.getObject().getSha());
371+
372+
// Make a reference to the newly created tag.
373+
GHRef ref = repo.createRef("refs/tags/" + tagName, tag.getSha());
374+
assertNotNull(ref);
375+
}
347376
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"login": "github-api-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/github-api-test-org",
6+
"repos_url": "https://api.github.com/orgs/github-api-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/github-api-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/github-api-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/github-api-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/github-api-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/github-api-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/github-api-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": 10,
39+
"seats": 0
40+
}
41+
}

src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/testCreateTag/__files/repos_github-api-test-org_github-api-638d04df-e1b4-4474-9fff-1c26cb3edb3b.json

Lines changed: 331 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"ref": "refs/tags/create_tag_test",
3+
"node_id": "MDM6UmVmMjA2ODg4MjAxOmNyZWF0ZV90YWdfdGVzdA==",
4+
"url": "https://api.github.com/repos/github-api-test-org/github-api/git/refs/tags/create_tag_test",
5+
"object": {
6+
"sha": "f54a11526c489c661fddc34c9ce1af83f0cabee7",
7+
"type": "tag",
8+
"url": "https://api.github.com/repos/github-api-test-org/github-api/git/tags/f54a11526c489c661fddc34c9ce1af83f0cabee7"
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"node_id": "MDM6VGFnMjA2ODg4MjAxOmY1NGExMTUyNmM0ODljNjYxZmRkYzM0YzljZTFhZjgzZjBjYWJlZTc=",
3+
"sha": "f54a11526c489c661fddc34c9ce1af83f0cabee7",
4+
"url": "https://api.github.com/repos/github-api-test-org/github-api/git/tags/f54a11526c489c661fddc34c9ce1af83f0cabee7",
5+
"tagger": {
6+
"name": "Martin van Zijl",
7+
"email": "martin.vanzijl@gmail.com",
8+
"date": "2019-12-22T04:32:08Z"
9+
},
10+
"object": {
11+
"sha": "dfe47235cfdcaa12292dab3b1a84ca53a1ceadaf",
12+
"type": "commit",
13+
"url": "https://api.github.com/repos/github-api-test-org/github-api/git/commits/dfe47235cfdcaa12292dab3b1a84ca53a1ceadaf"
14+
},
15+
"tag": "create_tag_test",
16+
"message": "Test Tag",
17+
"verification": {
18+
"verified": false,
19+
"reason": "unsigned",
20+
"signature": null,
21+
"payload": null
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"login": "martinvanzijl",
3+
"id": 24422213,
4+
"node_id": "MDQ6VXNlcjI0NDIyMjEz",
5+
"avatar_url": "https://avatars0.githubusercontent.com/u/24422213?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/martinvanzijl",
8+
"html_url": "https://github.com/martinvanzijl",
9+
"followers_url": "https://api.github.com/users/martinvanzijl/followers",
10+
"following_url": "https://api.github.com/users/martinvanzijl/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/martinvanzijl/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/martinvanzijl/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/martinvanzijl/subscriptions",
14+
"organizations_url": "https://api.github.com/users/martinvanzijl/orgs",
15+
"repos_url": "https://api.github.com/users/martinvanzijl/repos",
16+
"events_url": "https://api.github.com/users/martinvanzijl/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/martinvanzijl/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Martin van Zijl",
21+
"company": null,
22+
"blog": "",
23+
"location": null,
24+
"email": null,
25+
"hireable": true,
26+
"bio": "My strongest language is C++, but I also know C, Java, Python and SQL.",
27+
"public_repos": 14,
28+
"public_gists": 0,
29+
"followers": 2,
30+
"following": 0,
31+
"created_at": "2016-12-07T00:18:58Z",
32+
"updated_at": "2019-12-22T03:20:42Z"
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"id": "2d4a937c-c505-45d8-aedc-76ca741de1a6",
3+
"name": "orgs_github-api-test-org",
4+
"request": {
5+
"url": "/orgs/github-api-test-org",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "orgs_github-api-test-org-2d4a937c-c505-45d8-aedc-76ca741de1a6.json",
16+
"headers": {
17+
"Date": "Sun, 22 Dec 2019 04:32:07 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"X-RateLimit-Limit": "5000",
22+
"X-RateLimit-Remaining": "4964",
23+
"X-RateLimit-Reset": "1576992699",
24+
"Cache-Control": "private, max-age=60, s-maxage=60",
25+
"Vary": [
26+
"Accept, Authorization, Cookie, X-GitHub-OTP",
27+
"Accept-Encoding"
28+
],
29+
"ETag": "W/\"a3567729368292d9feff183a4bcab7e6\"",
30+
"Last-Modified": "Mon, 20 Apr 2015 00:42:30 GMT",
31+
"X-OAuth-Scopes": "admin:org, delete_repo, gist, repo",
32+
"X-Accepted-OAuth-Scopes": "admin:org, read:org, repo, user, write:org",
33+
"X-GitHub-Media-Type": "unknown, github.v3",
34+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
35+
"Access-Control-Allow-Origin": "*",
36+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
37+
"X-Frame-Options": "deny",
38+
"X-Content-Type-Options": "nosniff",
39+
"X-XSS-Protection": "1; mode=block",
40+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
41+
"Content-Security-Policy": "default-src 'none'",
42+
"X-GitHub-Request-Id": "CA26:7E99:2AD177:30DDEA:5DFEF1C4"
43+
}
44+
},
45+
"uuid": "2d4a937c-c505-45d8-aedc-76ca741de1a6",
46+
"persistent": true,
47+
"insertionIndex": 2
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"id": "638d04df-e1b4-4474-9fff-1c26cb3edb3b",
3+
"name": "repos_github-api-test-org_github-api",
4+
"request": {
5+
"url": "/repos/github-api-test-org/github-api",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "repos_github-api-test-org_github-api-638d04df-e1b4-4474-9fff-1c26cb3edb3b.json",
16+
"headers": {
17+
"Date": "Sun, 22 Dec 2019 04:32:08 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"X-RateLimit-Limit": "5000",
22+
"X-RateLimit-Remaining": "4963",
23+
"X-RateLimit-Reset": "1576992699",
24+
"Cache-Control": "private, max-age=60, s-maxage=60",
25+
"Vary": [
26+
"Accept, Authorization, Cookie, X-GitHub-OTP",
27+
"Accept-Encoding"
28+
],
29+
"ETag": "W/\"192a87deb3212a5e577e966d7e5a5510\"",
30+
"Last-Modified": "Fri, 20 Dec 2019 00:07:51 GMT",
31+
"X-OAuth-Scopes": "admin:org, delete_repo, gist, repo",
32+
"X-Accepted-OAuth-Scopes": "repo",
33+
"X-GitHub-Media-Type": "unknown, github.v3",
34+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
35+
"Access-Control-Allow-Origin": "*",
36+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
37+
"X-Frame-Options": "deny",
38+
"X-Content-Type-Options": "nosniff",
39+
"X-XSS-Protection": "1; mode=block",
40+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
41+
"Content-Security-Policy": "default-src 'none'",
42+
"X-GitHub-Request-Id": "CA26:7E99:2AD17E:30DE11:5DFEF1C7"
43+
}
44+
},
45+
"uuid": "638d04df-e1b4-4474-9fff-1c26cb3edb3b",
46+
"persistent": true,
47+
"insertionIndex": 3
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"id": "a57ad398-e8c3-42e6-82c2-9d36a61cddf4",
3+
"name": "repos_github-api-test-org_github-api_git_refs",
4+
"request": {
5+
"url": "/repos/github-api-test-org/github-api/git/refs",
6+
"method": "POST",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
},
12+
"bodyPatterns": [
13+
{
14+
"equalToJson": "{\"ref\":\"refs/tags/create_tag_test\",\"sha\":\"f54a11526c489c661fddc34c9ce1af83f0cabee7\"}",
15+
"ignoreArrayOrder": true,
16+
"ignoreExtraElements": true
17+
}
18+
]
19+
},
20+
"response": {
21+
"status": 201,
22+
"bodyFileName": "repos_github-api-test-org_github-api_git_refs-a57ad398-e8c3-42e6-82c2-9d36a61cddf4.json",
23+
"headers": {
24+
"Date": "Sun, 22 Dec 2019 04:32:09 GMT",
25+
"Content-Type": "application/json; charset=utf-8",
26+
"Server": "GitHub.com",
27+
"Status": "201 Created",
28+
"X-RateLimit-Limit": "5000",
29+
"X-RateLimit-Remaining": "4961",
30+
"X-RateLimit-Reset": "1576992699",
31+
"Cache-Control": "private, max-age=60, s-maxage=60",
32+
"Vary": [
33+
"Accept, Authorization, Cookie, X-GitHub-OTP",
34+
"Accept-Encoding"
35+
],
36+
"ETag": "\"b195d710be8c9bc11e4b16dadbebacf3\"",
37+
"X-OAuth-Scopes": "admin:org, delete_repo, gist, repo",
38+
"X-Accepted-OAuth-Scopes": "repo",
39+
"Location": "https://api.github.com/repos/github-api-test-org/github-api/git/refs/tags/create_tag_test",
40+
"X-GitHub-Media-Type": "unknown, github.v3",
41+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
42+
"Access-Control-Allow-Origin": "*",
43+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
44+
"X-Frame-Options": "deny",
45+
"X-Content-Type-Options": "nosniff",
46+
"X-XSS-Protection": "1; mode=block",
47+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
48+
"Content-Security-Policy": "default-src 'none'",
49+
"X-GitHub-Request-Id": "CA26:7E99:2AD18C:30DE20:5DFEF1C8"
50+
}
51+
},
52+
"uuid": "a57ad398-e8c3-42e6-82c2-9d36a61cddf4",
53+
"persistent": true,
54+
"insertionIndex": 5
55+
}

0 commit comments

Comments
 (0)