Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Commit acbbe0b

Browse files
authored
Ignore if in progress before queued (#15)
Ignore the logging if in progress action before queued
1 parent 59ecef0 commit acbbe0b

2 files changed

Lines changed: 55 additions & 22 deletions

File tree

src/app.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def process_workflow_job():
5757
requestor = job.get("sender", {}).get("login")
5858
runner_name = job["workflow_job"]["runner_name"]
5959
runner_group_name = job["workflow_job"]["runner_group_name"]
60-
runner_public = (runner_group_name == "GitHub Actions")
60+
runner_public = runner_group_name == "GitHub Actions"
6161

6262
context_details = {
6363
"action": action,
@@ -73,20 +73,28 @@ def process_workflow_job():
7373

7474
elif action == "in_progress":
7575
job_requested = jobs.get(job_id)
76+
time_to_start = None
7677
if not job_requested:
7778
app.logger.warning(f"Job {job_id} is {action} but not stored!")
78-
time_to_start = 0
7979
else:
80-
time_to_start = (time_start - datetime.fromtimestamp(job_requested)).seconds
80+
if time_start < datetime.fromtimestamp(job_requested):
81+
app.logger.error(f"Job {job_id} was in progress before being queued")
82+
del jobs[job_id]
83+
else:
84+
time_to_start = (
85+
time_start - datetime.fromtimestamp(job_requested)
86+
).seconds
8187

8288
context_details = {
8389
**context_details,
84-
"time_to_start": time_to_start,
8590
"runner_name": runner_name,
8691
"runner_public": runner_public,
87-
"repository_private": repository_private
92+
"repository_private": repository_private,
8893
}
8994

95+
if time_to_start:
96+
context_details["time_to_start"] = time_to_start
97+
9098
elif action == "completed":
9199
job_requested = jobs.get(job_id)
92100
if not job_requested:
@@ -102,7 +110,7 @@ def process_workflow_job():
102110
context_details = {
103111
**context_details,
104112
"time_to_finish": time_to_finish,
105-
"conclusion": conclusion
113+
"conclusion": conclusion,
106114
}
107115

108116
else:
@@ -116,9 +124,7 @@ def process_workflow_job():
116124
return True
117125

118126

119-
allowed_events = {
120-
"workflow_job": process_workflow_job
121-
}
127+
allowed_events = {"workflow_job": process_workflow_job}
122128

123129

124130
@app.route("/github-webhook", methods=["POST"])

tests/tests.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def test_method_not_allowed(client):
3636

3737

3838
def test_headers_not_correct(client, caplog):
39-
response = client.post("/github-webhook")
39+
response = client.post("/github-webhook", headers={'User-Agent': 'foo'})
4040
assert response.status_code == 401
4141
assert caplog.messages == [
42-
"User-Agent is werkzeug/2.2.2",
42+
"User-Agent is foo",
4343
"Content is not JSON",
4444
"No GitHub Event received!",
4545
]
@@ -66,8 +66,8 @@ def test_started_job_not_stored(client, caplog):
6666
assert response.status_code == 200
6767
assert caplog.messages == [
6868
"Job 2 is in_progress but not stored!",
69-
'action=in_progress repository=foo/foo job_id=2 workflow=CI requestor=testerbot time_to_start=0 '
70-
'runner_name= runner_public=false repository_private=false',
69+
"action=in_progress repository=foo/foo job_id=2 workflow=CI requestor=testerbot "
70+
"runner_name= runner_public=false repository_private=false",
7171
]
7272

7373

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

8585

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

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

111111

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

123124
body_started = BODY.copy()
@@ -127,9 +128,8 @@ def test_logging_flow(client, caplog):
127128
assert response.status_code == 200
128129
assert (
129130
caplog.messages[1]
130-
== 'action=in_progress repository=foo/foo job_id=5 workflow=CI requestor=testerbot time_to_start=5 '
131-
'runner_name= runner_public=false repository_private=false'
132-
131+
== "action=in_progress repository=foo/foo job_id=5 workflow=CI requestor=testerbot "
132+
"runner_name= runner_public=false repository_private=false time_to_start=5"
133133
)
134134

135135
body_completed = BODY.copy()
@@ -140,6 +140,33 @@ def test_logging_flow(client, caplog):
140140
assert response.status_code == 200
141141
assert (
142142
caplog.messages[2]
143-
== 'action=completed repository=foo/foo job_id=5 workflow=CI requestor=testerbot '
144-
'time_to_finish=295 conclusion=success'
143+
== "action=completed repository=foo/foo job_id=5 workflow=CI requestor=testerbot "
144+
"time_to_finish=295 conclusion=success"
145+
)
146+
147+
148+
def test_logging_flow_queued_after_in_progress(client, caplog):
149+
body_queued = BODY.copy()
150+
body_queued["action"] = "queued"
151+
body_queued["workflow_job"]["id"] = 6
152+
body_queued["workflow_job"]["started_at"] = "2023-02-17T06:57:48Z"
153+
154+
response = client.post("/github-webhook", headers=HEADERS, json=body_queued)
155+
assert response.status_code == 200
156+
assert (
157+
caplog.messages[0]
158+
== "action=queued repository=foo/foo job_id=6 workflow=CI requestor=testerbot"
159+
)
160+
161+
body_started = BODY.copy()
162+
body_started["action"] = "in_progress"
163+
body_started["workflow_job"]["started_at"] = "2023-02-17T06:57:46Z"
164+
response = client.post("/github-webhook", headers=HEADERS, json=body_started)
165+
166+
assert response.status_code == 200
167+
assert caplog.messages[1] == "Job 6 was in progress before being queued"
168+
assert (
169+
caplog.messages[2]
170+
== "action=in_progress repository=foo/foo job_id=6 workflow=CI"
171+
" requestor=testerbot runner_name= runner_public=false repository_private=false"
145172
)

0 commit comments

Comments
 (0)