Skip to content

Commit

Permalink
Remove payload key (#19)
Browse files Browse the repository at this point in the history
* remove payload key

* fix integration test
  • Loading branch information
cbartz authored Aug 7, 2024
1 parent d17ba8e commit cc4bcf2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 114 deletions.
24 changes: 11 additions & 13 deletions tests/integration/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ async def test_forward_webhook( # pylint: disable=too-many-locals
expected_jobs_by_flavour = {
flavour: [
Job(
status=payload["payload"]["action"],
run_url=payload["payload"]["workflow_job"]["run_url"],
labels=payload["payload"]["workflow_job"]["labels"],
status=payload["action"],
run_url=payload["workflow_job"]["run_url"],
labels=payload["workflow_job"]["labels"],
)
for payload in payloads_by_flavour[flavour]
]
Expand Down Expand Up @@ -228,16 +228,14 @@ def _create_valid_data(action: str, labels: list[str]) -> dict:
# we are not using random.randint here for cryptographic purposes
_id = random.randint(1, 10000) # nosec
return {
"payload": {
"action": action,
"workflow_job": {
"id": _id,
"run_id": 987654321,
"status": "completed",
"conclusion": "success",
"labels": labels,
"run_url": f"https://api.github.com/repos/f/actions/runs/{_id}",
},
"action": action,
"workflow_job": {
"id": _id,
"run_id": 987654321,
"status": "completed",
"conclusion": "success",
"labels": labels,
"run_url": f"https://api.github.com/repos/f/actions/runs/{_id}",
},
}

Expand Down
23 changes: 10 additions & 13 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ def test_webhook_logs(
"""
data = _create_valid_data(JobStatus.QUEUED)
expected_job = Job(
labels=data["payload"]["workflow_job"]["labels"],
labels=data["workflow_job"]["labels"],
status=JobStatus.QUEUED,
run_url=data["payload"]["workflow_job"]["run_url"],
run_url=data["workflow_job"]["run_url"],
)
response = client.post(
TEST_PATH,
Expand Down Expand Up @@ -386,16 +386,13 @@ def _create_valid_data(action: str) -> dict:
A valid payload for the supported event.
"""
return {
"event": app_module.SUPPORTED_GITHUB_EVENT,
"payload": {
"action": action,
"workflow_job": {
"id": 123456789,
"run_id": 987654321,
"status": "completed",
"conclusion": "success",
"labels": ["self-hosted", "linux", "arm64"],
"run_url": "https://api.github.com/repos/f/actions/runs/8200803099",
},
"action": action,
"workflow_job": {
"id": 123456789,
"run_id": 987654321,
"status": "completed",
"conclusion": "success",
"labels": ["self-hosted", "linux", "arm64"],
"run_url": "https://api.github.com/repos/f/actions/runs/8200803099",
},
}
105 changes: 38 additions & 67 deletions tests/unit/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,31 @@ def test_webhook_to_job(labels: list[str], status: JobStatus):
assert: The payload is translated.
"""
payload = {
"event": "workflow_job",
"payload": {
"action": status,
"workflow_job": {
"id": 22428484402,
"run_id": 8200803099,
"workflow_name": "Push Event Tests",
"head_branch": "github-hosted",
"run_url": FAKE_RUN_URL,
"run_attempt": 5,
"node_id": "CR_kwDOKQMbDc8AAAAFONeDMg",
"head_sha": "fc670c970f0c5e156a94d1935776d7ed43728067",
"url": "https://api.github.com/repos/f/actions/jobs/22428484402",
"html_url": "https://github.com/f/actions/runs/8200803099/job/22428484402",
"status": "queued",
"conclusion": None,
"created_at": "2024-03-08T08:46:26Z",
"started_at": "2024-03-08T08:46:26Z",
"completed_at": None,
"name": "push-event-tests",
"steps": [],
"check_run_url": "https://api.github.com/repos/f/check-runs/22428484402",
"labels": labels,
"runner_id": None,
"runner_name": None,
"runner_group_id": None,
"runner_group_name": None,
},
"action": status,
"workflow_job": {
"id": 22428484402,
"run_id": 8200803099,
"workflow_name": "Push Event Tests",
"head_branch": "github-hosted",
"run_url": FAKE_RUN_URL,
"run_attempt": 5,
"node_id": "CR_kwDOKQMbDc8AAAAFONeDMg",
"head_sha": "fc670c970f0c5e156a94d1935776d7ed43728067",
"url": "https://api.github.com/repos/f/actions/jobs/22428484402",
"html_url": "https://github.com/f/actions/runs/8200803099/job/22428484402",
"status": "queued",
"conclusion": None,
"created_at": "2024-03-08T08:46:26Z",
"started_at": "2024-03-08T08:46:26Z",
"completed_at": None,
"name": "push-event-tests",
"steps": [],
"check_run_url": "https://api.github.com/repos/f/check-runs/22428484402",
"labels": labels,
"runner_id": None,
"runner_name": None,
"runner_group_id": None,
"runner_group_name": None,
},
}

Expand Down Expand Up @@ -87,15 +84,12 @@ def test_webhook_invalid_values(labels: list[str], status: JobStatus, run_url: s
assert: A ParseError is raised.
"""
payload = {
"event": "workflow_job",
"payload": {
"action": status,
"workflow_job": {"id": 22428484402, "run_url": run_url, "labels": labels},
},
"action": status,
"workflow_job": {"id": 22428484402, "run_url": run_url, "labels": labels},
}
with pytest.raises(ParseError) as exc_info:
webhook_to_job(payload)
assert "Failed to create Webhook object for payload " in str(exc_info.value)
assert "Failed to create Webhook object for webhook " in str(exc_info.value)


def test_webhook_workflow_job_not_dict():
Expand All @@ -105,11 +99,8 @@ def test_webhook_workflow_job_not_dict():
assert: A ParseError is raised.
"""
payload = {
"event": "workflow_job",
"payload": {
"action": "queued",
"workflow_job": "not a dict",
},
"action": "queued",
"workflow_job": "not a dict",
}
with pytest.raises(ParseError) as exc_info:
webhook_to_job(payload)
Expand All @@ -124,19 +115,8 @@ def test_webhook_missing_keys():
"""
payload: dict

# payload key missing
payload = {
"event": "workflow_job",
"action": "queued",
"workflow_job": {"id": 22428484402, "run_url": FAKE_RUN_URL, "labels": FAKE_LABELS},
}
with pytest.raises(ParseError) as exc_info:
webhook_to_job(payload)
assert f"payload key not found in {payload}" in str(exc_info.value)

# action key is missing
payload = {
"event": "workflow_job",
"payload": {
"workflow_job": {"id": 22428484402, "run_url": FAKE_RUN_URL, "labels": FAKE_LABELS},
},
Expand All @@ -146,37 +126,28 @@ def test_webhook_missing_keys():
assert f"action key not found in {payload}" in str(exc_info.value)
# workflow_job key missing
payload = {
"event": "workflow_job",
"payload": {
"action": "queued",
"id": 22428484402,
"run_url": FAKE_RUN_URL,
"labels": FAKE_LABELS,
},
"action": "queued",
"id": 22428484402,
"run_url": FAKE_RUN_URL,
"labels": FAKE_LABELS,
}
with pytest.raises(ParseError) as exc_info:
webhook_to_job(payload)
assert f"workflow_job key not found in {payload}" in str(exc_info.value)

# labels key missing
payload = {
"event": "workflow_job",
"payload": {
"action": "queued",
"workflow_job": {"id": 22428484402, "run_url": FAKE_RUN_URL},
},
"action": "queued",
"workflow_job": {"id": 22428484402, "run_url": FAKE_RUN_URL},
}
with pytest.raises(ParseError) as exc_info:
webhook_to_job(payload)
assert f"labels key not found in {payload}" in str(exc_info.value)

# run_url key missing
payload = {
"event": "workflow_job",
"payload": {
"action": "queued",
"workflow_job": {"id": 22428484402, "labels": FAKE_LABELS},
},
"action": "queued",
"workflow_job": {"id": 22428484402, "labels": FAKE_LABELS},
}
with pytest.raises(ParseError) as exc_info:
webhook_to_job(payload)
Expand Down
35 changes: 14 additions & 21 deletions webhook_router/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,17 @@ def webhook_to_job(webhook: dict) -> Job:

# The enclosed code will be removed when compiling to optimised byte code.

assert "payload" in webhook, f"payload key not found in {webhook}" # nosec
assert "action" in webhook["payload"], f"action key not found in {webhook['payload']}" # nosec
assert "action" in webhook, f"action key not found in {webhook}" # nosec
assert "workflow_job" in webhook, f"workflow_job key not found in {webhook}" # nosec
assert ( # nosec
"workflow_job" in webhook["payload"]
), f"workflow_job key not found in {webhook['payload']}"
"labels" in webhook["workflow_job"]
), f"labels key not found in {webhook['workflow_job']}"
assert ( # nosec
"labels" in webhook["payload"]["workflow_job"]
), f"labels key not found in {webhook['payload']['workflow_job']}"
assert ( # nosec
"run_url" in webhook["payload"]["workflow_job"]
), f"run_url key not found in {webhook['payload']['workflow_job']}"
"run_url" in webhook["workflow_job"]
), f"run_url key not found in {webhook['workflow_job']}"

payload = webhook["payload"]
status = payload["action"]
workflow_job = payload["workflow_job"]
status = webhook["action"]
workflow_job = webhook["workflow_job"]

labels = workflow_job["labels"]
run_url = workflow_job["run_url"]
Expand All @@ -91,7 +87,7 @@ def webhook_to_job(webhook: dict) -> Job:
run_url=run_url,
)
except ValueError as exc:
raise ParseError(f"Failed to create Webhook object for payload {payload}: {exc}") from exc
raise ParseError(f"Failed to create Webhook object for webhook {webhook}: {exc}") from exc


def _validate_webhook(webhook: dict) -> ValidationResult:
Expand Down Expand Up @@ -122,14 +118,11 @@ def _validate_missing_keys(webhook: dict) -> ValidationResult:
(True, "") if all keys are there otherwise (False,error_msg)
if the payload is missing keys.
"""
if "payload" not in webhook:
return ValidationResult(False, f"payload key not found in {webhook}")
payload = webhook["payload"]
for expected_payload_key in ("action", "workflow_job"):
if expected_payload_key not in payload:
return ValidationResult(False, f"{expected_payload_key} key not found in {webhook}")

workflow_job = payload["workflow_job"]
for expected_webhook_key in ("action", "workflow_job"):
if expected_webhook_key not in webhook:
return ValidationResult(False, f"{expected_webhook_key} key not found in {webhook}")

workflow_job = webhook["workflow_job"]
if not isinstance(workflow_job, dict):
return ValidationResult(False, f"workflow_job is not a dict in {webhook}")
for expected_workflow_job_key in ("labels", "run_url"):
Expand Down

0 comments on commit cc4bcf2

Please sign in to comment.