Skip to content

Commit

Permalink
feat: trigger label analysis task after update
Browse files Browse the repository at this point in the history
From codecov/worker#21 we have a different way of handling updates for `LabelAnalysisRequest` objects.
This is important because we want to use these objects to extract metrics from ATS.

However to actually have the updated values we need to trigger the task again.
These changes trigger the task again :3

closes codecov/engineering-team#456
  • Loading branch information
giovanni-guidini committed Sep 13, 2023
1 parent ec03560 commit 1715048
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions labelanalysis/tests/integration/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ def test_simple_label_analysis_get_does_not_exist(db, mocker):


def test_simple_label_analysis_put_labels(db, mocker):
mocked_task_service = mocker.patch.object(TaskService, "schedule_task")
commit = CommitFactory.create(repository__active=True)
StaticAnalysisSuiteFactory.create(commit=commit)
base_commit = CommitFactory.create(repository=commit.repository)
Expand Down Expand Up @@ -411,9 +412,15 @@ def test_simple_label_analysis_put_labels(db, mocker):
)
assert response.status_code == 200
assert response.json() == expected_response_json
mocked_task_service.assert_called_with(
label_analysis_task_name,
kwargs=dict(request_id=label_analysis.id),
apply_async_kwargs=dict(),
)


def test_simple_label_analysis_put_labels_wrong_base_return_404(db, mocker):
mocked_task_service = mocker.patch.object(TaskService, "schedule_task")
commit = CommitFactory.create(repository__active=True)
StaticAnalysisSuiteFactory.create(commit=commit)
base_commit = CommitFactory.create(repository=commit.repository)
Expand Down Expand Up @@ -455,3 +462,4 @@ def test_simple_label_analysis_put_labels_wrong_base_return_404(db, mocker):
},
)
assert response.status_code == 404
mocked_task_service.assert_not_called()
17 changes: 17 additions & 0 deletions labelanalysis/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ class LabelAnalysisRequestDetailView(RetrieveAPIView, UpdateAPIView):
# TODO Consider using a different permission scope
required_scopes = ["static_analysis"]

def patch(self, request, *args, **kwargs):
# This is called by the CLI to patch the request_labels information after it's collected
# First we let rest_framework validate and update the larq object
response = super().patch(request, *args, **kwargs)
if response.status_code == 200:
# IF the larq update was successful
# we trigger the task again for the same larq to update the result saved
# The result saved is what we use to get metrics
uid = self.kwargs.get("external_id")
larq = LabelAnalysisRequest.objects.get(external_id=uid)
TaskService().schedule_task(
label_analysis_task_name,
kwargs=dict(request_id=larq.id),
apply_async_kwargs=dict(),
)
return response

def get_object(self):
uid = self.kwargs.get("external_id")
try:
Expand Down

0 comments on commit 1715048

Please sign in to comment.