Skip to content

Commit

Permalink
feat: allow defining a commit author
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Mar 4, 2024
1 parent 2a2adfb commit e3b2835
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/main/java/io/kestra/plugin/git/Push.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.kestra.plugin.git;

import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.annotations.PluginProperty;
Expand All @@ -26,6 +27,7 @@
import org.eclipse.jgit.api.RmCommand;
import org.eclipse.jgit.api.errors.EmptyCommitException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.slf4j.Logger;

import java.io.File;
Expand Down Expand Up @@ -158,6 +160,10 @@ public class Push extends AbstractGitTask implements RunnableTask<Push.Output>,
@Builder.Default
private List<String> addFilesPattern = List.of(".");

@Schema(title = "Commit author.")
@PluginProperty
private Author author;

private boolean branchExists(RunContext runContext, String branch) throws Exception {
if (this.url == null) {
try (Git git = Git.open(runContext.resolve(Path.of(runContext.render(this.directory))).toFile())) {
Expand Down Expand Up @@ -281,7 +287,12 @@ public Output run(RunContext runContext) throws Exception {

ObjectId commitId = null;
try {
commitId = git.commit().setAllowEmpty(false).setMessage(runContext.render(this.commitMessage)).call().getId();
commitId = git.commit()
.setAllowEmpty(false)
.setMessage(runContext.render(this.commitMessage))
.setAuthor(author(runContext))
.call()
.getId();
authentified(git.push(), runContext).call();
} catch (EmptyCommitException e) {
logger.info("No changes to commit. Skipping push.");
Expand All @@ -297,6 +308,20 @@ public Output run(RunContext runContext) throws Exception {
).build();
}

private PersonIdent author(RunContext runContext) throws IllegalVariableEvaluationException {
if (this.author == null) {
return null;
}
if (this.author.email != null && this.author.name != null) {
return new PersonIdent(runContext.render(this.author.name), runContext.render(this.author.email));
}
if (this.author.email != null && this.username != null) {
return new PersonIdent(runContext.render(this.username), runContext.render(this.author.email));
}

return null;
}

@Builder
@Getter
public static class Output implements io.kestra.core.models.tasks.Output {
Expand Down Expand Up @@ -336,4 +361,16 @@ public static class FlowFiles {
@Builder.Default
private String gitDirectory = "_flows";
}

@Builder
@Getter
public static class Author {
@Schema(title = "The commit author name, if null the username will be used instead")
@PluginProperty(dynamic = true)
private String name;

@Schema(title = "The commit author email, if null no author will be set on this commit")
@PluginProperty(dynamic = true)
private String email;
}
}
7 changes: 7 additions & 0 deletions src/test/java/io/kestra/plugin/git/PushTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ void oneTaskPush_WithFlowsNoChildNs() throws Exception {
.build())
.username(pat)
.password(pat)
.author(Push.Author.builder()
.name("loicmathieu")
.email("lmathieu@kestra.io")
.build())
.branch(branchName)
.build();

Expand Down Expand Up @@ -514,6 +518,9 @@ void oneTaskPush_WithFlowsAndDirectory() throws Exception {
.commitMessage("Push from CI - {{description}}")
.username(pat)
.password(pat)
.author(Push.Author.builder()
.email("lmathieu@kestra.io")
.build())
.branch(branchName)
.flows(Push.FlowFiles.builder()
.gitDirectory("my-flows")
Expand Down

0 comments on commit e3b2835

Please sign in to comment.