Skip to content

Commit

Permalink
Merge pull request #1061 from andham/JENKINS-65123
Browse files Browse the repository at this point in the history
[JENKINS-65123] Always sets GIT_URL
  • Loading branch information
MarkEWaite committed Apr 1, 2021
2 parents cdb2f75 + 31f3d73 commit b90a2a0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public class GitSCM extends GitSCMBackwardCompatibility {
public static final String GIT_COMMIT = "GIT_COMMIT";
public static final String GIT_PREVIOUS_COMMIT = "GIT_PREVIOUS_COMMIT";
public static final String GIT_PREVIOUS_SUCCESSFUL_COMMIT = "GIT_PREVIOUS_SUCCESSFUL_COMMIT";
public static final String GIT_URL = "GIT_URL";

/**
* All the configured extensions attached to this.
Expand Down Expand Up @@ -1537,12 +1538,13 @@ public void buildEnvironment(Run<?, ?> build, java.util.Map<String, String> env)
repoCount++;
}

if (userRemoteConfigs.size()==1){
env.put("GIT_URL", userRemoteConfigs.get(0).getUrl());
} else {
if (userRemoteConfigs.size()>0) {
env.put(GIT_URL, userRemoteConfigs.get(0).getUrl());
}
if (userRemoteConfigs.size()>1) {
int count=1;
for(UserRemoteConfig config:userRemoteConfigs) {
env.put("GIT_URL_"+count, config.getUrl());
for (UserRemoteConfig config:userRemoteConfigs) {
env.put(GIT_URL+"_"+count, config.getUrl());
count++;
}
}
Expand Down
85 changes: 85 additions & 0 deletions src/test/java/hudson/plugins/git/GitSCMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import org.junit.After;
Expand Down Expand Up @@ -3109,6 +3110,90 @@ public void testBuildEnvVarsLocalBranchNotSet() throws Exception {
verify(build, times(1)).getActions(BuildData.class);
}

@Test
public void testBuildEnvironmentVariablesSingleRemote() throws Exception {
ObjectId sha1 = ObjectId.fromString("2cec153f34767f7638378735dc2b907ed251a67d");

List<Branch> branchList = new ArrayList<>();
Branch branch = new Branch("origin/master", sha1);
branchList.add(branch);

Revision revision = new Revision(sha1, branchList);

/* BuildData mock that will use the Revision */
BuildData buildData = Mockito.mock(BuildData.class);
Mockito.when(buildData.getLastBuiltRevision()).thenReturn(revision);
Mockito.when(buildData.hasBeenReferenced(anyString())).thenReturn(true);

/* List of build data that will be returned by the mocked BuildData */
List<BuildData> buildDataList = new ArrayList<>();
buildDataList.add(buildData);

/* Run mock which returns the buildDataList */
Run<?, ?> build = Mockito.mock(Run.class);
Mockito.when(build.getActions(BuildData.class)).thenReturn(buildDataList);

FreeStyleProject project = setupSimpleProject("*/*");
GitSCM scm = (GitSCM) project.getScm();

Map<String, String> env = new HashMap<String, String>();
scm.buildEnvironment(build, env);

assertEquals("GIT_BRANCH is invalid", "origin/master", env.get("GIT_BRANCH"));
assertEquals("GIT_LOCAL_BRANCH is invalid", null, env.get("GIT_LOCAL_BRANCH"));
assertEquals("GIT_COMMIT is invalid", sha1.getName(), env.get("GIT_COMMIT"));
assertEquals("GIT_URL is invalid", testRepo.gitDir.getAbsolutePath(), env.get("GIT_URL"));
assertNull("GIT_URL_1 should not have been set", env.get("GIT_URL_1"));
}

@Test
public void testBuildEnvironmentVariablesMultipleRemotes() throws Exception {
ObjectId sha1 = ObjectId.fromString("2cec153f34767f7638378735dc2b907ed251a67d");

List<Branch> branchList = new ArrayList<>();
Branch branch = new Branch("origin/master", sha1);
branchList.add(branch);

Revision revision = new Revision(sha1, branchList);

/* BuildData mock that will use the Revision */
BuildData buildData = Mockito.mock(BuildData.class);
Mockito.when(buildData.getLastBuiltRevision()).thenReturn(revision);
Mockito.when(buildData.hasBeenReferenced(anyString())).thenReturn(true);

/* List of build data that will be returned by the mocked BuildData */
List<BuildData> buildDataList = new ArrayList<>();
buildDataList.add(buildData);

/* Run mock which returns the buildDataList */
Run<?, ?> build = Mockito.mock(Run.class);
Mockito.when(build.getActions(BuildData.class)).thenReturn(buildDataList);

FreeStyleProject project = setupSimpleProject("*/*");
/* Update project so we have two remote configs */
List<UserRemoteConfig> userRemoteConfigs = new ArrayList<>();
userRemoteConfigs.add(new UserRemoteConfig(testRepo.gitDir.getAbsolutePath(), "origin", "", null));
final String upstreamRepoUrl = "/upstream/url";
userRemoteConfigs.add(new UserRemoteConfig(upstreamRepoUrl, "upstream", "", null));
GitSCM scm = new GitSCM(
userRemoteConfigs,
Collections.singletonList(new BranchSpec(branch.getName())),
null, null,
Collections.<GitSCMExtension>emptyList());
project.setScm(scm);

Map<String, String> env = new HashMap<String, String>();
scm.buildEnvironment(build, env);

assertEquals("GIT_BRANCH is invalid", "origin/master", env.get("GIT_BRANCH"));
assertEquals("GIT_LOCAL_BRANCH is invalid", null, env.get("GIT_LOCAL_BRANCH"));
assertEquals("GIT_COMMIT is invalid", sha1.getName(), env.get("GIT_COMMIT"));
assertEquals("GIT_URL is invalid", testRepo.gitDir.getAbsolutePath(), env.get("GIT_URL"));
assertEquals("GIT_URL_1 is invalid", testRepo.gitDir.getAbsolutePath(), env.get("GIT_URL_1"));
assertEquals("GIT_URL_2 is invalid", upstreamRepoUrl, env.get("GIT_URL_2"));
assertNull("GIT_URL_3 should not have been set", env.get("GIT_URL_3"));
}

@Issue("JENKINS-38241")
@Test
public void testCommitMessageIsPrintedToLogs() throws Exception {
Expand Down

0 comments on commit b90a2a0

Please sign in to comment.