Skip to content
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
18 changes: 18 additions & 0 deletions .github/integration-producer-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"schema": 1,
"enabled": false,
"authority": "forge-single-attempt-v1",
"actorIDs": [],
"scopes": [],
"maxMembers": 1,
"protectedPaths": [
".github",
"src/.golangci.yml",
"src/scripts",
"bin"
],
"committer": {
"name": "github-actions[bot]",
"email": "41898282+github-actions[bot]@users.noreply.github.com"
}
}
166 changes: 166 additions & 0 deletions .github/scripts/generate-integration-ci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#!/usr/bin/env python3
"""Generate readonly candidate callees without changing ordinary/native CI."""

import argparse
import copy
import difflib
import re
from pathlib import Path

import yaml


ROOT = Path(__file__).resolve().parents[2]
SOURCES = {
"v2-ci.yml": {
"create-issue-on-failure",
"nightly-release",
},
"v2-tests.yml": {
"patch-coverage",
"create-issue-on-failure",
"create-issues-on-flakes",
"warm-build-cache",
},
"go-security-analysis.yml": set(),
}
FLAG = "inputs.integration_candidate"
PROFILE_VARIABLES = {
"vars.HIVE_GO_BUILD_CACHE": "'job'",
"vars.HIVE_RUNNER_LABELS": "'[\"ubuntu-latest\"]'",
"vars.HIVE_AFFECTED_TEST_SELECTION": "'false'",
}
HEADER = (
"# Generated by .github/scripts/generate-integration-ci.py; do not edit.\n"
"# Candidate-only technical CI. Ordinary and native CI remain in the source.\n"
)


class WorkflowLoader(yaml.SafeLoader):
pass


# GitHub's `on` is a string, not YAML 1.1's boolean spelling.
WorkflowLoader.yaml_implicit_resolvers = {
key: [
(tag, pattern)
for tag, pattern in resolvers
if tag != "tag:yaml.org,2002:bool"
]
for key, resolvers in yaml.SafeLoader.yaml_implicit_resolvers.items()
}
WorkflowLoader.add_implicit_resolver(
"tag:yaml.org,2002:bool", re.compile(r"^(?:true|false)$", re.I), list("tTfF")
)


class WorkflowDumper(yaml.SafeDumper):
def ignore_aliases(self, _data):
return True


def represent_string(dumper, value):
return dumper.represent_scalar(
"tag:yaml.org,2002:str", value, style="|" if "\n" in value else None
)


WorkflowDumper.add_representer(str, represent_string)


def candidate_only(value):
if isinstance(value, str):
value = value.replace(FLAG, "true")
value = re.sub(
r"\bvars\.[A-Za-z0-9_]+",
lambda match: PROFILE_VARIABLES.get(match.group(0), match.group(0)),
value,
)
if re.search(r"\bvars\.", value) or re.search(r"\$\{\{[^}]*\bvars\b", value):
raise ValueError("unqualified mutable workflow variable in candidate profile")
return value
if isinstance(value, list):
return [candidate_only(item) for item in value]
if isinstance(value, dict):
return {key: candidate_only(item) for key, item in value.items()}
return value


def render(source, document):
if source not in SOURCES:
raise ValueError(f"unsupported source {source}")
document = copy.deepcopy(document)
if set(document) != {
"name", "on", "permissions", "concurrency", "env", "jobs"
}:
raise ValueError(f"{source}: source workflow structure needs review")
candidate_input = (
document["on"].get("workflow_call", {}).get("inputs", {}).get(
"integration_candidate"
)
)
if not isinstance(candidate_input, dict) or (
candidate_input.get("type") != "boolean"
or candidate_input.get("default") is not False
):
raise ValueError(f"{source}: candidate entrypoint contract changed")
if document["permissions"] != {"contents": "read"}:
raise ValueError(f"{source}: readonly root permission contract changed")
jobs = document["jobs"]
for name in SOURCES[source]:
if name not in jobs or f"!{FLAG}" not in str(jobs[name].get("if", "")):
raise ValueError(f"{source}: excluded job {name} lost its guard")
del jobs[name]
for name, job in jobs.items():
if any(value not in {"read", "none"} for value in job.get("permissions", {}).values()):
raise ValueError(f"{source}: active job {name} requests write authority")
runner = job.get("runs-on", "")
if runner != "ubuntu-latest" and FLAG not in runner:
raise ValueError(f"{source}: active job {name} lacks candidate runner isolation")
job["runs-on"] = "ubuntu-latest"
needs = job.get("needs", [])
if isinstance(needs, str):
needs = [needs]
if any(dependency not in jobs for dependency in needs):
raise ValueError(f"{source}: active job {name} depends on an excluded job")
for step in job.get("steps", []):
if str(step.get("uses", "")).startswith("actions/checkout@"):
checkout = step.setdefault("with", {})
if "ref" in checkout:
raise ValueError(f"{source}: active job {name} overrides the tested subject")
checkout["persist-credentials"] = False
document["name"] = f"Integration candidate / {document['name']}"
document["on"] = {"workflow_call": None}
document["concurrency"]["cancel-in-progress"] = False
document = candidate_only(document)
return HEADER + yaml.dump(
document, Dumper=WorkflowDumper, sort_keys=False, width=10000
)


def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--check", action="store_true")
args = parser.parse_args()
different = False
for source in SOURCES:
original = ROOT / ".github" / "workflows" / source
output = original.with_name(f"integration-technical-{source}")
expected = render(source, yaml.load(original.read_text(), Loader=WorkflowLoader))
if args.check:
actual = output.read_text() if output.exists() else ""
if actual != expected:
different = True
print("".join(difflib.unified_diff(
actual.splitlines(True), expected.splitlines(True),
fromfile=str(output), tofile=f"{output} (generated)"
)))
else:
output.write_text(expected)
print(output.relative_to(ROOT))
if different:
raise SystemExit("Run python3 .github/scripts/generate-integration-ci.py")


if __name__ == "__main__":
main()
Loading
Loading