Skip to content

Commit 5bd78eb

Browse files
committed
Merge branch '__rultor'
2 parents 0a87da7 + 48436ea commit 5bd78eb

2 files changed

Lines changed: 95 additions & 56 deletions

File tree

sr-data/src/sr_data/steps/workflows.py

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
Collect information about GitHub workflows in the repo.
33
"""
4-
import numpy as np
54
# The MIT License (MIT)
65
#
76
# Copyright (c) 2024 Aliaksei Bialiauski
@@ -109,66 +108,74 @@ def fetch(path) -> str:
109108

110109

111110
def workflow_info(content):
112-
yml = yaml.safe_load(content)
113-
jobs = yml["jobs"].items()
114-
jcount = len(jobs)
115-
oss = []
111+
yml = yaml.safe_load(
112+
'\n'.join(line for line in content.splitlines() if not line.strip().startswith('#')).strip()
113+
)
114+
jcount = 0
116115
scount = 0
117-
for job, jdetails in jobs:
118-
runs = jdetails.get("runs-on")
119-
if runs is not None and not isinstance(runs, dict):
120-
if isinstance(runs, list):
121-
for r in runs:
122-
oss.append(r)
123-
if not isinstance(runs, list) and runs.startswith("$"):
124-
if jdetails.get("strategy"):
125-
matrix = jdetails.get("strategy").get("matrix")
126-
if matrix is not None:
127-
if isinstance(matrix, str):
128-
oss.append(runs)
129-
else:
130-
keys = [
131-
key.strip() for key in
132-
runs.strip().replace("${{", "").replace("}}", "")
133-
.split(".")[1:]
134-
]
135-
if len(keys) == 1:
136-
if matrix.get(keys[0]):
137-
for matrixed in matrix.get(keys[0]):
138-
if isinstance(matrixed, list):
139-
for r in matrixed:
140-
oss.append(r)
141-
else:
142-
oss.append(matrixed)
143-
elif len(keys) > 1:
144-
for system in dot_values(keys, matrix):
145-
oss.append(system)
146-
elif matrix.get("include"):
147-
for include in matrix.get("include"):
148-
oss.append(
149-
include.get(
150-
runs.strip()
151-
.replace("${{", "")
152-
.replace("}}", "")
153-
.split(".")[1].strip()
116+
oss = []
117+
w_release = False
118+
if yml is not None:
119+
jobs = yml["jobs"].items()
120+
jcount = len(jobs)
121+
oss = []
122+
scount = 0
123+
for job, jdetails in jobs:
124+
runs = jdetails.get("runs-on")
125+
if runs is not None and not isinstance(runs, dict):
126+
if isinstance(runs, list):
127+
for r in runs:
128+
oss.append(r)
129+
if not isinstance(runs, list) and runs.startswith("$"):
130+
if jdetails.get("strategy"):
131+
matrix = jdetails.get("strategy").get("matrix")
132+
if matrix is not None:
133+
if isinstance(matrix, str):
134+
oss.append(runs)
135+
else:
136+
keys = [
137+
key.strip() for key in
138+
runs.strip().replace("${{", "").replace("}}", "")
139+
.split(".")[1:]
140+
]
141+
if len(keys) == 1:
142+
if matrix.get(keys[0]):
143+
for matrixed in matrix.get(keys[0]):
144+
if isinstance(matrixed, list):
145+
for r in matrixed:
146+
oss.append(r)
147+
else:
148+
oss.append(matrixed)
149+
elif len(keys) > 1:
150+
for system in dot_values(keys, matrix):
151+
oss.append(system)
152+
elif matrix.get("include"):
153+
for include in matrix.get("include"):
154+
oss.append(
155+
include.get(
156+
runs.strip()
157+
.replace("${{", "")
158+
.replace("}}", "")
159+
.split(".")[1].strip()
160+
)
154161
)
155-
)
156-
elif not isinstance(runs, list):
162+
elif not isinstance(runs, list):
163+
oss.append(runs)
164+
elif isinstance(runs, dict):
165+
if runs.get("group"):
166+
oss.append(runs.get("group"))
167+
elif runs is not None:
157168
oss.append(runs)
158-
elif isinstance(runs, dict):
159-
if runs.get("group"):
160-
oss.append(runs.get("group"))
161-
elif runs is not None:
162-
oss.append(runs)
163-
steps = jdetails.get("steps")
164-
if steps is not None:
165-
scount = len(steps)
166-
oss = set(oss)
169+
steps = jdetails.get("steps")
170+
if steps is not None:
171+
scount = len(steps)
172+
oss = set(oss)
173+
w_release = used_for_releases(yml)
167174
return {
168175
"w_jobs": jcount,
169176
"w_steps": scount,
170177
"w_oss": sorted(oss),
171-
"w_release": used_for_releases(yml)
178+
"w_release": w_release
172179
}
173180

174181

sr-data/src/tests/test_workflows.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TestWorkflows(unittest.TestCase):
3737
@pytest.mark.fast
3838
def test_fetches_workflow_information(self):
3939
info = fetch(
40-
"h1alexbel/h1alexbel/refs/heads/main/.github/workflows/update-readme.yml"
40+
"h1alexbel/h1alexbel/refs/heads/main/.github/workflows/update-readme.yml",
4141
)
4242
expected = "jobs:"
4343
self.assertTrue(
@@ -407,7 +407,7 @@ def test_parses_oss_as_list_in_matrix(self):
407407
strategy:
408408
matrix:
409409
os:
410-
- [self-hosted]
410+
- [self-hosted]
411411
runs-on: ${{ matrix.os }}
412412
"""
413413
)
@@ -441,3 +441,35 @@ def test_calculates_simplicity_score(self):
441441
-0.85,
442442
"Calculated score does not match with expected"
443443
)
444+
445+
446+
@pytest.mark.fast
447+
def test_parses_commented_workflow(self):
448+
info = workflow_info(
449+
"""
450+
# name: test
451+
# on: push
452+
# jobs:
453+
# build:
454+
# strategy:
455+
# matrix:
456+
# os:
457+
# - [self-hosted]
458+
# runs-on: ${{ matrix.os }}
459+
"""
460+
)
461+
self.assertEqual(
462+
info["w_oss"],
463+
[],
464+
"Workflow OSs are not empty, but they should"
465+
)
466+
self.assertEqual(
467+
info["w_jobs"],
468+
0,
469+
"Jobs count in workflow is not zero, but should be"
470+
)
471+
self.assertEqual(
472+
info["w_steps"],
473+
0,
474+
"Steps count in workflow is not zero, but should be"
475+
)

0 commit comments

Comments
 (0)