Skip to content

Commit 368ced7

Browse files
committed
minor: Support setting credentials for HTTP
Fixes #115.
1 parent e77201b commit 368ced7

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ See the [Release Notes](https://github.com/ajoberstar/gradle-git-publish/release
2727

2828
> [!NOTE]
2929
> As of 5.0.0, the plugin uses the Git CLI for all behavior. `git` binary must be on the `PATH`.
30+
>
31+
> 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.
3032
3133
### Applying the Plugin
3234

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

src/main/java/org/ajoberstar/gradle/git/publish/GitPublication.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class GitPublication implements Named {
2121
private final Property<Boolean> sign;
2222
private final CopySpec contents;
2323
private final PatternFilterable preserve;
24+
private final Property<String> username;
25+
private final Property<String> password;
2426

2527
public GitPublication(String name, Project project, ObjectFactory objectFactory) {
2628
this.name = name;
@@ -35,6 +37,9 @@ public GitPublication(String name, Project project, ObjectFactory objectFactory)
3537
this.contents = project.copySpec();
3638
this.preserve = new PatternSet();
3739
this.preserve.include(".git/**/*");
40+
41+
this.username = objectFactory.property(String.class);
42+
this.password = objectFactory.property(String.class);
3843
}
3944

4045
@Override
@@ -85,4 +90,12 @@ public PatternFilterable getPreserve() {
8590
public void preserve(Action<? super PatternFilterable> action) {
8691
action.execute(preserve);
8792
}
93+
94+
public Property<String> getUsername() {
95+
return username;
96+
}
97+
98+
public Property<String> getPassword() {
99+
return password;
100+
}
88101
}

src/main/java/org/ajoberstar/gradle/git/publish/GitPublishExtension.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,12 @@ public PatternFilterable getPreserve() {
7070
public void preserve(Action<? super PatternFilterable> action) {
7171
publications.getByName("main").preserve(action);
7272
}
73+
74+
public Property<String> getUsername() {
75+
return publications.getByName("main").getUsername();
76+
}
77+
78+
public Property<String> getPassword() {
79+
return publications.getByName("main").getPassword();
80+
}
7381
}

src/main/java/org/ajoberstar/gradle/git/publish/GitPublishPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ private TaskProvider<GitPublishReset> createResetTask(Project project, GitPublic
5858
task.getBranch().set(publication.getBranch());
5959
task.getFetchDepth().set(publication.getFetchDepth());
6060
task.setPreserve(publication.getPreserve());
61+
task.getUsername().set(publication.getUsername());
62+
task.getPassword().set(publication.getPassword());
6163
});
6264
}
6365

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

src/main/java/org/ajoberstar/gradle/git/publish/tasks/GitPublishPush.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public abstract class GitPublishPush extends DefaultTask {
2626
@Input
2727
public abstract Property<String> getBranch();
2828

29+
@Internal
30+
public abstract Property<String> getUsername();
31+
32+
@Internal
33+
public abstract Property<String> getPassword();
34+
2935
@Inject
3036
protected abstract ExecOperations getExecOperations();
3137

@@ -36,6 +42,12 @@ public void push() {
3642
getExecOperations().exec(spec -> {
3743
var refSpec = String.format("refs/heads/%s:refs/heads/%s", pubBranch, pubBranch);
3844
spec.commandLine("git", "push", "--porcelain", "--set-upstream", "origin", refSpec);
45+
46+
if (getUsername().isPresent() && getPassword().isPresent()) {
47+
spec.environment("GIT_USERNAME", getUsername().get());
48+
spec.environment("GIT_PASSWORD", getPassword().get());
49+
}
50+
3951
spec.workingDir(getRepoDir().get());
4052
spec.setStandardOutput(output);
4153
});

src/main/java/org/ajoberstar/gradle/git/publish/tasks/GitPublishReset.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ public void setPreserve(PatternFilterable preserve) {
5858
this.preserve = preserve;
5959
}
6060

61+
@Internal
62+
public abstract Property<String> getUsername();
63+
64+
@Internal
65+
public abstract Property<String> getPassword();
66+
6167
@Inject
6268
protected abstract ObjectFactory getObjectFactory();
6369

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

87+
// (if credentials) set credential helper
88+
if (getUsername().isPresent() && getPassword().isPresent()) {
89+
getExecOperations().exec(spec -> {
90+
var script = "!f() { echo username=$GIT_USERNAME; echo password=$GIT_PASSWORD; }; f";
91+
spec.commandLine("git", "config", "--local", "credential.helper", script);
92+
spec.workingDir(repoDir);
93+
spec.setStandardOutput(OutputStream.nullOutputStream());
94+
});
95+
}
96+
8197
// set origin
8298
try {
8399
getExecOperations().exec(spec -> {
@@ -124,6 +140,12 @@ public void reset() throws IOException {
124140
getExecOperations().exec(spec -> {
125141
spec.commandLine("git", "ls-remote", "--exit-code", "origin", pubBranch);
126142
spec.workingDir(repoDir);
143+
144+
if (getUsername().isPresent() && getPassword().isPresent()) {
145+
spec.environment("GIT_USERNAME", getUsername().get());
146+
spec.environment("GIT_PASSWORD", getPassword().get());
147+
}
148+
127149
spec.setStandardOutput(OutputStream.nullOutputStream());
128150
spec.setErrorOutput(OutputStream.nullOutputStream());
129151
});
@@ -145,6 +167,11 @@ public void reset() throws IOException {
145167
spec.args("--no-tags");
146168
spec.args("origin", refSpec);
147169

170+
if (getUsername().isPresent() && getPassword().isPresent()) {
171+
spec.environment("GIT_USERNAME", getUsername().get());
172+
spec.environment("GIT_PASSWORD", getPassword().get());
173+
}
174+
148175
spec.workingDir(repoDir);
149176
spec.setStandardOutput(OutputStream.nullOutputStream());
150177
});

0 commit comments

Comments
 (0)