Skip to content

[JENKINS-56063] (Expansion of variables in refspec in case of honor refspec on initial clone) #830

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

Closed
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import hudson.plugins.git.util.GitUtils;
import hudson.slaves.NodeProperty;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.eclipse.jgit.transport.RefSpec;
Expand Down Expand Up @@ -148,7 +149,12 @@ public void decorateCloneCommand(GitSCM scm, Run<?, ?> build, GitClient git, Tas
// in a single job definition.
RemoteConfig rc = scm.getRepositories().get(0);
List<RefSpec> refspecs = rc.getFetchRefSpecs();
cmd.refspecs(refspecs);
List<RefSpec> expandedRefSpecs = new ArrayList<>();
EnvVars env = build.getEnvironment(listener);
for (RefSpec ref : refspecs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs automated tests to confirm:

  • Operating system environment variables (like PATH) are expanded as expected (PATH will generate a nonsense refspec, but it has the benefit of being defined on all supported platforms)
  • Jenkins variables (like GIT_BRANCH) are expanded as expected in a Freestyle project
  • Jenkins token macros (like GIT_REVISION) are expanded as expected in a Freestyle project

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a test file which is running successful tests for "BUILD_ID", "BUILD_NO" and "GIT_COMMIT".
I have two issues here:

  • I can't make a freestyle build with ${PATH} added in the refspec, it fails the build with a "can't file the commitFile in the working directory". Although I have tested this interactively where you can clearly see that the refspec is expanded but the build fails, I still would want your opinion on this.

Screenshot 2020-02-07 at 9 19 29 PM

  • By Jenkins token macros do you mean running a build with parameters? Is that how tokens are used? If yes then how does one set a parameter for a job? Should I put the variable in the EnvVars store?

Copy link
Contributor Author

@rishabhBudhouliya rishabhBudhouliya Feb 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen the Token Macro API but I don't think that serves our purpose here
assertThat(TokenMacro.expandAll(build2, listener, "${GIT_REVISION}"), is(sha1String));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After doing some RCA I found out that adding ${PATH} to refspec would make it illegal, hence the job will fail before doing anything else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't make a freestyle build with ${PATH} added in the refspec, it fails the build with a "can't file the commitFile in the working directory"

How did you tried? Just adding ${PATH} to permuteRefSpecVariable?

I'd also like to know how this PR behaves with job parameters (https://wiki.jenkins.io/display/JENKINS/Define+Parameters)

expandedRefSpecs.add(new RefSpec(env.expand(ref.toString())));
}
cmd.refspecs(expandedRefSpecs);
}
cmd.timeout(timeout);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package hudson.plugins.git.extensions.impl;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.plugins.git.AbstractGitTestCase;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.UserRemoteConfig;
import org.eclipse.jgit.transport.RefSpec;
import org.jenkinsci.plugins.gitclient.CloneCommand;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.mockito.Mockito.*;

@RunWith(Parameterized.class)
public class CloneOptionHonorRefSpecTest extends AbstractGitTestCase {

private GitSCM gitSCM;
private FreeStyleBuild build;
private String refSpecVar;
private TaskListener listener;

public CloneOptionHonorRefSpecTest(String useRefSpecVariable) {
this.refSpecVar = useRefSpecVariable;
}

@Parameterized.Parameters(name = "{0}")
public static Collection permuteRefSpecVariable() {
List<Object[]> values = new ArrayList<>();
String[] allowed = {"${BUILD_NUMBER}","${BUILD_ID}",
"${GIT_COMMIT}"};
for (String refSpecValue : allowed) {
Object[] combination = {refSpecValue};
values.add(combination);
}
return values;
}

@Before
public void setupProjectAndDependencies() throws Exception{
listener = mock(TaskListener.class);
// create initial commit
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit in master");

List<UserRemoteConfig> repos = new ArrayList<>();
repos.add(new UserRemoteConfig(testRepo.gitDir.getAbsolutePath(), "origin", "+refs/heads/master:refs/remotes/origin/master" + refSpecVar, null));

/* Creating a project */
FreeStyleProject project = setupProject(repos, Collections.singletonList(new BranchSpec("master" + refSpecVar)), null, false, null);
build = build(project, Result.SUCCESS, commitFile1);
gitSCM = (GitSCM) project.getScm();
}

@Test
public void decorateCloneCommandWithHonorRefSpec() throws Exception {
List<RefSpec> refSpecs = new ArrayList<>();
String envKey = refSpecVar.substring(refSpecVar.indexOf("{")+1, refSpecVar.indexOf("}"));
String refSpecVariable = build.getEnvironment(listener).get(envKey);
refSpecs.add(new RefSpec("+refs/heads/master:refs/remotes/origin/master" + refSpecVariable));

PrintStream logger = mock(PrintStream.class);
when(listener.getLogger()).thenReturn(logger);

CloneCommand cloneCommand = mock(CloneCommand.class);

/* Creating a clone option which would honor refspec for initial clone */
CloneOption cloneOption = new CloneOption(false, false, null, null);
cloneOption.setHonorRefspec(true);
cloneOption.decorateCloneCommand(gitSCM, build, git, listener, cloneCommand);

verify(cloneCommand).refspecs(refSpecs);
}
}