Skip to content
28 changes: 26 additions & 2 deletions src/main/java/org/jenkinsci/plugins/gogs/GogsProjectProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ associated documentation files (the "Software"), to deal in the Software without
public class GogsProjectProperty extends JobProperty<Job<?, ?>> {
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 @@ -52,13 +54,29 @@ public boolean getGogsUsePayload() {
return this.gogsUsePayload;
}

public String getGogsBranchFilter() {
return this.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;
}

private static final Logger LOGGER = Logger.getLogger(GogsWebHook.class.getName());

@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 @@ -68,6 +86,10 @@ public boolean getGogsUsePayload() {
return gogsUsePayload;
}

public String getGogsBranchFilter() {
return gogsBranchFilter;
}

public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) {
GogsProjectProperty tpp = req.bindJSON(
GogsProjectProperty.class,
Expand All @@ -76,15 +98,17 @@ public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) {
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";
}
}
}
14 changes: 12 additions & 2 deletions src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException

String jSecret = null;
boolean foundJob = false;
payloadProcessor.setPayload("ref", jsonObject.getString("ref"));

// filter branch, if ref not match branch filter, skip trigger job.
boolean isRefMatched = true;

String ref = (String) jsonObject.getString("ref");
payloadProcessor.setPayload("ref", ref);
payloadProcessor.setPayload("before", jsonObject.getString("before"));

SecurityContext saveCtx = ACL.impersonate(ACL.SYSTEM);
Expand All @@ -183,9 +188,9 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
if (property != null) { /* only if Gogs secret is defined on the job */
jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */
isRefMatched = property.filterBranch(ref);
}
} else {
String ref = (String) jsonObject.get("ref");
String[] components = ref.split("/");
if (components.length > 3) {
/* refs contains branch/tag with a slash */
Expand All @@ -203,6 +208,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
if (property != null) { /* only if Gogs secret is defined on the job */
jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */
isRefMatched = property.filterBranch(ref);
}
}
}
Expand All @@ -228,6 +234,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) {
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) && isNullOrEmpty(gSecret)) {
/* No password is set in Jenkins and Gogs, run without secrets */
result = payloadProcessor.triggerJobs(jobName, gogsDelivery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
<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>
</j:jelly>
28 changes: 28 additions & 0 deletions src/test/java/org/jenkinsci/plugins/gogs/GogsWebHookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.PrintWriter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -232,6 +233,33 @@ public void whenUriDoesNotContainUrlNameMustReturnError() throws Exception {
log.info("Test succeeded.");
}

@Test
public void whenJobBranchNotMatchMustReturnError() throws Exception {
String[][] 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 (int i = 0; i < test_vals.length; ++i) {
String filter = test_vals[i][0];
String ref = test_vals[i][1];
boolean ret = Boolean.parseBoolean(test_vals[i][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