-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathBitBucketPPRJobProbe.java
More file actions
242 lines (203 loc) · 9.65 KB
/
BitBucketPPRJobProbe.java
File metadata and controls
242 lines (203 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*******************************************************************************
* The MIT License
*
* Copyright (C) 2021, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
package io.jenkins.plugins.bitbucketpushandpullrequest;
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.PULL_REQUEST_MERGED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.PULL_REQUEST_SERVER_MERGED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.REPOSITORY_CLOUD_PUSH;
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.REPOSITORY_SERVER_PUSH;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.URIish;
import hudson.model.Job;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.GitStatus;
import hudson.plugins.mercurial.MercurialSCM;
import hudson.scm.SCM;
import hudson.security.ACL;
import hudson.security.ACLContext;
import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRAction;
import io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRUtils;
import io.jenkins.plugins.bitbucketpushandpullrequest.exception.TriggerNotSetException;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRHookEvent;
import io.jenkins.plugins.bitbucketpushandpullrequest.observer.BitBucketPPRObservable;
import jenkins.branch.MultiBranchProject;
import jenkins.model.Jenkins;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.triggers.SCMTriggerItem;
/**
*
* @author cdelmonte
*
*/
public class BitBucketPPRJobProbe {
private static final Logger logger = Logger.getLogger(BitBucketPPRJobProbe.class.getName());
public void triggerMatchingJobs(BitBucketPPRHookEvent bitbucketEvent,
BitBucketPPRAction bitbucketAction, BitBucketPPRObservable observable) {
// @todo deprecated. It will be removed in v3.0
if (!("git".equals(bitbucketAction.getScm()) || "hg".equals(bitbucketAction.getScm()))) {
throw new UnsupportedOperationException(
String.format("Unsupported SCM type %s", bitbucketAction.getScm()));
}
Function<String, URIish> f = (a) -> {
try {
return new URIish(a);
} catch (URISyntaxException e) {
logger.warning(String.format("Invalid URI %s.", e.getMessage()));
return null;
}
};
List<URIish> remotes = (List<URIish>) bitbucketAction.getScmUrls().stream().map(f)
.filter(Objects::nonNull).collect(Collectors.toList());
try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
Jenkins.get().getAllItems(Job.class).stream().forEach(job -> {
try {
triggerScm(job, remotes, bitbucketEvent, bitbucketAction, observable);
} catch (TriggerNotSetException e) {
logger.log(Level.FINE, "Trigger not set");
}
});
}
}
private void triggerScm(@Nonnull Job<?, ?> job, List<URIish> remotes,
BitBucketPPRHookEvent bitbucketEvent, BitBucketPPRAction bitbucketAction,
BitBucketPPRObservable observable) throws TriggerNotSetException {
BitBucketPPRTrigger bitbucketTrigger = getBitBucketTrigger(job)
.orElseThrow(() -> new TriggerNotSetException(job.getFullDisplayName()));
// @todo shouldn't be an instance variable?
List<SCM> scmTriggered = new ArrayList<>();
Optional<SCMTriggerItem> item =
Optional.ofNullable(SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job));
item.ifPresent(it -> it.getSCMs().stream().forEach(scmTrigger -> {
// @todo add comments to explain what is this check for
if (job.getParent() instanceof MultiBranchProject
&& mPJobShouldNotBeTriggered(job, bitbucketEvent, bitbucketAction)) {
logger.log(Level.FINEST, "Skipping job {0}.", job.getDisplayName());
return;
}
Predicate<URIish> p = (url) -> scmTrigger instanceof GitSCM ? matchGitScm(scmTrigger, url)
: scmTrigger instanceof MercurialSCM ? matchMercurialScm(scmTrigger, url) : false;
if (remotes.stream().anyMatch(p) && !scmTriggered.contains(scmTrigger)) {
scmTriggered.add(scmTrigger);
try {
bitbucketTrigger.onPost(bitbucketEvent, bitbucketAction, scmTrigger, observable);
return;
} catch (Throwable e) {
logger.log(Level.WARNING, "Error: {0}", e.getMessage());
e.printStackTrace();
}
}
logger.log(Level.FINE, "{0} SCM doesn't match remote repo {1} or it was already triggered.",
new Object[] {job.getName(), remotes});
}));
}
private boolean mPJobShouldNotBeTriggered(Job<?, ?> job, BitBucketPPRHookEvent bitbucketEvent,
BitBucketPPRAction bitbucketAction) {
if (job.getDisplayName() != null) {
String displayName = job.getDisplayName();
String sourceBranchName = bitbucketAction.getSourceBranch();
String targetBranchName = bitbucketAction.getTargetBranch();
logger.log(Level.FINEST,
"Bitbucket event is : {0}, Job Name : {1}, sourceBranchName: {2}, targetBranchName: {3}",
new String[] {bitbucketEvent.getAction(), displayName, sourceBranchName,
targetBranchName});
if (PULL_REQUEST_MERGED.equalsIgnoreCase(bitbucketEvent.getAction())) {
return !displayName.equalsIgnoreCase(targetBranchName);
}
if (PULL_REQUEST_SERVER_MERGED.equalsIgnoreCase(bitbucketEvent.getAction())) {
return !displayName.equalsIgnoreCase(targetBranchName);
}
if (sourceBranchName != null) {
return !displayName.equalsIgnoreCase(sourceBranchName);
}
if (REPOSITORY_CLOUD_PUSH.equalsIgnoreCase(bitbucketEvent.getAction())
&& targetBranchName != null) {
return !displayName.equals(targetBranchName);
}
if (REPOSITORY_SERVER_PUSH.equalsIgnoreCase(bitbucketEvent.getAction())
&& targetBranchName != null) {
return !displayName.equals(targetBranchName);
}
}
return true;
}
private Optional<BitBucketPPRTrigger> getBitBucketTrigger(Job<?, ?> job) {
Optional<BitBucketPPRTrigger> trigger = null;
if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
ParameterizedJobMixIn.ParameterizedJob<?, ?> pJob =
(ParameterizedJobMixIn.ParameterizedJob<?, ?>) job;
trigger = pJob.getTriggers().values().stream().filter(BitBucketPPRTrigger.class::isInstance)
.findFirst().map(BitBucketPPRTrigger.class::cast);
}
return trigger;
}
// @todo: deprecated, will be removed in v3.0
private boolean matchMercurialScm(SCM scm, URIish remote) {
try {
URI hgUri = new URI(((MercurialSCM) scm).getSource());
logger.log(Level.INFO, "Trying to match {0} ", hgUri.toString() + "<-->" + remote.toString());
return hgLooselyMatches(hgUri, remote.toString());
} catch (URISyntaxException ex) {
logger.log(Level.SEVERE, "Could not parse jobSource uri: {0} ", ex);
return false;
}
}
private boolean matchGitScm(SCM scm, URIish remote) {
return ((GitSCM) scm).getRepositories().stream()
.anyMatch((a) -> a.getURIs().stream().anyMatch((b) -> GitStatus.looselyMatches(b, remote)));
}
// @todo: deprecated, will be removed in v3.0
private boolean hgLooselyMatches(URI notifyUri, String repository) {
boolean result = false;
try {
if (!hgIsUnexpandedEnvVar(repository)) {
URI repositoryUri = new URI(repository);
logger.log(Level.INFO, "Mercurial loose match between {0} ",
notifyUri.toString() + "<- and ->" + repositoryUri.toString());
result = Objects.equals(notifyUri.getHost(), repositoryUri.getHost())
&& Objects.equals(StringUtils.stripEnd(notifyUri.getPath(), "/"),
StringUtils.stripEnd(repositoryUri.getPath(), "/"))
&& Objects.equals(notifyUri.getQuery(), repositoryUri.getQuery());
}
} catch (URISyntaxException ex) {
logger.log(Level.SEVERE, "could not parse repository uri " + repository, ex);
}
return result;
}
// @todo: deprecated, will be removed in v3.0
private boolean hgIsUnexpandedEnvVar(String str) {
return str.startsWith("$");
}
// @todo: deprecated, will be removed in v3.0
public boolean testMatchMercurialScm(SCM scm, URIish remote) {
return matchMercurialScm(scm, remote);
}
}