forked from jenkinsci/bitbucket-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitbucketJobProbe.java
More file actions
294 lines (268 loc) · 15.7 KB
/
BitbucketJobProbe.java
File metadata and controls
294 lines (268 loc) · 15.7 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.cloudbees.jenkins.plugins;
import hudson.model.CauseAction;
import hudson.model.Job;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.GitStatus;
import hudson.plugins.mercurial.MercurialSCM;
import hudson.plugins.mercurial.MercurialSCMSource;
import hudson.scm.SCM;
import hudson.security.ACL;
import java.net.URISyntaxException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.plugins.git.GitSCMSource;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceOwner;
import jenkins.triggers.SCMTriggerItem;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import com.google.common.base.Objects;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource;
public class BitbucketJobProbe {
private boolean isBranchPluginAvailable = false;
public BitbucketJobProbe() {
if (Jenkins.get().getPlugin("cloudbees-bitbucket-branch-source") != null) {
LOGGER.log(Level.FINEST, "Bitbucket branch source available");
isBranchPluginAvailable = true;
}
}
public void triggerMatchingJobs(String user, String url, String scm, String payload) {
triggerMatchingJobs(user, url, scm, payload, null);
}
public void triggerMatchingJobs(String user, String url, String scm, String payload, String branchName) {
if ("git".equals(scm) || "hg".equals(scm)) {
SecurityContext old = Jenkins.getInstance().getACL().impersonate2(ACL.SYSTEM2);
try {
URIish remote = new URIish(url);
for (Job<?, ?> job : Jenkins.getInstance().getAllItems(Job.class)) {
BitBucketTrigger bTrigger = null;
LOGGER.log(Level.FINE, "Considering candidate job [{0}]", job.getName());
if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
ParameterizedJobMixIn.ParameterizedJob pJob = (ParameterizedJobMixIn.ParameterizedJob) job;
for (Object trigger : pJob.getTriggers().values()) {
if (trigger instanceof BitBucketTrigger) {
bTrigger = (BitBucketTrigger) trigger;
LOGGER.log(Level.FINE, "Job [{0}] has BitBucketTrigger", job.getName());
break;
} else if (trigger instanceof BitBucketMultibranchTrigger) {
LOGGER.fine("Trigger is BitBucketMultibranchTrigger");
}
}
} else {
LOGGER.finest("job [" + job.getName() + "] is not ParameterizedJobMixIn.ParameterizedJob. [" + job.getClass().getSimpleName() + "]");
}
if (bTrigger == null) {
LOGGER.log(Level.FINE, "[{0}] hasn't BitBucketTrigger set", job.getName());
} else {
LOGGER.log(Level.FINE, "Considering to poke {0}", job.getFullDisplayName());
SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job);
if (item == null) {
LOGGER.log(Level.FINER, "item is null");
} else {
List<SCM> scmTriggered = new ArrayList<>();
if (item.getSCMs().isEmpty()) {
LOGGER.log(Level.FINE, "No SCM configuration was found!");
}
for (SCM scmTrigger : item.getSCMs()) {
if (match(scmTrigger, remote, bTrigger.getOverrideUrl()) && !hasBeenTriggered(scmTriggered, scmTrigger)) {
LOGGER.log(Level.FINER, "Triggering BitBucket job {0}", job.getFullDisplayName());
scmTriggered.add(scmTrigger);
bTrigger.onPost(user, payload, branchName);
} else {
LOGGER.log(Level.FINEST, String.format("[%s] SCM doesn't match remote repo [%s]", job.getName(), remote));
}
}
}
}
}
LOGGER.log(Level.FINE, "Now checking SCMSourceOwners/multiBranchProjects");
for (SCMSourceOwner scmSourceOwner : Jenkins.getInstance().getAllItems(SCMSourceOwner.class)) {
LOGGER.log(Level.FINE, "Considering candidate scmSourceOwner {0}", scmSourceOwner.getFullDisplayName());
List<SCMSource> scmSources = scmSourceOwner.getSCMSources();
for (SCMSource scmSource : scmSources) {
LOGGER.log(Level.FINER, "Considering candidate scmSource {0}", scmSource);
if (match(scmSource, remote)) {
if (scmSourceOwner instanceof WorkflowMultiBranchProject) {
LOGGER.finest("scmSourceOwner [" + scmSourceOwner.getName() + "] is of type WorkflowMultiBranchProject");
WorkflowMultiBranchProject workflowMultiBranchProject = (WorkflowMultiBranchProject) scmSourceOwner;
AtomicReference<BitBucketMultibranchTrigger> bitBucketMultibranchTrigger = new AtomicReference<>(null);
if (workflowMultiBranchProject.getTriggers().isEmpty()) {
LOGGER.finest("No triggers found");
} else {
workflowMultiBranchProject.getTriggers().forEach(((triggerDescriptor, trigger) -> {
if (trigger instanceof BitBucketMultibranchTrigger) {
LOGGER.finest("Found BitBucketMultibranchTrigger type");
bitBucketMultibranchTrigger.set((BitBucketMultibranchTrigger) trigger);
}
}));
}
if (bitBucketMultibranchTrigger.get() == null) {
scmSourceOwner.onSCMSourceUpdated(scmSource);
} else {
if (workflowMultiBranchProject.isBuildable()) {
bitBucketMultibranchTrigger.get().setPayload(payload);
BitBucketPushCause bitBucketPushCause = new BitBucketPushCause(user);
workflowMultiBranchProject.scheduleBuild2(0, new CauseAction(bitBucketPushCause));
} else {
LOGGER.finest("workflowMultiBranchProject is not builtable");
}
}
} else {
scmSourceOwner.onSCMSourceUpdated(scmSource);
}
} else if (scmSourceOwner instanceof WorkflowMultiBranchProject) {
LOGGER.finest("scmSourceOwner [" + scmSourceOwner.getName() + "] is of type WorkflowMultiBranchProject");
WorkflowMultiBranchProject workflowMultiBranchProject = (WorkflowMultiBranchProject) scmSourceOwner;
if (workflowMultiBranchProject.getTriggers().isEmpty()) {
LOGGER.finest("No triggers found");
} else {
workflowMultiBranchProject.getTriggers().forEach(((triggerDescriptor, trigger) -> {
if (trigger instanceof BitBucketMultibranchTrigger) {
LOGGER.finest("Found BitBucketMultibranchTrigger type");
BitBucketMultibranchTrigger bitBucketMultibranchTrigger = (BitBucketMultibranchTrigger) trigger;
if (bitBucketMultibranchTrigger.getOverrideUrl() == null || bitBucketMultibranchTrigger.getOverrideUrl().isEmpty()) {
LOGGER.finest("Ignoring empty overrideUrl");
} else {
LOGGER.fine("Found override URL [" + bitBucketMultibranchTrigger.getOverrideUrl() + "]");
LOGGER.log(Level.FINE, "Trying to match {0} ", remote + "<-->" + bitBucketMultibranchTrigger.getOverrideUrl());
if (bitBucketMultibranchTrigger.getOverrideUrl().equalsIgnoreCase(remote.toString())) {
LOGGER.info(String.format("Triggering BitBucket scmSourceOwner [%s] by overrideUrl [%s]",scmSourceOwner.getName(), bitBucketMultibranchTrigger.getOverrideUrl()));
scmSourceOwner.onSCMSourceUpdated(scmSource);
}
}
} else {
LOGGER.finest("Found BitBucketMultibranchTrigger type");
}
}));
}
} else {
LOGGER.log(Level.FINE, String.format("SCM [%s] doesn't match remote repo [%s]", scmSourceOwner.getFullDisplayName(), remote));
}
}
}
} catch (URISyntaxException e) {
LOGGER.log(Level.WARNING, "Invalid repository URL {0}", url);
} finally {
SecurityContextHolder.setContext(old);
}
} else {
throw new UnsupportedOperationException("Unsupported SCM type " + scm);
}
}
private boolean hasBeenTriggered(List<SCM> scmTriggered, SCM scmTrigger) {
for (SCM scm : scmTriggered) {
if (scm.equals(scmTrigger)) {
return true;
}
}
return false;
}
private boolean match(SCM scm, URIish url, String overrideUrl) {
if (scm instanceof GitSCM) {
LOGGER.log(Level.FINE, "SCM is instance of GitSCM");
for (RemoteConfig remoteConfig : ((GitSCM) scm).getRepositories()) {
for (URIish urIish : remoteConfig.getURIs()) {
// needed cause the ssh and https URI differs in Bitbucket Server.
if (urIish.getPath().startsWith("/scm")) {
urIish = urIish.setPath(urIish.getPath().substring(4));
}
// needed because bitbucket self hosted does not transfer any host information
if (url.getHost() == null || url.getHost().isEmpty()) {
urIish = urIish.setHost(url.getHost());
}
LOGGER.log(Level.FINE, "Trying to match {0} ", urIish.toString() + "<-->" + url);
if (GitStatus.looselyMatches(urIish, url)) {
return true;
} else if (overrideUrl != null && !overrideUrl.isEmpty()) {
LOGGER.log(Level.FINE, "Trying to match using override Repository URL {0} ", overrideUrl + "<-->" + url);
return overrideUrl.contentEquals(url.toString());
}
}
}
} else if (scm instanceof MercurialSCM) {
LOGGER.log(Level.FINEST, "SCM is instance of MercurialSCM");
try {
URI hgUri = new URI(((MercurialSCM) scm).getSource());
String remote = url.toString();
if (looselyMatches(hgUri, remote)) {
return true;
}
} catch (URISyntaxException ex) {
LOGGER.log(Level.SEVERE, "Could not parse jobSource uri: {0} ", ex);
}
} else {
LOGGER.log(Level.FINEST, "SCM is instance of [" + scm.getClass().getSimpleName() + "] which is not supported");
}
return false;
}
private boolean match(SCMSource scm, URIish url) {
if (scm instanceof GitSCMSource || (isBranchPluginAvailable && scm instanceof BitbucketSCMSource)) {
String gitRemote;
if (scm instanceof GitSCMSource) {
LOGGER.log(Level.FINEST, "SCMSource is GitSCMSource");
gitRemote = ((GitSCMSource) scm).getRemote();
} else if (isBranchPluginAvailable) {
LOGGER.log(Level.FINEST, "SCMSource is BitbucketSCMSource");
gitRemote = ((BitbucketSCMSource) scm).getServerUrl() + "/" +
((BitbucketSCMSource) scm).getRepoOwner() + "/" +
((BitbucketSCMSource) scm).getRepository();
} else {
return false;
}
URIish urIish;
LOGGER.log(Level.FINEST, "SCMSource remote is " + gitRemote);
try {
urIish = new URIish(gitRemote);
} catch (URISyntaxException e) {
LOGGER.log(Level.SEVERE, "Could not parse gitRemote: " + gitRemote, e);
return false;
}
// needed cause the ssh and https URI differs in Bitbucket Server.
if (urIish.getPath().startsWith("/scm")) {
urIish = urIish.setPath(urIish.getPath().substring(4));
}
// needed because bitbucket self hosted does not transfer any host information
if (url.getHost() == null || url.getHost().isEmpty()) {
urIish = urIish.setHost(url.getHost());
}
LOGGER.log(Level.FINE, "Trying to match {0} ", urIish.toString() + "<-->" + url);
return GitStatus.looselyMatches(urIish, url);
} else if (scm instanceof MercurialSCMSource) {
LOGGER.log(Level.FINEST, "SCMSource is MercurialSCMSource");
try {
URI hgUri = new URI(((MercurialSCMSource) scm).getSource());
String remote = url.toString();
if (looselyMatches(hgUri, remote)) {
return true;
}
} catch (URISyntaxException ex) {
LOGGER.log(Level.SEVERE, "Could not parse jobSource uri: {0} ", ex);
}
} else {
LOGGER.log(Level.FINEST, "SCMSource is [" + scm.getClass().getSimpleName() + "] which is not supported");
}
return false;
}
private boolean looselyMatches(URI notifyUri, String repository) {
boolean result = false;
try {
URI repositoryUri = new URI(repository);
result = Objects.equal(notifyUri.getHost(), repositoryUri.getHost())
&& Objects.equal(notifyUri.getPath(), repositoryUri.getPath())
&& Objects.equal(notifyUri.getQuery(), repositoryUri.getQuery());
} catch (URISyntaxException ex) {
LOGGER.log(Level.SEVERE, "Could not parse repository uri: {0}, {1}", new Object[]{repository, ex});
}
return result;
}
private static final Logger LOGGER = Logger.getLogger(BitbucketJobProbe.class.getName());
}