-
Notifications
You must be signed in to change notification settings - Fork 2
/
gha.py
executable file
·107 lines (88 loc) · 3.48 KB
/
gha.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
from os import environ as env, makedirs
from os.path import dirname
import click
from utz import check, process, run, err
import njsp_plots
import parse_njsp_xmls
import refresh_data
def configure_author(name, email):
run('git', 'config', '--global', 'user.name', name)
run('git', 'config', '--global', 'user.email', email)
@click.command()
@click.option('-b', '--branch', 'branches', multiple=True)
@click.option('-c', '--configure-author', 'do_configure_author', is_flag=True)
@click.option('-d', '--do-dispatch', is_flag=True)
@click.option('-f', '--force', count=True, help=f'Continue past initial no-op data update')
@click.option('-p', '--push', is_flag=True)
@click.option('-r/-R', '--rebase/--no-rebase', is_flag=True, default=None)
def main(branches, do_configure_author, do_dispatch, force, push, rebase):
refresh_data.main()
git_is_clean = check('git', 'diff', '--quiet', 'HEAD')
if git_is_clean:
err('No data changes found')
if force:
err(f'force={force}; continuing')
else:
return
did_commit = False
def commit(msg, amend=True):
nonlocal do_configure_author, did_commit
if do_configure_author:
configure_author(
env.get('GIT_AUTHOR_NAME', 'GitHub Actions'),
env.get('GIT_AUTHOR_EMAIL', 'ryan-williams@users.noreply.github.com'),
)
do_configure_author = False
if did_commit and amend:
err('Amending commit')
run('git', 'commit', '--amend', '-am', msg)
else:
run('git', 'commit', '-am', msg)
did_commit = True
if not git_is_clean:
commit('GHA: update data')
err("Calling parse_njsp_xmls.main()")
parse_njsp_xmls.main()
err("Calling njsp_plots.main()")
njsp_plots.main()
data_changed = not check('git', 'diff', '--quiet', 'HEAD', '--', 'data')
www_changed = not check('git', 'diff', '--quiet', 'origin/www', '--', 'www')
do_commit = data_changed or www_changed
changed_files = [ line[3:] for line in process.lines('git', 'status', '--porcelain') ]
if changed_files:
err(f'{len(changed_files)} changed files:')
for f in changed_files:
err(f'\t{f}')
if do_commit:
commit('GHA: update data/plots')
else:
err('No data/plot change found')
if push:
if did_commit:
if rebase is not False:
# None -> merge, True -> rebase, False -> skip
run('git', 'config', 'pull.rebase', str(bool(rebase)).lower())
for branch in branches:
run('git', 'pull', 'origin', branch)
for branch in branches:
run('git', 'push', 'origin', f'HEAD:{branch}')
GITHUB_OUTPUT = env.get('GITHUB_OUTPUT')
if GITHUB_OUTPUT:
sha = process.line('git', 'log', '-1', '--format=%h')
with open(GITHUB_OUTPUT, 'a') as f:
f.write(f'sha={sha}\n')
err(f"Wrote SHA {sha} to $GITHUB_OUTPUT")
if do_dispatch:
workflow = "slack-test.yml"
err(f"Dispatching to {workflow}")
cmd = [
"gh", "workflow",
"run", workflow,
"-f", f"commits={sha}",
]
process.run(cmd)
else:
err('Nothing to push')
if __name__ == '__main__':
main()