Skip to content

Commit

Permalink
reset to master and pick: #68 Scan Multibranch Pipeline Log
Browse files Browse the repository at this point in the history
  • Loading branch information
ca-stefan-cordes committed Aug 11, 2020
1 parent b653831 commit 7bbe84b
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 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,20 +4,27 @@
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.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
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.SCMHead;
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 +80,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 +149,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

0 comments on commit 7bbe84b

Please sign in to comment.