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

Fix path sanitization of colon #4

Merged
merged 2 commits into from
Jan 13, 2025
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
8 changes: 5 additions & 3 deletions gha_extract_shell_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def sanitize(path):
")": "",
"&": "",
"$": "",
":": "",
}
return path.translate(str.maketrans(replacements))

Expand All @@ -44,14 +45,14 @@ def process_workflow_file(workflow_path: Path, output_dir: Path, ignored_errors=
workflow = yaml.safe_load(f)
workflow_file = workflow_path.name
# GHA allows workflow names to be defined as empty (e.g. `name:`)
workflow_name = sanitize(workflow.get("name") or workflow_path.stem)
workflow_name = workflow.get("name") or workflow_path.stem
workflow_default_shell = workflow.get("defaults", {}).get("run", {}).get("shell")
workflow_env = workflow.get("env", {})
count = 0
print(f"Processing {workflow_path} ({workflow_name})")
for job_key, job in workflow.get("jobs", {}).items():
# GHA allows job names to be defined as empty (e.g. `name:`)
job_name = sanitize(job.get("name") or job_key)
job_name = job.get("name") or job_key
job_default_shell = (
job.get("defaults", {}).get("run", {}).get("shell", workflow_default_shell)
)
Expand All @@ -69,7 +70,8 @@ def process_workflow_file(workflow_path: Path, output_dir: Path, ignored_errors=
# GHA allows step names to be defined as empty (e.g. `name:`)
step_name = sanitize(step.get("name") or str(i + 1))
script_path = (
output_dir / workflow_file / f"job={job_name}" / f"step={step_name}.sh"
output_dir / workflow_file / f"job={sanitize(job_name)}" /
f"step={sanitize(step_name)}.sh"
)
script_path.parent.mkdir(parents=True, exist_ok=True)
with script_path.open("w") as f:
Expand Down
7 changes: 6 additions & 1 deletion test/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
TEST_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(TEST_DIR))

from gha_extract_shell_scripts import process_workflow_file
from gha_extract_shell_scripts import process_workflow_file, sanitize

def clean(string):
string = textwrap.dedent(string).rstrip()
return [l + "\n" for l in string.split("\n")]


class TestSanitize(unittest.TestCase):
def test_gha_expression(self):
self.assertEqual(sanitize("Reference test: ${{ matrix.test }}"), "Reference_test_{{_matrix.test_}}")


class TestReferenceWorkflows(unittest.TestCase):
def setUp(self):
self.maxDiff = None
Expand Down
Loading