Skip to content

Commit

Permalink
Fix the push event payload
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Nov 14, 2016
1 parent 9988a09 commit e544c7a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
44 changes: 24 additions & 20 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public String getLanguage() {
}

public GHUser getOwner() throws IOException {
return root.isOffline() ? owner : root.getUser(owner.login); // because 'owner' isn't fully populated
return root.isOffline() ? owner : root.getUser(getOwnerName()); // because 'owner' isn't fully populated
}

public GHIssue getIssue(int id) throws IOException {
Expand Down Expand Up @@ -339,7 +339,11 @@ public Map<String,Long> listLanguages() throws IOException {
}

public String getOwnerName() {
return owner.login;
// consistency of the GitHub API is super... some serialized forms of GHRepository populate
// a full GHUser while others populate only the owner and email. This later form is super helpful
// in putting the login in owner.name not owner.login... thankfully we can easily identify this
// second set because owner.login will be null
return owner.login != null ? owner.login : owner.name;
}

public boolean hasIssues() {
Expand Down Expand Up @@ -467,7 +471,7 @@ public Set<String> getCollaboratorNames() throws IOException {
* If this repository belongs to an organization, return a set of teams.
*/
public Set<GHTeam> getTeams() throws IOException {
return Collections.unmodifiableSet(new HashSet<GHTeam>(Arrays.asList(GHTeam.wrapUp(root.retrieve().to(getApiTailUrl("teams"), GHTeam[].class), root.getOrganization(owner.login)))));
return Collections.unmodifiableSet(new HashSet<GHTeam>(Arrays.asList(GHTeam.wrapUp(root.retrieve().to(getApiTailUrl("teams"), GHTeam[].class), root.getOrganization(getOwnerName())))));
}

public void addCollaborators(GHUser... users) throws IOException {
Expand Down Expand Up @@ -551,7 +555,7 @@ public void delete() throws IOException {
try {
new Requester(root).method("DELETE").to(getApiTailUrl(""));
} catch (FileNotFoundException x) {
throw (FileNotFoundException) new FileNotFoundException("Failed to delete " + owner.login + "/" + name + "; might not exist, or you might need the delete_repo scope in your token: http://stackoverflow.com/a/19327004/12916").initCause(x);
throw (FileNotFoundException) new FileNotFoundException("Failed to delete " + getOwnerName() + "/" + name + "; might not exist, or you might need the delete_repo scope in your token: http://stackoverflow.com/a/19327004/12916").initCause(x);
}
}

Expand Down Expand Up @@ -742,7 +746,7 @@ public GHCompare getCompare(GHBranch id1, GHBranch id2) throws IOException {
* @throws IOException on failure communicating with GitHub
*/
public GHRef[] getRefs() throws IOException {
return GHRef.wrap(root.retrieve().to(String.format("/repos/%s/%s/git/refs", owner.login, name), GHRef[].class), root);
return GHRef.wrap(root.retrieve().to(String.format("/repos/%s/%s/git/refs", getOwnerName(), name), GHRef[].class), root);
}

/**
Expand All @@ -752,7 +756,7 @@ public GHRef[] getRefs() throws IOException {
* @throws IOException on failure communicating with GitHub, potentially due to an invalid ref type being requested
*/
public GHRef[] getRefs(String refType) throws IOException {
return GHRef.wrap(root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refType), GHRef[].class),root);
return GHRef.wrap(root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", getOwnerName(), name, refType), GHRef[].class),root);
}
/**
* Retrive a ref of the given type for the current GitHub repository.
Expand All @@ -769,7 +773,7 @@ public GHRef getRef(String refName) throws IOException {
// FIXME: how about other URL unsafe characters, like space, @, : etc? do we need to be using URLEncoder.encode()?
// OTOH, '/' need no escaping
refName = refName.replaceAll("#", "%23");
return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refName), GHRef.class).wrap(root);
return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", getOwnerName(), name, refName), GHRef.class).wrap(root);
}
/**
* Retrive a tree of the given type for the current GitHub repository.
Expand All @@ -781,7 +785,7 @@ public GHRef getRef(String refName) throws IOException {
* invalid tree type being requested
*/
public GHTree getTree(String sha) throws IOException {
String url = String.format("/repos/%s/%s/git/trees/%s", owner.login, name, sha);
String url = String.format("/repos/%s/%s/git/trees/%s", getOwnerName(), name, sha);
return root.retrieve().to(url, GHTree.class).wrap(root);
}

Expand All @@ -796,7 +800,7 @@ public GHTree getTree(String sha) throws IOException {
* invalid tree type being requested
*/
public GHTree getTreeRecursive(String sha, int recursive) throws IOException {
String url = String.format("/repos/%s/%s/git/trees/%s?recursive=%d", owner.login, name, sha, recursive);
String url = String.format("/repos/%s/%s/git/trees/%s?recursive=%d", getOwnerName(), name, sha, recursive);
return root.retrieve().to(url, GHTree.class).wrap(root);
}

Expand All @@ -806,7 +810,7 @@ public GHTree getTreeRecursive(String sha, int recursive) throws IOException {
public GHCommit getCommit(String sha1) throws IOException {
GHCommit c = commits.get(sha1);
if (c==null) {
c = root.retrieve().to(String.format("/repos/%s/%s/commits/%s", owner.login, name, sha1), GHCommit.class).wrapUp(this);
c = root.retrieve().to(String.format("/repos/%s/%s/commits/%s", getOwnerName(), name, sha1), GHCommit.class).wrapUp(this);
commits.put(sha1,c);
}
return c;
Expand All @@ -818,7 +822,7 @@ public GHCommit getCommit(String sha1) throws IOException {
public PagedIterable<GHCommit> listCommits() {
return new PagedIterable<GHCommit>() {
public PagedIterator<GHCommit> _iterator(int pageSize) {
return new PagedIterator<GHCommit>(root.retrieve().asIterator(String.format("/repos/%s/%s/commits", owner.login, name), GHCommit[].class, pageSize)) {
return new PagedIterator<GHCommit>(root.retrieve().asIterator(String.format("/repos/%s/%s/commits", getOwnerName(), name), GHCommit[].class, pageSize)) {
protected void wrapUp(GHCommit[] page) {
for (GHCommit c : page)
c.wrapUp(GHRepository.this);
Expand All @@ -841,7 +845,7 @@ public GHCommitQueryBuilder queryCommits() {
public PagedIterable<GHCommitComment> listCommitComments() {
return new PagedIterable<GHCommitComment>() {
public PagedIterator<GHCommitComment> _iterator(int pageSize) {
return new PagedIterator<GHCommitComment>(root.retrieve().asIterator(String.format("/repos/%s/%s/comments", owner.login, name), GHCommitComment[].class, pageSize)) {
return new PagedIterator<GHCommitComment>(root.retrieve().asIterator(String.format("/repos/%s/%s/comments", getOwnerName(), name), GHCommitComment[].class, pageSize)) {
@Override
protected void wrapUp(GHCommitComment[] page) {
for (GHCommitComment c : page)
Expand Down Expand Up @@ -898,7 +902,7 @@ private GHContentWithLicense getLicenseContent_() throws IOException {
public PagedIterable<GHCommitStatus> listCommitStatuses(final String sha1) throws IOException {
return new PagedIterable<GHCommitStatus>() {
public PagedIterator<GHCommitStatus> _iterator(int pageSize) {
return new PagedIterator<GHCommitStatus>(root.retrieve().asIterator(String.format("/repos/%s/%s/statuses/%s", owner.login, name, sha1), GHCommitStatus[].class, pageSize)) {
return new PagedIterator<GHCommitStatus>(root.retrieve().asIterator(String.format("/repos/%s/%s/statuses/%s", getOwnerName(), name, sha1), GHCommitStatus[].class, pageSize)) {
@Override
protected void wrapUp(GHCommitStatus[] page) {
for (GHCommitStatus c : page)
Expand Down Expand Up @@ -933,7 +937,7 @@ public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, Strin
.with("target_url", targetUrl)
.with("description", description)
.with("context", context)
.to(String.format("/repos/%s/%s/statuses/%s",owner.login,this.name,sha1),GHCommitStatus.class).wrapUp(root);
.to(String.format("/repos/%s/%s/statuses/%s",getOwnerName(),this.name,sha1),GHCommitStatus.class).wrapUp(root);
}

/**
Expand All @@ -949,7 +953,7 @@ public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, Strin
public PagedIterable<GHEventInfo> listEvents() throws IOException {
return new PagedIterable<GHEventInfo>() {
public PagedIterator<GHEventInfo> _iterator(int pageSize) {
return new PagedIterator<GHEventInfo>(root.retrieve().asIterator(String.format("/repos/%s/%s/events", owner.login, name), GHEventInfo[].class, pageSize)) {
return new PagedIterator<GHEventInfo>(root.retrieve().asIterator(String.format("/repos/%s/%s/events", getOwnerName(), name), GHEventInfo[].class, pageSize)) {
@Override
protected void wrapUp(GHEventInfo[] page) {
for (GHEventInfo c : page)
Expand Down Expand Up @@ -1076,8 +1080,8 @@ public GHHook createWebHook(URL url) throws IOException {
// }

private void verifyMine() throws IOException {
if (!root.login.equals(owner.login))
throw new IOException("Operation not applicable to a repository owned by someone else: "+owner.login);
if (!root.login.equals(getOwnerName()))
throw new IOException("Operation not applicable to a repository owned by someone else: " + getOwnerName());
}

/**
Expand Down Expand Up @@ -1425,21 +1429,21 @@ public GHNotificationStream listNotifications() {

@Override
public int hashCode() {
return ("Repository:"+owner.login+":"+name).hashCode();
return ("Repository:"+getOwnerName()+":"+name).hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof GHRepository) {
GHRepository that = (GHRepository) obj;
return this.owner.login.equals(that.owner.login)
return this.getOwnerName().equals(that.getOwnerName())
&& this.name.equals(that.name);
}
return false;
}

String getApiTailUrl(String tail) {
if (tail.length()>0 && !tail.startsWith("/")) tail='/'+tail;
return "/repos/" + owner.login + "/" + name +tail;
return "/repos/" + getOwnerName() + "/" + name +tail;
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHEventPayloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void push() throws Exception {
assertThat(event.getCommits().get(0).getModified().size(), is(1));
assertThat(event.getCommits().get(0).getModified().get(0), is("README.md"));
assertThat(event.getRepository().getName(), is("public-repo"));
assertThat(event.getRepository().getOwner().getLogin(), is("baxterthehacker"));
assertThat(event.getRepository().getOwnerName(), is("baxterthehacker"));
assertThat(event.getRepository().getUrl().toExternalForm(), is("https://github.com/baxterthehacker/public-repo"));
assertThat(event.getPusher().getName(), is("baxterthehacker"));
assertThat(event.getPusher().getEmail(), is("[email protected]"));
Expand Down
19 changes: 2 additions & 17 deletions src/test/resources/org/kohsuke/github/GHEventPayloadTest/push.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,8 @@
"name": "public-repo",
"full_name": "baxterthehacker/public-repo",
"owner": {
"login": "baxterthehacker",
"id": 6752317,
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/baxterthehacker",
"html_url": "https://github.com/baxterthehacker",
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
"type": "User",
"site_admin": false
"name": "baxterthehacker",
"email": "[email protected]"
},
"private": false,
"html_url": "https://github.com/baxterthehacker/public-repo",
Expand Down

0 comments on commit e544c7a

Please sign in to comment.