diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 74fbb4d665..f233f8b4fe 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -298,6 +298,14 @@ public GHRef createRef(String name, String sha) throws IOException { public List getReleases() throws IOException { return listReleases().asList(); } + + public GHRelease getLatestRelease() throws IOException { + try { + return root.retrieve().to(getApiTailUrl("releases/latest"), GHRelease.class).wrap(this); + } catch (FileNotFoundException e) { + return null; // no latest release + } + } public PagedIterable listReleases() throws IOException { return new PagedIterable() { diff --git a/src/test/java/org/kohsuke/github/RepositoryTest.java b/src/test/java/org/kohsuke/github/RepositoryTest.java index 9955c34956..728f94a834 100644 --- a/src/test/java/org/kohsuke/github/RepositoryTest.java +++ b/src/test/java/org/kohsuke/github/RepositoryTest.java @@ -67,6 +67,32 @@ public void getPermission() throws Exception { } } } + + + + @Test + public void LatestRepositoryExist() { + try { + // add the repository that have latest release + GHRelease release = gitHub.getRepository("kamontat/CheckIDNumber").getLatestRelease(); + assertEquals("v3.0", release.getTagName()); + } catch (IOException e) { + e.printStackTrace(); + fail(); + } + } + + @Test + public void LatestRepositoryNotExist() { + try { + // add the repository that `NOT` have latest release + GHRelease release = gitHub.getRepository("kamontat/Java8Example").getLatestRelease(); + assertNull(release); + } catch (IOException e) { + e.printStackTrace(); + fail(); + } + } private GHRepository getRepository() throws IOException { return gitHub.getOrganization("github-api-test-org").getRepository("jenkins");