Enable Actions on existing Gitea through an exact configuration transition
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build the data-only, fixed Gitea Actions activation transition."""
|
||||
import argparse
|
||||
import gzip
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import tarfile
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
SOURCE = Path(__file__).resolve().parent
|
||||
ENTRY = "deployment/actions-enable-v1.json"
|
||||
|
||||
|
||||
def bundle(patch_id):
|
||||
if not re.fullmatch(r"gitea-actions-enable-v1-[a-zA-Z0-9._-]{1,65}", patch_id):
|
||||
raise ValueError("invalid_actions_transition_identity")
|
||||
descriptor = json.loads((SOURCE / "actions-enable-v1.json").read_text())
|
||||
descriptor["releaseId"] = patch_id
|
||||
files = {
|
||||
"manifest.env": ("id=" + patch_id + "\ncomponent=gitea-actions-config\ntype=app-overlay\n").encode(),
|
||||
"files.txt": (ENTRY + "\n").encode(),
|
||||
"payload/" + ENTRY: (json.dumps(descriptor, indent=2) + "\n").encode(),
|
||||
}
|
||||
output = io.BytesIO()
|
||||
with gzip.GzipFile(fileobj=output, mode="wb", filename="", mtime=0) as compressed:
|
||||
with tarfile.open(fileobj=compressed, mode="w", format=tarfile.USTAR_FORMAT) as archive:
|
||||
for name, data in sorted(files.items()):
|
||||
info = tarfile.TarInfo(name)
|
||||
info.size, info.mode, info.mtime = len(data), 0o644, 0
|
||||
archive.addfile(info, io.BytesIO(data))
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--patch-id", required=True)
|
||||
parser.add_argument("--source-commit", required=True)
|
||||
parser.add_argument("--output-dir", type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
actual = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip()
|
||||
if actual != args.source_commit or subprocess.check_output(["git", "status", "--porcelain"], cwd=ROOT, text=True).strip():
|
||||
raise SystemExit("clean_matching_commit_required")
|
||||
data = bundle(args.patch_id)
|
||||
args.output_dir.mkdir(parents=True, exist_ok=True)
|
||||
path = args.output_dir / ("nodedc-" + args.patch_id + ".tgz")
|
||||
with path.open("xb") as stream:
|
||||
stream.write(data)
|
||||
print(json.dumps({"artifact": str(path.resolve()), "patchId": args.patch_id,
|
||||
"component": "gitea-actions-config", "sourceCommit": actual,
|
||||
"sha256": hashlib.sha256(data).hexdigest(), "entries": [ENTRY],
|
||||
"services": ["gitea"], "newPorts": [], "productionDeployEnabled": False}))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user