Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 4b6c28d

Browse files
Add filter condition to update less often
1 parent aaed14e commit 4b6c28d

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ lint.install:
4848
pip install -Iv ruff
4949

5050
lint.run:
51-
ruff check
51+
ruff check --fix
5252
ruff format
5353

5454
lint.check:

webhook_handlers/tests/test_github.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,12 @@ def test_push_updates_only_unmerged_commits_with_branch_name(self):
275275

276276
@patch("redis.Redis.sismember", lambda x, y, z: False)
277277
def test_push_updates_commit_on_default_branch(self):
278-
commit1 = CommitFactory(merged=False, repository=self.repo)
279-
commit2 = CommitFactory(merged=False, repository=self.repo)
278+
commit1 = CommitFactory(
279+
merged=False, repository=self.repo, branch="feature-branch"
280+
)
281+
commit2 = CommitFactory(
282+
merged=False, repository=self.repo, branch="feature-branch"
283+
)
280284

281285
merged_branch_name = "merged"
282286
repo_branch = self.repo.branch

webhook_handlers/tests/test_github_enterprise.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,12 @@ def test_push_updates_only_unmerged_commits_with_branch_name(self):
231231

232232
@patch("redis.Redis.sismember", lambda x, y, z: False)
233233
def test_push_updates_commit_on_default_branch(self):
234-
commit1 = CommitFactory(merged=False, repository=self.repo)
235-
commit2 = CommitFactory(merged=False, repository=self.repo)
234+
commit1 = CommitFactory(
235+
merged=False, repository=self.repo, branch="feature-branch"
236+
)
237+
commit2 = CommitFactory(
238+
merged=False, repository=self.repo, branch="feature-branch"
239+
)
236240

237241
merged_branch_name = "merged"
238242
repo_branch = self.repo.branch

webhook_handlers/views/github.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from hashlib import sha1, sha256
66
from typing import Optional
77

8+
from django.db.models import Q
89
from django.utils import timezone
910
from django.utils.crypto import constant_time_compare
1011
from rest_framework import status
@@ -239,24 +240,25 @@ def push(self, request, *args, **kwargs):
239240
)
240241
return Response(data=WebhookHandlerErrorMessages.SKIP_WEBHOOK_IGNORED)
241242

242-
branch_name = self.request.data.get("ref")[11:]
243+
pushed_to_branch_name = self.request.data.get("ref")[11:]
243244
commits = self.request.data.get("commits", [])
244245

245246
if not commits:
246247
log.debug(
247-
f"No commits in webhook payload for branch {branch_name}",
248+
f"No commits in webhook payload for branch {pushed_to_branch_name}",
248249
extra=dict(repoid=repo.repoid, github_webhook_event=self.event),
249250
)
250251
return Response()
251252

252253
commits_queryset = Commit.objects.filter(
254+
~Q(branch=pushed_to_branch_name),
253255
repository=repo,
254256
commitid__in=[commit.get("id") for commit in commits],
255257
merged=False,
256258
)
257-
commits_queryset.update(branch=branch_name)
258-
if branch_name == repo.branch:
259-
commits_queryset.update(merged=True)
259+
260+
if pushed_to_branch_name == repo.branch:
261+
commits_queryset.update(branch=pushed_to_branch_name, merged=True)
260262
log.info(
261263
"Pushed commits to default branch; setting merged to True",
262264
extra=dict(
@@ -265,9 +267,11 @@ def push(self, request, *args, **kwargs):
265267
commits=[commit.get("id") for commit in commits],
266268
),
267269
)
270+
else:
271+
commits_queryset.update(branch=pushed_to_branch_name)
268272

269273
log.info(
270-
f"Branch name updated for commits to {branch_name}",
274+
f"Branch name updated for commits to {pushed_to_branch_name}",
271275
extra=dict(
272276
repoid=repo.repoid,
273277
github_webhook_event=self.event,
@@ -300,7 +304,7 @@ def push(self, request, *args, **kwargs):
300304
TaskService().status_set_pending(
301305
repoid=repo.repoid,
302306
commitid=most_recent_commit.get("id"),
303-
branch=branch_name,
307+
branch=pushed_to_branch_name,
304308
on_a_pull_request=False,
305309
)
306310

0 commit comments

Comments
 (0)