Skip to content

Commit 2c7fca6

Browse files
authored
Merge pull request #49 from czsilence/master
branch filter support
2 parents 1789e90 + 66fae1b commit 2c7fca6

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

src/main/java/org/jenkinsci/plugins/gogs/GogsProjectProperty.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ associated documentation files (the "Software"), to deal in the Software without
3737
public class GogsProjectProperty extends JobProperty<Job<?, ?>> {
3838
private final String gogsSecret;
3939
private final boolean gogsUsePayload;
40+
private final String gogsBranchFilter;
4041

4142
@DataBoundConstructor
42-
public GogsProjectProperty(String gogsSecret, boolean gogsUsePayload) {
43+
public GogsProjectProperty(String gogsSecret, boolean gogsUsePayload, String gogsBranchFilter) {
4344
this.gogsSecret = gogsSecret;
4445
this.gogsUsePayload = gogsUsePayload;
46+
this.gogsBranchFilter = gogsBranchFilter;
4547
}
4648

4749
public String getGogsSecret() {
@@ -52,13 +54,29 @@ public boolean getGogsUsePayload() {
5254
return this.gogsUsePayload;
5355
}
5456

57+
public String getGogsBranchFilter() {
58+
return this.gogsBranchFilter;
59+
}
60+
61+
public boolean getHasBranchFilter() {
62+
return gogsBranchFilter != null && gogsBranchFilter.length() > 0;
63+
}
64+
65+
public boolean filterBranch(String ref) {
66+
if (gogsBranchFilter != null && gogsBranchFilter.length() > 0 && !gogsBranchFilter.equals("*")) {
67+
return ref == null || ref.length() == 0 || ref.endsWith(gogsBranchFilter);
68+
}
69+
return true;
70+
}
71+
5572
private static final Logger LOGGER = Logger.getLogger(GogsWebHook.class.getName());
5673

5774
@Extension
5875
public static final class DescriptorImpl extends JobPropertyDescriptor {
5976
public static final String GOGS_PROJECT_BLOCK_NAME = "gogsProject";
6077
private String gogsSecret;
6178
private boolean gogsUsePayload;
79+
private String gogsBranchFilter;
6280

6381
public String getGogsSecret() {
6482
return gogsSecret;
@@ -68,6 +86,10 @@ public boolean getGogsUsePayload() {
6886
return gogsUsePayload;
6987
}
7088

89+
public String getGogsBranchFilter() {
90+
return gogsBranchFilter;
91+
}
92+
7193
public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) {
7294
GogsProjectProperty tpp = req.bindJSON(
7395
GogsProjectProperty.class,
@@ -76,15 +98,17 @@ public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) {
7698
if (tpp != null) {
7799
LOGGER.finest(formData.toString());
78100
LOGGER.finest(tpp.gogsSecret);
101+
LOGGER.finest(tpp.gogsBranchFilter);
79102

80103
gogsSecret = tpp.gogsSecret;
104+
gogsBranchFilter = tpp.gogsBranchFilter;
81105
}
82106
return tpp;
83107
}
84108

85109
@Override
86110
public String getDisplayName() {
87-
return "Gogs Secret";
111+
return "Gogs Project Property";
88112
}
89113
}
90114
}

src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,12 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
169169

170170
String jSecret = null;
171171
boolean foundJob = false;
172-
payloadProcessor.setPayload("ref", jsonObject.getString("ref"));
172+
173+
// filter branch, if ref not match branch filter, skip trigger job.
174+
boolean isRefMatched = true;
175+
176+
String ref = (String) jsonObject.getString("ref");
177+
payloadProcessor.setPayload("ref", ref);
173178
payloadProcessor.setPayload("before", jsonObject.getString("before"));
174179

175180
SecurityContext saveCtx = ACL.impersonate(ACL.SYSTEM);
@@ -183,9 +188,9 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
183188
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
184189
if (property != null) { /* only if Gogs secret is defined on the job */
185190
jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */
191+
isRefMatched = property.filterBranch(ref);
186192
}
187193
} else {
188-
String ref = (String) jsonObject.get("ref");
189194
String[] components = ref.split("/");
190195
if (components.length > 3) {
191196
/* refs contains branch/tag with a slash */
@@ -203,6 +208,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
203208
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
204209
if (property != null) { /* only if Gogs secret is defined on the job */
205210
jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */
211+
isRefMatched = property.filterBranch(ref);
206212
}
207213
}
208214
}
@@ -228,6 +234,10 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
228234
String msg = String.format("Job '%s' is not defined in Jenkins", jobName);
229235
result.setStatus(404, msg);
230236
LOGGER.warning(msg);
237+
} else if (!isRefMatched) {
238+
String msg = String.format("received ref ('%s') is not matched with branch filter in job '%s'", ref, jobName);
239+
result.setStatus(200, msg);
240+
LOGGER.info(msg);
231241
} else if (isNullOrEmpty(jSecret) && isNullOrEmpty(gSecret)) {
232242
/* No password is set in Jenkins and Gogs, run without secrets */
233243
result = payloadProcessor.triggerJobs(jobName, gogsDelivery);

src/main/resources/org/jenkinsci/plugins/gogs/GogsProjectProperty/config.jelly

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
<f:password field="gogsSecret" />
77
</f:entry>
88
</f:optionalBlock>
9+
<f:optionalBlock title="Branch Filter" inline="true" checked="${instance.hasBranchFilter}">
10+
<f:entry title="${%BranchFilter}">
11+
<f:textbox field="gogsBranchFilter" />
12+
</f:entry>
13+
</f:optionalBlock>
914
</f:section>
1015
</j:jelly>

src/test/java/org/jenkinsci/plugins/gogs/GogsWebHookTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.PrintWriter;
2626

2727
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertSame;
2829
import static org.junit.Assert.fail;
2930
import static org.mockito.Mockito.verify;
3031
import static org.mockito.Mockito.when;
@@ -232,6 +233,33 @@ public void whenUriDoesNotContainUrlNameMustReturnError() throws Exception {
232233
log.info("Test succeeded.");
233234
}
234235

236+
@Test
237+
public void whenJobBranchNotMatchMustReturnError() throws Exception {
238+
String[][] test_vals = {
239+
{null, "master", "true"},
240+
{null, "dev", "true"},
241+
{"", "master", "true"},
242+
{"", "dev", "true"},
243+
{"*", "master", "true"},
244+
{"*", "dev", "true"},
245+
{"dev", "master", "false"},
246+
{"dev", "dev", "true"},
247+
{"master", "master", "true"},
248+
{"master", "dev", "false"},
249+
};
250+
251+
for (int i = 0; i < test_vals.length; ++i) {
252+
String filter = test_vals[i][0];
253+
String ref = test_vals[i][1];
254+
boolean ret = Boolean.parseBoolean(test_vals[i][2]);
255+
256+
GogsProjectProperty property = new GogsProjectProperty(null, false, filter);
257+
assertSame(String.format("branch filter check failed for [%s -> %s]", ref, filter), ret, property.filterBranch(ref));
258+
}
259+
260+
log.info("Test succeeded.");
261+
}
262+
235263
//
236264
// Helper methods
237265
//

0 commit comments

Comments
 (0)