Skip to content

Commit 4f17d35

Browse files
authored
Merge pull request hub4j#411 from tadfisher/master
Add GHRepository.getRelease and GHRepository.getReleaseByTagName
2 parents 3cfcad7 + 75918c5 commit 4f17d35

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,22 @@ public GHRef createRef(String name, String sha) throws IOException {
308308
public List<GHRelease> getReleases() throws IOException {
309309
return listReleases().asList();
310310
}
311+
312+
public GHRelease getRelease(long id) throws IOException {
313+
try {
314+
return root.retrieve().to(getApiTailUrl("releases/" + id), GHRelease.class).wrap(this);
315+
} catch (FileNotFoundException e) {
316+
return null; // no release for this id
317+
}
318+
}
319+
320+
public GHRelease getReleaseByTagName(String tag) throws IOException {
321+
try {
322+
return root.retrieve().to(getApiTailUrl("releases/tags/" + tag), GHRelease.class).wrap(this);
323+
} catch (FileNotFoundException e) {
324+
return null; // no release for this tag
325+
}
326+
}
311327

312328
public GHRelease getLatestRelease() throws IOException {
313329
try {

src/test/java/org/kohsuke/github/RepositoryTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,36 @@ public void LatestRepositoryNotExist() {
9494
}
9595
}
9696

97+
@Test public void listReleases() throws IOException {
98+
PagedIterable<GHRelease> releases = gitHub.getOrganization("github").getRepository("hub").listReleases();
99+
assertTrue(releases.iterator().hasNext());
100+
}
101+
102+
@Test
103+
public void getReleaseExists() throws IOException {
104+
GHRelease release = gitHub.getOrganization("github").getRepository("hub").getRelease(6839710);
105+
assertEquals("v2.3.0-pre10", release.getTagName());
106+
}
107+
108+
@Test
109+
public void getReleaseDoesNotExist() throws IOException {
110+
GHRelease release = gitHub.getOrganization("github").getRepository("hub").getRelease(Long.MAX_VALUE);
111+
assertNull(release);
112+
}
113+
114+
@Test
115+
public void getReleaseByTagNameExists() throws IOException {
116+
GHRelease release = gitHub.getOrganization("github").getRepository("hub").getReleaseByTagName("v2.3.0-pre10");
117+
assertNotNull(release);
118+
assertEquals("v2.3.0-pre10", release.getTagName());
119+
}
120+
121+
@Test
122+
public void getReleaseByTagNameDoesNotExist() throws IOException {
123+
GHRelease release = getRepository().getReleaseByTagName("foo-bar-baz");
124+
assertNull(release);
125+
}
126+
97127
private GHRepository getRepository() throws IOException {
98128
return gitHub.getOrganization("github-api-test-org").getRepository("jenkins");
99129
}

0 commit comments

Comments
 (0)