-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[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
rishabhBudhouliya
wants to merge
5
commits into
jenkinsci:master
from
rishabhBudhouliya:JENKINS-56063-Fix
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2510bd3
=[JENKINS-56063] added expansion of env variables in refspec in case …
rishabhBudhouliya 7d7f670
=removed the check flag and changed the implementation
rishabhBudhouliya 58320c4
Changed minor indentation issues
rishabhBudhouliya 19fa1c4
=Added testfile for CloneOption fix
rishabhBudhouliya 145e17e
=Change in env variable
rishabhBudhouliya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/test/java/hudson/plugins/git/extensions/impl/CloneOptionHonorRefSpecTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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));
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did you tried? Just adding
${PATH}
topermuteRefSpecVariable
?I'd also like to know how this PR behaves with job parameters (https://wiki.jenkins.io/display/JENKINS/Define+Parameters)