Skip to content

Commit

Permalink
minor: Support setting credentials for HTTP
Browse files Browse the repository at this point in the history
Fixes #115.
  • Loading branch information
ajoberstar committed Dec 10, 2024
1 parent 8fda20e commit 496b40a
Showing 6 changed files with 71 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@ See the [Release Notes](https://github.com/ajoberstar/gradle-git-publish/release

> [!NOTE]
> As of 5.0.0, the plugin uses the Git CLI for all behavior. `git` binary must be on the `PATH`.
>
> If you relied on Grgit's authentication, such as `GRGIT_USER`, use the new `username` and `password` properties on the `GitPublication`. See the configuration documentation below.
### Applying the Plugin

@@ -77,6 +79,11 @@ gitPublish {
// for signing commits, omit to use the default from your gitconfig
sign = false
// if you need to provide credentials, set both of these
// use an external source, such as an environment variable
username = 'dont-harcode'
password = 'dont-hardcode'
}
```

Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@ public class GitPublication implements Named {
private final Property<Boolean> sign;
private final CopySpec contents;
private final PatternFilterable preserve;
private final Property<String> username;
private final Property<String> password;

public GitPublication(String name, Project project, ObjectFactory objectFactory) {
this.name = name;
@@ -35,6 +37,9 @@ public GitPublication(String name, Project project, ObjectFactory objectFactory)
this.contents = project.copySpec();
this.preserve = new PatternSet();
this.preserve.include(".git/**/*");

this.username = objectFactory.property(String.class);
this.password = objectFactory.property(String.class);
}

@Override
@@ -85,4 +90,12 @@ public PatternFilterable getPreserve() {
public void preserve(Action<? super PatternFilterable> action) {
action.execute(preserve);
}

public Property<String> getUsername() {
return username;
}

public Property<String> getPassword() {
return password;
}
}
Original file line number Diff line number Diff line change
@@ -70,4 +70,12 @@ public PatternFilterable getPreserve() {
public void preserve(Action<? super PatternFilterable> action) {
publications.getByName("main").preserve(action);
}

public Property<String> getUsername() {
return publications.getByName("main").getUsername();
}

public Property<String> getPassword() {
return publications.getByName("main").getPassword();
}
}
Original file line number Diff line number Diff line change
@@ -58,6 +58,8 @@ private TaskProvider<GitPublishReset> createResetTask(Project project, GitPublic
task.getBranch().set(publication.getBranch());
task.getFetchDepth().set(publication.getFetchDepth());
task.setPreserve(publication.getPreserve());
task.getUsername().set(publication.getUsername());
task.getPassword().set(publication.getPassword());
});
}

@@ -91,6 +93,8 @@ private TaskProvider<GitPublishPush> createPushTask(Project project, GitPublicat
task.setDescription("Pushes " + publication.getName() + " publication changes to git.");
task.getRepoDir().set(publication.getRepoDir());
task.getBranch().set(publication.getBranch());
task.getUsername().set(publication.getUsername());
task.getPassword().set(publication.getPassword());
});
}

Original file line number Diff line number Diff line change
@@ -26,6 +26,12 @@ public abstract class GitPublishPush extends DefaultTask {
@Input
public abstract Property<String> getBranch();

@Internal
public abstract Property<String> getUsername();

@Internal
public abstract Property<String> getPassword();

@Inject
protected abstract ExecOperations getExecOperations();

@@ -36,6 +42,12 @@ public void push() {
getExecOperations().exec(spec -> {
var refSpec = String.format("refs/heads/%s:refs/heads/%s", pubBranch, pubBranch);
spec.commandLine("git", "push", "--porcelain", "--set-upstream", "origin", refSpec);

if (getUsername().isPresent() && getPassword().isPresent()) {
spec.environment("GIT_USERNAME", getUsername().get());
spec.environment("GIT_PASSWORD", getPassword().get());
}

spec.workingDir(getRepoDir().get());
spec.setStandardOutput(output);
});
Original file line number Diff line number Diff line change
@@ -58,6 +58,12 @@ public void setPreserve(PatternFilterable preserve) {
this.preserve = preserve;
}

@Internal
public abstract Property<String> getUsername();

@Internal
public abstract Property<String> getPassword();

@Inject
protected abstract ObjectFactory getObjectFactory();

@@ -78,6 +84,16 @@ public void reset() throws IOException {
});
}

// (if credentials) set credential helper
if (getUsername().isPresent() && getPassword().isPresent()) {
getExecOperations().exec(spec -> {
var script = "!f() { echo username=$GIT_USERNAME; echo password=$GIT_PASSWORD; }; f";
spec.commandLine("git", "config", "--local", "credential.helper", script);
spec.workingDir(repoDir);
spec.setStandardOutput(OutputStream.nullOutputStream());
});
}

// set origin
try {
getExecOperations().exec(spec -> {
@@ -124,6 +140,12 @@ public void reset() throws IOException {
getExecOperations().exec(spec -> {
spec.commandLine("git", "ls-remote", "--exit-code", "origin", pubBranch);
spec.workingDir(repoDir);

if (getUsername().isPresent() && getPassword().isPresent()) {
spec.environment("GIT_USERNAME", getUsername().get());
spec.environment("GIT_PASSWORD", getPassword().get());
}

spec.setStandardOutput(OutputStream.nullOutputStream());
spec.setErrorOutput(OutputStream.nullOutputStream());
});
@@ -145,6 +167,11 @@ public void reset() throws IOException {
spec.args("--no-tags");
spec.args("origin", refSpec);

if (getUsername().isPresent() && getPassword().isPresent()) {
spec.environment("GIT_USERNAME", getUsername().get());
spec.environment("GIT_PASSWORD", getPassword().get());
}

spec.workingDir(repoDir);
spec.setStandardOutput(OutputStream.nullOutputStream());
});

0 comments on commit 496b40a

Please sign in to comment.