core: ruff format
This commit is contained in:
parent
b452f0598e
commit
9780721347
6 changed files with 43 additions and 62 deletions
|
|
@ -79,8 +79,11 @@ class HomeView(BaseAppView, DataGridAppView):
|
|||
_users = UserModel().get_users(name_contains=query, only_active=active)
|
||||
|
||||
def maybe_skip_user(usr):
|
||||
if (skip_default_user and usr["username"] == UserModel.cls.DEFAULT_USER
|
||||
or usr["username"] == UserModel.cls.GHOST_USER):
|
||||
if (
|
||||
skip_default_user
|
||||
and usr["username"] == UserModel.cls.DEFAULT_USER
|
||||
or usr["username"] == UserModel.cls.GHOST_USER
|
||||
):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
|
|
|||
|
|
@ -17,34 +17,28 @@ class TestAiCodeReview:
|
|||
route_path(
|
||||
"pullrequest_start_ai_code_review",
|
||||
repo_name=pull_request.target_repo.repo_name,
|
||||
pull_request_id=pull_request.pull_request_id
|
||||
pull_request_id=pull_request.pull_request_id,
|
||||
)
|
||||
)
|
||||
|
||||
session = response.get_session_from_response()
|
||||
err_msg = session.peek_flash('error')
|
||||
err_msg = session.peek_flash("error")
|
||||
assert response.status_code == 200
|
||||
assert response.json_body == {'response': True, 'redirect_url': None}
|
||||
assert response.json_body == {"response": True, "redirect_url": None}
|
||||
assert len(err_msg) == 1
|
||||
assert err_msg[0] == "Cannot start review for closed pull requests."
|
||||
assert run_task_mock.call_count == 0
|
||||
|
||||
def test_ai_review_finished(self, backend, pr_util):
|
||||
pull_request = pr_util.create_pull_request()
|
||||
pull_request.ai_code_review_state = {
|
||||
"review_state": "finished"
|
||||
}
|
||||
pull_request.ai_code_review_state = {"review_state": "finished"}
|
||||
|
||||
repo_name = pull_request.target_repo.repo_name
|
||||
pull_request_id = pull_request.pull_request_id
|
||||
|
||||
with patch("rhodecode.apps.repository.views.repo_pull_requests.run_task") as run_task_mock:
|
||||
response = self.app.post(
|
||||
route_path(
|
||||
"pullrequest_start_ai_code_review",
|
||||
repo_name=repo_name,
|
||||
pull_request_id=pull_request_id
|
||||
)
|
||||
route_path("pullrequest_start_ai_code_review", repo_name=repo_name, pull_request_id=pull_request_id)
|
||||
)
|
||||
redirect_url = route_path(
|
||||
"pullrequest_show",
|
||||
|
|
@ -53,11 +47,11 @@ class TestAiCodeReview:
|
|||
)
|
||||
|
||||
session = response.get_session_from_response()
|
||||
err_msg = session.peek_flash('error')
|
||||
success_message = session.peek_flash('success')
|
||||
err_msg = session.peek_flash("error")
|
||||
success_message = session.peek_flash("success")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json_body == {'response': True, 'redirect_url': redirect_url}
|
||||
assert response.json_body == {"response": True, "redirect_url": redirect_url}
|
||||
assert len(err_msg) == 0
|
||||
assert len(success_message) == 0
|
||||
assert run_task_mock.call_count == 0
|
||||
|
|
@ -71,7 +65,7 @@ class TestAiCodeReview:
|
|||
route_path(
|
||||
"pullrequest_start_ai_code_review",
|
||||
repo_name=pull_request.target_repo.repo_name,
|
||||
pull_request_id=pr_id
|
||||
pull_request_id=pr_id,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
@ -82,7 +76,7 @@ class TestAiCodeReview:
|
|||
ai_user = User.get_ai_user()
|
||||
|
||||
session = response.get_session_from_response()
|
||||
success_message = session.peek_flash('success')
|
||||
success_message = session.peek_flash("success")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert run_task_mock.call_count == 1
|
||||
|
|
|
|||
|
|
@ -1318,9 +1318,7 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
|
|||
ai_state = deepcopy(pull_request.ai_code_review_state)
|
||||
|
||||
if not ai_state:
|
||||
ai_state = {
|
||||
"review_state": "pending"
|
||||
}
|
||||
ai_state = {"review_state": "pending"}
|
||||
else:
|
||||
ai_state["review_state"] = "pending"
|
||||
|
||||
|
|
@ -1335,7 +1333,7 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
|
|||
already_finished = ai_state.get("review_state") in ["pending", "finished"]
|
||||
|
||||
if already_finished:
|
||||
log.warning(f"Code review already finished.")
|
||||
log.warning("Code review already finished.")
|
||||
return False
|
||||
|
||||
admin_permissions = (
|
||||
|
|
@ -1373,13 +1371,11 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
|
|||
["adds system ai user for review"],
|
||||
False,
|
||||
PullRequestReviewers.ROLE_REVIEWER,
|
||||
pull_request.reviewer_data
|
||||
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()
|
||||
|
||||
|
|
@ -1390,9 +1386,7 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
|
|||
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."
|
||||
)
|
||||
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",
|
||||
|
|
|
|||
|
|
@ -144,8 +144,9 @@ def send_email(recipients, subject, body="", html_body="", email_config=None, ex
|
|||
|
||||
|
||||
@async_task(ignore_result=True, base=RequestContextAndUniqueByPayloadTask)
|
||||
def transform_legacy_email_config(email_config: dict[Any, Any] | Any, mail_server: Any | None) -> dict[
|
||||
str, None | int | bool | str | Any]:
|
||||
def transform_legacy_email_config(
|
||||
email_config: dict[Any, Any] | Any, mail_server: Any | None
|
||||
) -> dict[str, None | int | bool | str | Any]:
|
||||
return dict(
|
||||
host=mail_server,
|
||||
port=email_config.get("smtp_port", 25),
|
||||
|
|
@ -679,9 +680,7 @@ def _set_ai_pr_state(pull_request: PullRequest, state: str | None):
|
|||
ai_state = deepcopy(pull_request.ai_code_review_state)
|
||||
|
||||
if not ai_state:
|
||||
ai_state = {
|
||||
"review_state": state
|
||||
}
|
||||
ai_state = {"review_state": state}
|
||||
else:
|
||||
ai_state["review_state"] = state
|
||||
|
||||
|
|
|
|||
|
|
@ -21,11 +21,11 @@ from rhodecode.model.user import UserModel
|
|||
log = logging.getLogger(__name__)
|
||||
SENSITIVE_FIELDS = {"password", "api_key", "user_auth_tokens", "user_ssh_keys"}
|
||||
EMAIL_SUBJECT = "Action needed: inactive account scheduled for deletion"
|
||||
EMAIL_BODY_TEMPLATE = ('''Hi {first_name},
|
||||
EMAIL_BODY_TEMPLATE = """Hi {first_name},
|
||||
We haven’t seen any activity on your account for for at least {days_inactive} days.
|
||||
To keep your account, please sign in and use the service within the next {reactivate_days} days.
|
||||
If no action is taken, your account will be permanently deleted.
|
||||
Need help? https://rhodecode.com/support''')
|
||||
Need help? https://rhodecode.com/support"""
|
||||
|
||||
|
||||
INACTIVITY_DAYS = 183
|
||||
|
|
@ -35,7 +35,9 @@ DAYS_FOR_REACTIVATION = 30
|
|||
def find_inactive_users(inactive_days: int = INACTIVITY_DAYS):
|
||||
log.info(f"Find users who are inactive for {inactive_days} days")
|
||||
inactive_users = User.list_inactive(older_then_days=inactive_days)
|
||||
log.debug(f"Found {len(inactive_users)} inactive users. Inactive user names: {[u.username for u in inactive_users]}")
|
||||
log.debug(
|
||||
f"Found {len(inactive_users)} inactive users. Inactive user names: {[u.username for u in inactive_users]}"
|
||||
)
|
||||
has_emails = [u for u in inactive_users if u.email]
|
||||
no_emails = [u for u in inactive_users if not u.email]
|
||||
return has_emails, no_emails
|
||||
|
|
@ -62,7 +64,7 @@ def dump_users_to_csv(
|
|||
return None
|
||||
|
||||
if not all(isinstance(u, Base) for u in users):
|
||||
log.error(f"Provide SQLAlchemy model list")
|
||||
log.error("Provide SQLAlchemy model list")
|
||||
return None
|
||||
|
||||
if model is None:
|
||||
|
|
@ -110,11 +112,11 @@ def dump_users_to_csv(
|
|||
|
||||
|
||||
def batch_send_email_to_inactive_users(
|
||||
users: list[User],
|
||||
subject: str = EMAIL_SUBJECT,
|
||||
body_template: str = EMAIL_BODY_TEMPLATE,
|
||||
inactive_days: int = INACTIVITY_DAYS,
|
||||
days_for_reactivation: int = DAYS_FOR_REACTIVATION,
|
||||
users: list[User],
|
||||
subject: str = EMAIL_SUBJECT,
|
||||
body_template: str = EMAIL_BODY_TEMPLATE,
|
||||
inactive_days: int = INACTIVITY_DAYS,
|
||||
days_for_reactivation: int = DAYS_FOR_REACTIVATION,
|
||||
) -> tuple[list[str], list[str]]:
|
||||
emails_sent = []
|
||||
emails_failed = []
|
||||
|
|
@ -140,9 +142,7 @@ def batch_send_email_to_inactive_users(
|
|||
prefix = f"{conf_prefix} " if conf_prefix else ""
|
||||
subject = f"{prefix}{subject}"
|
||||
transformed_email_confing = transform_legacy_email_config(email_config, mail_server)
|
||||
extra_headers = {
|
||||
"Date": formatdate(time.time())
|
||||
}
|
||||
extra_headers = {"Date": formatdate(time.time())}
|
||||
|
||||
mailer = get_mailer(transformed_email_confing, email_config)
|
||||
|
||||
|
|
@ -154,9 +154,7 @@ def batch_send_email_to_inactive_users(
|
|||
try:
|
||||
log.debug(f"Sending email to {u.email}")
|
||||
body = body_template.format(
|
||||
first_name=u.first_name,
|
||||
days_inactive=inactive_days,
|
||||
reactivate_days=days_for_reactivation
|
||||
first_name=u.first_name, days_inactive=inactive_days, reactivate_days=days_for_reactivation
|
||||
)
|
||||
|
||||
message = Message(
|
||||
|
|
|
|||
|
|
@ -726,17 +726,12 @@ class User(Base, BaseModel):
|
|||
|
||||
cutoff_date = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=older_then_days)
|
||||
|
||||
last_activity_long_ago_or_no_activity_since_creation = (
|
||||
(cls.last_activity < cutoff_date) | ((cls.last_activity == None) & (cls.created_on < cutoff_date))
|
||||
last_activity_long_ago_or_no_activity_since_creation = (cls.last_activity < cutoff_date) | (
|
||||
(cls.last_activity == null()) & (cls.created_on < cutoff_date)
|
||||
)
|
||||
exclude_system_users = ~cls.username.in_([
|
||||
cls.GHOST_USER, cls.AI_USER, cls.DEFAULT_USER
|
||||
])
|
||||
exclude_system_users = ~cls.username.in_([cls.GHOST_USER, cls.AI_USER, cls.DEFAULT_USER])
|
||||
|
||||
query = query.filter(
|
||||
exclude_system_users,
|
||||
last_activity_long_ago_or_no_activity_since_creation
|
||||
)
|
||||
query = query.filter(exclude_system_users, last_activity_long_ago_or_no_activity_since_creation)
|
||||
|
||||
return query.all()
|
||||
|
||||
|
|
@ -4505,10 +4500,8 @@ class _PullRequestBase(BaseModel):
|
|||
|
||||
ai_code_review_state = Column(
|
||||
"ai_code_review_state",
|
||||
MutationObj.as_mutable(
|
||||
JsonType(dialect_map=dict(mysql=UnicodeText(DEFAULT_JSON_OBJ_SIZE)))
|
||||
),
|
||||
default=dict
|
||||
MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=UnicodeText(DEFAULT_JSON_OBJ_SIZE)))),
|
||||
default=dict,
|
||||
)
|
||||
|
||||
@property
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue