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);
}
}
}}

0 answers

Suggest an answer

Log in or Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

660 views 0 4
Read article

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot