-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-push.py
118 lines (90 loc) · 3.35 KB
/
pre-push.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
108
109
110
111
112
113
114
115
116
117
118
#!/opt/continuum/anaconda/envs/lab_launch/bin/python
'''git pre-push script for Anaconda Enterprise
This script can act as the .git/hook/pre-push script itself
or be called by it.'''
import argparse
import configparser
import json
import os
import requests
import subprocess
import sys
import tempfile
from anaconda_project.project import Project
from pprint import pprint
VERSIONS_URL = f'{os.environ["TOOL_PROJECT_URL"]}/versions'
PROJECT_PATH = os.getcwd()
def cli():
parser = argparse.ArgumentParser(description='POST revision metadata')
parser.add_argument('tags', nargs='*', help='Git tag')
parser.add_argument('--dry-run', action='store_true', help='Do not POST the metadata. Useful to check for errors.')
parser.add_argument('-v', '--verbose', action='store_true', help='Print more.')
return parser
def main(cli_args: list = None):
if cli_args is not None:
args = cli().parse_args(cli_args)
else:
args = cli().parse_args()
if args.verbose:
print(f'-- Parsed arguments: {args}')
# Determine the tag to POST
if not args.tags:
all_tags = subprocess.check_output("git tag --sort=creatordate", shell=True).decode().splitlines()
else:
all_tags = args.tags
if args.verbose:
print(f'-- All known tags: {all_tags}')
# borrow token from .git/config
config = configparser.ConfigParser(strict=False)
config.read('/opt/continuum/project/.git/config')
_,bearer_token = config['http']['extraHeader'].split(':')
headers = {
'Authorization': bearer_token.strip(),
'Content-Type': 'application/vnd.api+json'
}
if args.verbose:
print(f"""-- Retrieved bearer token from .git/config
{headers}
""")
if args.verbose:
print(f'-- Project version URL: {VERSIONS_URL}')
print(f"-- Checking if {all_tags} have already been posted")
## to avoid conflicts later get the previously
## post tags (either from UI or this script)
res = requests.get(VERSIONS_URL, headers=headers)
res.raise_for_status()
posted_tags = [v['id'] for v in res.json()['data']]
remaining_tags = [t for t in all_tags if t not in posted_tags]
if args.verbose:
print(f"""-- Known version tags
{posted_tags}
""")
print(f"""-- Version tags to post
{remaining_tags}
""")
for tag in remaining_tags:
with tempfile.TemporaryDirectory() as tempdir:
project_file = subprocess.check_output(f'git --no-pager show {tag}:anaconda-project.yml', shell=True).decode()
with open(os.path.join(tempdir, 'anaconda-project.yml'), 'wt') as f:
f.write(project_file)
project = Project(tempdir)
body = {'data':{'type':'version','attributes':{'name':tag,'metadata':project.publication_info()}}}
if args.verbose:
print('-- The metadata to be posted:')
pprint(body)
if not args.dry_run:
res = requests.post(VERSIONS_URL, headers=headers, data=json.dumps(body))
if args.verbose:
print(f"""-- POST request returned
{res}
{res.reason}
""")
res.raise_for_status()
else:
print(f""" -- Dry Run POST request
requests.post({VERSIONS_URL},
headers={headers},
data={json.dumps(body)}
""")
if __name__ == '__main__':
main()