-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-pr-update-snapshot
executable file
·111 lines (83 loc) · 3.62 KB
/
gh-pr-update-snapshot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
import os
import json
import subprocess
import urllib.request
def latest_build(swift_webroot):
latest_build_endpoint = f"{swift_webroot}/latest-build.yml"
with urllib.request.urlopen(latest_build_endpoint) as response:
yaml_content = response.read().decode('utf-8')
for line in yaml_content.splitlines():
if line.startswith("dir: "):
return line.split(" ")[1]
return None
def replace_file_content(path: str, target: str, replacement: str):
with open(path, 'r') as file:
filedata = file.read()
filedata = filedata.replace(target, replacement)
with open(path, 'w') as file:
file.write(filedata)
def open_pr_to_update_base_tag(scheme: str, base_tag: str, latest_tag: str,
interactive: bool):
branch = f"update-base-tag/{scheme}-{latest_tag}"
status = subprocess.run(
["git", "rev-parse", "--verify", branch],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if status.returncode == 0:
print(f"Branch {branch} already exists, skipping")
return
status = subprocess.run(
["git", "ls-remote", "--exit-code", "--heads", "origin", branch],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if status.returncode == 0:
print(f"Branch {branch} already exists on remote, skipping")
return
subprocess.check_call(["git", "checkout", "-b", branch, "main"])
replace_file_content(f"schemes/{scheme}/manifest.json",
base_tag, latest_tag)
subprocess.check_call(["git", "add", f"schemes/{scheme}/manifest.json"])
subprocess.check_call(
["git", "commit",
"-m", f"Update base tag for {scheme} to {latest_tag}"])
subprocess.check_call(["git", "push", "origin", branch])
def github_compare(r1, r2):
return f"https://github.com/apple/swift/compare/{r1}...{r2}"
body = (f"Update base tag for {scheme} to {latest_tag}.\n\n"
f"{github_compare(base_tag, latest_tag)}\n\n"
"---\n"
"Generated by [`./tools/gh-pr-update-snapshot`]("
"https://github.com/swiftwasm/swiftwasm-build/"
"blob/main/tools/gh-pr-update-snapshot)")
subprocess.check_call([
"gh", "pr", "create",
"--title", f"Update base tag for {scheme} to {latest_tag}",
"--label", "downstreaming",
"--body", body,
"--base", "main", "--head", branch] + (
["--web"] if interactive else []))
subprocess.check_call(["git", "checkout", "-"])
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def check_latest_build(scheme: str, interactive: bool):
scheme_manifest = os.path.join(
REPO_ROOT, "schemes", scheme, "manifest.json")
with open(scheme_manifest) as f:
manifest = json.load(f)
channel = manifest['swift-org-download-channel']
if channel.endswith("-release"):
# Skip fixed release channels because they don't have latest-build.yml
return
swift_webroot = (
f"https://download.swift.org/{channel}/ubuntu2204")
base_tag = manifest["base-tag"]
latest_tag = latest_build(swift_webroot)
if base_tag != latest_tag:
open_pr_to_update_base_tag(scheme, base_tag, latest_tag, interactive)
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--no-interactive", action="store_true", default=False)
args = parser.parse_args()
for scheme in os.listdir(os.path.join(REPO_ROOT, "schemes")):
check_latest_build(scheme, not args.no_interactive)
if __name__ == '__main__':
main()