Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Branch: Scan multibranch pipeline log #68

Merged
merged 3 commits into from
Aug 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/main/java/com/cloudbees/jenkins/plugins/BitbucketJobProbe.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;

Expand All @@ -18,6 +19,9 @@
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.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
Expand Down Expand Up @@ -73,6 +77,18 @@ public void triggerMatchingJobs(String user, String url, String scm, String payl
} else
LOGGER.log(Level.FINE, "{0} hasn't BitBucketTrigger set", job.getName());
}
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);
List<SCMSource> scmSources = scmSourceOwner.getSCMSources();
for (SCMSource scmSource : scmSources) {
LOGGER.log(Level.FINER, "Considering candidate scmSource {0}", scmSource);
if (match(scmSource, remote)) {
LOGGER.log(Level.INFO, "Triggering BitBucket scmSourceOwner {0}", scmSourceOwner);
scmSourceOwner.onSCMSourceUpdated(scmSource);
} else LOGGER.log(Level.FINE, "{0} SCM doesn't match remote repo {1}", new Object[]{scmSourceOwner.getName(), remote});
}
}
} catch (URISyntaxException e) {
LOGGER.log(Level.WARNING, "Invalid repository URL {0}", url);
} finally {
Expand Down Expand Up @@ -130,6 +146,44 @@ private boolean match(SCM scm, URIish url, String overrideUrl) {
return false;
}

private boolean match(SCMSource scm, URIish url) {
if (scm instanceof GitSCMSource) {
String gitRemote = ((GitSCMSource) scm).getRemote();
URIish urIish;
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 (StringUtils.isEmpty(url.getHost())) {
urIish = urIish.setHost(url.getHost());
}

LOGGER.log(Level.FINE, "Trying to match {0} ", urIish.toString() + "<-->" + url.toString());
if (GitStatus.looselyMatches(urIish, url)) {
return true;
}
} else if (scm instanceof 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);
}
}
return false;
}

private boolean looselyMatches(URI notifyUri, String repository) {
boolean result = false;
try {
Expand Down