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 Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
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...

1,751 views 1 5
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