Skip to content
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

Ignore if in progress before queued #15

Merged
merged 3 commits into from
Feb 17, 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
24 changes: 15 additions & 9 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def process_workflow_job():
requestor = job.get("sender", {}).get("login")
runner_name = job["workflow_job"]["runner_name"]
runner_group_name = job["workflow_job"]["runner_group_name"]
runner_public = (runner_group_name == "GitHub Actions")
runner_public = runner_group_name == "GitHub Actions"

context_details = {
"action": action,
Expand All @@ -73,20 +73,28 @@ def process_workflow_job():

elif action == "in_progress":
job_requested = jobs.get(job_id)
time_to_start = None
if not job_requested:
app.logger.warning(f"Job {job_id} is {action} but not stored!")
time_to_start = 0
else:
time_to_start = (time_start - datetime.fromtimestamp(job_requested)).seconds
if time_start < datetime.fromtimestamp(job_requested):
app.logger.error(f"Job {job_id} was in progress before being queued")
del jobs[job_id]
else:
time_to_start = (
time_start - datetime.fromtimestamp(job_requested)
).seconds

context_details = {
**context_details,
"time_to_start": time_to_start,
"runner_name": runner_name,
"runner_public": runner_public,
"repository_private": repository_private
"repository_private": repository_private,
}

if time_to_start:
context_details["time_to_start"] = time_to_start

elif action == "completed":
job_requested = jobs.get(job_id)
if not job_requested:
Expand All @@ -102,7 +110,7 @@ def process_workflow_job():
context_details = {
**context_details,
"time_to_finish": time_to_finish,
"conclusion": conclusion
"conclusion": conclusion,
}

else:
Expand All @@ -116,9 +124,7 @@ def process_workflow_job():
return True


allowed_events = {
"workflow_job": process_workflow_job
}
allowed_events = {"workflow_job": process_workflow_job}


@app.route("/github-webhook", methods=["POST"])
Expand Down
53 changes: 40 additions & 13 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def test_method_not_allowed(client):


def test_headers_not_correct(client, caplog):
response = client.post("/github-webhook")
response = client.post("/github-webhook", headers={'User-Agent': 'foo'})
assert response.status_code == 401
assert caplog.messages == [
"User-Agent is werkzeug/2.2.2",
"User-Agent is foo",
"Content is not JSON",
"No GitHub Event received!",
]
Expand All @@ -66,8 +66,8 @@ def test_started_job_not_stored(client, caplog):
assert response.status_code == 200
assert caplog.messages == [
"Job 2 is in_progress but not stored!",
'action=in_progress repository=foo/foo job_id=2 workflow=CI requestor=testerbot time_to_start=0 '
'runner_name= runner_public=false repository_private=false',
"action=in_progress repository=foo/foo job_id=2 workflow=CI requestor=testerbot "
"runner_name= runner_public=false repository_private=false",
]


Expand All @@ -79,7 +79,7 @@ def test_finished_job_not_stored(client, caplog):
assert response.status_code == 200
assert caplog.messages == [
"Job 3 is completed but not stored!",
'action=completed repository=foo/foo job_id=3 workflow=CI requestor=testerbot time_to_finish=0 conclusion=',
"action=completed repository=foo/foo job_id=3 workflow=CI requestor=testerbot time_to_finish=0 conclusion=",
]


Expand All @@ -93,7 +93,7 @@ def test_unknown_action(client, caplog):
response = client.post("/github-webhook", headers=HEADERS, json=body_failed)
assert response.status_code == 200
assert caplog.messages == [
'action=queued repository=foo/foo job_id=4 workflow=CI requestor=testerbot',
"action=queued repository=foo/foo job_id=4 workflow=CI requestor=testerbot",
"Unknown action failed, removing from memory",
]

Expand All @@ -105,7 +105,7 @@ def test_queued_job(client, caplog):
response = client.post("/github-webhook", headers=HEADERS, json=body_queued)
assert response.status_code == 200
assert caplog.messages == [
'action=queued repository=foo/foo job_id=1 workflow=CI requestor=testerbot'
"action=queued repository=foo/foo job_id=1 workflow=CI requestor=testerbot"
]


Expand All @@ -117,7 +117,8 @@ def test_logging_flow(client, caplog):
response = client.post("/github-webhook", headers=HEADERS, json=body_queued)
assert response.status_code == 200
assert (
caplog.messages[0] == 'action=queued repository=foo/foo job_id=5 workflow=CI requestor=testerbot'
caplog.messages[0]
== "action=queued repository=foo/foo job_id=5 workflow=CI requestor=testerbot"
)

body_started = BODY.copy()
Expand All @@ -127,9 +128,8 @@ def test_logging_flow(client, caplog):
assert response.status_code == 200
assert (
caplog.messages[1]
== 'action=in_progress repository=foo/foo job_id=5 workflow=CI requestor=testerbot time_to_start=5 '
'runner_name= runner_public=false repository_private=false'

== "action=in_progress repository=foo/foo job_id=5 workflow=CI requestor=testerbot "
"runner_name= runner_public=false repository_private=false time_to_start=5"
)

body_completed = BODY.copy()
Expand All @@ -140,6 +140,33 @@ def test_logging_flow(client, caplog):
assert response.status_code == 200
assert (
caplog.messages[2]
== 'action=completed repository=foo/foo job_id=5 workflow=CI requestor=testerbot '
'time_to_finish=295 conclusion=success'
== "action=completed repository=foo/foo job_id=5 workflow=CI requestor=testerbot "
"time_to_finish=295 conclusion=success"
)


def test_logging_flow_queued_after_in_progress(client, caplog):
body_queued = BODY.copy()
body_queued["action"] = "queued"
body_queued["workflow_job"]["id"] = 6
body_queued["workflow_job"]["started_at"] = "2023-02-17T06:57:48Z"

response = client.post("/github-webhook", headers=HEADERS, json=body_queued)
assert response.status_code == 200
assert (
caplog.messages[0]
== "action=queued repository=foo/foo job_id=6 workflow=CI requestor=testerbot"
)

body_started = BODY.copy()
body_started["action"] = "in_progress"
body_started["workflow_job"]["started_at"] = "2023-02-17T06:57:46Z"
response = client.post("/github-webhook", headers=HEADERS, json=body_started)

assert response.status_code == 200
assert caplog.messages[1] == "Job 6 was in progress before being queued"
assert (
caplog.messages[2]
== "action=in_progress repository=foo/foo job_id=6 workflow=CI"
" requestor=testerbot runner_name= runner_public=false repository_private=false"
)