Skip to content

Commit 68327bd

Browse files
author
Yang Guo
committed
support coding PR event
1 parent 9a96655 commit 68327bd

File tree

8 files changed

+257
-38
lines changed

8 files changed

+257
-38
lines changed

platform-util-git/src/main/java/com/flow/platform/util/git/hooks/CodingEvents.java

Lines changed: 118 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package com.flow.platform.util.git.hooks;
1818

1919
import com.flow.platform.util.git.GitException;
20-
import com.flow.platform.util.git.model.GitCommit;
2120
import com.flow.platform.util.git.model.GitEvent;
2221
import com.flow.platform.util.git.model.GitEventAuthor;
2322
import com.flow.platform.util.git.model.GitEventCommit;
2423
import com.flow.platform.util.git.model.GitEventType;
24+
import com.flow.platform.util.git.model.GitPullRequestEvent;
25+
import com.flow.platform.util.git.model.GitPullRequestEvent.State;
26+
import com.flow.platform.util.git.model.GitPullRequestInfo;
2527
import com.flow.platform.util.git.model.GitPushTagEvent;
2628
import com.flow.platform.util.git.model.GitSource;
2729
import com.google.gson.annotations.SerializedName;
@@ -37,20 +39,40 @@ public static class Hooks {
3739

3840
public final static String HEADER = "x-coding-event";
3941

40-
public final static String EVENT_TYPE_PUSH = "push";
42+
public final static String EVENT_TYPE_PUSH_OR_TAG = "push";
4143

42-
public final static String EVENT_TYPE_TAG = "push";
44+
public final static String EVENT_TYPE_PR = "merge_request";
45+
}
46+
47+
private class CodingUserHelper {
48+
49+
private String path;
50+
51+
@SerializedName(value = "web_url")
52+
private String url;
53+
54+
private String name;
4355

44-
public final static String EVENT_TYPE_PR = "pull_request";
56+
}
57+
58+
private class CodingRepoHelper {
59+
60+
@SerializedName(value = "web_url")
61+
private String url;
62+
63+
@SerializedName(value = "project_id")
64+
private String id;
65+
66+
private String name;
4567
}
4668

4769
public static class PushAndTagAdapter extends GitHookEventAdapter {
4870

4971
private class PushHelper {
5072

51-
private PushUserHelper user;
73+
private CodingUserHelper user;
5274

53-
private RepoHelper repository;
75+
private CodingRepoHelper repository;
5476

5577
private List<CommitHelper> commits;
5678
}
@@ -79,26 +101,6 @@ public GitEventCommit toGitEventCommit() {
79101

80102
}
81103

82-
private class PushUserHelper {
83-
84-
private String path;
85-
86-
@SerializedName(value = "web_url")
87-
private String url;
88-
89-
private String name;
90-
91-
}
92-
93-
private class RepoHelper {
94-
95-
@SerializedName(value = "web_url")
96-
private String url;
97-
98-
@SerializedName(value = "project_id")
99-
private String id;
100-
}
101-
102104
PushAndTagAdapter(GitSource gitSource, GitEventType eventType) {
103105
super(gitSource, eventType);
104106
}
@@ -158,4 +160,94 @@ public GitEvent convert(String json) throws GitException {
158160
}
159161
}
160162

163+
/**
164+
* Coding PR event only for 'open' pull request,
165+
* The close PR event the same as push
166+
*/
167+
public static class PullRequestAdapter extends GitHookEventAdapter {
168+
169+
public final static String STATE_OPEN = "create";
170+
171+
private class RequestRootHelper {
172+
173+
@SerializedName("merge_request")
174+
private MergeRequestHelper mergeRequest;
175+
176+
private CodingRepoHelper repository;
177+
178+
private String event;
179+
}
180+
181+
private class MergeRequestHelper {
182+
183+
@SerializedName("target_branch")
184+
private String targetBranch;
185+
186+
@SerializedName("target_sha")
187+
private String targetSha;
188+
189+
private String title;
190+
191+
private String body;
192+
193+
@SerializedName("source_branch")
194+
private String sourceBranch;
195+
196+
@SerializedName("source_sha")
197+
private String sourceSha;
198+
199+
@SerializedName("web_url")
200+
private String url;
201+
202+
@SerializedName("merge_commit_sha")
203+
private String mergeCommitSha;
204+
205+
private String action;
206+
207+
private String id;
208+
209+
private String status;
210+
211+
private CodingUserHelper user;
212+
}
213+
214+
public PullRequestAdapter(GitSource gitSource, GitEventType eventType) {
215+
super(gitSource, eventType);
216+
}
217+
218+
@Override
219+
public GitEvent convert(String json) throws GitException {
220+
RequestRootHelper root = GSON.fromJson(json, RequestRootHelper.class);
221+
MergeRequestHelper mr = root.mergeRequest;
222+
CodingRepoHelper repo = root.repository;
223+
224+
// set general info of PR
225+
GitPullRequestEvent event = new GitPullRequestEvent(gitSource, eventType);
226+
event.setAction(mr.action);
227+
event.setState(State.OPEN);
228+
event.setDescription(mr.body);
229+
event.setTitle(mr.title);
230+
231+
event.setRequestId((int) Double.parseDouble(mr.id));
232+
event.setUrl(mr.url);
233+
event.setSubmitter(mr.user.name);
234+
event.setSource(new GitPullRequestInfo());
235+
event.setTarget(new GitPullRequestInfo());
236+
237+
// set source info
238+
event.getSource().setBranch(mr.sourceBranch);
239+
event.getSource().setSha(mr.sourceSha);
240+
event.getSource().setProjectId(Integer.parseInt(repo.id));
241+
event.getSource().setProjectName(repo.name);
242+
243+
// set target info
244+
event.getTarget().setBranch(mr.targetBranch);
245+
event.getTarget().setSha(mr.targetSha);
246+
event.getTarget().setProjectId(Integer.parseInt(repo.id));
247+
event.getTarget().setProjectName(repo.name);
248+
249+
return event;
250+
}
251+
}
252+
161253
}

platform-util-git/src/main/java/com/flow/platform/util/git/hooks/GitHookEventFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.flow.platform.util.git.hooks;
1818

19+
import static com.flow.platform.util.git.model.GitEventType.NONE;
1920
import static com.flow.platform.util.git.model.GitEventType.PR;
2021
import static com.flow.platform.util.git.model.GitEventType.PUSH;
2122
import static com.flow.platform.util.git.model.GitEventType.TAG;
@@ -53,12 +54,12 @@ public class GitHookEventFactory {
5354
// init GitHub hook data adaptor
5455
Map<String, GitHookEventAdapter> gitHubAdaptors = new HashMap<>(3);
5556
gitHubAdaptors.put(GitHubEvents.Hooks.EVENT_TYPE_PR, new PullRequestAdapter(GITHUB, PR));
56-
gitHubAdaptors.put(GitHubEvents.Hooks.EVENT_TYPE_PUSH, new GitHubEvents.PushAndTagAdapter(GITHUB, PUSH));
57-
gitHubAdaptors.put(GitHubEvents.Hooks.EVENT_TYPE_TAG, new GitHubEvents.PushAndTagAdapter(GITHUB, TAG));
57+
gitHubAdaptors.put(GitHubEvents.Hooks.EVENT_TYPE_PUSH_OR_TAG, new GitHubEvents.PushAndTagAdapter(GITHUB, NONE));
5858
adaptors.put(GitHubEvents.Hooks.HEADER, gitHubAdaptors);
5959

6060
Map<String, GitHookEventAdapter> codingAdaptors = new HashMap<>(3);
61-
codingAdaptors.put(CodingEvents.Hooks.EVENT_TYPE_PUSH, new CodingEvents.PushAndTagAdapter(CODING, PUSH));
61+
codingAdaptors.put(CodingEvents.Hooks.EVENT_TYPE_PR, new CodingEvents.PullRequestAdapter(CODING, PR));
62+
codingAdaptors.put(CodingEvents.Hooks.EVENT_TYPE_PUSH_OR_TAG, new CodingEvents.PushAndTagAdapter(CODING, NONE));
6263
adaptors.put(CodingEvents.Hooks.HEADER, codingAdaptors);
6364
}
6465

platform-util-git/src/main/java/com/flow/platform/util/git/hooks/GitHubEvents.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ public static class Hooks {
4444

4545
public final static String EVENT_TYPE_PING = "ping";
4646

47-
public final static String EVENT_TYPE_PUSH = "push";
48-
49-
public final static String EVENT_TYPE_TAG = "push";
47+
public final static String EVENT_TYPE_PUSH_OR_TAG = "push";
5048

5149
public final static String EVENT_TYPE_PR = "pull_request";
5250
}

platform-util-git/src/main/java/com/flow/platform/util/git/model/GitEventType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
public enum GitEventType {
2323

24+
NONE,
25+
2426
MANUAL,
2527

2628
PING,

platform-util-git/src/test/java/com/flow/platform/util/git/test/CodingWebHooksEventTest.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import com.flow.platform.util.git.hooks.CodingEvents.Hooks;
2020
import com.flow.platform.util.git.hooks.GitHookEventFactory;
2121
import com.flow.platform.util.git.model.GitEventType;
22+
import com.flow.platform.util.git.model.GitPullRequestEvent;
23+
import com.flow.platform.util.git.model.GitPullRequestEvent.State;
24+
import com.flow.platform.util.git.model.GitPullRequestInfo;
2225
import com.flow.platform.util.git.model.GitPushTagEvent;
2326
import com.flow.platform.util.git.model.GitSource;
2427
import com.google.common.io.Files;
@@ -41,7 +44,7 @@ public void should_convert_to_push_event_obj() throws Throwable {
4144
// given:
4245
String pushEventContent = loadWebhookSampleJson("coding/webhook_push.json");
4346
Map<String, String> dummyHeader = new HashMap<>();
44-
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PUSH);
47+
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PUSH_OR_TAG);
4548

4649
// when: build event from header and json content
4750
GitPushTagEvent pushEvent = (GitPushTagEvent) GitHookEventFactory.build(dummyHeader, pushEventContent);
@@ -71,12 +74,12 @@ public void should_convert_to_push_event_obj() throws Throwable {
7174
@Test
7275
public void should_convert_to_tag_event_obj() throws Throwable {
7376
// given:
74-
String pushEventContent = loadWebhookSampleJson("coding/webhook_tag.json");
77+
String tagEventContent = loadWebhookSampleJson("coding/webhook_tag.json");
7578
Map<String, String> dummyHeader = new HashMap<>();
76-
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PUSH);
79+
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PUSH_OR_TAG);
7780

7881
// when: build event from header and json content
79-
GitPushTagEvent tagEvent = (GitPushTagEvent) GitHookEventFactory.build(dummyHeader, pushEventContent);
82+
GitPushTagEvent tagEvent = (GitPushTagEvent) GitHookEventFactory.build(dummyHeader, tagEventContent);
8083
Assert.assertNotNull(tagEvent);
8184

8285
// then:
@@ -100,6 +103,42 @@ public void should_convert_to_tag_event_obj() throws Throwable {
100103
tagEvent.getHeadCommitUrl());
101104
}
102105

106+
@Test
107+
public void should_convert_to_pr_event_obj() throws Throwable {
108+
// given:
109+
String mrEventContent = loadWebhookSampleJson("coding/webhook_pr_open.json");
110+
Map<String, String> dummyHeader = new HashMap<>();
111+
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PR);
112+
113+
// when: build event from header and json content
114+
GitPullRequestEvent prEvent = (GitPullRequestEvent) GitHookEventFactory.build(dummyHeader, mrEventContent);
115+
Assert.assertNotNull(prEvent);
116+
117+
// then:
118+
Assert.assertEquals(GitSource.CODING, prEvent.getGitSource());
119+
Assert.assertEquals(GitEventType.PR, prEvent.getType());
120+
121+
Assert.assertEquals("create", prEvent.getAction());
122+
Assert.assertEquals(State.OPEN, prEvent.getState());
123+
Assert.assertEquals("test pr", prEvent.getTitle());
124+
Assert.assertEquals("\u003cp\u003ehello this is the PR test\u003c/p\u003e", prEvent.getDescription());
125+
Assert.assertEquals("https://coding.net/u/benqyang2006/p/flowclibasic/git/merge/1", prEvent.getUrl());
126+
Assert.assertEquals(924870, prEvent.getRequestId().intValue());
127+
Assert.assertEquals("benqyang2006", prEvent.getSubmitter());
128+
129+
GitPullRequestInfo source = prEvent.getSource();
130+
Assert.assertEquals("develop", source.getBranch());
131+
Assert.assertEquals("", source.getSha());
132+
Assert.assertEquals(1243975, source.getProjectId().intValue());
133+
Assert.assertEquals("flowclibasic", source.getProjectName());
134+
135+
GitPullRequestInfo target = prEvent.getTarget();
136+
Assert.assertEquals("master", target.getBranch());
137+
Assert.assertEquals("", target.getSha());
138+
Assert.assertEquals(1243975, target.getProjectId().intValue());
139+
Assert.assertEquals("flowclibasic", target.getProjectName());
140+
}
141+
103142
private static String loadWebhookSampleJson(String classPath) throws IOException {
104143
URL resource = GitLabHooksEventTest.class.getClassLoader().getResource(classPath);
105144
return Files.toString(new File(resource.getFile()), Charset.forName("UTF-8"));

platform-util-git/src/test/java/com/flow/platform/util/git/test/GitHubHooksEventTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void should_convert_to_push_event_obj() throws Throwable {
4646
// given:
4747
String pushEventContent = loadWebhookSampleJson("github/webhook_push.json");
4848
Map<String, String> dummyHeader = new HashMap<>();
49-
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PUSH);
49+
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PUSH_OR_TAG);
5050

5151
// when:
5252
GitPushTagEvent pushEvent = (GitPushTagEvent) GitHookEventFactory.build(dummyHeader, pushEventContent);
@@ -85,7 +85,7 @@ public void should_convert_to_tag_event_obj() throws Throwable {
8585
// given:
8686
String tagEventContent = loadWebhookSampleJson("github/webhook_tag.json");
8787
Map<String, String> dummyHeader = new HashMap<>();
88-
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PUSH);
88+
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PUSH_OR_TAG);
8989

9090
// when:
9191
GitPushTagEvent tagEvent = (GitPushTagEvent) GitHookEventFactory.build(dummyHeader, tagEventContent);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"ref": "refs/heads/master",
3+
"before": "b972e2edd91e85ec25ec28c29d7dc3e823f28e8a",
4+
"commits": [
5+
{
6+
"committer": {
7+
"name": "benqyang2006",
8+
"email": "[email protected]"
9+
},
10+
"web_url": "https://coding.net/u/benqyang2006/p/flowclibasic/git/commit/aa818aed288c6ca4c20a1d8144a2e9bcafa9f3a8",
11+
"short_message": "Accept Merge Request #1 test pr : (develop -\u003e master)\n",
12+
"sha": "aa818aed288c6ca4c20a1d8144a2e9bcafa9f3a8"
13+
},
14+
{
15+
"committer": {
16+
"name": "benqyang2006",
17+
"email": "[email protected]"
18+
},
19+
"web_url": "https://coding.net/u/benqyang2006/p/flowclibasic/git/commit/d2e8158fbd4f13c0672004e1bbb2a7ee33fb6371",
20+
"short_message": "update .flow.yml\n",
21+
"sha": "d2e8158fbd4f13c0672004e1bbb2a7ee33fb6371"
22+
}
23+
],
24+
"after": "aa818aed288c6ca4c20a1d8144a2e9bcafa9f3a8",
25+
"event": "push",
26+
"repository": {
27+
"owner": {
28+
"path": "/u/benqyang2006",
29+
"web_url": "https://coding.net/u/benqyang2006",
30+
"global_key": "benqyang2006",
31+
"name": "benqyang2006",
32+
"avatar": "https://dn-coding-net-production-static.qbox.me/16e639e2-45dd-4959-b38e-576e2ac0041c.jpg?imageMogr2/auto-orient/format/jpeg/crop/!498x498a48a302"
33+
},
34+
"https_url": "https://git.coding.net/benqyang2006/flowclibasic.git",
35+
"web_url": "https://coding.net/u/benqyang2006/p/flowclibasic",
36+
"project_id": "1243975",
37+
"ssh_url": "[email protected]:benqyang2006/flowclibasic.git",
38+
"name": "flowclibasic",
39+
"description": ""
40+
},
41+
"user": {
42+
"path": "/u/benqyang2006",
43+
"web_url": "https://coding.net/u/benqyang2006",
44+
"global_key": "benqyang2006",
45+
"name": "benqyang2006",
46+
"avatar": "https://dn-coding-net-production-static.qbox.me/16e639e2-45dd-4959-b38e-576e2ac0041c.jpg?imageMogr2/auto-orient/format/jpeg/crop/!498x498a48a302"
47+
}
48+
}

0 commit comments

Comments
 (0)