Skip to content

Commit

Permalink
[JENKINS-32548] Added support for mercurial repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
dbuedo committed Jan 31, 2016
1 parent ca4a9ed commit f1adc84
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -60,6 +60,11 @@
<artifactId>git</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mercurial</artifactId>
<version>1.54</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Expand Up @@ -10,6 +10,7 @@
import hudson.model.Result;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.util.BuildData;
import hudson.plugins.mercurial.MercurialSCM;
import hudson.scm.SCM;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
Expand Down Expand Up @@ -150,23 +151,21 @@ private String guessBitbucketBuildState(final Result result) {
}

private BitbucketBuildStatusResource createBuildStatusResourceFromBuild(final AbstractBuild build) throws Exception {

SCM scm = build.getProject().getScm();
if (scm == null) {
throw new Exception("Bitbucket build notifier only works with SCM");
}

if (!(scm instanceof GitSCM)) {
throw new Exception("Bitbucket build notifier requires a git repo as SCM");
}

GitSCM gitSCM = (GitSCM) scm;
List<RemoteConfig> repoList = gitSCM.getRepositories();
if (repoList.size() != 1) {
throw new Exception("None or multiple repos");
ScmAdapter scmAdapter;
if (scm instanceof GitSCM) {
scmAdapter = new GitScmAdapter(build);
} else if (scm instanceof MercurialSCM) {
scmAdapter = new MercurialScmAdapter((MercurialSCM) scm);
} else {
throw new Exception("Bitbucket build notifier requires a git repo or a mercurial repo as SCM");
}

URIish urIish = repoList.get(0).getURIs().get(0);
URIish urIish = scmAdapter.getRepositoryUri();
if (!urIish.getHost().equals("bitbucket.org")) {
throw new Exception("Bitbucket build notifier support only repositories hosted in bitbucket.org");
}
Expand All @@ -189,13 +188,7 @@ private BitbucketBuildStatusResource createBuildStatusResourceFromBuild(final Ab
throw new Exception("Bitbucket build notifier could not extract the user name from the repository URL");
}

// find current revision
BuildData buildData = build.getAction(BuildData.class);
if(buildData == null || buildData.getLastBuiltRevision() == null) {
throw new Exception("Revision could not be found");
}

String commitId = buildData.getLastBuiltRevision().getSha1String();
String commitId = scmAdapter.findCurrentCommitId();
if (commitId == null) {
throw new Exception("Commit ID could not be found!");
}
Expand Down Expand Up @@ -237,6 +230,62 @@ private BitbucketBuildStatus createBitbucketBuildStatusFromBuild(AbstractBuild b
return new BitbucketBuildStatus(buildState, buildKey, buildUrl, buildName);
}

private interface ScmAdapter {
URIish getRepositoryUri() throws Exception;
String findCurrentCommitId() throws Exception;
}

private class GitScmAdapter implements ScmAdapter {

private final GitSCM gitSCM;
private final AbstractBuild build;

public GitScmAdapter(AbstractBuild build) {
this.gitSCM = (GitSCM) build.getProject().getScm();
this.build = build;
}

public URIish getRepositoryUri() throws Exception {
List<RemoteConfig> repoList = gitSCM.getRepositories();
if (repoList.size() != 1) {
throw new Exception("None or multiple repos");
}

return repoList.get(0).getURIs().get(0);
}

public String findCurrentCommitId() throws Exception {
BuildData buildData = build.getAction(BuildData.class);
if(buildData == null || buildData.getLastBuiltRevision() == null) {
throw new Exception("Revision could not be found");
}

return buildData.getLastBuiltRevision().getSha1String();
}
}

private class MercurialScmAdapter implements ScmAdapter {

private final MercurialSCM hgSCM;

public MercurialScmAdapter(MercurialSCM scm) {
hgSCM = scm;
}

public URIish getRepositoryUri() throws Exception {
String source = hgSCM.getSource();
if (source == null || source.isEmpty()) {
throw new Exception("None or multiple repos");
}

return new URIish(source);
}

public String findCurrentCommitId() throws Exception {
return hgSCM.getRevision();
}
}

@Extension // This indicates to Jenkins that this is an implementation of an extension point.
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {

Expand Down

0 comments on commit f1adc84

Please sign in to comment.