BitBucket Plugin Development For updating commit message on SQUASH Merge

I have developed a plugin to update the bitbucket commit message on (SQUASH MERGE ) 

The problem is if I create 2 Pull Request from Master :

Upon 1st Pull request Merge commit message is getting updated as I wanted But

the 2nd pull request Im not ablet to merge it is giving the following error.

"You are attempting to modify a pull request based on out-of-date information."

 

Java code from Bitbucket plugin any suggestion would be helpfull:

@EventListener
public void onMergeEvent(PullRequestMergedEvent mergeEvent)
{

Repository Repo = mergeEvent.getRepository();
System.out.println(Repo);
String repoName = Repo.toString();
System.out.println(repoName);
if (repoName.equals("VI/fabric[192]"))
{
System.out.println(mergeEvent.getStrategyId());
if(mergeEvent.getStrategyId().equals("squash"))

{

String message = mergeEvent.getMessage();


File repo_dir = application_properties_service.getRepositoryDir(Repo);
String commitID = mergeEvent.getCommit().getId();
try
{

org.eclipse.jgit.lib.Repository jgit_repo = new FileRepositoryBuilder().setGitDir(repo_dir).build();
ObjectId objectID = ObjectId.fromString(commitID);
System.out.println("objectID"+objectID);
RevWalk revWalk = new RevWalk(jgit_repo);
RevCommit oldCommit = revWalk.parseCommit(objectID);

PersonIdent author = oldCommit.getAuthorIdent();
PersonIdent committer = oldCommit.getCommitterIdent();
int prentPosition = oldCommit.getParentCount();
RevCommit parent = oldCommit.getParent(prentPosition-1);
Charset encoding = oldCommit.getEncoding();
RevTree indexTreeId = oldCommit.getTree();
ObjectId headId = jgit_repo.resolve(Constants.HEAD + "^{commit}");

CommitBuilder commit = new CommitBuilder();
commit.setCommitter(committer);
commit.setAuthor(author);
commit.setMessage(message);
commit.setEncoding(encoding);
commit.setParentIds(parent);
commit.setTreeId(indexTreeId);
try
{
ObjectInserter odi = jgit_repo.newObjectInserter();
ObjectId objCommitId = odi.insert(commit);
odi.flush();

RevCommit revCommit = revWalk.parseCommit(objCommitId);
RefUpdate ru = jgit_repo.updateRef(Constants.HEAD);
ru.setNewObjectId(objCommitId);
System.out.println("objCommitId"+objCommitId);
//ru.setExpectedOldObjectId(headId);
ru.forceUpdate();
}
catch (Exception ie) {
ie.printStackTrace();

System.out.println(ie);
}

 

revWalk.close();
}
catch (Exception ie) {
ie.printStackTrace();

System.out.println(ie);
}
}
}}

1 answer

0 vote
Adam Ahmed Atlassian Team May 17, 2018

Sorry for the late response on this - just found it and I think it deserves at least some answer.

The main issue I think is that you're updating a merge commit after-the-fact. While you can certainly do this, the experience won't be great. For example the activity for the PR will say it was merged via the old commit hash, not your new one. While there are ways to do it beforehand (e.g. a repository pre-hook on PR merges), I believe they still wouldn't quite fit with the rest of the PR experience. I can't think of a good way to create a custom merge commit with our current API, but I'd be interested in hearing what you want to change about the commit. It's possible our Merge Strategies feature (which supports rebases, squashes, fast-forward, etc) will cover your requirements already, or perhaps I could find a dev who can help a bit more if I know what you're trying to acheive.

In your Java code, it also looks like you're using JGit. Just a heads up that JGit is great, but we've had issues using it in Bitbucket Server before from a performance and stability POV. You probably don't need to add that external dependency anyway - we have built-in APIs that should be helpful for crafting Git commands. This file has a few examples of using the very generic CLI builder. You start with a GitCommandBuilderFactory and can create, for example, a builder for creating a commit

private GitCommandBuildFactory builderFactory;

...

GitCommitBuilder commitBuilder = builderFactory.builder(repository).commit();
GitCommand<Void> commitCommand = commitBuilder.message("Hello").all(true).committer("Pegasus", "horse@sky.example.com").build();
commitCommand.call();

 Cheers!

Adam

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jun 06, 2018 in Bitbucket

Do you use Bitbucket Cloud and Jira Cloud? If so, let us know!

Hi Community, I'm Julia and I'm on the Jira Software Cloud marketing team!  We're looking for companies or teams using Bitbucket Cloud and Jira Software Cloud. If your team fits the t...

70 views 2 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you