-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathmain.py
53 lines (40 loc) · 1.69 KB
/
main.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
# Copyright 2024 Canonical Ltd.
"""Updates pinned versions of charms in tests."""
import logging
import os
import sys
from httpx import Client
from ruamel.yaml import YAML
yaml = YAML(typ='rt')
yaml.indent(mapping=2, sequence=4, offset=2)
github = Client(
base_url='https://api.github.com/repos',
headers={
'Authorization': f'token {os.getenv("GITHUB_TOKEN")}',
'Accept': 'application/vnd.github.v3+json',
},
)
def update_charm_pins(workflow):
"""Update pinned versions of charms in the given GitHub Actions workflow."""
with open(workflow) as file:
doc = yaml.load(file)
# Assume the workflow has a single job or the first job is parameterized with charm repos
job_name = next(iter(doc['jobs']))
for idx, item in enumerate(doc['jobs'][job_name]['strategy']['matrix']['include']):
charm_repo = item['charm-repo']
commit = github.get(f'{charm_repo}/commits').raise_for_status().json()[0]
data = github.get(f'{charm_repo}/tags').raise_for_status().json()
comment = ' '.join(
[tag['name'] for tag in data if tag['commit']['sha'] == commit['sha']]
+ [commit['commit']['committer']['date']]
)
# A YAML node, as opposed to a plain value, can be updated in place to tweak comments
node = doc.mlget(['jobs', job_name, 'strategy', 'matrix', 'include', idx], list_ok=True)
node['commit'] = commit['sha']
node.yaml_add_eol_comment(comment, key='commit')
with open(workflow, 'w') as file:
yaml.dump(doc, file)
if __name__ == '__main__':
logging.basicConfig(level='INFO')
for workflow in ' '.join(sys.argv[1:]).split():
update_charm_pins(workflow)