Skip to content

Commit 5180935

Browse files
authored
Pull/149 (#158)
* - refactored to use java 8 functions - re-introduced branch name filter restriction Signed-off-by: Jae Gangemi <jgangemi@gmail.com> * Undo rename Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * Match to commont Str for UI pattern. Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * fixup * restriction not in trigger now * Not in trigger atm Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * checkstyle Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com>
1 parent f956115 commit 5180935

13 files changed

Lines changed: 589 additions & 83 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,4 @@ local.properties
8181
Session.vim
8282
.netrwhist
8383
*~
84+
some/

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/branch/GitHubBranchTrigger.java

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.kostyasha.github.integration.branch;
22

33
import antlr.ANTLRException;
4+
45
import com.cloudbees.jenkins.GitHubWebHook;
56
import com.github.kostyasha.github.integration.branch.events.GitHubBranchEvent;
67
import com.github.kostyasha.github.integration.branch.events.GitHubBranchEventDescriptor;
@@ -34,25 +35,26 @@
3435
import javax.annotation.CheckForNull;
3536
import javax.annotation.Nonnull;
3637
import javax.annotation.Nullable;
38+
3739
import java.io.IOException;
3840
import java.net.URISyntaxException;
3941
import java.util.ArrayList;
4042
import java.util.Collections;
4143
import java.util.Date;
4244
import java.util.LinkedHashSet;
4345
import java.util.List;
46+
import java.util.Objects;
4447
import java.util.Set;
48+
import java.util.stream.Collectors;
4549

4650
import static com.github.kostyasha.github.integration.branch.trigger.check.BranchToCauseConverter.toGitHubBranchCause;
4751
import static com.github.kostyasha.github.integration.branch.trigger.check.LocalRepoUpdater.updateLocalRepo;
4852
import static com.github.kostyasha.github.integration.branch.trigger.check.SkipFirstRunForBranchFilter.ifSkippedFirstRun;
4953
import static com.github.kostyasha.github.integration.branch.webhook.WebhookInfoBranchPredicates.withHookTriggerMode;
54+
import static com.github.kostyasha.github.integration.generic.GitHubTriggerDescriptor.githubFor;
5055
import static com.google.common.base.Charsets.UTF_8;
51-
import static com.google.common.base.Predicates.and;
5256
import static com.google.common.base.Predicates.not;
53-
import static com.google.common.base.Predicates.notNull;
5457
import static java.text.DateFormat.getDateTimeInstance;
55-
import static org.jenkinsci.plugins.github.pullrequest.GitHubPRTrigger.DescriptorImpl.githubFor;
5658
import static org.jenkinsci.plugins.github.pullrequest.GitHubPRTriggerMode.LIGHT_HOOKS;
5759
import static org.jenkinsci.plugins.github.pullrequest.utils.ObjectsUtil.isNull;
5860
import static org.jenkinsci.plugins.github.pullrequest.utils.ObjectsUtil.nonNull;
@@ -103,33 +105,16 @@ public void setPreStatus(boolean preStatus) {
103105
this.preStatus = preStatus;
104106
}
105107

106-
@DataBoundSetter
107-
public void setUserRestriction(GitHubPRUserRestriction userRestriction) {
108-
this.userRestriction = userRestriction;
109-
}
110-
111-
@DataBoundSetter
112-
public void setBranchRestriction(GitHubPRBranchRestriction branchRestriction) {
113-
this.branchRestriction = branchRestriction;
114-
}
115-
116108
public boolean isPreStatus() {
117109
return preStatus;
118110
}
119111

120-
public GitHubPRUserRestriction getUserRestriction() {
121-
return userRestriction;
122-
}
123-
124-
public GitHubPRBranchRestriction getBranchRestriction() {
125-
return branchRestriction;
126-
}
127-
128112
@CheckForNull
129113
public List<GitHubBranchEvent> getEvents() {
130114
return events;
131115
}
132116

117+
@CheckForNull
133118
public void setEvents(List<GitHubBranchEvent> events) {
134119
this.events = events;
135120
}
@@ -144,12 +129,14 @@ public void start(Job<?, ?> project, boolean newInstance) {
144129
}
145130
}
146131

132+
@Override
147133
public void run() {
148134
if (getTriggerMode() != LIGHT_HOOKS) {
149135
doRun(null);
150136
}
151137
}
152138

139+
@Override
153140
@CheckForNull
154141
public GitHubBranchPollingLogAction getPollingLogAction() {
155142
if (isNull(pollingLogAction) && nonNull(job)) {
@@ -237,23 +224,13 @@ private List<GitHubBranchCause> readyToBuildCauses(GitHubBranchRepository localR
237224
GHRepository remoteRepo = getRemoteRepository();
238225
Set<GHBranch> remoteBranches = branchesToCheck(branch, remoteRepo, localRepository);
239226

240-
Set<GHBranch> prepared = from(remoteBranches)
241-
// .transform(prepareUserRestrictionFilter(localRepository, this))
242-
.toSet();
243-
244-
List<GitHubBranchCause> causes = from(prepared)
245-
.filter(and(
246-
ifSkippedFirstRun(listener, skipFirstRun)//,
247-
// withBranchRestriction(listener, branchRestriction),
248-
// withUserRestriction(listener, userRestriction)
249-
))
250-
.transform(toGitHubBranchCause(localRepository, listener, this))
251-
.filter(notNull())
252-
.toList();
253-
254-
LOG.trace("Causes count for {}: {}", localRepository.getFullName(), causes.size());
255-
from(prepared).transform(updateLocalRepo(localRepository)).toSet();
227+
List<GitHubBranchCause> causes = checkBranches(remoteBranches, localRepository, listener);
256228

229+
/*
230+
* update details about the local repo after the causes are determined as they expect
231+
* new branches to not be found in the local details
232+
*/
233+
updateLocalRepository(remoteBranches, localRepository);
257234
saveIfSkipFirstRun();
258235

259236
GHRateLimit rateLimitAfter = github.getRateLimit();
@@ -288,6 +265,27 @@ private Set<GHBranch> branchesToCheck(String branch, @Nonnull GHRepository remot
288265
return ghBranches;
289266
}
290267

268+
private List<GitHubBranchCause> checkBranches(Set<GHBranch> remoteBranches,
269+
GitHubBranchRepository localRepository, LoggingTaskListenerWrapper listener) {
270+
List<GitHubBranchCause> causes = remoteBranches.stream()
271+
// TODO: update user whitelist filter
272+
.filter(ifSkippedFirstRun(listener, skipFirstRun))
273+
.filter(Objects::nonNull)
274+
.map(toGitHubBranchCause(localRepository, listener, this))
275+
.filter(Objects::nonNull)
276+
.collect(Collectors.toList());
277+
278+
LOG.debug("Build trigger count for [{}] : {}", localRepository.getFullName(), causes.size());
279+
return causes;
280+
}
281+
282+
private void updateLocalRepository(Set<GHBranch> remoteBranches, GitHubBranchRepository localRepository) {
283+
long count = remoteBranches.stream()
284+
.map(updateLocalRepo(localRepository))
285+
.count();
286+
LOG.trace("Updated local branch details with [{}] repositories.", count);
287+
}
288+
291289
private static boolean isSupportedTriggerMode(GitHubPRTriggerMode mode) {
292290
return mode != LIGHT_HOOKS;
293291
}

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/branch/dsl/GitHubBranchJobDslExtenstion.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import antlr.ANTLRException;
44
import com.github.kostyasha.github.integration.branch.GitHubBranchTrigger;
55
import com.github.kostyasha.github.integration.branch.dsl.context.GitHubBranchTriggerDslContext;
6-
7-
import org.jenkinsci.plugins.github.pullrequest.restrictions.GitHubPRBranchRestriction;
8-
96
import hudson.Extension;
107
import javaposse.jobdsl.dsl.helpers.triggers.TriggerContext;
118
import javaposse.jobdsl.plugin.ContextExtensionPoint;
@@ -27,7 +24,6 @@ public Object onBranch(Runnable closure) throws ANTLRException {
2724
trigger.setPreStatus(context.isSetPreStatus());
2825
trigger.setCancelQueued(context.isCancelQueued());
2926
trigger.setAbortRunning(context.isAbortRunning());
30-
trigger.setBranchRestriction(new GitHubPRBranchRestriction(context.whitelistedBranches()));
3127

3228
return trigger;
3329
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.github.kostyasha.github.integration.branch.events.impl;
2+
3+
import com.github.kostyasha.github.integration.branch.GitHubBranch;
4+
import com.github.kostyasha.github.integration.branch.GitHubBranchCause;
5+
import com.github.kostyasha.github.integration.branch.GitHubBranchRepository;
6+
import com.github.kostyasha.github.integration.branch.GitHubBranchTrigger;
7+
import com.github.kostyasha.github.integration.branch.events.GitHubBranchEvent;
8+
import com.github.kostyasha.github.integration.branch.events.GitHubBranchEventDescriptor;
9+
10+
import hudson.Extension;
11+
import hudson.model.TaskListener;
12+
13+
import org.kohsuke.github.GHBranch;
14+
import org.kohsuke.stapler.DataBoundConstructor;
15+
import org.kohsuke.stapler.DataBoundSetter;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
19+
import java.util.Set;
20+
import java.util.regex.Pattern;
21+
import java.util.regex.PatternSyntaxException;
22+
import java.util.stream.Collectors;
23+
import java.util.stream.Stream;
24+
25+
public class GitHubBranchRestrictionFilter extends GitHubBranchEvent {
26+
27+
private static final String DISPLAY_NAME = "Branch Restrictions";
28+
29+
private static final String LINE_SEPARATOR = System.lineSeparator();
30+
private static final Logger LOGGER = LoggerFactory.getLogger(GitHubBranchRestrictionFilter.class);
31+
32+
private boolean exclude;
33+
34+
private boolean matchAsPattern;
35+
private Set<String> matchCriteria;
36+
37+
@DataBoundConstructor
38+
public GitHubBranchRestrictionFilter() {
39+
}
40+
41+
public String getMatchCriteriaStr() {
42+
return String.join(LINE_SEPARATOR, matchCriteria);
43+
}
44+
45+
@DataBoundSetter
46+
public void setMatchCriteriaStr(String matchCriteria) {
47+
this.matchCriteria = Stream.of(matchCriteria
48+
.split(LINE_SEPARATOR))
49+
.collect(Collectors.toSet());
50+
}
51+
52+
public boolean isExclude() {
53+
return exclude;
54+
}
55+
56+
@DataBoundSetter
57+
public void setExclude(boolean exclude) {
58+
this.exclude = exclude;
59+
}
60+
61+
public boolean isMatchAsPattern() {
62+
return matchAsPattern;
63+
}
64+
65+
@DataBoundSetter
66+
public void setMatchAsPattern(boolean matchAsPattern) {
67+
this.matchAsPattern = matchAsPattern;
68+
}
69+
70+
@Override
71+
public GitHubBranchCause check(GitHubBranchTrigger gitHubBranchTrigger, GHBranch remoteBranch, GitHubBranch localBranch,
72+
GitHubBranchRepository localRepo, TaskListener listener) {
73+
String name = remoteBranch.getName();
74+
if (matchCriteria.isEmpty() || branchIsAllowed(name)) {
75+
if (matchCriteria.isEmpty()) {
76+
LOGGER.warn("Branch restriction filter added but no match criteria set, all branches allowed");
77+
}
78+
return toCause(remoteBranch, localRepo, false, "Branch [%s] allowed by branch name restriction filter", name);
79+
}
80+
81+
return toCause(remoteBranch, localRepo, true, "Branch [%s] filtered by branch name restriction filter", name);
82+
}
83+
84+
private boolean branchIsAllowed(String name) {
85+
for (String pattern : matchCriteria) {
86+
LOGGER.trace("Checking branch [{}] against pattern [{}] - exclude [{}]", name, pattern, exclude);
87+
if (matches(name, pattern)) {
88+
if (exclude) {
89+
LOGGER.debug("Branch [{}] matches pattern [{}], will be excluded", name, pattern);
90+
return false;
91+
}
92+
LOGGER.debug("Branch [%s] matched pattern [{}] and is marked for inclusion", name, pattern);
93+
return true;
94+
}
95+
}
96+
LOGGER.trace("Branch [{}] matched no patterns, included [{}]", name, exclude);
97+
return exclude;
98+
}
99+
100+
private boolean matches(String name, String pattern) {
101+
if (!matchAsPattern) {
102+
LOGGER.debug("Checking branch [{}] against exact match [{}]", name, pattern);
103+
return name.equals(pattern);
104+
}
105+
106+
try {
107+
LOGGER.debug("Checking branch [{}] against pattern [{}].", name, pattern);
108+
return Pattern.compile(pattern).matcher(name).matches();
109+
} catch (PatternSyntaxException e) {
110+
LOGGER.error("Invalid pattern [{}] detected checking branch [{}]", pattern, name, e);
111+
return false;
112+
}
113+
}
114+
115+
private GitHubBranchCause toCause(GHBranch remoteBranch, GitHubBranchRepository localRepo, boolean skip,
116+
String message, Object... args) {
117+
return new GitHubBranchCause(remoteBranch, localRepo, String.format(message, args), skip);
118+
}
119+
120+
@Extension
121+
public static class Descriptor extends GitHubBranchEventDescriptor {
122+
@Override
123+
public String getDisplayName() {
124+
return DISPLAY_NAME;
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)