diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index b40f387b7f..6247e99138 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -310,6 +310,22 @@ public GHRef createRef(String name, String sha) throws IOException { public List getReleases() throws IOException { return listReleases().asList(); } + + public GHRelease getRelease(long id) throws IOException { + try { + return root.retrieve().to(getApiTailUrl("releases/" + id), GHRelease.class).wrap(this); + } catch (FileNotFoundException e) { + return null; // no release for this id + } + } + + public GHRelease getReleaseByTagName(String tag) throws IOException { + try { + return root.retrieve().to(getApiTailUrl("releases/tags/" + tag), GHRelease.class).wrap(this); + } catch (FileNotFoundException e) { + return null; // no release for this tag + } + } public GHRelease getLatestRelease() throws IOException { try { diff --git a/src/test/java/org/kohsuke/github/RepositoryTest.java b/src/test/java/org/kohsuke/github/RepositoryTest.java index 728f94a834..66fe689507 100644 --- a/src/test/java/org/kohsuke/github/RepositoryTest.java +++ b/src/test/java/org/kohsuke/github/RepositoryTest.java @@ -94,6 +94,36 @@ public void LatestRepositoryNotExist() { } } + @Test public void listReleases() throws IOException { + PagedIterable releases = gitHub.getOrganization("github").getRepository("hub").listReleases(); + assertTrue(releases.iterator().hasNext()); + } + + @Test + public void getReleaseExists() throws IOException { + GHRelease release = gitHub.getOrganization("github").getRepository("hub").getRelease(6839710); + assertEquals("v2.3.0-pre10", release.getTagName()); + } + + @Test + public void getReleaseDoesNotExist() throws IOException { + GHRelease release = gitHub.getOrganization("github").getRepository("hub").getRelease(Long.MAX_VALUE); + assertNull(release); + } + + @Test + public void getReleaseByTagNameExists() throws IOException { + GHRelease release = gitHub.getOrganization("github").getRepository("hub").getReleaseByTagName("v2.3.0-pre10"); + assertNotNull(release); + assertEquals("v2.3.0-pre10", release.getTagName()); + } + + @Test + public void getReleaseByTagNameDoesNotExist() throws IOException { + GHRelease release = getRepository().getReleaseByTagName("foo-bar-baz"); + assertNull(release); + } + private GHRepository getRepository() throws IOException { return gitHub.getOrganization("github-api-test-org").getRepository("jenkins"); }