Skip to content

Commit 30c70bc

Browse files
authored
Merge pull request hub4j#651 from martinvanzijl/issue_500_create_tag_method
Add createTag() method to GHRepository
2 parents ec31e94 + 0df48c3 commit 30c70bc

12 files changed

Lines changed: 783 additions & 0 deletions

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,4 +2655,31 @@ public void setTopics(List<String> topics) throws IOException {
26552655
.withUrlPath(getApiTailUrl("topics"))
26562656
.send();
26572657
}
2658+
2659+
/**
2660+
* Create a tag. See https://developer.github.com/v3/git/tags/#create-a-tag-object
2661+
*
2662+
* @param tag
2663+
* The tag's name.
2664+
* @param message
2665+
* The tag message.
2666+
* @param object
2667+
* The SHA of the git object this is tagging.
2668+
* @param type
2669+
* The type of the object we're tagging: "commit", "tree" or "blob".
2670+
* @return The newly created tag.
2671+
* @throws java.io.IOException
2672+
* The IO exception.
2673+
*/
2674+
public GHTagObject createTag(String tag, String message, String object, String type) throws IOException {
2675+
return root.createRequest()
2676+
.method("POST")
2677+
.with("tag", tag)
2678+
.with("message", message)
2679+
.with("object", object)
2680+
.with("type", type)
2681+
.withUrlPath(getApiTailUrl("git/tags"))
2682+
.fetch(GHTagObject.class)
2683+
.wrap(this);
2684+
}
26582685
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.IOException;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertNotNull;
11+
12+
/**
13+
*
14+
* @author Martin van Zijl
15+
*/
16+
public class GHTagTest extends AbstractGitHubWireMockTest {
17+
18+
@Before
19+
@After
20+
public void cleanUpTags() throws Exception {
21+
// Cleanup is only needed when proxying
22+
if (!mockGitHub.isUseProxy()) {
23+
return;
24+
}
25+
26+
try {
27+
GHRef ref = getRepository(this.gitHubBeforeAfter).getRef("tags/create_tag_test");
28+
if (ref != null) {
29+
ref.delete();
30+
}
31+
} catch (IOException e) {
32+
// The reference probably does not exist.
33+
// This is safe to ignore.
34+
}
35+
}
36+
37+
@Test
38+
public void testCreateTag() throws Exception {
39+
GHRepository repo = getRepository();
40+
41+
String commitSha = "dfe47235cfdcaa12292dab3b1a84ca53a1ceadaf";
42+
String tagName = "create_tag_test";
43+
String tagMessage = "Test Tag";
44+
String tagType = "commit";
45+
46+
GHTagObject tag = repo.createTag(tagName, tagMessage, commitSha, tagType);
47+
assertEquals(tagName, tag.getTag());
48+
assertEquals(tagMessage, tag.getMessage());
49+
assertEquals(commitSha, tag.getObject().getSha());
50+
51+
// Make a reference to the newly created tag.
52+
GHRef ref = repo.createRef("refs/tags/" + tagName, tag.getSha());
53+
assertNotNull(ref);
54+
}
55+
56+
protected GHRepository getRepository() throws IOException {
57+
return getRepository(gitHub);
58+
}
59+
60+
private GHRepository getRepository(GitHub gitHub) throws IOException {
61+
return gitHub.getOrganization("github-api-test-org").getRepository("github-api");
62+
}
63+
}
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": 10,
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": 147,
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": 11,
39+
"seats": 0
40+
}
41+
}

0 commit comments

Comments
 (0)