Skip to content

Commit 0c31bd4

Browse files
committed
Fixed issue unmarhalling merge_request system hook events (#332).
1 parent 04a7698 commit 0c31bd4

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

src/main/java/org/gitlab4j/api/systemhooks/MergeRequestSystemHookEvent.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22

33
import org.gitlab4j.api.webhook.MergeRequestEvent;
44

5-
import com.fasterxml.jackson.annotation.JsonIgnore;
6-
import com.fasterxml.jackson.annotation.JsonSubTypes;
7-
import com.fasterxml.jackson.annotation.JsonTypeInfo;
8-
9-
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, property="object_kind")
10-
@JsonSubTypes({
11-
@JsonSubTypes.Type(value = MergeRequestSystemHookEvent.class, name = MergeRequestSystemHookEvent.OBJECT_KIND),
12-
})
135
public class MergeRequestSystemHookEvent extends MergeRequestEvent implements SystemHookEvent {
146

157
public static final String X_GITLAB_EVENT = "System Hook";
8+
public static final String MERGE_REQUEST_EVENT = "merge_request";
9+
10+
@Override
11+
public String getObjectKind() {
12+
return (MERGE_REQUEST_EVENT);
13+
}
1614

17-
@JsonIgnore
1815
@Override
1916
public String getEventName() {
20-
return (OBJECT_KIND);
17+
return (MERGE_REQUEST_EVENT);
2118
}
2219
}

src/main/java/org/gitlab4j/api/systemhooks/SystemHookEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
@JsonSubTypes.Type(value = RemoveGroupMemberSystemHookEvent.class, name = GroupMemberSystemHookEvent.GROUP_MEMBER_REMOVED_EVENT),
2727
@JsonSubTypes.Type(value = PushSystemHookEvent.class, name = PushSystemHookEvent.PUSH_EVENT),
2828
@JsonSubTypes.Type(value = TagPushSystemHookEvent.class, name = TagPushSystemHookEvent.TAG_PUSH_EVENT),
29-
@JsonSubTypes.Type(value = RepositorySystemHookEvent.class, name = RepositorySystemHookEvent.REPOSITORY_UPDATE_EVENT)
29+
@JsonSubTypes.Type(value = RepositorySystemHookEvent.class, name = RepositorySystemHookEvent.REPOSITORY_UPDATE_EVENT),
30+
@JsonSubTypes.Type(value = MergeRequestSystemHookEvent.class, name = MergeRequestSystemHookEvent.MERGE_REQUEST_EVENT)
3031
})
3132
public interface SystemHookEvent {
3233

src/main/java/org/gitlab4j/api/systemhooks/SystemHookManager.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.gitlab4j.api.utils.JacksonJson;
1717

1818
import com.fasterxml.jackson.databind.JsonNode;
19+
import com.fasterxml.jackson.databind.node.ObjectNode;
1920

2021
/**
2122
* This class provides a handler for processing GitLab System Hook callouts.
@@ -90,23 +91,25 @@ public void handleEvent(HttpServletRequest request) throws GitLabApiException {
9091
tree = jacksonJson.readTree(reader);
9192
}
9293

93-
if (tree.has("object_kind")) {
94+
// NOTE: This is a hack based on the GitLab documentation showing that the "event_name" property
95+
// is missing from the merge_request system hook event
96+
if (!tree.has("event_name") && tree.has("object_kind")) {
9497
String objectKind = tree.asText("object_kind");
9598
switch (objectKind) {
96-
case MergeRequestSystemHookEvent.OBJECT_KIND:
97-
event = jacksonJson.unmarshal(MergeRequestSystemHookEvent.class, tree);
99+
case MergeRequestSystemHookEvent.MERGE_REQUEST_EVENT:
100+
ObjectNode node = (ObjectNode)tree;
101+
node.put("event_name", MergeRequestSystemHookEvent.MERGE_REQUEST_EVENT);
98102
break;
99103

100104
default:
101105
String message = "Unsupported object_kind, object_kind=" + objectKind;
102106
LOGGER.warning(message);
103107
throw new GitLabApiException(message);
104108
}
105-
106-
} else {
107-
event = jacksonJson.unmarshal(SystemHookEvent.class, tree);
108109
}
109110

111+
event = jacksonJson.unmarshal(SystemHookEvent.class, tree);
112+
110113
if (LOGGER.isLoggable(Level.FINE)) {
111114
LOGGER.fine(event.getEventName() + "\n" + jacksonJson.marshal(event) + "\n");
112115
}

0 commit comments

Comments
 (0)