Skip to content

Commit

Permalink
feat: update commit in github webhook
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joseph-sentry committed Nov 19, 2024
1 parent 4a2bb9b commit 4b99a6e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
54 changes: 47 additions & 7 deletions webhook_handlers/tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ def test_public_sets_repo_private_false_and_activated_false(self):
assert not self.repo.activated

@patch("redis.Redis.sismember", lambda x, y, z: False)
def test_push_updates_only_unmerged_commits_with_branch_name(self):
@patch("services.task.TaskService.update_commit")
def test_push_updates_only_unmerged_commits_with_branch_name(
self, update_commit_mock
):
commit1 = CommitFactory(merged=False, repository=self.repo)
commit2 = CommitFactory(merged=False, repository=self.repo)

Expand Down Expand Up @@ -273,8 +276,15 @@ def test_push_updates_only_unmerged_commits_with_branch_name(self):

assert merged_commit.branch == merged_branch_name

update_commit_mock.assert_has_calls(
[
call(repoid=self.repo.repoid, commitid=merged_commit.commitid),
]
)

@patch("redis.Redis.sismember", lambda x, y, z: False)
def test_push_updates_commit_on_default_branch(self):
@patch("services.task.TaskService.update_commit")
def test_push_updates_commit_on_default_branch(self, update_commit_mock):
commit1 = CommitFactory(merged=False, repository=self.repo)
commit2 = CommitFactory(merged=False, repository=self.repo)

Expand Down Expand Up @@ -309,7 +319,14 @@ def test_push_updates_commit_on_default_branch(self):

assert merged_commit.branch == merged_branch_name

def test_push_exits_early_with_200_if_repo_not_active(self):
update_commit_mock.assert_has_calls(
[
call(repoid=self.repo.repoid, commitid=merged_commit.commitid),
]
)

@patch("services.task.TaskService.update_commit")
def test_push_exits_early_with_200_if_repo_not_active(self, update_commit_mock):
self.repo.active = False
self.repo.save()
unmerged_commit = CommitFactory(repository=self.repo, merged=False)
Expand All @@ -331,8 +348,13 @@ def test_push_exits_early_with_200_if_repo_not_active(self):
unmerged_commit.refresh_from_db()
assert unmerged_commit.branch != branch_name

update_commit_mock.assert_not_called()

@patch("webhook_handlers.views.github.get_config")
def test_push_exits_early_with_200_if_repo_name_is_ignored(self, get_config_mock):
@patch("services.task.TaskService.update_commit")
def test_push_exits_early_with_200_if_repo_name_is_ignored(
self, update_commit_mock, get_config_mock
):
get_config_mock.side_effect = [WEBHOOK_SECRET.decode("utf-8"), [self.repo.name]]

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

assert unmerged_commit.branch != branch_name

update_commit_mock.assert_not_called()

@patch("redis.Redis.sismember", lambda x, y, z: True)
@patch("services.task.TaskService.status_set_pending")
@patch("services.task.TaskService.update_commit")
def test_push_triggers_set_pending_task_on_most_recent_commit(
self, set_pending_mock
self, update_commit_mock, set_pending_mock
):
commit1 = CommitFactory(merged=False, repository=self.repo)
commit2 = CommitFactory(merged=False, repository=self.repo)
Expand All @@ -384,10 +409,15 @@ def test_push_triggers_set_pending_task_on_most_recent_commit(
on_a_pull_request=False,
)

update_commit_mock.assert_called_once_with(
repoid=self.repo.repoid, commitid=commit2.commitid
)

@patch("redis.Redis.sismember", lambda x, y, z: False)
@patch("services.task.TaskService.status_set_pending")
@patch("services.task.TaskService.update_commit")
def test_push_doesnt_trigger_task_if_repo_not_part_of_beta_set(
self, set_pending_mock
self, update_commit_mock, set_pending_mock
):
commit1 = CommitFactory(merged=False, repository=self.repo)

Expand All @@ -401,10 +431,16 @@ def test_push_doesnt_trigger_task_if_repo_not_part_of_beta_set(
)

set_pending_mock.assert_not_called()
update_commit_mock.assert_called_once_with(
repoid=self.repo.repoid, commitid=commit1.commitid
)

@patch("redis.Redis.sismember", lambda x, y, z: True)
@patch("services.task.TaskService.status_set_pending")
def test_push_doesnt_trigger_task_if_ci_skipped(self, set_pending_mock):
@patch("services.task.TaskService.update_commit")
def test_push_doesnt_trigger_task_if_ci_skipped(
self, update_commit_mock, set_pending_mock
):
commit1 = CommitFactory(merged=False, repository=self.repo, message="[ci skip]")

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

update_commit_mock.assert_called_once_with(
repoid=self.repo.repoid, commitid=commit1.commitid
)

def test_status_exits_early_if_repo_not_active(self):
self.repo.active = False
self.repo.save()
Expand Down
8 changes: 7 additions & 1 deletion webhook_handlers/views/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import re
from contextlib import suppress
from hashlib import sha1, sha256
from typing import Optional
from typing import Any, Optional

from django.utils import timezone
from django.utils.crypto import constant_time_compare
from rest_framework import status
from rest_framework.exceptions import NotFound, PermissionDenied
from rest_framework.permissions import AllowAny
from rest_framework.request import Empty, Request
from rest_framework.response import Response
from rest_framework.views import APIView


from codecov_auth.models import (
GITHUB_APP_INSTALLATION_DEFAULT_NAME,
GithubAppInstallation,
Expand Down Expand Up @@ -277,6 +279,10 @@ def push(self, request, *args, **kwargs):

most_recent_commit = commits[-1]

TaskService().update_commit(
commitid=most_recent_commit.get("id"), repoid=repo.repoid
)

if regexp_ci_skip(most_recent_commit.get("message")):
log.info(
"CI skip tag on head commit, not setting status",
Expand Down

0 comments on commit 4b99a6e

Please sign in to comment.