Skip to content

Commit bc517b0

Browse files
joshgoldman-googlecopybara-github
authored andcommitted
Add support for filtering Gerrit changes by status.
This allows users to specify `STATUS` in the `events` field of the `git.gerrit_trigger`. The desired statuses (e.g., "PENDING", "ABANDONED", "MERGED", "CLOSED", "REVIEWED", "OPEN") can be provided in the `SUBTYPES_STATUS` list. This enables workflows to react to changes in Gerrit status, such as triggering garbage collection on abandoned changes. BUG=491868096 GWSQ_IGNORE: joshgoldman@google.com PiperOrigin-RevId: 885767818 Change-Id: I82538311797ae8c2896edf34c705c8f7b8760a6b
1 parent 3c87678 commit bc517b0

File tree

4 files changed

+116
-5
lines changed

4 files changed

+116
-5
lines changed

java/com/google/copybara/git/GerritTrigger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class GerritTrigger implements Trigger {
3737
private final ImmutableSet<GerritEventTrigger> events;
3838
private final Console console;
3939
private final boolean allowSubmitChange;
40-
40+
4141
GerritTrigger(
4242
LazyResourceLoader<GerritApi> apiSupplier,
4343
String url,

java/com/google/copybara/git/GitModule.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3179,10 +3179,20 @@ private ImmutableSet<GerritEventTrigger> handleGerritEventTypes(Object events)
31793179
GerritEventType eventType = stringToEnum("events", event.getKey(), GerritEventType.class);
31803180

31813181
check(types.add(eventType), "Repeated element %s", event);
3182+
if (eventType == GerritEventType.STATUS) {
3183+
// TODO: Consider supporting all Gerrit statuses.
3184+
ImmutableSet<String> allowedStatuses =
3185+
ImmutableSet.of("PENDING", "ABANDONED", "MERGED", "CLOSED", "REVIEWED", "OPEN");
3186+
for (String status : event.getValue()) {
3187+
check(
3188+
allowedStatuses.contains(status),
3189+
"Invalid status '%s'. Valid values are %s",
3190+
status,
3191+
allowedStatuses);
3192+
}
3193+
}
31823194
eventBuilder.add(
3183-
GerritEventTrigger.create(
3184-
eventType,
3185-
ImmutableSet.copyOf(event.getValue())));
3195+
GerritEventTrigger.create(eventType, ImmutableSet.copyOf(event.getValue())));
31863196
}
31873197
}
31883198

java/com/google/copybara/git/gerritapi/GerritEventType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ public enum GerritEventType {
2222
UNKNOWN, // Used if we don't know the event type.
2323

2424
LABELS,
25-
SUBMIT_REQUIREMENTS;
25+
SUBMIT_REQUIREMENTS,
26+
STATUS;
2627
}

javatests/com/google/copybara/git/GerritTriggerTest.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package com.google.copybara.git;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertThrows;
2021

2122
import com.google.common.collect.ImmutableSet;
2223
import com.google.copybara.testing.DummyChecker;
2324
import com.google.copybara.testing.OptionsBuilder;
2425
import com.google.copybara.testing.SkylarkTestExecutor;
26+
import com.google.copybara.exception.ValidationException;
2527
import com.google.copybara.util.console.testing.TestingConsole;
2628
import org.junit.Before;
2729
import org.junit.Test;
@@ -61,6 +63,104 @@ public void testParsing() throws Exception {
6163
"gerritSubmit", "true");
6264
}
6365

66+
@Test
67+
public void testParsing_statusPending() throws Exception {
68+
GerritTrigger gerritTrigger =
69+
skylarkTestExecutor.eval(
70+
"e",
71+
"e = git.gerrit_trigger("
72+
+ "url = 'https://test.googlesource.com/example',"
73+
+ "events = {'STATUS': ['PENDING'], 'LABELS': []}, allow_submit = True)");
74+
75+
assertThat(gerritTrigger.describe())
76+
.containsExactly(
77+
"type",
78+
"gerrit_trigger",
79+
"url",
80+
"https://test.googlesource.com/example",
81+
"events",
82+
"STATUS",
83+
"events",
84+
"LABELS",
85+
"SUBTYPES_STATUS",
86+
"PENDING",
87+
"gerritSubmit",
88+
"true");
89+
}
90+
91+
@Test
92+
public void testParsing_statusAbandoned() throws Exception {
93+
GerritTrigger gerritTrigger =
94+
skylarkTestExecutor.eval(
95+
"e",
96+
"e = git.gerrit_trigger("
97+
+ "url = 'https://test.googlesource.com/example',"
98+
+ "events = {'STATUS': ['ABANDONED']}, allow_submit = True)");
99+
100+
assertThat(gerritTrigger.describe())
101+
.containsExactly(
102+
"type",
103+
"gerrit_trigger",
104+
"url",
105+
"https://test.googlesource.com/example",
106+
"events",
107+
"STATUS",
108+
"SUBTYPES_STATUS",
109+
"ABANDONED",
110+
"gerritSubmit",
111+
"true");
112+
}
113+
114+
@Test
115+
public void testParsing_statusMultiple() throws Exception {
116+
GerritTrigger gerritTrigger =
117+
skylarkTestExecutor.eval(
118+
"e",
119+
"e = git.gerrit_trigger(\n"
120+
+ "url = 'https://test.googlesource.com/example',"
121+
+ "events = {'STATUS': ['PENDING', 'ABANDONED']})");
122+
123+
assertThat(gerritTrigger.describe())
124+
.containsExactly(
125+
"type", "gerrit_trigger",
126+
"url", "https://test.googlesource.com/example",
127+
"events", "STATUS",
128+
"SUBTYPES_STATUS", "PENDING",
129+
"SUBTYPES_STATUS", "ABANDONED",
130+
"gerritSubmit", "false");
131+
}
132+
133+
@Test
134+
public void testParsing_statusMultipleValid() throws Exception {
135+
GerritTrigger gerritTrigger =
136+
skylarkTestExecutor.eval(
137+
"e",
138+
"e = git.gerrit_trigger(\n"
139+
+ "url = 'https://test.googlesource.com/example',events = {'STATUS': ['PENDING',"
140+
+ " 'ABANDONED', 'MERGED', 'CLOSED', 'REVIEWED', 'OPEN']})");
141+
142+
assertThat(gerritTrigger.describe())
143+
.containsExactly(
144+
"type", "gerrit_trigger",
145+
"url", "https://test.googlesource.com/example",
146+
"events", "STATUS",
147+
"SUBTYPES_STATUS", "PENDING",
148+
"SUBTYPES_STATUS", "ABANDONED",
149+
"SUBTYPES_STATUS", "MERGED",
150+
"SUBTYPES_STATUS", "CLOSED",
151+
"SUBTYPES_STATUS", "REVIEWED",
152+
"SUBTYPES_STATUS", "OPEN",
153+
"gerritSubmit", "false");
154+
}
155+
156+
@Test
157+
public void testParsing_invalidStatus() throws Exception {
158+
skylarkTestExecutor.evalFails(
159+
"git.gerrit_trigger(url = 'https://test.googlesource.com/example', events = {'STATUS':"
160+
+ " ['INVALID']})",
161+
"Invalid status 'INVALID'. Valid values are ");
162+
}
163+
64164
@Test
65165
public void testParsing_Dict() throws Exception {
66166
GerritTrigger gerritTrigger =

0 commit comments

Comments
 (0)