Skip to content

Commit

Permalink
disable tag.gpgsign as it could be active for some systems (#1553)
Browse files Browse the repository at this point in the history
* disable tag.gpgsign as it could be active for some systems

Signed-off-by: Olivier Lamy <[email protected]>

* case is important for this one...

Signed-off-by: Olivier Lamy <[email protected]>

* wip

Signed-off-by: Olivier Lamy <[email protected]>

* fix more test failing due to auto sign

Signed-off-by: Olivier Lamy <[email protected]>

* git-client 4.7.0

Signed-off-by: Olivier Lamy <[email protected]>

* Remove explicit version of git client plugin

---------

Signed-off-by: Olivier Lamy <[email protected]>
Co-authored-by: Mark Waite <[email protected]>
  • Loading branch information
olamy and MarkEWaite authored Mar 17, 2024
1 parent 6dec48d commit 7f8afeb
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 32 deletions.
12 changes: 11 additions & 1 deletion src/main/java/hudson/plugins/git/GitPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ private String replaceAdditionalEnvironmentalVariables(String input, AbstractBui
input = input.replaceAll("\\$BUILDDURATION", buildDuration);
return input;
}

protected GitClient getGitClient(
GitSCM gitSCM,
BuildListener listener,
EnvVars environment,
AbstractBuild<?, ?> build,
UnsupportedCommand cmd)
throws IOException, InterruptedException {
return gitSCM.createClient(listener, environment, build, build.getWorkspace(), cmd);
}

@Override
public boolean perform(AbstractBuild<?, ?> build,
Expand Down Expand Up @@ -180,7 +190,7 @@ public boolean perform(AbstractBuild<?, ?> build,

UnsupportedCommand cmd = new UnsupportedCommand();
cmd.gitPublisher(true);
final GitClient git = gitSCM.createClient(listener, environment, build, build.getWorkspace(), cmd);
final GitClient git = getGitClient(gitSCM, listener, environment, build, cmd);

URIish remoteURI;

Expand Down
14 changes: 9 additions & 5 deletions src/main/java/hudson/plugins/git/GitTagAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,26 @@ void scheduleTagCreation(Map<String, String> newTags, String comment) throws IOE
/**
* The thread that performs tagging operation asynchronously.
*/
public final class TagWorkerThread extends TaskThread {
public class TagWorkerThread extends TaskThread {
private final Map<String, String> tagSet;

public TagWorkerThread(Map<String, String> tagSet,String ignoredComment) {
super(GitTagAction.this, ListenerAndText.forMemory(null));
this.tagSet = tagSet;
}

protected GitClient getGitClient(TaskListener listener, EnvVars environment, FilePath workspace)
throws IOException, InterruptedException {
return Git.with(listener, environment)
.in(workspace)
.getClient();
}

@Override
protected void perform(final TaskListener listener) throws Exception {
final EnvVars environment = getRun().getEnvironment(listener);
final FilePath workspace = new FilePath(new File(ws));
final GitClient git = Git.with(listener, environment)
.in(workspace)
.getClient();

final GitClient git = getGitClient(listener, environment, workspace);

for (Map.Entry<String, String> entry : tagSet.entrySet()) {
try {
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/hudson/plugins/git/AbstractGitRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.ArrayList;
import java.util.List;

import hudson.plugins.git.util.GitUtilsTest;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Before;
import org.junit.Rule;

Expand Down Expand Up @@ -38,10 +40,11 @@ public abstract class AbstractGitRepository {

@Before
public void createGitRepository() throws Exception {
SystemReader.getInstance().getUserConfig().clear();
TaskListener listener = StreamTaskListener.fromStderr();
repo.init();
testGitDir = repo.getRoot();
testGitClient = Git.with(listener, new EnvVars()).in(testGitDir).getClient();
testGitClient = Git.with(listener, GitUtilsTest.getConfigNoSystemEnvsVars()).in(testGitDir).getClient();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public static void createRepo() throws Exception {
assertThat(gitCmd.run(), is(expectedResult));
// we have to setup the repo as commitOneFile doesn't to use the env vars
gitCmd = new CliGitCommand(gitClient, "config", "commit.gpgsign", "false");
gitCmd = new CliGitCommand(gitClient, "config", "tag.gpgSign", "false");
assertThat(gitCmd.run(), is(expectedResult));
}

Expand All @@ -145,6 +146,8 @@ private ObjectId commitOneFile(GitClient gitClient, final String commitSummary)
/* randomize whether commit message is single line or multi-line */
String commitMessageBody = random.nextBoolean() ? "\n\n" + "committing " + path + " with content:\n\n" + content : "";
String commitMessage = commitSummary + commitMessageBody;
gitClient.config(GitClient.ConfigLevel.LOCAL, "commit.gpgsign", "false");
gitClient.config(GitClient.ConfigLevel.LOCAL, "tag.gpgSign", "false");
createFile(path, content);
gitClient.add(path);
gitClient.commit(commitMessage);
Expand Down
64 changes: 51 additions & 13 deletions src/test/java/hudson/plugins/git/GitPublisherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package hudson.plugins.git;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.Axis;
Expand All @@ -45,6 +46,7 @@
import org.eclipse.jgit.util.SystemReader;
import org.jenkinsci.plugins.gitclient.JGitTool;
import org.jenkinsci.plugins.gitclient.MergeCommand;
import org.jenkinsci.plugins.gitclient.UnsupportedCommand;
import org.jvnet.hudson.test.Issue;

import java.io.File;
Expand Down Expand Up @@ -102,7 +104,7 @@ public void testMatrixBuild() throws Exception {
MatrixProject mp = r.createProject(MatrixProject.class, "xyz");
mp.setAxes(new AxisList(new Axis("VAR","a","b")));
mp.setScm(new GitSCM(testGitDir.getAbsolutePath()));
mp.getPublishersList().add(new GitPublisher(
mp.getPublishersList().add(new TestGitPublisher(
Collections.singletonList(new TagToPush("origin","foo","message",true, false)),
Collections.emptyList(),
Collections.emptyList(),
Expand Down Expand Up @@ -157,7 +159,7 @@ public void GitPublisherFreestylePushBranchWithJGit() throws Exception {
scm.getExtensions().add(new LocalBranch("integration"));
project.setScm(scm);

project.getPublishersList().add(new GitPublisher(
project.getPublishersList().add(new TestGitPublisher(
Collections.emptyList(),
Collections.singletonList(new BranchToPush("origin", "integration")),
Collections.emptyList(),
Expand Down Expand Up @@ -201,7 +203,7 @@ public void GitPublisherFailWithJGit() throws Exception {
mp.setScm(new GitSCM(repoList,
Collections.singletonList(new BranchSpec("")),
null, tool.getName(), Collections.emptyList()));
mp.getPublishersList().add(new GitPublisher(
mp.getPublishersList().add(new TestGitPublisher(
Collections.singletonList(new TagToPush("origin","foo","message",true, false)),
Collections.emptyList(),
Collections.emptyList(),
Expand Down Expand Up @@ -249,7 +251,7 @@ public void testMergeAndPush() throws Exception {
scm.getExtensions().add(new LocalBranch("integration"));
project.setScm(scm);

project.getPublishersList().add(new GitPublisher(
project.getPublishersList().add(new TestGitPublisher(
Collections.emptyList(),
Collections.singletonList(new BranchToPush("origin", "integration")),
Collections.emptyList(),
Expand Down Expand Up @@ -285,7 +287,7 @@ public void testMergeAndPushFF() throws Exception {
scm.getExtensions().add(new LocalBranch("integration"));
project.setScm(scm);

project.getPublishersList().add(new GitPublisher(
project.getPublishersList().add(new TestGitPublisher(
Collections.emptyList(),
Collections.singletonList(new BranchToPush("origin", "integration")),
Collections.emptyList(),
Expand Down Expand Up @@ -369,7 +371,7 @@ public void testMergeAndPushNoFF() throws Exception {
scm.getExtensions().add(new LocalBranch("integration"));
project.setScm(scm);

project.getPublishersList().add(new GitPublisher(
project.getPublishersList().add(new TestGitPublisher(
Collections.emptyList(),
Collections.singletonList(new BranchToPush("origin", "integration")),
Collections.emptyList(),
Expand Down Expand Up @@ -457,7 +459,7 @@ public void testMergeAndPushFFOnly() throws Exception {
scm.getExtensions().add(new LocalBranch("integration"));
project.setScm(scm);

project.getPublishersList().add(new GitPublisher(
project.getPublishersList().add(new TestGitPublisher(
Collections.emptyList(),
Collections.singletonList(new BranchToPush("origin", "integration")),
Collections.emptyList(),
Expand Down Expand Up @@ -554,7 +556,7 @@ public void testPushEnvVarsInRemoteConfig() throws Exception{
String tag_name = "test-tag";
String note_content = "Test Note";

project.getPublishersList().add(new GitPublisher(
project.getPublishersList().add(new TestGitPublisher(
Collections.singletonList(new TagToPush("$TARGET_NAME", tag_name, "", false, false)),
Collections.singletonList(new BranchToPush("$TARGET_NAME", "$TARGET_BRANCH")),
Collections.singletonList(new NoteToPush("$TARGET_NAME", note_content, Constants.R_NOTES_COMMITS, false)),
Expand Down Expand Up @@ -584,7 +586,7 @@ public void testForcePush() throws Exception {
Collections.emptyList());
project.setScm(scm);

GitPublisher forcedPublisher = new GitPublisher(
GitPublisher forcedPublisher = new TestGitPublisher(
Collections.emptyList(),
Collections.singletonList(new BranchToPush("origin", "otherbranch")),
Collections.emptyList(),
Expand Down Expand Up @@ -631,7 +633,7 @@ public void testForcePush() throws Exception {

// Remove forcedPublisher, add unforcedPublisher
project.getPublishersList().remove(forcedPublisher);
GitPublisher unforcedPublisher = new GitPublisher(
GitPublisher unforcedPublisher = new TestGitPublisher(
Collections.emptyList(),
Collections.singletonList(new BranchToPush("origin", "otherbranch")),
Collections.emptyList(),
Expand Down Expand Up @@ -680,7 +682,7 @@ public void testMergeAndPushWithSkipTagEnabled() throws Exception {
project.setScm(scm);


project.getPublishersList().add(new GitPublisher(
project.getPublishersList().add(new TestGitPublisher(
Collections.emptyList(),
Collections.singletonList(new BranchToPush("origin", "integration")),
Collections.emptyList(),
Expand Down Expand Up @@ -715,7 +717,7 @@ public void testRebaseBeforePush() throws Exception {
BranchToPush btp = new BranchToPush("origin", "master");
btp.setRebaseBeforePush(true);

GitPublisher rebasedPublisher = new GitPublisher(
GitPublisher rebasedPublisher = new TestGitPublisher(
Collections.emptyList(),
Collections.singletonList(btp),
Collections.emptyList(),
Expand Down Expand Up @@ -747,6 +749,9 @@ public void testRebaseBeforePush() throws Exception {
@Issue("JENKINS-24786")
@Test
public void testMergeAndPushWithCharacteristicEnvVar() throws Exception {
// jgit doesn't work because of missing PerBuildTag
//GitTool tool = new JGitTool(Collections.emptyList());
//r.jenkins.getDescriptorByType(GitTool.DescriptorImpl.class).setInstallations(tool);
FreeStyleProject project = setupSimpleProject("master");

/*
Expand Down Expand Up @@ -799,7 +804,7 @@ private void checkEnvVar(FreeStyleProject project, String envName, String envVal
String tagMessageReference = envReference + " tag message";
String noteReference = "note for " + envReference;
String noteValue = "note for " + envValue;
GitPublisher publisher = new GitPublisher(
GitPublisher publisher = new TestGitPublisher(
Collections.singletonList(new TagToPush("origin", tagNameReference, tagMessageReference, false, true)),
Collections.singletonList(new BranchToPush("origin", envReference)),
Collections.singletonList(new NoteToPush("origin", noteReference, Constants.R_NOTES_COMMITS, false)),
Expand Down Expand Up @@ -853,6 +858,36 @@ private void checkEnvVar(FreeStyleProject project, String envName, String envVal

}

/**
* doing this because some system and/or users may commit/tag gpgSign activated,
* and we cannot answer the passphrase if needed so disabled it locally for the test
*/
private static class TestGitPublisher extends GitPublisher {
public TestGitPublisher(
List<TagToPush> tagsToPush,
List<BranchToPush> branchesToPush,
List<NoteToPush> notesToPush,
boolean pushOnlyIfSuccess,
boolean pushMerge,
boolean forcePush) {
super(tagsToPush, branchesToPush, notesToPush, pushOnlyIfSuccess, pushMerge, forcePush);
}

@Override
protected GitClient getGitClient(
GitSCM gitSCM,
BuildListener listener,
EnvVars environment,
AbstractBuild<?, ?> build,
UnsupportedCommand cmd)
throws IOException, InterruptedException {
GitClient gitClient = super.getGitClient(gitSCM, listener, environment, build, cmd);
gitClient.config(GitClient.ConfigLevel.LOCAL, "commit.gpgsign", "false");
gitClient.config(GitClient.ConfigLevel.LOCAL, "tag.gpgSign", "false");
return gitClient;
}
}

private boolean existsTag(String tag) throws InterruptedException {
return existsTagInRepo(testGitClient, tag);
}
Expand Down Expand Up @@ -907,4 +942,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

return true;
}



}
22 changes: 20 additions & 2 deletions src/test/java/hudson/plugins/git/GitSCMSlowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import static org.junit.Assume.assumeTrue;

import org.eclipse.jgit.util.SystemReader;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
Expand Down Expand Up @@ -289,7 +290,7 @@ public void testMergeWithAgent() throws Exception {
Collections.singletonList(new BranchSpec("*")),
null, null,
Collections.emptyList());
scm.getExtensions().add(new PreBuildMerge(new UserMergeOptions("origin", "integration", null, null)));
scm.getExtensions().add(new TestPreBuildMerge(new UserMergeOptions("origin", "integration", null, null)));
addChangelogToBranchExtension(scm);
project.setScm(scm);

Expand Down Expand Up @@ -319,6 +320,23 @@ public void testMergeWithAgent() throws Exception {
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}

/**
* because of auto gpgsign we must disable it at repo level
*/
public static class TestPreBuildMerge extends PreBuildMerge {
public TestPreBuildMerge(UserMergeOptions options) {
super(options);
}

@Override
public GitClient decorate(GitSCM scm, GitClient git) throws IOException, InterruptedException, GitException {
GitClient gitClient = super.decorate(scm, git);
gitClient.config(GitClient.ConfigLevel.LOCAL, "commit.gpgsign", "false");
gitClient.config(GitClient.ConfigLevel.LOCAL, "tag.gpgSign", "false");
return gitClient;
}
}

@Test
public void testMergeWithMatrixBuild() throws Exception {
assumeTrue("Test class max time " + MAX_SECONDS_FOR_THESE_TESTS + " exceeded", isTimeAvailable());
Expand All @@ -331,7 +349,7 @@ public void testMergeWithMatrixBuild() throws Exception {
Collections.singletonList(new BranchSpec("*")),
null, null,
Collections.emptyList());
scm.getExtensions().add(new PreBuildMerge(new UserMergeOptions("origin", "integration", null, null)));
scm.getExtensions().add(new TestPreBuildMerge(new UserMergeOptions("origin", "integration", null, null)));
addChangelogToBranchExtension(scm);
project.setScm(scm);

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/hudson/plugins/git/GitSCMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,7 @@ public void testMerge() throws Exception {
Collections.singletonList(new BranchSpec("*")),
null, null,
Collections.emptyList());
scm.getExtensions().add(new PreBuildMerge(new UserMergeOptions("origin", "integration", "default", MergeCommand.GitPluginFastForwardMode.FF)));
scm.getExtensions().add(new GitSCMSlowTest.TestPreBuildMerge(new UserMergeOptions("origin", "integration", "default", MergeCommand.GitPluginFastForwardMode.FF)));
addChangelogToBranchExtension(scm);
project.setScm(scm);

Expand Down
Loading

0 comments on commit 7f8afeb

Please sign in to comment.