[FEAT] Check if commit is already present in target branch

- Check if someone is (accidentally) trying to create a pull request via
AGit with changes already in the target branch and fail if that is the
case.
- Added integration test.
This commit is contained in:
Gusted 2024-02-23 21:42:15 +01:00
parent 6531d765a0
commit f0ed6de89d
No known key found for this signature in database
GPG key ID: FD821B732837125F
2 changed files with 34 additions and 4 deletions

View file

@ -101,6 +101,21 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
return nil, fmt.Errorf("failed to get unmerged AGit flow pull request in repository %q: %w", repo.FullName(), err)
}
// Check if the changes are already in the target branch.
stdout, _, gitErr := git.NewCommand(ctx, "branch", "--contains").AddDynamicArguments(opts.NewCommitIDs[i], baseBranchName).RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
if gitErr != nil {
return nil, fmt.Errorf("failed to check if the target branch already contains the new commit in repository %q: %w", repo.FullName(), err)
}
if len(stdout) > 0 {
results = append(results, private.HookProcReceiveRefResult{
OriginalRef: opts.RefFullNames[i],
OldOID: opts.OldCommitIDs[i],
NewOID: opts.NewCommitIDs[i],
Err: "The target branch already contains this commit",
})
continue
}
// Automatically fill out the title and the description from the first commit.
shouldGetCommit := len(title) == 0 || len(description) == 0