Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

6.4 KB
Raw Blame
"""Artifact names must be unique within every workflow run, across call paths.

actions/upload-artifact fails when an artifact with the same name already
exists in the run (``overwrite`` defaults to false). A reusable workflow that
one run reaches twice -- release.yml calls backwards-compatibility.yml
directly and again through release-gate.yml -- therefore uploads twice under
the same name unless each call path renders a distinct name.
"""

# allow: no-sut-import — parses the real GitHub workflow files.

import re
from collections import Counter
from pathlib import Path

import pytest
import yaml


WORKFLOWS = Path(__file__).resolve().parents[2] / ".github" / "workflows"
_LOCAL_CALL = re.compile(r"\.github/workflows/([^/@]+\.ya?ml)$")
_EXPRESSION = re.compile(r"\$\{\{\s*(.*?)\s*\}\}")
_LITERAL = re.compile(r"^'((?:[^']|'')*)'$")


def _load(directory):
    return {
        path.name: yaml.safe_load(path.read_text())
        for path in sorted(directory.glob("*.y*ml"))
    }


def _triggers(doc):
    # PyYAML parses the bare key `on` as the boolean True.
    return doc.get("on", doc.get(True)) or {}


def _input_defaults(doc):
    call = _triggers(doc)
    call = call.get("workflow_call") if isinstance(call, dict) else None
    inputs = (call or {}).get("inputs") or {}
    return {
        name: str(spec.get("default", ""))
        for name, spec in inputs.items()
        if isinstance(spec, dict)
    }


def _render(template, inputs):
    """Render the parts of a name that differ between call paths.

    ``inputs.*`` take the caller's ``with:`` value (or the declared default)
    and ``a || b`` picks the first non-empty operand. Anything else, such as
    ``github.run_attempt`` or ``matrix.*``, is identical for every call path
    of one run attempt, so it is kept verbatim.
    """

    def operand(text):
        text = text.strip()
        literal = _LITERAL.match(text)
        if literal:
            return literal.group(1).replace("''", "'")
        if text.startswith("inputs."):
            return inputs.get(text[len("inputs.") :], "")
        return "${{ " + text + " }}"

    def expression(match):
        for part in match.group(1).split("||"):
            value = operand(part)
            if value:
                return value
        return ""

    return _EXPRESSION.sub(expression, str(template))


def artifact_names_per_run(docs, entry):
    """Every artifact name one run of ``entry`` uploads, one per call path."""
    names = []

    def visit(workflow, inputs, path):
        for job_id, job in (docs[workflow].get("jobs") or {}).items():
            uses = job.get("uses")
            target = _LOCAL_CALL.search(uses) if isinstance(uses, str) else None
            if target:
                called = target.group(1)
                assert called not in path, f"call cycle via {called}"
                passed = {
                    **_input_defaults(docs[called]),
                    **{
                        key: _render(value, inputs)
                        for key, value in (job.get("with") or {}).items()
                    },
                }
                visit(called, passed, (*path, f"{workflow}:{job_id}"))
                continue
            for step in job.get("steps") or []:
                if "actions/upload-artifact@" not in str(step.get("uses", "")):
                    continue
                name = (step.get("with") or {}).get("name", "artifact")
                names.append(
                    (
                        _render(name, inputs),
                        " -> ".join((*path, f"{workflow}:{job_id}")),
                    )
                )

    visit(entry, _input_defaults(docs[entry]), ())
    return names


def _duplicates(docs, entry):
    names = artifact_names_per_run(docs, entry)
    counts = Counter(name for name, _ in names)
    return {
        name: [where for n, where in names if n == name]
        for name, count in counts.items()
        if count > 1
    }


REAL_DOCS = _load(WORKFLOWS)


@pytest.mark.parametrize("entry", sorted(REAL_DOCS))
def test_artifact_names_are_unique_within_each_run(entry):
    assert _duplicates(REAL_DOCS, entry) == {}


def test_release_run_uploads_both_compatibility_results_distinctly():
    names = [
        name
        for name, _ in artifact_names_per_run(REAL_DOCS, "release.yml")
        if name.startswith("pypi-compatibility-results")
    ]
    # Reached directly (compat-test-gate) and via release-gate.
    assert len(names) == 2
    assert len(set(names)) == 2
    assert all("${{ github.run_attempt }}" in name for name in names)


def test_checker_detects_a_shared_name_reached_twice(tmp_path):
    reusable = {
        "on": {"workflow_call": {}},
        "jobs": {
            "probe": {
                "steps": [
                    {
                        "uses": "actions/upload-artifact@x",
                        "with": {"name": "results"},
                    }
                ]
            }
        },
    }
    gate = {
        "on": {"workflow_call": {}},
        "jobs": {"compat": {"uses": "./.github/workflows/reusable.yml"}},
    }
    release = {
        "on": {"push": {}},
        "jobs": {
            "gate": {"uses": "./.github/workflows/gate.yml"},
            "compat": {"uses": "./.github/workflows/reusable.yml"},
        },
    }
    for name, doc in {
        "reusable.yml": reusable,
        "gate.yml": gate,
        "release.yml": release,
    }.items():
        (tmp_path / name).write_text(yaml.safe_dump(doc))
    docs = _load(tmp_path)
    assert list(_duplicates(docs, "release.yml")) == ["results"]

    # A caller-supplied input in the name resolves the collision.
    reusable["on"]["workflow_call"]["inputs"] = {
        "caller": {"required": True, "type": "string"}
    }
    reusable["jobs"]["probe"]["steps"][0]["with"]["name"] = (
        "results-${{ inputs.caller || 'standalone' }}"
    )
    gate["jobs"]["compat"]["with"] = {"caller": "gate"}
    release["jobs"]["compat"]["with"] = {"caller": "direct"}
    for name, doc in {
        "reusable.yml": reusable,
        "gate.yml": gate,
        "release.yml": release,
    }.items():
        (tmp_path / name).write_text(yaml.safe_dump(doc))
    docs = _load(tmp_path)
    assert _duplicates(docs, "release.yml") == {}
    assert sorted(
        n for n, _ in artifact_names_per_run(docs, "release.yml")
    ) == [
        "results-direct",
        "results-gate",
    ]
    assert [n for n, _ in artifact_names_per_run(docs, "reusable.yml")] == [
        "results-standalone"
    ]