Commit a2f83183 authored by Greg Messner's avatar Greg Messner
Browse files

Misc. webhook related fixes.

parent 3b03f971
package com.messners.gitlab.api.webhook;
import javax.xml.bind.annotation.XmlAccessType;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import javax.xml.bind.annotation.XmlAccessorType;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "object_kind",
visible = true
)
@JsonSubTypes({
@JsonSubTypes.Type(value = IssueEvent.class, name = IssueEvent.OBJECT_KIND),
@JsonSubTypes.Type(value = PushEvent.class, name = PushEvent.OBJECT_KIND),
@JsonSubTypes.Type(value = MergeRequestEvent.class, name = MergeRequestEvent.OBJECT_KIND)
})
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class EventObject {
public static enum ObjectKind {
ISSUE, MERGE_REQUEST, PUSH;
@Override
public String toString() {
return (name().toLowerCase());
}
}
private ObjectKind objectKind;
public ObjectKind getObjectKind() {
return (this.objectKind);
}
public void setObjectKind(ObjectKind objectKind) {
this.objectKind = objectKind;
}
}
...@@ -9,7 +9,7 @@ import com.messners.gitlab.api.models.Assignee; ...@@ -9,7 +9,7 @@ import com.messners.gitlab.api.models.Assignee;
import com.messners.gitlab.api.models.User; import com.messners.gitlab.api.models.User;
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class IssueEvent extends EventObject { public class IssueEvent implements Event {
public static final String X_GITLAB_EVENT = "Issue Hook"; public static final String X_GITLAB_EVENT = "Issue Hook";
public static final String OBJECT_KIND = "issue"; public static final String OBJECT_KIND = "issue";
...@@ -19,6 +19,15 @@ public class IssueEvent extends EventObject { ...@@ -19,6 +19,15 @@ public class IssueEvent extends EventObject {
private EventRepository repository; private EventRepository repository;
private ObjectAttributes objectAttributes; private ObjectAttributes objectAttributes;
private Assignee assignee; private Assignee assignee;
public String getObjectKind() {
return (OBJECT_KIND);
}
public void setObjectKind(String objectKind) {
if (!OBJECT_KIND.equals(objectKind))
throw new RuntimeException("Invalid object_kind (" + objectKind + "), must be '" + OBJECT_KIND + "'");
}
public User getUser() { public User getUser() {
return user; return user;
......
...@@ -6,15 +6,34 @@ import javax.xml.bind.annotation.XmlAccessType; ...@@ -6,15 +6,34 @@ import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import com.messners.gitlab.api.models.Assignee; import com.messners.gitlab.api.models.Assignee;
import com.messners.gitlab.api.models.User;
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class MergeRequestEvent extends EventObject { public class MergeRequestEvent implements Event {
public static final String X_GITLAB_EVENT = "Merge Request Hook"; public static final String X_GITLAB_EVENT = "Merge Request Hook";
public static final String OBJECT_KIND = "merge_request"; public static final String OBJECT_KIND = "merge_request";
private User user;
private ObjectAttributes objectAttributes; private ObjectAttributes objectAttributes;
public String getObjectKind() {
return (OBJECT_KIND);
}
public void setObjectKind(String objectKind) {
if (!OBJECT_KIND.equals(objectKind))
throw new RuntimeException("Invalid object_kind (" + objectKind + "), must be '" + OBJECT_KIND + "'");
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public ObjectAttributes getObjectAttributes() { public ObjectAttributes getObjectAttributes() {
return this.objectAttributes; return this.objectAttributes;
} }
...@@ -53,7 +72,7 @@ public class MergeRequestEvent extends EventObject { ...@@ -53,7 +72,7 @@ public class MergeRequestEvent extends EventObject {
private Boolean workInProgress; private Boolean workInProgress;
private String url; private String url;
private String action; private String action;
private Assignee assigneee; private Assignee assignee;
public Integer getAssigneeId() { public Integer getAssigneeId() {
return this.assigneeId; return this.assigneeId;
...@@ -263,12 +282,12 @@ public class MergeRequestEvent extends EventObject { ...@@ -263,12 +282,12 @@ public class MergeRequestEvent extends EventObject {
this.action = action; this.action = action;
} }
public Assignee getAssigneee() { public Assignee getAssignee() {
return assigneee; return assignee;
} }
public void setAssigneee(Assignee assigneee) { public void setAssignee(Assignee assignee) {
this.assigneee = assigneee; this.assignee = assignee;
} }
} }
} }
...@@ -9,10 +9,12 @@ import javax.xml.bind.annotation.XmlAccessorType; ...@@ -9,10 +9,12 @@ import javax.xml.bind.annotation.XmlAccessorType;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class PushEvent extends EventObject { public class PushEvent implements Event {
public static final String X_GITLAB_EVENT = "Push Hook"; public static final String X_GITLAB_EVENT = "Push Hook";
public static final String OBJECT_KIND = "push"; public static final String OBJECT_KIND = "push";
private String eventName;
private String after; private String after;
private String before; private String before;
...@@ -29,6 +31,23 @@ public class PushEvent extends EventObject { ...@@ -29,6 +31,23 @@ public class PushEvent extends EventObject {
private EventRepository repository; private EventRepository repository;
private List<EventCommit> commits; private List<EventCommit> commits;
private Integer totalCommitsCount; private Integer totalCommitsCount;
public String getObjectKind() {
return (OBJECT_KIND);
}
public void setObjectKind(String objectKind) {
if (!OBJECT_KIND.equals(objectKind))
throw new RuntimeException("Invalid object_kind (" + objectKind + "), must be '" + OBJECT_KIND + "'");
}
public String getEventName() {
return (eventName);
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public String getAfter() { public String getAfter() {
return this.after; return this.after;
......
...@@ -9,10 +9,14 @@ import org.junit.BeforeClass; ...@@ -9,10 +9,14 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.messners.gitlab.api.webhook.EventObject; import com.fasterxml.jackson.databind.SerializationFeature;
import com.messners.gitlab.api.webhook.Event;
import com.messners.gitlab.api.webhook.IssueEvent;
import com.messners.gitlab.api.webhook.MergeRequestEvent;
import com.messners.gitlab.api.webhook.PushEvent; import com.messners.gitlab.api.webhook.PushEvent;
public class TestGitLabApiEvents { public class TestGitLabApiEvents {
...@@ -26,13 +30,14 @@ public class TestGitLabApiEvents { ...@@ -26,13 +30,14 @@ public class TestGitLabApiEvents {
@BeforeClass @BeforeClass
public static void setup() { public static void setup() {
jacksonJson = new JacksonJson(); jacksonJson = new JacksonJson();
jacksonJson.getObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
} }
@Test @Test
public void testIssueEvent() { public void testIssueEvent() {
try { try {
EventObject issueEvent = makeFakeApiCall(EventObject.class, "issue-event"); Event issueEvent = makeFakeApiCall(IssueEvent.class, "issue-event");
assertTrue(compareJson(issueEvent, "issue-event")); assertTrue(compareJson(issueEvent, "issue-event"));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -43,7 +48,7 @@ public class TestGitLabApiEvents { ...@@ -43,7 +48,7 @@ public class TestGitLabApiEvents {
public void testMergeRequestEvent() { public void testMergeRequestEvent() {
try { try {
EventObject mergeRequestEvent = makeFakeApiCall(EventObject.class, "merge-request-event"); Event mergeRequestEvent = makeFakeApiCall(MergeRequestEvent.class, "merge-request-event");
assertTrue(compareJson(mergeRequestEvent, "merge-request-event")); assertTrue(compareJson(mergeRequestEvent, "merge-request-event"));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -74,7 +79,18 @@ public class TestGitLabApiEvents { ...@@ -74,7 +79,18 @@ public class TestGitLabApiEvents {
String objectJson = jacksonJson.marshal(apiObject); String objectJson = jacksonJson.marshal(apiObject);
JsonNode tree1 = jacksonJson.getObjectMapper().readTree(objectJson.getBytes()); JsonNode tree1 = jacksonJson.getObjectMapper().readTree(objectJson.getBytes());
JsonNode tree2 = jacksonJson.getObjectMapper().readTree(reader); JsonNode tree2 = jacksonJson.getObjectMapper().readTree(reader);
boolean sameJson = tree1.equals(tree2); boolean sameJson = tree1.equals(tree2);
if (!sameJson) {
System.out.println("JSON did not match:");
sortedDump(tree1);
sortedDump(tree2);
}
return (sameJson); return (sameJson);
} }
private void sortedDump(final JsonNode node) throws JsonProcessingException {
final Object obj = jacksonJson.getObjectMapper().treeToValue(node, Object.class);
System.out.println(jacksonJson.getObjectMapper().writeValueAsString(obj));
}
} }
{ {
"object_kind":"issue", "object_kind": "issue",
"object_attributes":{ "user": {
"id":301, "name": "Administrator",
"title":"New API - create/update/delete file", "username": "root",
"assignee_id":51, "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
"author_id":51, },
"project_id":14, "project":{
"created_at":"2013-12-03T17:15:43Z", "name":"Gitlab Test",
"updated_at":"2013-12-03T17:15:43Z", "description":"Aut reprehenderit ut est.",
"position":0, "web_url":"http://example.com/gitlabhq/gitlab-test",
"description":"Create new API for manipulations with repository", "git_ssh_url":"git@example.com:gitlabhq/gitlab-test.git",
"state":"opened", "git_http_url":"http://example.com/gitlabhq/gitlab-test.git",
"iid":23 "namespace":"GitlabHQ",
"visibility_level":20,
"path_with_namespace":"gitlabhq/gitlab-test",
"default_branch":"master",
"homepage":"http://example.com/gitlabhq/gitlab-test",
"url":"http://example.com/gitlabhq/gitlab-test.git",
"ssh_url":"git@example.com:gitlabhq/gitlab-test.git",
"http_url":"http://example.com/gitlabhq/gitlab-test.git"
},
"repository":{
"name": "Gitlab Test",
"url": "http://example.com/gitlabhq/gitlab-test.git",
"description": "Aut reprehenderit ut est.",
"homepage": "http://example.com/gitlabhq/gitlab-test"
},
"object_attributes": {
"id": 301,
"title": "New API: create/update/delete file",
"assignee_id": 51,
"author_id": 51,
"project_id": 14,
"created_at": "2013-12-03T17:15:43Z",
"updated_at": "2013-12-03T17:15:43Z",
"position": 0,
"description": "Create new API for manipulations with repository",
"state": "opened",
"iid": 23,
"url": "http://example.com/diaspora/issues/23",
"action": "open"
},
"assignee": {
"name": "User1",
"username": "user1",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
} }
} }
\ No newline at end of file
{ {
"object_kind":"merge_request", "object_kind": "merge_request",
"object_attributes":{ "user": {
"id":99, "name": "Administrator",
"target_branch":"master", "username": "root",
"source_branch":"ms-viewport", "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
"source_project_id":14, },
"author_id":51, "object_attributes": {
"assignee_id":6, "id": 99,
"title":"MS-Viewport", "target_branch": "master",
"created_at":"2013-12-03T17:23:34Z", "source_branch": "ms-viewport",
"updated_at":"2013-12-03T17:23:34Z", "source_project_id": 14,
"state":"opened", "author_id": 51,
"merge_status":"unchecked", "assignee_id": 6,
"target_project_id":14, "title": "MS-Viewport",
"iid":1, "created_at": "2013-12-03T17:23:34Z",
"description":"" "updated_at": "2013-12-03T17:23:34Z",
"state": "opened",
"merge_status": "unchecked",
"target_project_id": 14,
"iid": 1,
"description": "",
"source":{
"name":"Awesome Project",
"description":"Aut reprehenderit ut est.",
"web_url":"http://example.com/awesome_space/awesome_project",
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
"namespace":"Awesome Space",
"visibility_level":20,
"path_with_namespace":"awesome_space/awesome_project",
"default_branch":"master",
"homepage":"http://example.com/awesome_space/awesome_project",
"url":"http://example.com/awesome_space/awesome_project.git",
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
"http_url":"http://example.com/awesome_space/awesome_project.git"
},
"target": {
"name":"Awesome Project",
"description":"Aut reprehenderit ut est.",
"web_url":"http://example.com/awesome_space/awesome_project",
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
"namespace":"Awesome Space",
"visibility_level":20,
"path_with_namespace":"awesome_space/awesome_project",
"default_branch":"master",
"homepage":"http://example.com/awesome_space/awesome_project",
"url":"http://example.com/awesome_space/awesome_project.git",
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
"http_url":"http://example.com/awesome_space/awesome_project.git"
},
"last_commit": {
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "fixed readme",
"timestamp": "2012-01-03T23:36:29Z",
"url": "http://example.com/awesome_space/awesome_project/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"author": {
"name": "GitLab dev user",
"email": "gitlabdev@dv6700.(none)"
}
},
"work_in_progress": false,
"url": "http://example.com/diaspora/merge_requests/1",
"action": "open",
"assignee": {
"name": "User1",
"username": "user1",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
}
} }
} }
\ No newline at end of file
{ {
"object_kind": "push", "object_kind": "push",
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22", "before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
"after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"ref": "refs/heads/master", "ref": "refs/heads/master",
"checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", "checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"user_id": 4, "user_id": 4,
"user_name": "John Smith", "user_name": "John Smith",
"user_email": "john@example.com", "user_email": "john@example.com",
"user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80", "user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
"project_id": 15, "project_id": 15,
"project":{ "project":{
"name":"Diaspora", "name":"Diaspora",
"description":"", "description":"",
"web_url":"http://example.com/mike/diaspora", "web_url":"http://example.com/mike/diaspora",
"git_ssh_url":"git@example.com:mike/diaspora.git", "git_ssh_url":"git@example.com:mike/diaspora.git",
"git_http_url":"http://example.com/mike/diaspora.git", "git_http_url":"http://example.com/mike/diaspora.git",
"namespace":"Mike", "namespace":"Mike",
"visibility_level":0, "visibility_level":0,
"path_with_namespace":"mike/diaspora", "path_with_namespace":"mike/diaspora",
"default_branch":"master", "default_branch":"master",
"homepage":"http://example.com/mike/diaspora", "homepage":"http://example.com/mike/diaspora",
"url":"git@example.com:mike/diaspora.git", "url":"git@example.com:mike/diaspora.git",
"ssh_url":"git@example.com:mike/diaspora.git", "ssh_url":"git@example.com:mike/diaspora.git",
"http_url":"http://example.com/mike/diaspora.git" "http_url":"http://example.com/mike/diaspora.git"
}, },
"repository":{ "repository":{
"name": "Diaspora", "name": "Diaspora",
"url": "git@example.com:mike/diaspora.git", "url": "git@example.com:mike/diaspora.git",
"description": "", "description": "",
"homepage": "http://example.com/mike/diaspora", "homepage": "http://example.com/mike/diaspora",
"git_http_url":"http://example.com/mike/diaspora.git", "git_http_url":"http://example.com/mike/diaspora.git",
"git_ssh_url":"git@example.com:mike/diaspora.git", "git_ssh_url":"git@example.com:mike/diaspora.git",
"visibility_level":0 "visibility_level":0
}, },
"commits": [ "commits": [
{ {
"id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"message": "Update Catalan translation to e38cb41.", "message": "Update Catalan translation to e38cb41.",
"timestamp": "2011-12-12T14:27:31Z", "timestamp": "2011-12-12T14:27:31Z",
"url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", "url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"author": { "author": {
"name": "Jordi Mallach", "name": "Jordi Mallach",
"email": "jordi@softcatala.org" "email": "jordi@softcatala.org"
}, },
"added": ["CHANGELOG"], "added": ["CHANGELOG"],
"modified": ["app/controller/application.rb"], "modified": ["app/controller/application.rb"],
"removed": [] "removed": []
}, },
{ {
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "fixed readme", "message": "fixed readme",
"timestamp": "2012-01-03T23:36:29Z", "timestamp": "2012-01-03T23:36:29Z",
"url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7", "url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"author": { "author": {
"name": "GitLab dev user", "name": "GitLab dev user",
"email": "gitlabdev@dv6700.(none)" "email": "gitlabdev@dv6700.(none)"
}, },
"added": ["CHANGELOG"], "added": ["CHANGELOG"],
"modified": ["app/controller/application.rb"], "modified": ["app/controller/application.rb"],
"removed": [] "removed": []
} }
], ],
"total_commits_count": 4 "total_commits_count": 4
} }
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment