Skip to content

Commit

Permalink
Return number of successful/failed resends from /resend-pending-builds
Browse files Browse the repository at this point in the history
  • Loading branch information
sersorrel committed Feb 21, 2024
1 parent eb062a3 commit af63546
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
32 changes: 27 additions & 5 deletions softpack_core/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import typer
import uvicorn
from fastapi import APIRouter, Request, UploadFile
from fastapi import APIRouter, Request, Response, UploadFile
from typer import Typer
from typing_extensions import Annotated

Expand All @@ -19,6 +19,7 @@
CreateEnvironmentSuccess,
Environment,
EnvironmentInput,
PackageInput,
WriteArtifactSuccess,
)

Expand Down Expand Up @@ -96,11 +97,32 @@ async def upload_artifacts( # type: ignore[no-untyped-def]

@staticmethod
@router.post("/resend-pending-builds")
async def resend_pending_builds(): # type: ignore[no-untyped-def]
async def resend_pending_builds( # type: ignore[no-untyped-def]
response: Response,
):
"""Resubmit any pending builds to the builder."""
successes = 0
failures = 0

Check warning on line 105 in softpack_core/service.py

View check run for this annotation

Codecov / codecov/patch

softpack_core/service.py#L104-L105

Added lines #L104 - L105 were not covered by tests
for env in Environment.iter():
if env.state != State.queued:
continue

Check warning on line 108 in softpack_core/service.py

View check run for this annotation

Codecov / codecov/patch

softpack_core/service.py#L108

Added line #L108 was not covered by tests
Environment.submit_env_to_builder(env)

return {"message": "Successfully triggered resend"}
result = Environment.submit_env_to_builder(
EnvironmentInput(
name=env.name,
path=env.path,
description=env.description,
packages=[PackageInput(**vars(p)) for p in env.packages],
)
)
if result is None:
successes += 1

Check warning on line 118 in softpack_core/service.py

View check run for this annotation

Codecov / codecov/patch

softpack_core/service.py#L118

Added line #L118 was not covered by tests
else:
failures += 1

Check warning on line 120 in softpack_core/service.py

View check run for this annotation

Codecov / codecov/patch

softpack_core/service.py#L120

Added line #L120 was not covered by tests

if failures == 0:
message = "Successfully triggered resends"

Check warning on line 123 in softpack_core/service.py

View check run for this annotation

Codecov / codecov/patch

softpack_core/service.py#L123

Added line #L123 was not covered by tests
else:
response.status_code = 500
message = "Failed to trigger all resends"

Check warning on line 126 in softpack_core/service.py

View check run for this annotation

Codecov / codecov/patch

softpack_core/service.py#L125-L126

Added lines #L125 - L126 were not covered by tests

return {"message": message, "successes": successes, "failures": failures}

Check warning on line 128 in softpack_core/service.py

View check run for this annotation

Codecov / codecov/patch

softpack_core/service.py#L128

Added line #L128 was not covered by tests
15 changes: 14 additions & 1 deletion tests/integration/test_resend_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
def test_resend_pending_builds(
httpx_post, testable_env_input: EnvironmentInput
):
Environment.delete("test_environment", "users/test_user")
Environment.delete("test_environment", "groups/test_group")
ServiceAPI.register()
client = TestClient(app.router)

Expand All @@ -40,7 +42,18 @@ def test_resend_pending_builds(
url="/resend-pending-builds",
)
assert resp.status_code == 200
assert resp.json().get("message") == "Successfully triggered resend"
assert resp.json().get("message") == "Successfully triggered resends"
assert resp.json().get("successes") == 1
assert resp.json().get("failures") == 0

httpx_post.assert_called_once()
builder_called_correctly(httpx_post, testable_env_input)

httpx_post.side_effect = Exception('could not contact builder')
resp = client.post(
url="/resend-pending-builds",
)
assert resp.status_code == 500
assert resp.json().get("message") == "Failed to trigger all resends"
assert resp.json().get("successes") == 0
assert resp.json().get("failures") == 1

0 comments on commit af63546

Please sign in to comment.