Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Move hunks getting executed on agents to separate classes #1652

Merged
Merged
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
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;

Check warning on line 21 in src/main/java/jenkins/plugins/git/DisableHooks.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 21 is only partially covered, one branch is missing
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)) {

Check warning on line 22 in src/main/java/jenkins/plugins/git/UnsetHooks.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 22 is only partially covered, 2 branches are missing
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;
}
}