Set author for merge

This commit is contained in:
René Pfeuffer
2018-11-07 14:02:02 +01:00
parent 2d04e6c2f0
commit 8771f68081
4 changed files with 49 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package sonia.scm.repository.api;
import com.google.common.base.Preconditions;
import sonia.scm.repository.Person;
import sonia.scm.repository.spi.MergeCommand;
import sonia.scm.repository.spi.MergeCommandRequest;
@@ -23,6 +24,11 @@ public class MergeCommandBuilder {
return this;
}
public MergeCommandBuilder setAuthor(Person author) {
request.setAuthor(author);
return this;
}
public MergeCommandBuilder reset() {
request.reset();
return this;

View File

@@ -4,6 +4,8 @@ import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import sonia.scm.Validateable;
import sonia.scm.repository.Person;
import sonia.scm.util.Util;
import java.io.Serializable;
@@ -13,6 +15,7 @@ public class MergeCommandRequest implements Validateable, Resetable, Serializabl
private String branchToMerge;
private String targetBranch;
private Person author;
public String getBranchToMerge() {
return branchToMerge;
@@ -30,8 +33,18 @@ public class MergeCommandRequest implements Validateable, Resetable, Serializabl
this.targetBranch = targetBranch;
}
public Person getAuthor() {
return author;
}
public void setAuthor(Person author) {
this.author = author;
}
public boolean isValid() {
return !Strings.isNullOrEmpty(getBranchToMerge()) && !Strings.isNullOrEmpty(getTargetBranch());
return !Strings.isNullOrEmpty(getBranchToMerge())
&& !Strings.isNullOrEmpty(getTargetBranch())
&& getAuthor() != null;
}
public void reset() {
@@ -52,12 +65,13 @@ public class MergeCommandRequest implements Validateable, Resetable, Serializabl
final MergeCommandRequest other = (MergeCommandRequest) obj;
return Objects.equal(branchToMerge, other.branchToMerge)
&& Objects.equal(targetBranch, other.targetBranch);
&& Objects.equal(targetBranch, other.targetBranch)
&& Objects.equal(author, other.author);
}
@Override
public int hashCode() {
return Objects.hashCode(branchToMerge, targetBranch);
return Objects.hashCode(branchToMerge, targetBranch, author);
}
@Override
@@ -65,6 +79,7 @@ public class MergeCommandRequest implements Validateable, Resetable, Serializabl
return MoreObjects.toStringHelper(this)
.add("branchToMerge", branchToMerge)
.add("targetBranch", targetBranch)
.add("author", author)
.toString();
}
}

View File

@@ -71,8 +71,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
MergeResult result;
try {
result = clone.merge()
.setCommit(true)
.setMessage(String.format(MERGE_COMMIT_MESSAGE_TEMPLATE, toMerge, target))
.setCommit(false) // we want to set the author manually
.include(toMerge, resolveRevision(toMerge))
.call();
} catch (GitAPIException e) {
@@ -80,6 +79,14 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
}
if (result.getMergeStatus().isSuccessful()) {
logger.debug("merged branch {} into {}", toMerge, target);
try {
clone.commit()
.setAuthor(request.getAuthor().getName(), request.getAuthor().getMail())
.setMessage(String.format(MERGE_COMMIT_MESSAGE_TEMPLATE, toMerge, target))
.call();
} catch (GitAPIException e) {
throw new InternalRepositoryException("could not commit merge between branch " + toMerge + " and " + target, e);
}
try {
clone.push().call();
} catch (GitAPIException e) {

View File

@@ -1,9 +1,17 @@
package sonia.scm.repository.spi;
import org.assertj.core.api.Assertions;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;
import sonia.scm.repository.Person;
import sonia.scm.repository.api.MergeCommandResult;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
@@ -34,15 +42,22 @@ public class GitMergeCommandTest extends AbstractGitCommandTestBase {
}
@Test
public void shouldMergeMergeableBranches() {
public void shouldMergeMergeableBranches() throws IOException, GitAPIException {
GitMergeCommand command = createCommand();
MergeCommandRequest request = new MergeCommandRequest();
request.setTargetBranch("master");
request.setBranchToMerge("mergeable");
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
MergeCommandResult mergeCommandResult = command.merge(request);
assertThat(mergeCommandResult.isSuccess()).isTrue();
Repository repository = createContext().open();
Iterable<RevCommit> mergeCommit = new Git(repository).log().add(repository.resolve("master")).setMaxCount(1).call();
PersonIdent mergeAuthor = mergeCommit.iterator().next().getAuthorIdent();
assertThat(mergeAuthor.getName()).isEqualTo("Dirk Gently");
assertThat(mergeAuthor.getEmailAddress()).isEqualTo("dirk@holistic.det");
}
@Test