Skip to content
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

[JENKINS-70303] Remove leading and trailing spaces from refspec #1096

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

Jagrutiti
Copy link

@Jagrutiti Jagrutiti commented Jan 14, 2024

JENKINS-70303 - Remove leading and trailing spaces from refspec

If a refspec is included in the checkout scm definition of a multibranch Pipeline using JGit for checkout and the refspec has a leading space character, the checkout fails with the JGit message

Remote does not have  available for fetch.

The same refspec with leading spaces does not affect command line git checkout. It works in either case with command line git. Leading space handling by command line git is preferred rather than the way that the git client plugin honors the leading spaces and passes them to JGit.

Command line git does not see the issue because extra spaces between arguments of a command line program are generally not an issue. Lucky circumstances that allow the CLI git checkout to be better behaved than the JGit checkout.

Checklist

  • I have read the CONTRIBUTING doc
  • I have referenced the Jira issue related to my changes in one or more commit messages
  • I have added tests that verify my changes
  • Unit tests pass locally with my changes
  • I have added documentation as necessary
  • No Javadoc warnings were introduced with my changes
  • No spotbugs warnings were introduced with my changes
  • I have interactively tested my changes

Types of changes

What types of changes does your code introduce?

  • Bug fix (non-breaking change which fixes an issue)

@Jagrutiti Jagrutiti requested a review from a team as a code owner January 14, 2024 12:17
@Jagrutiti
Copy link
Author

I have tested this PR using mvn clean install. All the test cases in CliGitAPIImplTest.java pass.

How shall I write a test case of my own? I mean how do I test refspecs?

@@ -861,7 +870,8 @@ public void execute() throws GitException, InterruptedException {
}

if (refspecs == null) {
refspecs = Collections.singletonList(new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*"));
refspecs = Collections.singletonList(
new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*".trim()));
Copy link
Author

Choose a reason for hiding this comment

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

As this hard-coded we can remove trim(). I was not sure if I should.

Copy link
Contributor

Choose a reason for hiding this comment

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

The existing trim should be removed. It won't change the RefSpec definition in any way because "/*".trim() is the same as "/*"

@github-actions github-actions bot added the tests Automated test addition or improvement label Jan 14, 2024
Copy link
Contributor

@MarkEWaite MarkEWaite left a comment

Choose a reason for hiding this comment

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

Thanks very much for the pull request. The two cases seem reasonable at first glance. The automated tests that you'll create should help confirm that they are sufficient to resolve the issue.

Have you tested this interactively based on the description in the issue report?

I think that you can add an automated test in the GitClientCloneTest. It already has the test_clone_refspecs() test that defines a new RefSpec. You can modify that test to sometimes include leading or trailing spaces on the RefSpec or you can copy that test and test for spaces in the RefSpec in the copied test.

I've provided a sample of the way I would write the test and pushed it to your branch as 2b04ed4 . The test is currently failing for JGit. I think that means there are more places that will need to be changed.

I also added the Issue annotation to the modified test with a comment explaining the change.

I found that location by using git grep RefSpec -- src/test to search for existing uses of RefSpec in the tests. That test seemed like the easiest one to extend for this case. You should do the same search and confirm that my choice was reasonable. There may be other tests that assert RefSpec contents that could be extended in a similar way.

@Jagrutiti
Copy link
Author

Thanks very much for the pull request. The two cases seem reasonable at first glance. The automated tests that you'll create should help confirm that they are sufficient to resolve the issue.

Have you tested this interactively based on the description in the issue report?

I think that you can add an automated test in the GitClientCloneTest. It already has the test_clone_refspecs() test that defines a new RefSpec. You can modify that test to sometimes include leading or trailing spaces on the RefSpec or you can copy that test and test for spaces in the RefSpec in the copied test.

I've provided a sample of the way I would write the test and pushed it to your branch as 2b04ed4 . The test is currently failing for JGit. I think that means there are more places that will need to be changed.

I also added the Issue annotation to the modified test with a comment explaining the change.

I found that location by using git grep RefSpec -- src/test to search for existing uses of RefSpec in the tests. That test seemed like the easiest one to extend for this case. You should do the same search and confirm that my choice was reasonable. There may be other tests that assert RefSpec contents that could be extended in a similar way.

I will work on this. Thanks.

@MarkEWaite MarkEWaite changed the title Removed leading and trailing spaces from refspec [JENKINS-70303] Removed leading and trailing spaces from refspec Jan 14, 2024
@MarkEWaite MarkEWaite changed the title [JENKINS-70303] Removed leading and trailing spaces from refspec [JENKINS-70303] Remove leading and trailing spaces from refspec Jan 14, 2024
Co-authored-by: Mark Waite <mark.earl.waite@gmail.com>
@MarkEWaite
Copy link
Contributor

I've included the issue description in the pull request description because it helps reviewers to have the issue described clearly in the pull request, without requiring that they open the bug report. That's my personal preference rather than a standard across the entire Jenkins organization, but since I'm one of the maintainers of this plugin, I applied my preference.

@Jagrutiti
Copy link
Author

I've included the issue description in the pull request description because it helps reviewers to have the issue described clearly in the pull request, without requiring that they open the bug report. That's my personal preference rather than a standard across the entire Jenkins organization, but since I'm one of the maintainers of this plugin, I applied my preference.

I noticed. I had added the description in the PR title itself.

@Jagrutiti
Copy link
Author

Jagrutiti commented Jan 14, 2024

I was not able to fix the test case for jgit and jgitapache. I know the test executes this part of the code.

if (refspecs == null) {
refspecs =
Collections.singletonList(new RefSpec("+refs/heads/*:refs/remotes/" + remote + "/*"));
}
FetchCommand fetch = new Git(repository)
.fetch()
.setProgressMonitor(new JGitProgressMonitor(listener))
.setRemote(url.trim())
.setCredentialsProvider(getProvider())
.setTagOpt(tags ? TagOpt.FETCH_TAGS : TagOpt.NO_TAGS)
.setRefSpecs(refspecs);
setTransportTimeout(fetch, "fetch", timeout);
if (shallow) {
if (depth == null) {
depth = 1;
}
fetch.setDepth(depth);
}
fetch.call();

@MarkEWaite
Copy link
Contributor

I was not able to fix the test case for jgit and jgitapache. I know the test executes this part of the code.

At the location where you're looking, the RefSpec object already includes the leading space. It seems like it would be better to look at the locations where RefSpec objects are constructed. Trim the spaces before passing the string to the RefSpec constructor.

I'm making a guess by that idea, but I think it is a guess that is worth exploring.

Comment on lines +340 to +341
new RefSpec(randomSpace() + "+refs/heads/master:refs/remotes/origin/master" + randomSpace()),
new RefSpec(randomSpace() + "+refs/heads/1.4.x:refs/remotes/origin/1.4.x" + randomSpace()));
Copy link
Contributor

Choose a reason for hiding this comment

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

This may be exactly the wrong place for this type of test. We generally prefer tests that are nearer to what a user would do. This is changing an argument to the RefSpec constructor when the real end user provides a string into a data entry field on a Jenkins form or they provide a string to an option of a Pipeline checkout step.

Copy link
Author

Choose a reason for hiding this comment

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

The test case did pass in my local.

Copy link
Author

Choose a reason for hiding this comment

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

Which test case tests the Pipeline checkout step?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that we'll be able to test Pipeline checkout from this plugin because it would require a dependency on the git plugin. That would result in a circular dependency, since the git plugin depends on this plugin. We'll need to test interactively.

@MarkEWaite
Copy link
Contributor

Interactive testing of the pull request shows that a leading space in the refspec from the UI still fails to clone. The job definition is:

<project>
  <actions/>
  <description/>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <scm class="hudson.plugins.git.GitSCM" plugin="git@5.2.1">
    <configVersion>2</configVersion>
    <userRemoteConfigs>
      <hudson.plugins.git.UserRemoteConfig>
        <name>origin</name>
        <refspec> +refs/heads/3.11.x:refs/remotes/origin/3.11.x</refspec>
        <url>https://github.com/jenkinsci/git-client-plugin</url>
      </hudson.plugins.git.UserRemoteConfig>
    </userRemoteConfigs>
    <branches>
      <hudson.plugins.git.BranchSpec>
        <name>3.11.x</name>
      </hudson.plugins.git.BranchSpec>
    </branches>
    <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
    <gitTool>jgit</gitTool>
    <submoduleCfg class="empty-list"/>
    <extensions/>
  </scm>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders/>
  <publishers/>
  <buildWrappers/>
</project>

@Jagrutiti
Copy link
Author

hudson.plugins.git.UserRemoteConfig

I have found this link for UserRemoteConfig: https://javadoc.jenkins.io/plugin/git/hudson/plugins/git/UserRemoteConfig.html

But not able to find where it is called in the code.

@Jagrutiti
Copy link
Author

Hi @MarkEWaite

I have updated almost all the references I found for refspecs.

One of the test cases is failing because of:


ERROR]   GitClientFetchTest.test_fetch:256 » JGitInternal Exception caught during execution of merge command. org.eclipse.jgit.errors.MissingObjectException: Missing unknown dadd5f9e276467580ddb00c2403eec0e9501a6e3
[ERROR]   GitClientFetchTest.test_fetch:256 » JGitInternal Exception caught during execution of merge command. org.eclipse.jgit.errors.MissingObjectException: Missing unknown cb6a175eac031a7acf8270842a6eeda2ef1f27ad

I used debugger. It fails at this line:

newAreaWorkspace.getGitClient().merge().setRevisionToMerge(bareCommit3).execute();

but then the next line:

assertThat("bare3 != newArea3", newAreaWorkspace.getGitClient().revParse("HEAD"), is(bareCommit3));
has the following id as shown by the debugger.

AnyObjectId[224664d7fc3196baca45c60b06a6b269cea76932]

What should I fix here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Automated test addition or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants