Skip to content

Commit

Permalink
Add branch filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderv32 committed Nov 25, 2018
1 parent 12ffa3a commit 178daa8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.11</version>
<version>2.30</version>
</parent>
<artifactId>gogs-webhook</artifactId>
<version>1.0.15-SNAPSHOT</version>
Expand All @@ -16,7 +16,7 @@
<java.level.test>8</java.level.test>

<!-- Jenkins -->
<jenkins.version>2.60.3</jenkins.version>
<jenkins.version>2.138.3</jenkins.version>
<jenkins-test-harness.version>2.38</jenkins-test-harness.version>

<!-- Security -->
Expand Down
30 changes: 27 additions & 3 deletions src/main/java/org/jenkinsci/plugins/gogs/GogsProjectProperty.java
Expand Up @@ -36,13 +36,17 @@ associated documentation files (the "Software"), to deal in the Software without

@SuppressWarnings("ALL")
public class GogsProjectProperty extends JobProperty<Job<?, ?>> {
private static final Logger LOGGER = Logger.getLogger(GogsWebHook.class.getName());

private final String gogsSecret;
private final boolean gogsUsePayload;
private final String gogsBranchFilter;

@DataBoundConstructor
public GogsProjectProperty(String gogsSecret, boolean gogsUsePayload) {
public GogsProjectProperty(String gogsSecret, boolean gogsUsePayload, String gogsBranchFilter) {
this.gogsSecret = gogsSecret;
this.gogsUsePayload = gogsUsePayload;
this.gogsBranchFilter = gogsBranchFilter;
}

public String getGogsSecret() {
Expand All @@ -53,13 +57,27 @@ public boolean getGogsUsePayload() {
return this.gogsUsePayload;
}

private static final Logger LOGGER = Logger.getLogger(GogsWebHook.class.getName());
public String getGogsBranchFilter() {
return gogsBranchFilter;
}

public boolean getHasBranchFilter() {
return gogsBranchFilter != null && gogsBranchFilter.length() > 0;
}

public boolean filterBranch(String ref) {
if (gogsBranchFilter != null && gogsBranchFilter.length() > 0 && !gogsBranchFilter.equals("*")) {
return ref == null || ref.length() == 0 || ref.endsWith(gogsBranchFilter);
}
return true;
}

@Extension
public static final class DescriptorImpl extends JobPropertyDescriptor {
public static final String GOGS_PROJECT_BLOCK_NAME = "gogsProject";
private String gogsSecret;
private boolean gogsUsePayload;
private String gogsBranchFilter;

public String getGogsSecret() {
return gogsSecret;
Expand All @@ -69,6 +87,10 @@ public boolean getGogsUsePayload() {
return gogsUsePayload;
}

public String getGogsBranchFilter() {
return gogsBranchFilter;
}

public JobProperty<?> newInstance(@Nonnull StaplerRequest req, @Nonnull JSONObject formData) {
GogsProjectProperty tpp = req.bindJSON(
GogsProjectProperty.class,
Expand All @@ -77,15 +99,17 @@ public JobProperty<?> newInstance(@Nonnull StaplerRequest req, @Nonnull JSONObje
if (tpp != null) {
LOGGER.finest(formData.toString());
LOGGER.finest(tpp.gogsSecret);
LOGGER.finest(tpp.gogsBranchFilter);

gogsSecret = tpp.gogsSecret;
gogsBranchFilter = tpp.gogsBranchFilter;
}
return tpp;
}

@Override
public String getDisplayName() {
return "Gogs Secret";
return "Gogs Project Property";
}
}
}
19 changes: 13 additions & 6 deletions src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java
Expand Up @@ -102,6 +102,7 @@ private void setGogsSignature(String gogsSignature) {
* @param rsp response
* @throws IOException problem while parsing
*/
@SuppressWarnings("WeakerAccess")
public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException {
GogsPayloadProcessor payloadProcessor = new GogsPayloadProcessor();
GogsCause gogsCause = new GogsCause();
Expand All @@ -121,7 +122,8 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
if (!body.isEmpty() && req.getRequestURI().contains("/" + URLNAME + "/")) {
JSONObject jsonObject = JSONObject.fromObject(body);
JSONObject commits = (JSONObject) jsonObject.getJSONArray("commits").get(0);
String message = (String) commits.get("message");
String ref = jsonObject.getString("ref");
String message = commits.getString("message");

if (message.startsWith("[IGNORE]")) {
// Ignore commits starting with message "[IGNORE]"
Expand All @@ -140,22 +142,23 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException

AtomicReference<String> jSecret = new AtomicReference<>(null);
AtomicBoolean foundJob = new AtomicBoolean(false);
AtomicBoolean isRefMatched = new AtomicBoolean(true);
gogsCause.setGogsPayloadData(jsonObject.toString());
gogsCause.setDeliveryID(getGogsDelivery());
payloadProcessor.setCause(gogsCause);

try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
try (ACLContext ignored = ACL.as(ACL.SYSTEM)) {
StringJoiner stringJoiner = new StringJoiner("%2F");
Pattern.compile("/").splitAsStream((String) jsonObject.get("ref")).skip(2)
.forEach(stringJoiner::add);
String ref = stringJoiner.toString();
Pattern.compile("/").splitAsStream(jsonObject.getString("ref")).skip(2).forEach(stringJoiner::add);
String ref_strj = stringJoiner.toString();

/* secret is stored in the properties of Job */
Stream.of(jobName, jobName + "/" + ref).map(j -> GogsUtils.find(j, Job.class)).filter(Objects::nonNull).forEach(job -> {
Stream.of(jobName, jobName + "/" + ref_strj).map(j -> GogsUtils.find(j, Job.class)).filter(Objects::nonNull).forEach(job -> {
foundJob.set(true);
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
if (property != null) { /* only if Gogs secret is defined on the job */
jSecret.set(property.getGogsSecret()); /* Secret provided by Jenkins */
isRefMatched.set(property.filterBranch(ref));
}
});
}
Expand All @@ -178,6 +181,10 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
String msg = String.format("Job '%s' is not defined in Jenkins", jobName);
result.setStatus(404, msg);
LOGGER.warning(msg);
} else if (!isRefMatched.get()) {
String msg = String.format("received ref ('%s') is not matched with branch filter in job '%s'", ref, jobName);
result.setStatus(200, msg);
LOGGER.info(msg);
} else if (isNullOrEmpty(jSecret.get()) && isNullOrEmpty(gSecret)) {
/* No password is set in Jenkins and Gogs, run without secrets */
result = payloadProcessor.triggerJobs(jobName);
Expand Down
@@ -1,9 +1,14 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:section title="Gogs Webhook" name="${descriptor.GOGS_PROJECT_BLOCK_NAME}">
<f:optionalBlock title="Use Gogs secret" inline="true" checked="${instance.gogsSecret != null}">
<f:entry title="${%Secret}">
<f:password field="gogsSecret" />
<f:password field="gogsSecret"/>
</f:entry>
</f:optionalBlock>
<f:optionalBlock title="Branch Filter" inline="true" checked="${instance.hasBranchFilter}">
<f:entry title="${%BranchFilter}">
<f:textbox field="gogsBranchFilter"/>
</f:entry>
</f:optionalBlock>
</f:section>
Expand Down
27 changes: 25 additions & 2 deletions src/test/java/org/jenkinsci/plugins/gogs/GogsWebHookTest.java
Expand Up @@ -24,8 +24,7 @@
import java.io.InputStream;
import java.io.PrintWriter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -232,6 +231,30 @@ public void whenUriDoesNotContainUrlNameMustReturnError() throws Exception {
log.info("Test succeeded.");
}

@Test
public void whenJobBranchNotMatchMustReturnError() throws Exception {
Object[][] test_vals = {
{null, "master", true},
{null, "dev", true},
{"", "master", true},
{"", "dev", true},
{"*", "master", true},
{"*", "dev", true},
{"dev", "master", false},
{"dev", "dev", true},
{"master", "master", true},
{"master", "dev", false},
};
for (Object[] test_val : test_vals) {
String filter = (String) test_val[0];
String ref = (String) test_val[1];
boolean ret = (Boolean) test_val[2];
GogsProjectProperty property = new GogsProjectProperty(null, false, filter);
assertSame(String.format("branch filter check failed for [%s -> %s]", ref, filter), ret, property.filterBranch(ref));
}
log.info("Test succeeded.");
}

//
// Helper methods
//
Expand Down

0 comments on commit 178daa8

Please sign in to comment.