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

Commit 4b99a6e

Browse files
committed
feat: update commit in github webhook
when we receive news that a commit has been updated via a webhook we should queue up a task to sync it completely. i mostly just want the upsert pull & branch part of the commit update task but might as well make sure the commit is accurate
1 parent 4a2bb9b commit 4b99a6e

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

webhook_handlers/tests/test_github.py

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,10 @@ def test_public_sets_repo_private_false_and_activated_false(self):
238238
assert not self.repo.activated
239239

240240
@patch("redis.Redis.sismember", lambda x, y, z: False)
241-
def test_push_updates_only_unmerged_commits_with_branch_name(self):
241+
@patch("services.task.TaskService.update_commit")
242+
def test_push_updates_only_unmerged_commits_with_branch_name(
243+
self, update_commit_mock
244+
):
242245
commit1 = CommitFactory(merged=False, repository=self.repo)
243246
commit2 = CommitFactory(merged=False, repository=self.repo)
244247

@@ -273,8 +276,15 @@ def test_push_updates_only_unmerged_commits_with_branch_name(self):
273276

274277
assert merged_commit.branch == merged_branch_name
275278

279+
update_commit_mock.assert_has_calls(
280+
[
281+
call(repoid=self.repo.repoid, commitid=merged_commit.commitid),
282+
]
283+
)
284+
276285
@patch("redis.Redis.sismember", lambda x, y, z: False)
277-
def test_push_updates_commit_on_default_branch(self):
286+
@patch("services.task.TaskService.update_commit")
287+
def test_push_updates_commit_on_default_branch(self, update_commit_mock):
278288
commit1 = CommitFactory(merged=False, repository=self.repo)
279289
commit2 = CommitFactory(merged=False, repository=self.repo)
280290

@@ -309,7 +319,14 @@ def test_push_updates_commit_on_default_branch(self):
309319

310320
assert merged_commit.branch == merged_branch_name
311321

312-
def test_push_exits_early_with_200_if_repo_not_active(self):
322+
update_commit_mock.assert_has_calls(
323+
[
324+
call(repoid=self.repo.repoid, commitid=merged_commit.commitid),
325+
]
326+
)
327+
328+
@patch("services.task.TaskService.update_commit")
329+
def test_push_exits_early_with_200_if_repo_not_active(self, update_commit_mock):
313330
self.repo.active = False
314331
self.repo.save()
315332
unmerged_commit = CommitFactory(repository=self.repo, merged=False)
@@ -331,8 +348,13 @@ def test_push_exits_early_with_200_if_repo_not_active(self):
331348
unmerged_commit.refresh_from_db()
332349
assert unmerged_commit.branch != branch_name
333350

351+
update_commit_mock.assert_not_called()
352+
334353
@patch("webhook_handlers.views.github.get_config")
335-
def test_push_exits_early_with_200_if_repo_name_is_ignored(self, get_config_mock):
354+
@patch("services.task.TaskService.update_commit")
355+
def test_push_exits_early_with_200_if_repo_name_is_ignored(
356+
self, update_commit_mock, get_config_mock
357+
):
336358
get_config_mock.side_effect = [WEBHOOK_SECRET.decode("utf-8"), [self.repo.name]]
337359

338360
self.repo.save()
@@ -356,10 +378,13 @@ def test_push_exits_early_with_200_if_repo_name_is_ignored(self, get_config_mock
356378

357379
assert unmerged_commit.branch != branch_name
358380

381+
update_commit_mock.assert_not_called()
382+
359383
@patch("redis.Redis.sismember", lambda x, y, z: True)
360384
@patch("services.task.TaskService.status_set_pending")
385+
@patch("services.task.TaskService.update_commit")
361386
def test_push_triggers_set_pending_task_on_most_recent_commit(
362-
self, set_pending_mock
387+
self, update_commit_mock, set_pending_mock
363388
):
364389
commit1 = CommitFactory(merged=False, repository=self.repo)
365390
commit2 = CommitFactory(merged=False, repository=self.repo)
@@ -384,10 +409,15 @@ def test_push_triggers_set_pending_task_on_most_recent_commit(
384409
on_a_pull_request=False,
385410
)
386411

412+
update_commit_mock.assert_called_once_with(
413+
repoid=self.repo.repoid, commitid=commit2.commitid
414+
)
415+
387416
@patch("redis.Redis.sismember", lambda x, y, z: False)
388417
@patch("services.task.TaskService.status_set_pending")
418+
@patch("services.task.TaskService.update_commit")
389419
def test_push_doesnt_trigger_task_if_repo_not_part_of_beta_set(
390-
self, set_pending_mock
420+
self, update_commit_mock, set_pending_mock
391421
):
392422
commit1 = CommitFactory(merged=False, repository=self.repo)
393423

@@ -401,10 +431,16 @@ def test_push_doesnt_trigger_task_if_repo_not_part_of_beta_set(
401431
)
402432

403433
set_pending_mock.assert_not_called()
434+
update_commit_mock.assert_called_once_with(
435+
repoid=self.repo.repoid, commitid=commit1.commitid
436+
)
404437

405438
@patch("redis.Redis.sismember", lambda x, y, z: True)
406439
@patch("services.task.TaskService.status_set_pending")
407-
def test_push_doesnt_trigger_task_if_ci_skipped(self, set_pending_mock):
440+
@patch("services.task.TaskService.update_commit")
441+
def test_push_doesnt_trigger_task_if_ci_skipped(
442+
self, update_commit_mock, set_pending_mock
443+
):
408444
commit1 = CommitFactory(merged=False, repository=self.repo, message="[ci skip]")
409445

410446
response = self._post_event_data(
@@ -419,6 +455,10 @@ def test_push_doesnt_trigger_task_if_ci_skipped(self, set_pending_mock):
419455
assert response.data == "CI Skipped"
420456
set_pending_mock.assert_not_called()
421457

458+
update_commit_mock.assert_called_once_with(
459+
repoid=self.repo.repoid, commitid=commit1.commitid
460+
)
461+
422462
def test_status_exits_early_if_repo_not_active(self):
423463
self.repo.active = False
424464
self.repo.save()

webhook_handlers/views/github.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
import re
44
from contextlib import suppress
55
from hashlib import sha1, sha256
6-
from typing import Optional
6+
from typing import Any, Optional
77

88
from django.utils import timezone
99
from django.utils.crypto import constant_time_compare
1010
from rest_framework import status
1111
from rest_framework.exceptions import NotFound, PermissionDenied
1212
from rest_framework.permissions import AllowAny
13+
from rest_framework.request import Empty, Request
1314
from rest_framework.response import Response
1415
from rest_framework.views import APIView
1516

17+
1618
from codecov_auth.models import (
1719
GITHUB_APP_INSTALLATION_DEFAULT_NAME,
1820
GithubAppInstallation,
@@ -277,6 +279,10 @@ def push(self, request, *args, **kwargs):
277279

278280
most_recent_commit = commits[-1]
279281

282+
TaskService().update_commit(
283+
commitid=most_recent_commit.get("id"), repoid=repo.repoid
284+
)
285+
280286
if regexp_ci_skip(most_recent_commit.get("message")):
281287
log.info(
282288
"CI skip tag on head commit, not setting status",

0 commit comments

Comments
 (0)