Skip to content

Commit 491d0e4

Browse files
author
Yang Guo
committed
support compare id on PR event
1 parent 68327bd commit 491d0e4

File tree

6 files changed

+144
-36
lines changed

6 files changed

+144
-36
lines changed

platform-api/src/main/java/com/flow/platform/api/controller/GitWebHookController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void onEventReceived(@RequestHeader HttpHeaders headers, HttpServletReque
8787
// extract git related env variables from event, and temporary set to node for git loading
8888
final Map<String, String> gitEnvs = GitEventEnvConverter.convert(hookEvent);
8989

90-
if (!allowEventType(flow, gitEnvs)) {
90+
if (!canExecuteGitEvent(flow, gitEnvs)) {
9191
LOGGER.warn("The git event not match flow settings");
9292
return;
9393
}
@@ -103,7 +103,7 @@ public void onEventReceived(@RequestHeader HttpHeaders headers, HttpServletReque
103103
}
104104
}
105105

106-
private boolean allowEventType(Node flow, Map<String, String> gitEnvs) {
106+
private boolean canExecuteGitEvent(Node flow, Map<String, String> gitEnvs) {
107107
String gitEventType = gitEnvs.get(GitEnvs.FLOW_GIT_EVENT_TYPE.name());
108108
String gitBranch = gitEnvs.get(GitEnvs.FLOW_GIT_BRANCH.name());
109109

platform-api/src/main/java/com/flow/platform/api/git/GitEventEnvConverter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ public static Map<String, String> convert(GitEvent event) {
7676
info.put(GitEnvs.FLOW_GIT_AUTHOR.name(), pr.getMergedBy());
7777
}
7878

79+
// set commit id and url from PR request id and pr url
80+
if (pr.getRequestId() != null && pr.getUrl() != null) {
81+
info.put(GitEnvs.FLOW_GIT_COMMIT_ID.name(), pr.getRequestId().toString());
82+
info.put(GitEnvs.FLOW_GIT_COMMIT_URL.name(), pr.getUrl());
83+
}
84+
85+
// set compare id and url from PR request
86+
if (pr.getCompareId() != null && pr.getCompareUrl() != null) {
87+
info.put(GitEnvs.FLOW_GIT_COMPARE_ID.name(), pr.getCompareId());
88+
info.put(GitEnvs.FLOW_GIT_COMPARE_URL.name(), pr.getCompareUrl());
89+
}
90+
7991
info.put(GitEnvs.FLOW_GIT_CHANGELOG.name(), pr.getTitle());
8092
info.put(GitEnvs.FLOW_GIT_PR_URL.name(), pr.getUrl());
8193
return info;

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.gson.annotations.SerializedName;
3030
import java.util.ArrayList;
3131
import java.util.List;
32+
import java.util.Objects;
3233

3334
/**
3435
* @author yang
@@ -168,6 +169,8 @@ public static class PullRequestAdapter extends GitHookEventAdapter {
168169

169170
public final static String STATE_OPEN = "create";
170171

172+
public final static String STATE_CLOSE = "merge";
173+
171174
private class RequestRootHelper {
172175

173176
@SerializedName("merge_request")
@@ -224,13 +227,22 @@ public GitEvent convert(String json) throws GitException {
224227
// set general info of PR
225228
GitPullRequestEvent event = new GitPullRequestEvent(gitSource, eventType);
226229
event.setAction(mr.action);
227-
event.setState(State.OPEN);
230+
231+
if (Objects.equals(mr.action, STATE_OPEN)) {
232+
event.setState(State.OPEN);
233+
event.setSubmitter(mr.user.name);
234+
}
235+
236+
if (Objects.equals(mr.action, STATE_CLOSE)) {
237+
event.setState(State.CLOSE);
238+
event.setMergedBy(mr.user.name);
239+
}
240+
228241
event.setDescription(mr.body);
229242
event.setTitle(mr.title);
230243

231244
event.setRequestId((int) Double.parseDouble(mr.id));
232245
event.setUrl(mr.url);
233-
event.setSubmitter(mr.user.name);
234246
event.setSource(new GitPullRequestInfo());
235247
event.setTarget(new GitPullRequestInfo());
236248

@@ -246,6 +258,11 @@ public GitEvent convert(String json) throws GitException {
246258
event.getTarget().setProjectId(Integer.parseInt(repo.id));
247259
event.getTarget().setProjectName(repo.name);
248260

261+
// set compare id
262+
final String compareId = GitPullRequestEvent.buildCompareId(event.getSource(), event.getTarget());
263+
event.setCompareId(compareId);
264+
event.setCompareUrl(repo.url + "/git/compare/" + compareId);
265+
249266
return event;
250267
}
251268
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,27 @@
1616

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

19+
import com.google.common.base.Strings;
20+
1921
/**
2022
* Pull Request with Closed event (Merge Request)
2123
*
2224
* @author yang
2325
*/
2426
public class GitPullRequestEvent extends GitEvent {
2527

28+
public static String buildCompareId(GitPullRequestInfo source, GitPullRequestInfo target) {
29+
if (!Strings.isNullOrEmpty(target.getSha()) && !Strings.isNullOrEmpty(source.getSha())) {
30+
return target.getSha().substring(0, 12) + "..." + source.getSha().substring(0, 12);
31+
}
32+
33+
if (!Strings.isNullOrEmpty(target.getBranch()) && !Strings.isNullOrEmpty(source.getBranch())) {
34+
return target.getBranch() + "..." + source.getBranch();
35+
}
36+
37+
return "";
38+
}
39+
2640
public enum State {
2741
OPEN,
2842

@@ -58,6 +72,16 @@ public enum State {
5872
*/
5973
private String mergedBy;
6074

75+
/**
76+
* Compare source and target branch
77+
*/
78+
private String compareId;
79+
80+
/**
81+
* Http web view for compare id
82+
*/
83+
private String compareUrl;
84+
6185
public GitPullRequestEvent(GitSource gitSource, GitEventType type) {
6286
super(gitSource, type);
6387
}
@@ -141,4 +165,20 @@ public String getMergedBy() {
141165
public void setMergedBy(String mergedBy) {
142166
this.mergedBy = mergedBy;
143167
}
168+
169+
public String getCompareId() {
170+
return compareId;
171+
}
172+
173+
public void setCompareId(String compareId) {
174+
this.compareId = compareId;
175+
}
176+
177+
public String getCompareUrl() {
178+
return compareUrl;
179+
}
180+
181+
public void setCompareUrl(String compareUrl) {
182+
this.compareUrl = compareUrl;
183+
}
144184
}

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.flow.platform.util.git.test;
1818

1919
import com.flow.platform.util.git.hooks.CodingEvents.Hooks;
20+
import com.flow.platform.util.git.hooks.CodingEvents.PullRequestAdapter;
2021
import com.flow.platform.util.git.hooks.GitHookEventFactory;
2122
import com.flow.platform.util.git.model.GitEventType;
2223
import com.flow.platform.util.git.model.GitPullRequestEvent;
@@ -104,7 +105,7 @@ public void should_convert_to_tag_event_obj() throws Throwable {
104105
}
105106

106107
@Test
107-
public void should_convert_to_pr_event_obj() throws Throwable {
108+
public void should_convert_to_pr_open_event_obj() throws Throwable {
108109
// given:
109110
String mrEventContent = loadWebhookSampleJson("coding/webhook_pr_open.json");
110111
Map<String, String> dummyHeader = new HashMap<>();
@@ -118,13 +119,14 @@ public void should_convert_to_pr_event_obj() throws Throwable {
118119
Assert.assertEquals(GitSource.CODING, prEvent.getGitSource());
119120
Assert.assertEquals(GitEventType.PR, prEvent.getType());
120121

121-
Assert.assertEquals("create", prEvent.getAction());
122+
Assert.assertEquals(PullRequestAdapter.STATE_OPEN, prEvent.getAction());
122123
Assert.assertEquals(State.OPEN, prEvent.getState());
123124
Assert.assertEquals("test pr", prEvent.getTitle());
124125
Assert.assertEquals("\u003cp\u003ehello this is the PR test\u003c/p\u003e", prEvent.getDescription());
125126
Assert.assertEquals("https://coding.net/u/benqyang2006/p/flowclibasic/git/merge/1", prEvent.getUrl());
126127
Assert.assertEquals(924870, prEvent.getRequestId().intValue());
127128
Assert.assertEquals("benqyang2006", prEvent.getSubmitter());
129+
Assert.assertNull(prEvent.getMergedBy());
128130

129131
GitPullRequestInfo source = prEvent.getSource();
130132
Assert.assertEquals("develop", source.getBranch());
@@ -137,6 +139,52 @@ public void should_convert_to_pr_event_obj() throws Throwable {
137139
Assert.assertEquals("", target.getSha());
138140
Assert.assertEquals(1243975, target.getProjectId().intValue());
139141
Assert.assertEquals("flowclibasic", target.getProjectName());
142+
143+
Assert.assertEquals("develop...master", prEvent.getCompareId());
144+
Assert.assertEquals("https://coding.net/u/benqyang2006/p/flowclibasic/git/compare/develop...master",
145+
prEvent.getCompareUrl());
146+
}
147+
148+
@Test
149+
public void should_convert_to_pr_close_obj() throws Throwable {
150+
// given:
151+
String mrEventContent = loadWebhookSampleJson("coding/webhook_pr_close.json");
152+
Map<String, String> dummyHeader = new HashMap<>();
153+
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PR);
154+
155+
// when: build event from header and json content
156+
GitPullRequestEvent prEvent = (GitPullRequestEvent) GitHookEventFactory.build(dummyHeader, mrEventContent);
157+
Assert.assertNotNull(prEvent);
158+
159+
// then:
160+
Assert.assertEquals(GitSource.CODING, prEvent.getGitSource());
161+
Assert.assertEquals(GitEventType.PR, prEvent.getType());
162+
163+
Assert.assertEquals(PullRequestAdapter.STATE_CLOSE, prEvent.getAction());
164+
Assert.assertEquals(State.CLOSE, prEvent.getState());
165+
Assert.assertEquals("23123123", prEvent.getTitle());
166+
Assert.assertEquals("<p>12312321</p>", prEvent.getDescription());
167+
Assert.assertEquals(924984, prEvent.getRequestId().intValue());
168+
Assert.assertEquals("https://coding.net/u/benqyang2006/p/flowclibasic/git/merge/5", prEvent.getUrl());
169+
170+
Assert.assertNull(prEvent.getSubmitter());
171+
Assert.assertEquals("benqyang2006", prEvent.getMergedBy());
172+
173+
GitPullRequestInfo source = prEvent.getSource();
174+
Assert.assertEquals("develop", source.getBranch());
175+
Assert.assertEquals("db2da0ba563a323614f3d974a67a2c63e2929177", source.getSha());
176+
Assert.assertEquals(1243975, source.getProjectId().intValue());
177+
Assert.assertEquals("flowclibasic", source.getProjectName());
178+
179+
GitPullRequestInfo target = prEvent.getTarget();
180+
Assert.assertEquals("master", target.getBranch());
181+
Assert.assertEquals("985ae638e0cc8bd4dc2cd8c7a8a6800b00b33997", target.getSha());
182+
Assert.assertEquals(1243975, target.getProjectId().intValue());
183+
Assert.assertEquals("flowclibasic", target.getProjectName());
184+
185+
Assert.assertEquals("985ae638e0cc...db2da0ba563a", prEvent.getCompareId());
186+
Assert.assertEquals("https://coding.net/u/benqyang2006/p/flowclibasic/git/compare/985ae638e0cc...db2da0ba563a",
187+
prEvent.getCompareUrl());
140188
}
141189

142190
private static String loadWebhookSampleJson(String classPath) throws IOException {
Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
{
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"
2+
"merge_request": {
3+
"target_branch": "master",
4+
"merged_commit_sha": "b98872eeb4fcfa4a736dcf1df76d110b1cc62832",
5+
"title": "23123123",
6+
"body": "\u003cp\u003e12312321\u003c/p\u003e",
7+
"source_sha": "db2da0ba563a323614f3d974a67a2c63e2929177",
8+
"source_branch": "develop",
9+
"number": 5.0,
10+
"web_url": "https://coding.net/u/benqyang2006/p/flowclibasic/git/merge/5",
11+
"target_sha": "985ae638e0cc8bd4dc2cd8c7a8a6800b00b33997",
12+
"action": "merge",
13+
"id": 924984.0,
14+
"user": {
15+
"path": "/u/benqyang2006",
16+
"web_url": "https://coding.net/u/benqyang2006",
17+
"global_key": "benqyang2006",
18+
"name": "benqyang2006",
19+
"avatar": "https://dn-coding-net-production-static.qbox.me/16e639e2-45dd-4959-b38e-576e2ac0041c.jpg?imageMogr2/auto-orient/format/jpeg/crop/!498x498a48a302"
1320
},
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",
21+
"status": "ACCEPTED"
22+
},
2623
"repository": {
2724
"owner": {
2825
"path": "/u/benqyang2006",
@@ -38,11 +35,5 @@
3835
"name": "flowclibasic",
3936
"description": ""
4037
},
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-
}
38+
"event": "merge_request"
4839
}

0 commit comments

Comments
 (0)