feat: add repo setting to not pre-populate target branch in PR

This commit is contained in:
Andrii V 2025-08-01 16:35:21 +02:00
parent 58fce87f39
commit c1c982956a
6 changed files with 39 additions and 5 deletions

View file

@ -907,6 +907,7 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
branch=branch_ref,
bookmark=bookmark_ref,
translator=self.request.translate,
prepopulate_branch=True, # Always pre-populate for source repo
)
except CommitDoesNotExistError as e:
log.exception(e)
@ -921,7 +922,14 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
# change default if we have a parent repo
default_target_repo = source_repo.parent
target_repo_data = PullRequestModel().generate_repo_data(default_target_repo, translator=self.request.translate)
# Get the target branch pre-population setting
prepopulate_target_branch = self._get_repo_setting(
default_target_repo, "rhodecode_pr_target_branch_prepopulate", True
)
target_repo_data = PullRequestModel().generate_repo_data(
default_target_repo, translator=self.request.translate, prepopulate_branch=prepopulate_target_branch
)
selected_source_ref = source_repo_data["refs"]["selected_ref"]
title_source_ref = ""
@ -936,6 +944,7 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
"source_refs_json": ext_json.str_json(source_repo_data),
"target_repo_name": default_target_repo.repo_name,
"target_refs_json": ext_json.str_json(target_repo_data),
"pr_target_branch_prepopulate": prepopulate_target_branch,
}
c.default_source_ref = selected_source_ref
@ -958,7 +967,12 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
if not target_perm:
raise HTTPNotFound()
return PullRequestModel().generate_repo_data(repo, translator=self.request.translate)
# Get the target branch pre-population setting for the target repo
prepopulate_target_branch = self._get_repo_setting(repo, "pr_target_branch_prepopulate", True)
return PullRequestModel().generate_repo_data(
repo, translator=self.request.translate, prepopulate_branch=prepopulate_target_branch
)
@LoginRequired()
@NotAnonymous()

View file

@ -453,6 +453,9 @@ class _BaseVcsSettingsForm(formencode.Schema):
# cache
rhodecode_diff_cache = v.StringBoolean(if_missing=False)
# Pull Request settings
pr_target_branch_prepopulate = v.StringBoolean(if_missing=True)
def ApplicationUiSettingsForm(localizer):
_ = localizer

View file

@ -2053,13 +2053,19 @@ class PullRequestModel(BaseModel):
workspace_id = "pr-%s" % pull_request.pull_request_id
return workspace_id
def generate_repo_data(self, repo, commit_id=None, branch=None, bookmark=None, translator=None):
def generate_repo_data(
self, repo, commit_id=None, branch=None, bookmark=None, translator=None, prepopulate_branch=True
):
from rhodecode.model.repo import RepoModel
all_refs, selected_ref = self._get_repo_pullrequest_sources(
repo.scm_instance(), commit_id=commit_id, branch=branch, bookmark=bookmark, translator=translator
)
# If branch pre-population is disabled, don't auto-select any branch
if not prepopulate_branch:
selected_ref = None
refs_select2 = []
for element in all_refs:
children = [{"id": x[0], "text": x[1]} for x in element[0]]

View file

@ -457,6 +457,7 @@ class VcsSettingsModel(object):
"git_close_branch_before_merging",
"git_merge_strategy_selector",
"diff_cache",
"pr_target_branch_prepopulate",
)
HOOKS_SETTINGS = (

View file

@ -234,6 +234,14 @@
<div class="label">
<span class="help-block">${_('During the update of a pull request, the position of inline comments will be updated and outdated inline comments will be hidden.')}</span>
</div>
<div class="checkbox">
${h.checkbox('pr_target_branch_prepopulate' + suffix, 'True', **kwargs)}
<label for="pr_target_branch_prepopulate${suffix}">${_('Pre-populate target branch when creating pull requests')}</label>
</div>
<div class="label">
<span class="help-block">${_('When enabled, the target branch field will be automatically populated when creating pull requests. Disable this to prevent expensive diff calculations on repositories with many divergent branches.')}</span>
</div>
</div>
</div>
% endif

View file

@ -245,6 +245,7 @@
var defaultSourceRepoData = ${c.default_repo_data['source_refs_json']|n};
var defaultTargetRepo = '${c.default_repo_data['target_repo_name']}';
var defaultTargetRepoData = ${c.default_repo_data['target_refs_json']|n};
var prTargetBranchPrepopulate = ${'true' if c.default_repo_data['pr_target_branch_prepopulate'] else 'false'};
var $pullRequestForm = $('#pull_request_form');
var $pullRequestSubmit = $('#pr_submit', $pullRequestForm);
@ -530,8 +531,9 @@
$('#switch_base').html("<a class=\"tooltip\" title=\"{0}\" href=\"{1}\">Switch sides</a>".format(title, prLink))
// generate dynamic select2 for refs.
initTargetRefs(repoData['refs']['select2_refs'],
repoData['refs']['selected_ref']);
// Only pre-populate target branch if the setting allows it
var selectedRef = prTargetBranchPrepopulate ? repoData['refs']['selected_ref'] : null;
initTargetRefs(repoData['refs']['select2_refs'], selectedRef);
};