-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit_lockfile.py
95 lines (81 loc) · 2.22 KB
/
commit_lockfile.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
import json
import os
import subprocess
import click
import github
def _get_repo_owner_and_name():
res = subprocess.run(
["git", "remote", "get-url", "--push", "origin"],
check=True,
stdout=subprocess.PIPE,
text=True,
)
parts = res.stdout.strip().split("/")
if parts[-1].endswith(".git"):
parts[-1] = parts[-1][: -len(".git")]
return parts[-2], parts[-1]
def _get_current_branch():
res = subprocess.run(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
check=True,
stdout=subprocess.PIPE,
text=True,
)
return res.stdout.strip()
def _get_owner_repo_branch():
res = subprocess.run(
[
"gh",
"pr",
"view",
"--json",
"headRefName",
"--json",
"headRepository",
"--json",
"headRepositoryOwner",
],
text=True,
check=True,
stdout=subprocess.PIPE,
)
data = json.loads(res.stdout.strip())
return (
data["headRepositoryOwner"]["login"],
data["headRepository"]["name"],
data["headRefName"],
)
@click.command()
@click.option("--lock-file", required=True, type=click.Path())
@click.option("--event-name", required=False, default=None, type=str)
def main(
lock_file,
event_name,
):
if event_name == "issue_comment":
repo_owner, repo_name, branch = _get_owner_repo_branch()
else:
repo_owner, repo_name = _get_repo_owner_and_name()
branch = _get_current_branch()
print(
f"Updating '{lock_file}' in '{repo_owner}/{repo_name}' on branch '{branch}'...",
flush=True,
)
gh = github.Github(auth=github.Auth.Token(os.environ["GH_TOKEN"]))
repo = gh.get_repo(f"{repo_owner}/{repo_name}")
contents = repo.get_contents(lock_file, ref=branch)
with open(lock_file, "r") as f:
new_lockfile_content = f.read()
res = repo.update_file(
contents.path,
"relock w/ conda-lock",
new_lockfile_content,
contents.sha,
branch=branch,
)
print(
f"Updated w/ commit {res['commit'].sha}",
flush=True,
)
if __name__ == "__main__":
main()