Skip to content

Commit 6337d36

Browse files
committed
add GogsProjectProperty.filterBranch(), used in GogsWebHook to filter pushed branch.
1 parent 34e3889 commit 6337d36

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ public boolean getHasBranchFilter() {
6262
return gogsBranchFilter != null && gogsBranchFilter.length() > 0;
6363
}
6464

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+
6572
private static final Logger LOGGER = Logger.getLogger(GogsWebHook.class.getName());
6673

6774
@Extension

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
168168
}
169169

170170
String jSecret = null;
171-
String branchFilter = null;
172171
boolean foundJob = false;
173172

173+
// filter branch, if ref not match branch filter, skip trigger job.
174+
boolean isRefMatched = true;
175+
174176
String ref = (String) jsonObject.getString("ref");
175177
payloadProcessor.setPayload("ref", ref);
176178
payloadProcessor.setPayload("before", jsonObject.getString("before"));
@@ -186,7 +188,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
186188
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
187189
if (property != null) { /* only if Gogs secret is defined on the job */
188190
jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */
189-
branchFilter = property.getGogsBranchFilter(); /* branch filter provided by jenkins */
191+
isRefMatched = property.filterBranch(ref);
190192
}
191193
} else {
192194
String[] components = ref.split("/");
@@ -206,7 +208,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
206208
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
207209
if (property != null) { /* only if Gogs secret is defined on the job */
208210
jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */
209-
branchFilter = property.getGogsBranchFilter(); /* branch filter provided by jenkins */
211+
isRefMatched = property.filterBranch(ref);
210212
}
211213
}
212214
}
@@ -228,18 +230,12 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
228230
}
229231
}
230232

231-
// filter branch, not ref not match branch filter, skip trigger.
232-
boolean isRefMatched = true;
233-
if (branchFilter != null && !branchFilter.equals("*") && branchFilter.length() > 0) {
234-
isRefMatched = ref == null || ref.length() == 0 || ref.endsWith(branchFilter);
235-
}
236-
237233
if (!foundJob) {
238234
String msg = String.format("Job '%s' is not defined in Jenkins", jobName);
239235
result.setStatus(404, msg);
240236
LOGGER.warning(msg);
241237
} else if (!isRefMatched) {
242-
String msg = String.format("received ref ('%s') is not matched with branch filter in job '%s' ('%s')", ref, jobName, branchFilter);
238+
String msg = String.format("received ref ('%s') is not matched with branch filter in job '%s'", ref, jobName);
243239
result.setStatus(200, msg);
244240
LOGGER.info(msg);
245241
} else if (isNullOrEmpty(jSecret) && isNullOrEmpty(gSecret)) {

0 commit comments

Comments
 (0)