diff --git a/apps/codecov-api/webhook_handlers/constants.py b/apps/codecov-api/webhook_handlers/constants.py index 0f8a5a6014..054a4a86d1 100644 --- a/apps/codecov-api/webhook_handlers/constants.py +++ b/apps/codecov-api/webhook_handlers/constants.py @@ -23,6 +23,26 @@ class GitHubWebhookEvents: repository_events = [PULL_REQUEST, DELETE, PUSH, PUBLIC, STATUS, REPOSITORY, MEMBER] +class GitHubPullRequestActions: + CLOSED = "closed" + LABELED = "labeled" + OPENED = "opened" + READY_FOR_REVIEW = "ready_for_review" + REOPENED = "reopened" + SYNCHRONIZE = "synchronize" + + EDITED = "edited" + + PULLS_SYNC_ACTIONS = [ + CLOSED, + LABELED, + OPENED, + READY_FOR_REVIEW, + REOPENED, + SYNCHRONIZE, + ] + + class BitbucketHTTPHeaders: EVENT = "HTTP_X_EVENT_KEY" UUID = "HTTP_X_HOOK_UUID" diff --git a/apps/codecov-api/webhook_handlers/tests/test_github.py b/apps/codecov-api/webhook_handlers/tests/test_github.py index 563cf63e33..890853861a 100644 --- a/apps/codecov-api/webhook_handlers/tests/test_github.py +++ b/apps/codecov-api/webhook_handlers/tests/test_github.py @@ -509,6 +509,21 @@ def test_pull_request_triggers_pulls_sync_task_for_valid_actions( [call(repoid=self.repo.repoid, pullid=pull.pullid)] * len(valid_actions) ) + @patch("services.task.TaskService.pulls_sync") + def test_pull_request_ready_for_review_triggers_pulls_sync(self, pulls_sync_mock): + pull = PullFactory(repository=self.repo) + self._post_event_data( + event=GitHubWebhookEvents.PULL_REQUEST, + data={ + "repository": {"id": self.repo.service_id}, + "action": "ready_for_review", + "number": pull.pullid, + }, + ) + pulls_sync_mock.assert_called_once_with( + repoid=self.repo.repoid, pullid=pull.pullid + ) + def test_pull_request_updates_title_if_edited(self): pull = PullFactory(repository=self.repo) new_title = "brand new dang title" diff --git a/apps/codecov-api/webhook_handlers/views/github.py b/apps/codecov-api/webhook_handlers/views/github.py index e986fce2c8..a6cad2e46e 100644 --- a/apps/codecov-api/webhook_handlers/views/github.py +++ b/apps/codecov-api/webhook_handlers/views/github.py @@ -28,6 +28,7 @@ from utils.config import get_config from webhook_handlers.constants import ( GitHubHTTPHeaders, + GitHubPullRequestActions, GitHubWebhookEvents, WebhookHandlerErrorMessages, ) @@ -316,15 +317,17 @@ def pull_request(self, request, *args, **kwargs): if not repo.active: return Response(data=WebhookHandlerErrorMessages.SKIP_NOT_ACTIVE) - action, pullid = request.data.get("action"), request.data.get("number") - - if action in ["opened", "closed", "reopened", "synchronize", "labeled"]: - TaskService().pulls_sync(repoid=repo.repoid, pullid=pullid) + action = request.data.get("action") + pullid = request.data.get("number") - elif action == "edited": + if action == GitHubPullRequestActions.EDITED: Pull.objects.filter(repository=repo, pullid=pullid).update( title=request.data.get("pull_request", {}).get("title") ) + return Response() + + if action in GitHubPullRequestActions.PULLS_SYNC_ACTIONS: + TaskService().pulls_sync(repoid=repo.repoid, pullid=pullid) return Response()