From 166e26d10184b3e2a1cb2c086d6b9ed3e9b50305 Mon Sep 17 00:00:00 2001 From: kamontat Date: Sun, 19 Feb 2017 23:57:44 +0700 Subject: [PATCH 1/4] add latest release --- src/main/java/org/kohsuke/github/GHRepository.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 74fbb4d665..4a71f64ace 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -298,6 +298,10 @@ public GHRef createRef(String name, String sha) throws IOException { public List getReleases() throws IOException { return listReleases().asList(); } + + public GHRelease latestRelease() throws IOException { + return root.retrieve().to(getApiTailUrl("releases/latest"), GHRelease.class).wrap(this); + } public PagedIterable listReleases() throws IOException { return new PagedIterable() { From fd859815b039a84c3dd53e38ff8d41fefc2c2b23 Mon Sep 17 00:00:00 2001 From: kamontat Date: Mon, 20 Feb 2017 09:36:28 +0700 Subject: [PATCH 2/4] fixed indent, rename to getLastestRelease --- src/main/java/org/kohsuke/github/GHRepository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 4a71f64ace..22ab01e796 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -299,9 +299,9 @@ public List getReleases() throws IOException { return listReleases().asList(); } - public GHRelease latestRelease() throws IOException { + public GHRelease getLatestRelease() throws IOException { return root.retrieve().to(getApiTailUrl("releases/latest"), GHRelease.class).wrap(this); - } + } public PagedIterable listReleases() throws IOException { return new PagedIterable() { From 1cffea892b3b2927cf4733259c3095ec04309b4d Mon Sep 17 00:00:00 2001 From: kamontat Date: Mon, 20 Feb 2017 10:20:08 +0700 Subject: [PATCH 3/4] add test --- .../org/kohsuke/github/RepositoryTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) 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"); From 5554332b5be0df4b801813a903c8ab98aef8d0b7 Mon Sep 17 00:00:00 2001 From: kamontat Date: Mon, 20 Feb 2017 10:20:53 +0700 Subject: [PATCH 4/4] add return null if latest release not found --- src/main/java/org/kohsuke/github/GHRepository.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 22ab01e796..f233f8b4fe 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -300,7 +300,11 @@ public List getReleases() throws IOException { } public GHRelease getLatestRelease() throws IOException { - return root.retrieve().to(getApiTailUrl("releases/latest"), GHRelease.class).wrap(this); + 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 {