Repository navigation
Expand file tree
/
Copy pathupdate_master_issue.py
More file actions
125 lines (106 loc) · 3.55 KB
/
Copy pathupdate_master_issue.py
File metadata and controls
125 lines (106 loc) · 3.55 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
"""Keep the "pending review" section of the master issue in sync.
Lists every open issue carrying the sync label (created by the
"Sync with CPython" workflow) and rewrites that section of the master issue.
If an issue has an open linked PR, "درخواست ادغام: #<PR>" is added next to it.
Only the text between the two HTML-comment markers is ever touched. On the
first run the markers are inserted right below the section heading.
Env: GH_TOKEN, GITHUB_REPOSITORY (set by Actions); optional MASTER_ISSUE,
REVIEW_LABEL.
"""
import json
import os
import re
import subprocess
import sys
import tempfile
REPO = os.environ["GITHUB_REPOSITORY"]
MASTER = os.environ.get("MASTER_ISSUE", "83")
LABEL = os.environ.get("REVIEW_LABEL", "تغییرات خودکار در ترجمهها")
HEADING = "### تغییرات خودکارِ در انتظار بررسی"
START = "<!-- auto-review:start -->"
END = "<!-- auto-review:end -->"
QUERY = """
query($owner: String!, $name: String!, $label: String!) {
repository(owner: $owner, name: $name) {
issues(first: 100, states: OPEN, labels: [$label],
orderBy: {field: CREATED_AT, direction: ASC}) {
nodes {
number
closedByPullRequestsReferences(first: 5, includeClosedPrs: false) {
nodes { number state }
}
}
}
}
}
"""
def gh(*args):
return subprocess.run(
["gh", *args], check=True, capture_output=True, text=True
).stdout
def fetch_items():
owner, name = REPO.split("/", 1)
out = gh(
"api",
"graphql",
"-f",
f"query={QUERY}",
"-F",
f"owner={owner}",
"-F",
f"name={name}",
"-f",
f"label={LABEL}",
)
nodes = json.loads(out)["data"]["repository"]["issues"]["nodes"]
items = []
for n in nodes:
if str(n["number"]) == str(MASTER):
continue
prs = [
p["number"]
for p in n["closedByPullRequestsReferences"]["nodes"]
if p["state"] == "OPEN"
]
items.append((n["number"], prs))
return items
def render(items):
if not items:
return "_موردی در انتظار بررسی نیست._"
lines = []
for number, prs in items:
line = f"- #{number}"
if prs:
line += " — درخواست ادغام: " + "، ".join(f"#{p}" for p in prs)
lines.append(line)
return "\n".join(lines)
def splice(body, block):
body = body.replace("\r\n", "\n")
section = f"{START}\n{block}\n{END}"
if START in body and END in body:
pattern = re.compile(re.escape(START) + r".*?" + re.escape(END), re.S)
return pattern.sub(lambda _: section, body, count=1)
lines = body.split("\n")
for i, line in enumerate(lines):
if line.strip() == HEADING:
lines[i + 1 : i + 1] = ["", section]
return "\n".join(lines)
raise SystemExit(f"Heading not found in issue #{MASTER}: {HEADING}")
def main():
body = gh(
"issue", "view", str(MASTER), "--repo", REPO, "--json", "body", "-q", ".body"
)
old = body.replace("\r\n", "\n").rstrip("\n")
new = splice(body, render(fetch_items())).rstrip("\n")
if new == old:
print("Master issue already up to date.")
return
with tempfile.NamedTemporaryFile(
"w", suffix=".md", delete=False, encoding="utf-8"
) as f:
f.write(new + "\n")
gh("issue", "edit", str(MASTER), "--repo", REPO, "--body-file", f.name)
print(f"Updated issue #{MASTER}.")
if __name__ == "__main__":
sys.exit(main())

