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

Commit 3032ae2

Browse files
committed
change: Define workflow data as a dict and use dict_to_logfmt
1 parent 8333678 commit 3032ae2

2 files changed

Lines changed: 31 additions & 16 deletions

File tree

src/app.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
from const import GithubHeaders, LOGGING_CONFIG
10-
from utils import parse_datetime
10+
from utils import parse_datetime, dict_to_logfmt
1111

1212
dictConfig(LOGGING_CONFIG)
1313

@@ -53,13 +53,16 @@ def process_workflow_job():
5353
repository = job["repository"]["full_name"]
5454
action = job["action"]
5555

56+
context_details = {
57+
"action": action,
58+
"repository": repository,
59+
"job_id": job_id,
60+
"workflow": workflow,
61+
}
62+
5663
if action == "queued":
5764
# add to memory as timestamp
5865
jobs[job_id] = int(time_start.timestamp())
59-
msg = (
60-
f"action={action} repository={repository} job_id={job_id}"
61-
f' workflow="{workflow}"'
62-
)
6366

6467
elif action == "in_progress":
6568
job_requested = jobs.get(job_id)
@@ -68,10 +71,7 @@ def process_workflow_job():
6871
time_to_start = 0
6972
else:
7073
time_to_start = (time_start - datetime.fromtimestamp(job_requested)).seconds
71-
msg = (
72-
f"action={action} repository={repository} job_id={job_id}"
73-
f' workflow="{workflow}" time_to_start={time_to_start}'
74-
)
74+
context_details["time_to_start"] = time_to_start
7575

7676
elif action == "completed":
7777
job_requested = jobs.get(job_id)
@@ -84,18 +84,15 @@ def process_workflow_job():
8484
).seconds
8585
# delete from memory
8686
del jobs[job_id]
87-
msg = (
88-
f"action={action} repository={repository} job_id={job_id}"
89-
f' workflow="{workflow}" time_to_finish={time_to_finish}'
90-
)
87+
context_details["time_to_finish"] = time_to_finish
9188
else:
9289
app.logger.warning(f"Unknown action {action}, removing from memory")
9390
if job_id in jobs:
9491
del jobs[job_id]
95-
msg = None
92+
context_details = None
9693

97-
if msg:
98-
app.logger.info(msg)
94+
if context_details:
95+
app.logger.info(dict_to_logfmt(context_details))
9996
return True
10097

10198

src/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,21 @@ def parse_datetime(date: str) -> datetime:
55
"""Parse GitHub date to object"""
66
exp = "%Y-%m-%dT%H:%M:%SZ"
77
return datetime.strptime(date, exp)
8+
9+
10+
def dict_to_logfmt(data: dict) -> str:
11+
"""Convert a dict to logfmt string"""
12+
outstr = list()
13+
for k, v in data.items():
14+
if v is None:
15+
outstr.append(f"{k}=")
16+
continue
17+
if isinstance(v, bool):
18+
v = "true" if v else "false"
19+
elif isinstance(v, (dict, object, int)):
20+
v = str(v)
21+
22+
if " " in v:
23+
v = '"%s"' % v.replace('"', '\\"')
24+
outstr.append(f"{k}={v}")
25+
return " ".join(outstr)

0 commit comments

Comments
 (0)