Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add issue events API #479

Merged
merged 10 commits into from
Oct 11, 2019
18 changes: 18 additions & 0 deletions src/main/java/org/kohsuke/github/GHIssue.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,22 @@ public URL getUrl() {
return GitHub.parseURL(html_url);
}
}

/**
* Lists events for this issue.
* See https://developer.github.com/v3/issues/events/
*/
public PagedIterable<GHIssueEvent> listEvents() throws IOException {
return new PagedIterable<GHIssueEvent>() {
public PagedIterator<GHIssueEvent> _iterator(int pageSize) {
return new PagedIterator<GHIssueEvent>(root.retrieve().asIterator(owner.getApiTailUrl(String.format("/issues/%s/events", number)), GHIssueEvent[].class, pageSize)) {
@Override
protected void wrapUp(GHIssueEvent[] page) {
for (GHIssueEvent c : page)
c.wrapUp(GHIssue.this);
}
};
}
};
}
}
81 changes: 81 additions & 0 deletions src/main/java/org/kohsuke/github/GHIssueEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.kohsuke.github;

import java.util.Date;

/**
* @author Martin van Zijl
*/
public class GHIssueEvent {
private GitHub root;

private long id;
private String node_id;
private String url;
private GHUser actor;
private String event;
private String commit_id;
private String commit_url;
private String created_at;

private GHIssue issue;

public long getId() {
return id;
}

public String getNodeId() {
return node_id;
}

public String getUrl() {
return url;
}

public GHUser getActor() {
return actor;
}

public String getEvent() {
return event;
}

public String getCommitId() {
return commit_id;
}

public String getCommitUrl() {
return commit_url;
}

public Date getCreatedAt() {
return GitHub.parseDate(created_at);
}

public GitHub getRoot() {
return root;
}

public GHIssue getIssue() {
return issue;
}

public GHIssueEvent wrapUp(GitHub root) {
this.root = root;
return this;
}

public GHIssueEvent wrapUp(GHIssue parent) {
this.issue = parent;
this.root = parent.root;
return this;
}

@Override
public String toString() {
return String.format("Issue %d was %s by %s on %s",
getIssue().getNumber(),
getEvent(),
getActor().getLogin(),
getCreatedAt().toString());
}
}
26 changes: 26 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -1775,4 +1775,30 @@ String getApiTailUrl(String tail) {
if (tail.length()>0 && !tail.startsWith("/")) tail='/'+tail;
return "/repos/" + getOwnerName() + "/" + name +tail;
}

/**
* Get all issue events for this repository.
* See https://developer.github.com/v3/issues/events/#list-events-for-a-repository
*/
public PagedIterable<GHIssueEvent> listIssueEvents() throws IOException {
return new PagedIterable<GHIssueEvent>() {
public PagedIterator<GHIssueEvent> _iterator(int pageSize) {
return new PagedIterator<GHIssueEvent>(root.retrieve().asIterator(getApiTailUrl("issues/events"), GHIssueEvent[].class, pageSize)) {
@Override
protected void wrapUp(GHIssueEvent[] page) {
for (GHIssueEvent c : page)
c.wrapUp(root);
}
};
}
};
}

/**
* Get a single issue event.
* See https://developer.github.com/v3/issues/events/#get-a-single-event
*/
public GHIssueEvent getIssueEvent(long id) throws IOException {
return root.retrieve().to(getApiTailUrl("issues/events/" + id), GHIssueEvent.class).wrapUp(root);
}
}
59 changes: 59 additions & 0 deletions src/test/java/org/kohsuke/github/GHIssueEventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.kohsuke.github;

import java.io.IOException;
import java.util.List;
import org.junit.Test;

/**
* @author Martin van Zijl
*/
public class GHIssueEventTest extends AbstractGitHubApiTestBase {

@Test
public void testEventsForSingleIssue() throws Exception {
// Create the issue.
GHRepository repo = getRepository();
GHIssueBuilder builder = repo.createIssue("Test from the API");
GHIssue issue = builder.create();

// Generate some events.
issue.addLabels("test-label");

// Test that the events are present.
List<GHIssueEvent> list = issue.listEvents().asList();
assertEquals(1, list.size());

GHIssueEvent event = list.get(0);
assertEquals(issue.getNumber(), event.getIssue().getNumber());
assertEquals("labeled", event.getEvent());

// Test that we can get a single event directly.
GHIssueEvent eventFromRepo = repo.getIssueEvent(event.getId());
assertEquals(event.getId(), eventFromRepo.getId());
assertEquals(event.getCreatedAt(), eventFromRepo.getCreatedAt());

// Close the issue.
issue.close();
}

@Test
public void testRepositoryEvents() throws Exception {
GHRepository repo = getRepository();
List<GHIssueEvent> list = repo.listIssueEvents().asList();
assertTrue(list.size() > 0);

int i = 0;
for (GHIssueEvent event : list) {
assertNotNull(event.getIssue());
if (i++ > 10) break;
}
}

protected GHRepository getRepository() throws IOException {
return getRepository(gitHub);
}

private GHRepository getRepository(GitHub gitHub) throws IOException {
return gitHub.getOrganization("github-api-test-org").getRepository("github-api");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"login": "github-api-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"url": "https://api.github.com/orgs/github-api-test-org",
"repos_url": "https://api.github.com/orgs/github-api-test-org/repos",
"events_url": "https://api.github.com/orgs/github-api-test-org/events",
"hooks_url": "https://api.github.com/orgs/github-api-test-org/hooks",
"issues_url": "https://api.github.com/orgs/github-api-test-org/issues",
"members_url": "https://api.github.com/orgs/github-api-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/github-api-test-org/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"description": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 10,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/github-api-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2015-04-20T00:42:30Z",
"type": "Organization",
"total_private_repos": 0,
"owned_private_repos": 0,
"private_gists": 0,
"disk_usage": 132,
"collaborators": 0,
"billing_email": "kk@kohsuke.org",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 0,
"filled_seats": 7,
"seats": 0
}
}
Loading