Skip to content

Commit ea1480e

Browse files
committed
support change merged gerrit event
1 parent 858662c commit ea1480e

File tree

9 files changed

+92
-121
lines changed

9 files changed

+92
-121
lines changed

core/src/main/java/com/flowci/core/common/domain/Variables.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,11 @@ public abstract static class Git {
183183
public static final String PATCHSET_CHANGE_ID = "FLOWCI_GIT_PATCHSET_CHANGE_ID";
184184
public static final String PATCHSET_CHANGE_NUM = "FLOWCI_GIT_PATCHSET_CHANGE_NUM";
185185
public static final String PATCHSET_CHANGE_URL = "FLOWCI_GIT_PATCHSET_CHANGE_URL";
186+
public static final String PATCHSET_CHANGE_STATUS = "FLOWCI_GIT_PATCHSET_CHANGE_STATUS";
186187
public static final String PATCHSET_PATCH_NUM = "FLOWCI_GIT_PATCHSET_PATCH_NUM";
187188
public static final String PATCHSET_PATCH_URL = "FLOWCI_GIT_PATCHSET_PATCH_URL";
188-
public static final String PATCHSET_REVISION = "FLOWCI_GIT_PATCHSET_REVISION";
189-
public static final String PATCHSET_REF = "FLOWCI_GIT_PATCHSET_REF";
189+
public static final String PATCHSET_PATCH_REVISION = "FLOWCI_GIT_PATCHSET_REVISION";
190+
public static final String PATCHSET_PATCH_REF = "FLOWCI_GIT_PATCHSET_REF";
190191
public static final String PATCHSET_CREATE_TIME = "FLOWCI_GIT_PATCHSET_CREATE_TIME";
191192
public static final String PATCHSET_INSERT_SIZE = "FLOWCI_GIT_PATCHSET_INSERT_SIZE";
192193
public static final String PATCHSET_DELETE_SIZE = "FLOWCI_GIT_PATCHSET_DELETE_SIZE";
@@ -199,10 +200,11 @@ public abstract static class Git {
199200
.add(PATCHSET_CHANGE_ID)
200201
.add(PATCHSET_CHANGE_NUM)
201202
.add(PATCHSET_CHANGE_URL)
203+
.add(PATCHSET_CHANGE_STATUS)
202204
.add(PATCHSET_PATCH_NUM)
203205
.add(PATCHSET_PATCH_URL)
204-
.add(PATCHSET_REVISION)
205-
.add(PATCHSET_REF)
206+
.add(PATCHSET_PATCH_REVISION)
207+
.add(PATCHSET_PATCH_REF)
206208
.add(PATCHSET_CREATE_TIME)
207209
.add(PATCHSET_INSERT_SIZE)
208210
.add(PATCHSET_DELETE_SIZE)

core/src/main/java/com/flowci/core/githook/converter/GerritConverter.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.flowci.core.githook.converter;
22

3+
import com.fasterxml.jackson.annotation.JsonAlias;
34
import com.flowci.core.common.domain.GitSource;
45
import com.flowci.core.githook.domain.GitPatchSetTrigger;
56
import com.flowci.core.githook.domain.GitTrigger;
67
import com.flowci.core.githook.domain.GitTriggerable;
78
import com.flowci.core.githook.domain.GitUser;
9+
import com.flowci.core.githook.util.BranchHelper;
810
import com.flowci.util.StringHelper;
911
import com.google.common.collect.ImmutableMap;
1012
import lombok.extern.log4j.Log4j2;
@@ -24,7 +26,6 @@ public class GerritConverter extends TriggerConverter {
2426
public static final String AllEvent = "all";
2527

2628
private static final String EventPathsetCreated = "patchset-created";
27-
private static final String EventRefUpdated = "ref-updated";
2829
private static final String EventChangeMerged = "change-merged";
2930

3031
private final Map<String, Function<InputStream, GitTrigger>> mapping =
@@ -50,8 +51,8 @@ public GitTrigger apply(InputStream inputStream) {
5051
String payload = StringHelper.toString(inputStream);
5152
Event event = objectMapper.readValue(payload, Event.class);
5253

53-
if (Objects.equals(EventPathsetCreated, event.type)) {
54-
PatchSetCreateEvent pce = objectMapper.readValue(payload, PatchSetCreateEvent.class);
54+
if (Objects.equals(EventPathsetCreated, event.type) || Objects.equals(EventChangeMerged, event.type)) {
55+
PatchSetEvent pce = objectMapper.readValue(payload, PatchSetEvent.class);
5556
return pce.toTrigger();
5657
}
5758

@@ -74,12 +75,18 @@ private static class Event {
7475
public String type;
7576
}
7677

77-
private static class PatchSetCreateEvent implements GitTriggerable {
78+
79+
private static class PatchSetEvent implements GitTriggerable {
80+
81+
@JsonAlias({"uploader", "submitter"})
82+
public Author author;
7883

7984
public PatchSet patchSet;
8085

8186
public Change change;
8287

88+
public String refName;
89+
8390
@Override
8491
public GitTrigger toTrigger() {
8592
GitPatchSetTrigger t = new GitPatchSetTrigger();
@@ -89,17 +96,19 @@ public GitTrigger toTrigger() {
8996
t.setSubject(change.subject);
9097
t.setMessage(change.commitMessage);
9198
t.setProject(change.project);
92-
t.setBranch(change.branch);
9399
t.setChangeId(change.id);
94100
t.setChangeNumber(change.number);
95101
t.setChangeUrl(change.url);
102+
t.setStatus(GitPatchSetTrigger.Status.valueOf(change.status));
96103

97104
t.setPatchNumber(patchSet.number);
98105
t.setPatchUrl(change.url + "/" + patchSet.number);
99-
t.setRevision(patchSet.revision);
100-
t.setRef(patchSet.ref);
106+
t.setPatchRevision(patchSet.revision);
107+
t.setPatchRef(patchSet.ref);
108+
109+
t.setBranch(BranchHelper.getBranchName(refName));
101110
t.setCreatedOn(patchSet.createdOn);
102-
t.setAuthor(patchSet.author.toGitUser());
111+
t.setAuthor(author.toGitUser());
103112
t.setSizeInsertions(patchSet.sizeInsertions);
104113
t.setSizeDeletions(patchSet.sizeDeletions);
105114

@@ -115,10 +124,6 @@ private static class PatchSet {
115124

116125
public String ref;
117126

118-
public Author uploader;
119-
120-
public Author author;
121-
122127
public String createdOn;
123128

124129
public Integer sizeInsertions;
@@ -141,6 +146,8 @@ public static class Change {
141146
public String url;
142147

143148
public String commitMessage;
149+
150+
public String status;
144151
}
145152

146153
private static class Author {

core/src/main/java/com/flowci/core/githook/domain/GitPatchSetTrigger.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
@Setter
1515
public class GitPatchSetTrigger extends GitTrigger {
1616

17+
public enum Status {
18+
NEW,
19+
20+
MERGED
21+
}
22+
23+
private Status status;
24+
1725
private String subject;
1826

1927
private String message; // commit message
@@ -32,9 +40,9 @@ public class GitPatchSetTrigger extends GitTrigger {
3240

3341
private String patchUrl;
3442

35-
private String revision;
43+
private String patchRef;
3644

37-
private String ref;
45+
private String patchRevision;
3846

3947
private String createdOn;
4048

@@ -54,14 +62,24 @@ public StringVars toVariableMap() {
5462
map.put(PATCHSET_CHANGE_ID, changeId);
5563
map.put(PATCHSET_CHANGE_NUM, String.valueOf(changeNumber));
5664
map.put(PATCHSET_CHANGE_URL, changeUrl);
65+
map.put(PATCHSET_CHANGE_STATUS, status.name());
5766
map.put(PATCHSET_PATCH_NUM, String.valueOf(patchNumber));
5867
map.put(PATCHSET_PATCH_URL, patchUrl);
59-
map.put(PATCHSET_REVISION, revision);
60-
map.put(PATCHSET_REF, ref);
68+
map.put(PATCHSET_PATCH_REF, patchRef);
69+
map.put(PATCHSET_PATCH_REVISION, patchRevision);
6170
map.put(PATCHSET_CREATE_TIME, createdOn);
6271
map.put(PATCHSET_INSERT_SIZE, String.valueOf(sizeInsertions));
6372
map.put(PATCHSET_DELETE_SIZE, String.valueOf(sizeDeletions));
6473
map.put(PATCHSET_AUTHOR, author.getEmail());
74+
75+
if (status == Status.MERGED) {
76+
map.put(BRANCH, branch);
77+
}
78+
79+
if (status == Status.NEW) {
80+
map.put(BRANCH, patchRef);
81+
}
82+
6583
return map;
6684
}
6785

core/src/main/java/com/flowci/core/githook/domain/GitPrTrigger.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public StringVars toVariableMap() {
7474
map.put(PR_BASE_REPO_BRANCH, base.ref);
7575
map.put(PR_BASE_REPO_COMMIT, base.commit);
7676

77+
map.put(BRANCH, merged ? base.ref : head.ref);
7778
return map;
7879
}
7980

core/src/main/java/com/flowci/core/job/domain/JobItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public Context convert(org.bson.Document source) {
9696
context.putIfNotEmpty(PATCHSET_PATCH_NUM);
9797
context.putIfNotEmpty(PATCHSET_PATCH_URL);
9898
context.putIfNotEmpty(PATCHSET_AUTHOR);
99-
context.putIfNotEmpty(PATCHSET_REF);
99+
context.putIfNotEmpty(PATCHSET_PATCH_REF);
100100

101101
return context;
102102
}

core/src/test/java/com/flowci/core/test/githook/GerritConverterTest.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.flowci.core.test.githook;
22

3+
import com.flowci.core.common.domain.Variables;
34
import com.flowci.core.githook.converter.GerritConverter;
45
import com.flowci.core.githook.domain.GitPatchSetTrigger;
56
import com.flowci.core.githook.domain.GitTrigger;
@@ -28,17 +29,37 @@ public void should_parse_patchset_create_event() {
2829
Assert.assertEquals("task 1 try", t.getSubject());
2930
Assert.assertEquals("task 1 try\n\nChange-Id: I508fde11a59c36f9ab8b086d13d41b5d9c597db6\n", t.getMessage());
3031
Assert.assertEquals("gerrit_test", t.getProject());
31-
Assert.assertEquals("master", t.getBranch());
3232
Assert.assertEquals("I508fde11a59c36f9ab8b086d13d41b5d9c597db6", t.getChangeId());
3333
Assert.assertEquals(1, t.getChangeNumber().intValue());
3434
Assert.assertEquals("http://192.168.31.173:8088/c/gerrit_test/+/1", t.getChangeUrl());
3535

3636
Assert.assertEquals(2, t.getPatchNumber().intValue());
3737
Assert.assertEquals("http://192.168.31.173:8088/c/gerrit_test/+/1/2", t.getPatchUrl());
38-
Assert.assertEquals("34ebc1c63ef2173b3704663c32bcd14471b68a9b", t.getRevision());
39-
Assert.assertEquals("refs/changes/01/1/2", t.getRef());
38+
Assert.assertEquals("34ebc1c63ef2173b3704663c32bcd14471b68a9b", t.getPatchRevision());
39+
Assert.assertEquals("refs/changes/01/1/2", t.getPatchRef());
40+
41+
Assert.assertEquals("master", t.getBranch());
42+
Assert.assertEquals(GitPatchSetTrigger.Status.NEW, t.getStatus());
4043
Assert.assertEquals("1642965660", t.getCreatedOn());
4144
Assert.assertEquals(3, t.getSizeInsertions().intValue());
4245
Assert.assertEquals(0, t.getSizeDeletions().intValue());
46+
47+
var vars = t.toVariableMap();
48+
Assert.assertEquals("refs/changes/01/1/2", vars.get(Variables.Git.BRANCH));
49+
}
50+
51+
@Test
52+
public void should_parse_patchset_merged_event() {
53+
InputStream stream = load("gerrit/change_merged.json");
54+
55+
Optional<GitTrigger> optional = gerritConverter.convert(GerritConverter.AllEvent, stream);
56+
Assert.assertTrue(optional.isPresent());
57+
Assert.assertTrue(optional.get() instanceof GitPatchSetTrigger);
58+
59+
GitPatchSetTrigger t = (GitPatchSetTrigger) optional.get();
60+
Assert.assertEquals(GitPatchSetTrigger.Status.MERGED, t.getStatus());
61+
62+
var vars = t.toVariableMap();
63+
Assert.assertEquals("master", vars.get(Variables.Git.BRANCH));
4364
}
4465
}
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
{
22
"submitter": {
3-
"name": "Administrator",
3+
"name": "admin",
44
"email": "[email protected]",
55
"username": "admin"
66
},
7-
"newRev": "a3ec654afcaae6fb244a71d5dbed02a0c1f97a20",
7+
"newRev": "bd4082009476bd39837e18ce987c27931f59d2ed",
88
"patchSet": {
99
"number": 2,
10-
"revision": "a3ec654afcaae6fb244a71d5dbed02a0c1f97a20",
10+
"revision": "34ebc1c63ef2173b3704663c32bcd14471b68a9b",
1111
"parents": [
12-
"f906c056351154c1ca511b7a3d2f2fe1658b7c8d"
12+
"ca53a79dfad47123180a3c37892e6548889b4883"
1313
],
1414
"ref": "refs/changes/01/1/2",
1515
"uploader": {
16-
"name": "Administrator",
16+
"name": "admin",
1717
"email": "[email protected]",
1818
"username": "admin"
1919
},
20-
"createdOn": 1642361836,
20+
"createdOn": 1642965660,
2121
"author": {
22-
"name": "Administrator",
22+
"name": "admin",
2323
"email": "[email protected]",
2424
"username": "admin"
2525
},
2626
"kind": "REWORK",
27-
"sizeInsertions": 2,
28-
"sizeDeletions": 1
27+
"sizeInsertions": 3,
28+
"sizeDeletions": 0
2929
},
3030
"change": {
31-
"project": "gerrit_demo",
31+
"project": "gerrit_test",
3232
"branch": "master",
33-
"id": "Iba9125f6902ba137825bab84c42be575ff2df86a",
33+
"id": "I508fde11a59c36f9ab8b086d13d41b5d9c597db6",
3434
"number": 1,
35-
"subject": "from test 1",
35+
"subject": "task 1 try",
3636
"owner": {
37-
"name": "Administrator",
37+
"name": "admin",
3838
"email": "[email protected]",
3939
"username": "admin"
4040
},
41-
"url": "http://d7fadbe75334/c/gerrit_demo/+/1",
42-
"commitMessage": "from test 1\n\nChange-Id: Iba9125f6902ba137825bab84c42be575ff2df86a\n",
43-
"createdOn": 1642361597,
41+
"url": "http://192.168.31.173:8088/c/gerrit_test/+/1",
42+
"commitMessage": "task 1 try\n\nChange-Id: I508fde11a59c36f9ab8b086d13d41b5d9c597db6\n",
43+
"createdOn": 1642965568,
4444
"status": "MERGED"
4545
},
4646
"project": {
47-
"name": "gerrit_demo"
47+
"name": "gerrit_test"
4848
},
4949
"refName": "refs/heads/master",
5050
"changeKey": {
51-
"key": "Iba9125f6902ba137825bab84c42be575ff2df86a"
51+
"key": "I508fde11a59c36f9ab8b086d13d41b5d9c597db6"
5252
},
5353
"type": "change-merged",
54-
"eventCreatedOn": 1642363860
54+
"eventCreatedOn": 1643490542
5555
}

core/src/test/resources/gerrit/push.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)