Skip to content

Commit d6d779d

Browse files
authored
Merge pull request #61 from jenkinsci/branch-filter
Branch filter
2 parents 9642e1c + c60b048 commit d6d779d

File tree

8 files changed

+190
-80
lines changed

8 files changed

+190
-80
lines changed

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
buildPlugin()
1+
buildPlugin(platforms: ['linux'])

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Gogs-Webhook Plugin
66
This plugin integrates [Gogs](https://gogs.io/) to Jenkins.<br>
77

88
In Gogs configure your webhook like this:<br>
9-
http(s)://<< jenkins-server >>/gogs-webhook/?job=<< jobname >>
9+
http(s)://<< jenkins-server >>/gogs-webhook/?job=<< jobname >>[&branch=<< branch name>>]
1010

1111
Example how your the webhook in Gogs should look like:
1212
![Example webhook](https://github.com/jenkinsci/gogs-webhook-plugin/raw/master/bin/gogs-webhook-screenshot.png)

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ associated documentation files (the "Software"), to deal in the Software without
3333
import org.acegisecurity.context.SecurityContextHolder;
3434
import org.apache.commons.codec.binary.Hex;
3535
import org.apache.commons.io.IOUtils;
36+
import org.apache.commons.lang.StringUtils;
3637
import org.kohsuke.stapler.StaplerRequest;
3738
import org.kohsuke.stapler.StaplerResponse;
3839

@@ -146,6 +147,8 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
146147
jobName = jobObject.toString();
147148
}
148149

150+
final Object branchName = queryStringMap.get("branch");
151+
149152
// Get the POST stream
150153
String body = IOUtils.toString(req.getInputStream(), DEFAULT_CHARSET);
151154
if (!body.isEmpty() && req.getRequestURI().contains("/" + URLNAME + "/")) {
@@ -160,6 +163,17 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
160163
return;
161164
}
162165

166+
String ref = jsonObject.getString("ref");
167+
LOGGER.fine("found ref " + ref);
168+
LOGGER.fine("found branch " + branchName);
169+
if (null != branchName && !StringUtils.containsIgnoreCase(ref, (String) branchName)) {
170+
// ignore all commit if they is not in context
171+
LOGGER.fine("build was rejected");
172+
result.setStatus(200, String.format("Commit is not relevant. Relevant context is %s", branchName));
173+
exitWebHook(result, rsp);
174+
return;
175+
}
176+
163177
String contentType = req.getContentType();
164178
if (contentType != null && contentType.startsWith("application/x-www-form-urlencoded")) {
165179
body = URLDecoder.decode(body, DEFAULT_CHARSET);
@@ -174,7 +188,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
174188
// filter branch, if ref not match branch filter, skip trigger job.
175189
boolean isRefMatched = true;
176190

177-
String ref = (String) jsonObject.getString("ref");
191+
178192
payloadProcessor.setPayload("ref", ref);
179193
payloadProcessor.setPayload("before", jsonObject.getString("before"));
180194

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ void createEmptyRepo(String projectName) throws IOException {
193193
}
194194
}
195195

196+
void removeRepo(String projectName) throws IOException {
197+
Executor executor = getExecutor();
198+
String gogsHookConfigUrl = getGogsServer_apiUrl() + "repos/" + this.gogsServer_user + "/" + projectName;
199+
Request request = Request.Delete(gogsHookConfigUrl);
200+
201+
if (this.gogsAccessToken != null) {
202+
request.addHeader("Authorization", "token " + this.gogsAccessToken);
203+
}
204+
205+
int result = executor.execute(request).returnResponse().getStatusLine().getStatusCode();
206+
if (result != 204) {
207+
throw new IOException("Repository deletion call did not return the expected value (returned " + result + ")");
208+
}
209+
}
196210

197211
/**
198212
* Gets a Executor object. The Executor object allows to cache the authentication data.

0 commit comments

Comments
 (0)