Skip to content

Build: skip duplicated commands #10573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions readthedocs/api/v2/views/model_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,14 @@ def perform_create(self, serializer):
build_api_key = self.request.build_api_key
if not build_api_key.project.builds.filter(pk=build_pk).exists():
raise PermissionDenied()

if BuildCommandResult.objects.filter(
build=serializer.validated_data["build"],
start_time=serializer.validated_data["start_time"],
).exists():
log.warning("Build command is duplicated. Skipping...")
return

return super().perform_create(serializer)

def get_queryset_for_api_key(self, api_key):
Expand Down
42 changes: 42 additions & 0 deletions readthedocs/rtd_tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,48 @@ def test_build_read_and_write_endpoints_for_build_api_token(self):
resp = client.patch(f"/api/v2/build/{build.pk}/")
self.assertEqual(resp.status_code, 404)

def test_build_commands_duplicated_command(self):
"""Sending the same request twice should only create one BuildCommandResult."""
project = get(
Project,
language="en",
)
version = project.versions.first()
build = Build.objects.create(project=project, version=version)

self.assertEqual(BuildCommandResult.objects.count(), 0)

client = APIClient()
_, build_api_key = BuildAPIKey.objects.create_key(project)
client.credentials(HTTP_AUTHORIZATION=f"Token {build_api_key}")

now = timezone.now()
start_time = now - datetime.timedelta(seconds=5)
end_time = now

data = {
"build": build.pk,
"command": "git status",
"description": "Git status",
"exit_code": 0,
"start_time": start_time,
"end_time": end_time,
}

response = client.post(
"/api/v2/command/",
data,
format="json",
)
self.assertEqual(response.status_code, 201)
response = client.post(
"/api/v2/command/",
data,
format="json",
)
self.assertEqual(response.status_code, 201)
self.assertEqual(BuildCommandResult.objects.count(), 1)

def test_build_commands_read_only_endpoints_for_normal_user(self):
user_normal = get(User, is_staff=False)
user_admin = get(User, is_staff=True)
Expand Down