Skip to content

Commit

Permalink
Merge branch 'master' into JENKINS-73677
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEWaite committed Sep 17, 2024
2 parents 04bed09 + 9ba71f1 commit 233e3d7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 45 deletions.
13 changes: 2 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@
</roles>
<url>http://rsandell.com</url>
</developer>
<developer>
<id>mramonleon</id>
<name>M Ramon Leon</name>
<email>[email protected]</email>
<roles>
<role>developer</role>
</roles>
</developer>
<developer>
<id>olamy</id>
<name>Olivier Lamy</name>
Expand Down Expand Up @@ -89,7 +81,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>3289.v3ff9637cd241</version>
<version>3358.vea_fa_1f41504d</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -187,7 +179,6 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>promoted-builds</artifactId>
<version>892.vd6219fc0a_efb</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down Expand Up @@ -250,7 +241,7 @@
<dependency>
<groupId>io.github.sparsick.testcontainers.gitserver</groupId>
<artifactId>testcontainers-gitserver</artifactId>
<version>0.8.0</version>
<version>0.9.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/jenkins/plugins/git/DisableHooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package jenkins.plugins.git;

import hudson.Functions;
import hudson.remoting.VirtualChannel;
import java.io.IOException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.jenkinsci.plugins.gitclient.RepositoryCallback;

/**
* Disables git hooks. This can get remotely executed on agents.
*/
class DisableHooks implements RepositoryCallback<Object> {
private static final long serialVersionUID = 1L;

static final String DISABLED_WIN = "NUL:";
static final String DISABLED_NIX = "/dev/null";

@Override
public Object invoke(Repository repo, VirtualChannel channel) throws IOException, InterruptedException {
final String VAL = Functions.isWindows() ? DISABLED_WIN : DISABLED_NIX;
final StoredConfig repoConfig = repo.getConfig();
repoConfig.setString("core", null, "hooksPath", VAL);
repoConfig.save();
return null;
}
}
35 changes: 2 additions & 33 deletions src/main/java/jenkins/plugins/git/GitHooksConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,23 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Functions;
import hudson.model.PersistentDescriptor;
import hudson.plugins.git.GitException;
import hudson.remoting.Channel;
import jenkins.model.GlobalConfiguration;
import jenkins.model.GlobalConfigurationCategory;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import java.io.IOException;
import java.util.logging.Logger;



@Extension @Symbol("gitHooks") @Restricted(NoExternalUse.class)
public class GitHooksConfiguration extends GlobalConfiguration implements PersistentDescriptor {

public static final String DISABLED_WIN = "NUL:";
public static final String DISABLED_NIX = "/dev/null";
static final Logger LOGGER = Logger.getLogger(GitHooksConfiguration.class.getName());

private boolean allowedOnController = false;
private boolean allowedOnAgents = false;

Expand Down Expand Up @@ -109,33 +101,10 @@ public static void configure(GitClient client, final boolean allowedOnController

public static void configure(GitClient client, final boolean allowed) throws GitException, IOException, InterruptedException {
if (!allowed) {
client.withRepository((repo, channel) -> {
disable(repo);
return null;
});
client.withRepository(new DisableHooks());
} else {
client.withRepository((repo, channel) -> {
unset(repo);
return null;
});
client.withRepository(new UnsetHooks());
}
}

private static void unset(final Repository repo) throws IOException {
final StoredConfig repoConfig = repo.getConfig();
final String val = repoConfig.getString("core", null, "hooksPath");
if (val != null && !val.isEmpty() && !DISABLED_NIX.equals(val) && !DISABLED_WIN.equals(val)) {
LOGGER.warning(() -> String.format("core.hooksPath explicitly set to %s and will be left intact on %s.", val, repo.getDirectory()));
} else {
repoConfig.unset("core", null, "hooksPath");
repoConfig.save();
}
}

private static void disable(final Repository repo) throws IOException {
final String VAL = Functions.isWindows() ? DISABLED_WIN : DISABLED_NIX;
final StoredConfig repoConfig = repo.getConfig();
repoConfig.setString("core", null, "hooksPath", VAL);
repoConfig.save();
}
}
30 changes: 30 additions & 0 deletions src/main/java/jenkins/plugins/git/UnsetHooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package jenkins.plugins.git;

import hudson.remoting.VirtualChannel;
import java.io.IOException;
import java.util.logging.Logger;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.jenkinsci.plugins.gitclient.RepositoryCallback;

/**
* Unsets git hooks. This can get remotely executed on agents.
*/
class UnsetHooks implements RepositoryCallback<Object> {
private static final Logger LOGGER = Logger.getLogger(UnsetHooks.class.getName());

private static final long serialVersionUID = 1L;

@Override
public Object invoke(Repository repo, VirtualChannel channel) throws IOException, InterruptedException {
final StoredConfig repoConfig = repo.getConfig();
final String val = repoConfig.getString("core", null, "hooksPath");
if (val != null && !val.isEmpty() && !DisableHooks.DISABLED_NIX.equals(val) && !DisableHooks.DISABLED_WIN.equals(val)) {
LOGGER.warning(() -> String.format("core.hooksPath explicitly set to %s and will be left intact on %s.", val, repo.getDirectory()));
} else {
repoConfig.unset("core", null, "hooksPath");
repoConfig.save();
}
return null;
}
}
2 changes: 1 addition & 1 deletion src/test/java/hudson/plugins/git/FIPSModeUrlCheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testGitSCMSourceCheck() throws Throwable {
SystemCredentialsProvider.getInstance()
.getCredentials()
.add(new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL, "mycreds", null, "jenkins", "s3cr3t"));
CredentialsScope.GLOBAL, "mycreds", null, "jenkins", "s3cr3t-that-needs-to-be-long"));
SystemCredentialsProvider.getInstance().save();
MultiBranchProject<?, ?> mbp = r.createProject(WorkflowMultiBranchProject.class, "mbp");
GitSCMSource.DescriptorImpl descriptor = ExtensionList.lookupSingleton(GitSCMSource.DescriptorImpl.class);
Expand Down

0 comments on commit 233e3d7

Please sign in to comment.