-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit_lockfile.py
64 lines (52 loc) · 1.46 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
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()
@click.command()
@click.option("--lock-file", required=True, type=click.Path())
def main(
lock_file,
):
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()