}
dependency 'org.elasticsearch:mocksocket:1.0'
dependency 'org.codelibs.elasticsearch.module:analysis-common:6.8.4'
+ dependency 'org.eclipse.jgit:org.eclipse.jgit:5.6.1.202002131546-r'
dependency 'org.freemarker:freemarker:2.3.20'
dependency 'org.hamcrest:hamcrest-all:1.3'
dependency 'org.jsoup:jsoup:1.12.1'
compile 'org.codehaus.staxmate:staxmate'
compile 'org.codehaus.woodstox:stax2-api'
compile 'org.codehaus.woodstox:woodstox-core-lgpl'
+ compile 'org.eclipse.jgit:org.eclipse.jgit'
compile 'org.picocontainer:picocontainer'
compile 'org.slf4j:jcl-over-slf4j'
compile 'org.slf4j:jul-to-slf4j'
*/
package org.sonar.scanner.ci.vendors;
+import java.nio.file.Path;
import org.apache.commons.lang.StringUtils;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.RepositoryBuilder;
+import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.utils.System2;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.scanner.ci.CiConfiguration;
import org.sonar.scanner.ci.CiConfigurationImpl;
import org.sonar.scanner.ci.CiVendor;
import static org.apache.commons.lang.StringUtils.isNotBlank;
public class Jenkins implements CiVendor {
+ private final static Logger log = Loggers.get(Jenkins.class);
private final System2 system;
+ private final DefaultInputProject inputProject;
- public Jenkins(System2 system) {
+ public Jenkins(System2 system, DefaultInputProject inputProject) {
this.system = system;
+ this.inputProject = inputProject;
}
@Override
// https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin#GitHubpullrequestbuilderplugin-EnvironmentVariables
// https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project
String revision = system.envVariable("ghprbActualCommit");
- if (StringUtils.isBlank(revision)) {
- revision = system.envVariable("GIT_COMMIT");
- if (StringUtils.isBlank(revision)) {
- revision = system.envVariable("SVN_COMMIT");
+ if (StringUtils.isNotBlank(revision)) {
+ return new CiConfigurationImpl(revision);
+ }
+
+ revision = system.envVariable("GIT_COMMIT");
+
+ if (StringUtils.isNotBlank(revision)) {
+ if (StringUtils.isNotBlank(system.envVariable("CHANGE_ID"))) {
+ String jenkinsGitPrSha1 = getJenkinsGitPrSha1();
+ if (StringUtils.isNotBlank(jenkinsGitPrSha1)) {
+ return new CiConfigurationImpl(jenkinsGitPrSha1);
+ }
}
+ return new CiConfigurationImpl(revision);
}
+
+ revision = system.envVariable("SVN_COMMIT");
return new CiConfigurationImpl(revision);
}
+
+ private String getJenkinsGitPrSha1() {
+ String gitBranch = system.envVariable("GIT_BRANCH");
+ if (StringUtils.isBlank(gitBranch)) {
+ return null;
+ }
+
+ Path baseDir = inputProject.getBaseDir();
+
+ RepositoryBuilder builder = new RepositoryBuilder()
+ .findGitDir(baseDir.toFile())
+ .setMustExist(true);
+
+ if (builder.getGitDir() == null) {
+ return null;
+ }
+
+ String refName = "refs/remotes/origin/" + gitBranch;
+ try (Repository repo = builder.build()) {
+ Ref ref = repo.exactRef(refName);
+ if (ref != null) {
+ return ref.getObjectId().getName();
+ }
+ } catch (Exception e) {
+ log.debug("Couldn't find git sha1 in '{}': {}", refName, e.getMessage());
+ }
+ return null;
+ }
+
}
*/
package org.sonar.scanner.ci.vendors;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
import javax.annotation.Nullable;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.utils.System2;
+import org.sonar.api.utils.ZipUtils;
import org.sonar.scanner.ci.CiVendor;
import static org.assertj.core.api.Assertions.assertThat;
public class JenkinsTest {
private System2 system = mock(System2.class);
- private CiVendor underTest = new Jenkins(system);
+ private DefaultInputProject project = mock(DefaultInputProject.class);
+ private CiVendor underTest = new Jenkins(system, project);
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
@Test
public void getName() {
@Test
public void loadConfiguration_with_deprecated_pull_request_plugin() {
- setEnvVariable("CI", "true");
setEnvVariable("ghprbActualCommit", "abd12fc");
assertThat(underTest.loadConfiguration().getScmRevision()).hasValue("abd12fc");
@Test
public void loadConfiguration_of_git_repo() {
- setEnvVariable("CI", "true");
setEnvVariable("GIT_COMMIT", "abd12fc");
assertThat(underTest.loadConfiguration().getScmRevision()).hasValue("abd12fc");
}
+ @Test
+ public void loadConfiguration_of_git_repo_with_branch_plugin() throws IOException {
+ // prepare fake git clone
+ Path baseDir = temp.newFolder().toPath();
+ File unzip = ZipUtils.unzip(this.getClass().getResourceAsStream("gitrepo.zip"), baseDir.toFile());
+ when(project.getBaseDir()).thenReturn(unzip.toPath().resolve("gitrepo"));
+
+ setEnvVariable("CHANGE_ID", "3");
+ setEnvVariable("GIT_BRANCH", "PR-3");
+ // this will be ignored
+ setEnvVariable("GIT_COMMIT", "abd12fc");
+
+ assertThat(underTest.loadConfiguration().getScmRevision()).hasValue("e6013986eff4f0ce0a85f5d070070e7fdabead48");
+ }
+
+ @Test
+ public void loadConfiguration_of_git_repo_with_branch_plugin_without_git_repo() throws IOException {
+ // prepare fake git clone
+ Path baseDir = temp.newFolder().toPath();
+ when(project.getBaseDir()).thenReturn(baseDir);
+
+ setEnvVariable("CHANGE_ID", "3");
+ setEnvVariable("GIT_BRANCH", "PR-3");
+ setEnvVariable("GIT_COMMIT", "abc");
+
+ assertThat(underTest.loadConfiguration().getScmRevision()).hasValue("abc");
+ }
+
@Test
public void loadConfiguration_of_svn_repo() {
- setEnvVariable("CI", "true");
setEnvVariable("SVN_COMMIT", "abd12fc");
assertThat(underTest.loadConfiguration().getScmRevision()).hasValue("abd12fc");