CI and release cycle: release branches, tag, Packagist, sync, tests, audit, Trivy
This commit is contained in:
1 parent
f74ab3a94b
commit
302a1c699d
20 files changed
+1029
-9
No files matched your search
Executable
+76
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Turns on auto-merge for a pull request to a release branch (`release/vX.Y.Z`).
|
||||
|
||||
Run by the `release-automerge` job whenever such a pull request is opened or updated. Gitea then
|
||||
merges it by itself once the required check is green, so features flow into the release branch
|
||||
without anyone pressing the button. A pull request to anything else is left alone: `production`
|
||||
is merged by hand.
|
||||
|
||||
Environment: GT_URL, GT_OWNER, GT_REPO, GT_TOKEN (a token that may edit pull requests),
|
||||
PULL_REQUEST_NUMBER. Optional: AUTO_MERGE_PREFIX (default "release/"), MERGE_STYLE (default "merge").
|
||||
Standard library only.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
URL = os.environ.get("GT_URL", "").rstrip("/")
|
||||
OWNER = os.environ.get("GT_OWNER", "")
|
||||
REPO = os.environ.get("GT_REPO", "")
|
||||
TOKEN = os.environ.get("GT_TOKEN", "")
|
||||
NUMBER = os.environ.get("PULL_REQUEST_NUMBER", "").lstrip("#")
|
||||
PREFIX = os.environ.get("AUTO_MERGE_PREFIX") or "release/"
|
||||
MERGE_STYLE = os.environ.get("MERGE_STYLE") or "merge"
|
||||
|
||||
ALREADY_ON = object()
|
||||
|
||||
|
||||
def api(method, path, body=None):
|
||||
request = urllib.request.Request(
|
||||
f"{URL}/api/v1/repos/{OWNER}/{REPO}/{path}",
|
||||
data=json.dumps(body).encode() if body is not None else None,
|
||||
method=method,
|
||||
headers={"Authorization": f"token {TOKEN}", "Content-Type": "application/json"},
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=30) as response:
|
||||
text = response.read().decode()
|
||||
except urllib.error.HTTPError as e:
|
||||
detail = e.read().decode()
|
||||
# A second push runs this job again while auto-merge is still on: that is what was wanted.
|
||||
if e.code == 409 and "already scheduled to auto merge" in detail:
|
||||
return ALREADY_ON
|
||||
sys.exit(f"Gitea API {method} {path} failed: {e.code} {detail}")
|
||||
return json.loads(text) if text else None
|
||||
|
||||
|
||||
def main():
|
||||
if not (URL and OWNER and REPO and TOKEN and NUMBER.isdigit()):
|
||||
sys.exit("GT_URL, GT_OWNER, GT_REPO, GT_TOKEN and PULL_REQUEST_NUMBER are required.")
|
||||
|
||||
pull = api("GET", f"pulls/{NUMBER}") or {}
|
||||
base_ref = (pull.get("base") or {}).get("ref", "")
|
||||
|
||||
if not base_ref.startswith(PREFIX):
|
||||
print(f"Pull request #{NUMBER} targets {base_ref}, not {PREFIX}*: left alone.")
|
||||
return
|
||||
if pull.get("state") != "open" or pull.get("merged"):
|
||||
print(f"Pull request #{NUMBER} is not open: nothing to do.")
|
||||
return
|
||||
|
||||
answer = api("POST", f"pulls/{NUMBER}/merge", {
|
||||
"do": MERGE_STYLE,
|
||||
"merge_when_checks_succeed": True,
|
||||
"delete_branch_after_merge": False,
|
||||
})
|
||||
if answer is ALREADY_ON:
|
||||
print(f"Auto-merge was already on for pull request #{NUMBER}.")
|
||||
return
|
||||
print(f"Auto-merge is on for pull request #{NUMBER}: it merges once the required check is green.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user