Skip to content

Commit

Permalink
Add filter condition to update less often (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-codecov authored Jan 9, 2025
1 parent 902a5cb commit 8660eab
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 40 deletions.
2 changes: 1 addition & 1 deletion api/internal/owner/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def validate_value(self, value):
extra=dict(owner_id=current_owner.pk, plan=value),
)
raise serializers.ValidationError(
f"Invalid value for plan: {value}; " f"must be one of {plan_values}"
f"Invalid value for plan: {value}; must be one of {plan_values}"
)
return value

Expand Down
4 changes: 3 additions & 1 deletion codecov_auth/commands/owner/interactors/set_yaml_on_owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def convert_yaml_to_dict(self, yaml_input: str) -> Optional[dict]:
except yaml.scanner.ScannerError as e:
line = e.problem_mark.line
column = e.problem_mark.column
message = f"Syntax error at line {line+1}, column {column+1}: {e.problem}"
message = (
f"Syntax error at line {line + 1}, column {column + 1}: {e.problem}"
)
raise ValidationError(message)
if not yaml_dict:
return None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async def test_user_is_part_of_org_and_yaml_is_good(self):
"codecov": {
"require_ci_to_pass": True,
},
"to_string": "\n" "codecov:\n" " require_ci_to_pass: yes\n",
"to_string": "\ncodecov:\n require_ci_to_pass: yes\n",
}

async def test_user_is_part_of_org_and_yaml_has_quotes(self):
Expand All @@ -109,7 +109,7 @@ async def test_user_is_part_of_org_and_yaml_has_quotes(self):
"codecov": {
"bot": "codecov",
},
"to_string": "\n" "codecov:\n" " bot: 'codecov'\n",
"to_string": "\ncodecov:\n bot: 'codecov'\n",
}

async def test_user_is_part_of_org_and_yaml_is_empty(self):
Expand Down
2 changes: 1 addition & 1 deletion services/tests/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ def _report_lines(self, hits):
]

def _src(self, n):
return [f"line{i+1}" for i in range(n)]
return [f"line{i + 1}" for i in range(n)]

def setUp(self):
self.file_comparison = FileComparison(
Expand Down
2 changes: 1 addition & 1 deletion upload/tokenless/appveyor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def verify(self):
self.upload_params.get("job")
if "/" in self.upload_params.get("job")
else (
f'{self.upload_params.get("owner")}/{self.upload_params.get("repo")}/{self.upload_params.get("job")}'
f"{self.upload_params.get('owner')}/{self.upload_params.get('repo')}/{self.upload_params.get('job')}"
)
)

Expand Down
2 changes: 1 addition & 1 deletion upload/tokenless/cirrus.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_build(self):
}}
}}",
"variables": {{
"buildId": {self.upload_params.get('build')}
"buildId": {self.upload_params.get("build")}
}}
}}"""

Expand Down
8 changes: 4 additions & 4 deletions utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def app(self):
migrate_to = None

def setUp(self):
assert (
self.migrate_from and self.migrate_to
), "TestCase '{}' must define migrate_from and migrate_to properties".format(
type(self).__name__
assert self.migrate_from and self.migrate_to, (
"TestCase '{}' must define migrate_from and migrate_to properties".format(
type(self).__name__
)
)
self.migrate_from = [(self.app, self.migrate_from)]
self.migrate_to = [(self.app, self.migrate_to)]
Expand Down
10 changes: 6 additions & 4 deletions webhook_handlers/tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,19 @@ def test_push_updates_only_unmerged_commits_with_branch_name(self):
commit2.refresh_from_db()
merged_commit.refresh_from_db()

assert commit1.branch == unmerged_branch_name
assert commit2.branch == unmerged_branch_name
assert not commit1.merged
assert not commit2.merged

assert merged_commit.branch == merged_branch_name

@patch("redis.Redis.sismember", lambda x, y, z: False)
def test_push_updates_commit_on_default_branch(self):
commit1 = CommitFactory(merged=False, repository=self.repo)
commit2 = CommitFactory(merged=False, repository=self.repo)
commit1 = CommitFactory(
merged=False, repository=self.repo, branch="feature-branch"
)
commit2 = CommitFactory(
merged=False, repository=self.repo, branch="feature-branch"
)

merged_branch_name = "merged"
repo_branch = self.repo.branch
Expand Down
10 changes: 6 additions & 4 deletions webhook_handlers/tests/test_github_enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,19 @@ def test_push_updates_only_unmerged_commits_with_branch_name(self):
commit2.refresh_from_db()
merged_commit.refresh_from_db()

assert commit1.branch == unmerged_branch_name
assert commit2.branch == unmerged_branch_name
assert not commit1.merged
assert not commit2.merged

assert merged_commit.branch == merged_branch_name

@patch("redis.Redis.sismember", lambda x, y, z: False)
def test_push_updates_commit_on_default_branch(self):
commit1 = CommitFactory(merged=False, repository=self.repo)
commit2 = CommitFactory(merged=False, repository=self.repo)
commit1 = CommitFactory(
merged=False, repository=self.repo, branch="feature-branch"
)
commit2 = CommitFactory(
merged=False, repository=self.repo, branch="feature-branch"
)

merged_branch_name = "merged"
repo_branch = self.repo.branch
Expand Down
34 changes: 13 additions & 21 deletions webhook_handlers/views/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from hashlib import sha1, sha256
from typing import Optional

from django.db.models import Q
from django.utils import timezone
from django.utils.crypto import constant_time_compare
from rest_framework import status
Expand Down Expand Up @@ -239,42 +240,33 @@ def push(self, request, *args, **kwargs):
)
return Response(data=WebhookHandlerErrorMessages.SKIP_WEBHOOK_IGNORED)

branch_name = self.request.data.get("ref")[11:]
pushed_to_branch_name = self.request.data.get("ref")[11:]
commits = self.request.data.get("commits", [])

if not commits:
log.debug(
f"No commits in webhook payload for branch {branch_name}",
f"No commits in webhook payload for branch {pushed_to_branch_name}",
extra=dict(repoid=repo.repoid, github_webhook_event=self.event),
)
return Response()

commits_queryset = Commit.objects.filter(
repository=repo,
commitid__in=[commit.get("id") for commit in commits],
merged=False,
)
commits_queryset.update(branch=branch_name)
if branch_name == repo.branch:
commits_queryset.update(merged=True)
if pushed_to_branch_name == repo.branch:
commits_queryset = Commit.objects.filter(
~Q(branch=pushed_to_branch_name),
repository=repo,
commitid__in=[commit.get("id") for commit in commits],
merged=False,
)
commits_queryset.update(branch=pushed_to_branch_name, merged=True)
log.info(
"Pushed commits to default branch; setting merged to True",
f"Branch name updated for commits to {pushed_to_branch_name}; setting merged to True",
extra=dict(
repoid=repo.repoid,
github_webhook_event=self.event,
commits=[commit.get("id") for commit in commits],
),
)

log.info(
f"Branch name updated for commits to {branch_name}",
extra=dict(
repoid=repo.repoid,
github_webhook_event=self.event,
commits=[commit.get("id") for commit in commits],
),
)

most_recent_commit = commits[-1]

if regexp_ci_skip(most_recent_commit.get("message")):
Expand All @@ -300,7 +292,7 @@ def push(self, request, *args, **kwargs):
TaskService().status_set_pending(
repoid=repo.repoid,
commitid=most_recent_commit.get("id"),
branch=branch_name,
branch=pushed_to_branch_name,
on_a_pull_request=False,
)

Expand Down

0 comments on commit 8660eab

Please sign in to comment.