From 75918c59cc69dd08653bfbd13d8cb965f972f494 Mon Sep 17 00:00:00 2001 From: Tad Fisher Date: Mon, 5 Feb 2018 14:30:24 -0800 Subject: [PATCH] Add GHRepository.getRelease and GHRepository.getReleaseByTagName These implement the API endpoints for: - GET /repos/:owner/:repo/releases/:id - GET /repos/:owner/:repo/releases/tags/:tag --- .../java/org/kohsuke/github/GHRepository.java | 16 ++++++++++ .../org/kohsuke/github/RepositoryTest.java | 30 +++++++++++++++++++ 2 files changed, 46 insertions(+) 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"); }