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

Commit e7f22be

Browse files
authored
Merge branch 'main' into jan_09_mypy
2 parents baf4a5e + 8660eab commit e7f22be

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

webhook_handlers/tests/test_github.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,19 @@ def test_push_updates_only_unmerged_commits_with_branch_name(self):
266266
commit2.refresh_from_db()
267267
merged_commit.refresh_from_db()
268268

269-
assert commit1.branch == unmerged_branch_name
270-
assert commit2.branch == unmerged_branch_name
271269
assert not commit1.merged
272270
assert not commit2.merged
273271

274272
assert merged_commit.branch == merged_branch_name
275273

276274
@patch("redis.Redis.sismember", lambda x, y, z: False)
277275
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)
276+
commit1 = CommitFactory(
277+
merged=False, repository=self.repo, branch="feature-branch"
278+
)
279+
commit2 = CommitFactory(
280+
merged=False, repository=self.repo, branch="feature-branch"
281+
)
280282

281283
merged_branch_name = "merged"
282284
repo_branch = self.repo.branch

webhook_handlers/tests/test_github_enterprise.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,19 @@ def test_push_updates_only_unmerged_commits_with_branch_name(self):
222222
commit2.refresh_from_db()
223223
merged_commit.refresh_from_db()
224224

225-
assert commit1.branch == unmerged_branch_name
226-
assert commit2.branch == unmerged_branch_name
227225
assert not commit1.merged
228226
assert not commit2.merged
229227

230228
assert merged_commit.branch == merged_branch_name
231229

232230
@patch("redis.Redis.sismember", lambda x, y, z: False)
233231
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)
232+
commit1 = CommitFactory(
233+
merged=False, repository=self.repo, branch="feature-branch"
234+
)
235+
commit2 = CommitFactory(
236+
merged=False, repository=self.repo, branch="feature-branch"
237+
)
236238

237239
merged_branch_name = "merged"
238240
repo_branch = self.repo.branch

webhook_handlers/views/github.py

Lines changed: 13 additions & 21 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,42 +240,33 @@ 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

252-
commits_queryset = Commit.objects.filter(
253-
repository=repo,
254-
commitid__in=[commit.get("id") for commit in commits],
255-
merged=False,
256-
)
257-
commits_queryset.update(branch=branch_name)
258-
if branch_name == repo.branch:
259-
commits_queryset.update(merged=True)
253+
if pushed_to_branch_name == repo.branch:
254+
commits_queryset = Commit.objects.filter(
255+
~Q(branch=pushed_to_branch_name),
256+
repository=repo,
257+
commitid__in=[commit.get("id") for commit in commits],
258+
merged=False,
259+
)
260+
commits_queryset.update(branch=pushed_to_branch_name, merged=True)
260261
log.info(
261-
"Pushed commits to default branch; setting merged to True",
262+
f"Branch name updated for commits to {pushed_to_branch_name}; setting merged to True",
262263
extra=dict(
263264
repoid=repo.repoid,
264265
github_webhook_event=self.event,
265266
commits=[commit.get("id") for commit in commits],
266267
),
267268
)
268269

269-
log.info(
270-
f"Branch name updated for commits to {branch_name}",
271-
extra=dict(
272-
repoid=repo.repoid,
273-
github_webhook_event=self.event,
274-
commits=[commit.get("id") for commit in commits],
275-
),
276-
)
277-
278270
most_recent_commit = commits[-1]
279271

280272
if regexp_ci_skip(most_recent_commit.get("message")):
@@ -300,7 +292,7 @@ def push(self, request, *args, **kwargs):
300292
TaskService().status_set_pending(
301293
repoid=repo.repoid,
302294
commitid=most_recent_commit.get("id"),
303-
branch=branch_name,
295+
branch=pushed_to_branch_name,
304296
on_a_pull_request=False,
305297
)
306298

0 commit comments

Comments
 (0)