Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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);
}
}
}}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.