diff --git a/src/main/java/org/kohsuke/github/GHRefBuilder.java b/src/main/java/org/kohsuke/github/GHRefBuilder.java new file mode 100644 index 0000000000..232989f9bd --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHRefBuilder.java @@ -0,0 +1,22 @@ +package org.kohsuke.github; + + +import java.io.IOException; + +public class GHRefBuilder { + + private final GHRepository repo; + private final Requester builder; + + public GHRefBuilder(GHRepository ghRepository, String ref, String sha) { + this.repo = ghRepository; + this.builder = new Requester(repo.root); + builder.with("ref", "refs/heads/" +ref); + builder.with("sha", sha); + } + + public void create() throws IOException { + builder.method("POST").to(repo.getApiTailUrl("git/refs")); + } + +} diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 6a6b93ae73..cfb1442170 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -183,6 +183,11 @@ public List getReleases() throws IOException { GHRelease[].class), this)); } + public List getTags() throws IOException { + return Arrays.asList(GHTag.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/tags", + GHTag[].class), this)); + } + protected String getOwnerName() { return owner.login; } @@ -498,6 +503,21 @@ protected void wrapUp(GHCommit[] page) { }; } + private String sha; + public PagedIterable listCommitsSinceSha(String pSha) { + sha=pSha; + return new PagedIterable() { + public PagedIterator iterator() { + return new PagedIterator(root.retrieve().asIterator(String.format("/repos/%s/%s/commits?sha=%s", owner.login, name, sha), GHCommit[].class)) { + protected void wrapUp(GHCommit[] page) { + for (GHCommit c : page) + c.wrapUp(GHRepository.this); + } + }; + } + }; + } + /** * Lists up all the commit comments in this repository. */ diff --git a/src/main/java/org/kohsuke/github/GHTag.java b/src/main/java/org/kohsuke/github/GHTag.java new file mode 100644 index 0000000000..1ec0161350 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHTag.java @@ -0,0 +1,56 @@ +package org.kohsuke.github; + + +public class GHTag { + private GHRepository owner; + private GitHub root; + + private String name; + private GHCommit commit; + + GHTag wrap(GHRepository owner) { + this.owner = owner; + this.root = owner.root; + return this; + } + + static GHTag[] wrap(GHTag[] tags, GHRepository owner) { + for (GHTag tag : tags) { + tag.wrap(owner); + } + return tags; + } + + + public GHRepository getOwner() { + return owner; + } + + public void setOwner(GHRepository owner) { + this.owner = owner; + } + + public GitHub getRoot() { + return root; + } + + public void setRoot(GitHub root) { + this.root = root; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public GHCommit getCommit() { + return commit; + } + + public void setCommit(GHCommit commit) { + this.commit = commit; + } +} diff --git a/src/test/java/org/kohsuke/AppTest.java b/src/test/java/org/kohsuke/AppTest.java index 2847c0f8ec..fe3c163fe1 100644 --- a/src/test/java/org/kohsuke/AppTest.java +++ b/src/test/java/org/kohsuke/AppTest.java @@ -6,12 +6,15 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import org.junit.Assume; import org.junit.Before; import org.junit.Test; @@ -34,7 +37,9 @@ import org.kohsuke.github.GHOrganization; import org.kohsuke.github.GHOrganization.Permission; import org.kohsuke.github.GHPullRequest; +import org.kohsuke.github.GHRefBuilder; import org.kohsuke.github.GHRepository; +import org.kohsuke.github.GHTag; import org.kohsuke.github.GHTeam; import org.kohsuke.github.GHUser; import org.kohsuke.github.GitHub; @@ -489,6 +494,38 @@ public void testCheckMembership() throws Exception { assertFalse(j.hasPublicMember(b)); } + @Test + public void testCreateRelease() throws Exception { + + String tagName = UUID.randomUUID().toString(); + String releaseName = "release-" + tagName; + String repo = "fanfansama/github-api-test-1"; + + + gitHub.getRepository(repo) + .createRelease(tagName) + .name(releaseName) + .prerelease(false) + .create(); + + for(GHTag tag : gitHub.getRepository(repo).getTags()){ + if(tagName.equals(tag.getName())){ + String ash = tag.getCommit().getSHA1(); + new GHRefBuilder(gitHub.getRepository(repo), releaseName, ash ).create(); // create a release branch + + for (Map.Entry entry : gitHub.getRepository(repo).getBranches().entrySet()) + { + System.out.println(entry.getKey() + "/" + entry.getValue()); + if(releaseName.equals(entry.getValue().getName())){ + return; + } + } + fail("banch not found"); + } + } + fail("release creation failed ! tag not found"); + } + private void kohsuke() { Assume.assumeTrue(getUser().getLogin().equals("kohsuke")); }