feature: implements AI code review PR checkbox; adds AI PR settings

This commit is contained in:
ievgenii vdovenko 2026-01-21 11:59:03 +01:00
parent bf9cd8455f
commit c71db28d3f
9 changed files with 132 additions and 48 deletions

View file

@ -913,6 +913,9 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
self.assure_not_empty_repo()
source_repo = self.db_repo
app_settings = c.rc_config
c.ai_enabled = app_settings.get("rhodecode_ai_features_enabled", False)
commit_id = self.request.GET.get("commit")
branch_ref = self.request.GET.get("branch")
bookmark_ref = self.request.GET.get("bookmark")
@ -971,6 +974,9 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
# Get security scan setting for pre-checking the checkbox
c.pr_security_scan_enabled = self._get_repo_setting(source_repo, "rhodecode_pr_security_scan_enabled", False)
c.ai_pr_review = self._get_repo_setting(
source_repo, "rhodecode_ai_default_code_review", app_settings.get("rhodecode_ai_default_code_review", False)
)
return self._get_template_context(c)
@LoginRequired()
@ -1311,6 +1317,11 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
except ImportError:
log.warning("EE security audit tasks not available, skipping")
rc_settings = SettingsModel()
ai_enabled = rc_settings.get_setting_by_name("ai_features_enabled")
if ai_enabled and _form.get("run_ai_codereview", False):
self._try_start_ai_code_review(_, pull_request, add_existing_users=True)
Session().commit()
h.flash(_("Successfully opened new pull request"), category="success")
@ -1373,62 +1384,46 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
log.warning("Code review already finished.")
return False
admin_permissions = (
"repository.admin",
"hg.admin",
)
is_admin = h.HasRepoPermissionAny(*admin_permissions)(
user=self._rhodecode_user, repo_name=pull_request.target_repo.repo_name
)
ai_user = User.get_ai_user()
log.debug("Initiating code review for pull request id: %s", pull_request.pull_request_id)
if is_admin:
ai_user = User.get_ai_user()
log.debug("Initiating code review for pull request id: %s", pull_request.pull_request_id)
reviewers_data = []
reviewers_data = []
if add_existing_users:
current_reviewers = PullRequestReviewers.get_pull_request_reviewers(
pull_request.pull_request_id, role=PullRequestReviewers.ROLE_REVIEWER
)
for existing_reviewer in current_reviewers:
reviewers_data.append(
(
existing_reviewer.user_id,
existing_reviewer.reasons,
existing_reviewer.mandatory,
existing_reviewer.role,
existing_reviewer.rule_data,
)
if add_existing_users:
current_reviewers = PullRequestReviewers.get_pull_request_reviewers(
pull_request.pull_request_id, role=PullRequestReviewers.ROLE_REVIEWER
)
for existing_reviewer in current_reviewers:
reviewers_data.append(
(
existing_reviewer.user_id,
existing_reviewer.reasons,
existing_reviewer.mandatory,
existing_reviewer.role,
existing_reviewer.rule_data,
)
reviewers_data.append(
(
ai_user.user_id,
["adds system ai user for review"],
False,
PullRequestReviewers.ROLE_REVIEWER,
pull_request.reviewer_data,
)
reviewers_data.append(
(
ai_user.user_id,
["adds system ai user for review"],
False,
PullRequestReviewers.ROLE_REVIEWER,
pull_request.reviewer_data,
)
)
PullRequestModel().update_reviewers(pull_request, reviewers_data, self._rhodecode_db_user)
PullRequestModel().update_reviewers(pull_request, reviewers_data, self._rhodecode_db_user)
Session().commit()
Session().commit()
run_task(tasks.start_ai_code_review, pull_request.pull_request_id)
self._set_ai_pr_state_pending(pull_request)
run_task(tasks.start_ai_code_review, pull_request.pull_request_id)
self._set_ai_pr_state_pending(pull_request)
msg = _("AI code review has started. The review will be ready in about 13 minutes.")
h.flash(msg, category="success")
msg = _("AI code review has started. The review will be ready in about 13 minutes.")
h.flash(msg, category="success")
else:
log.debug("AI code review canceled, only admin can initiate AI code review.")
msg = _("AI code review is not available. This feature is in BETA and can only be initiated by an admin.")
h.flash(
msg,
category="warning",
)
return False
return True
@LoginRequired()