Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: autoupdate ic deps #706

Merged
merged 12 commits into from
Aug 12, 2024
7 changes: 5 additions & 2 deletions .github/workflows/update-dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ name: Update dependencies
on:
workflow_dispatch:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '30 1 * * Mon,Wed,Fri'
- cron: "30 1 * * Mon,Wed,Fri"

jobs:
update:
Expand All @@ -24,6 +23,10 @@ jobs:
- name: "🐍 Install Poetry"
uses: snok/install-poetry@v1

- name: "⚒️ Run autoupdate for ic-deps"
run: |
python scripts/auto-update-revisions.py

- name: "⚒️ Completely delete bazel cache and then recreate it"
run: |
set -eExou pipefail
Expand Down
10 changes: 10 additions & 0 deletions ic-revisions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"https://github.com/dfinity/ic.git": {
"commit": "7dee90107a88b836fc72e78993913988f4f73ca2",
"ref": "refs/heads/master"
},
"https://github.com/dfinity/cdk-rs.git": {
"commit": "59795716487fbb8a9910ac503bcea1e0cb08c932",
"ref": "refs/heads/main"
}
}
86 changes: 86 additions & 0 deletions scripts/auto-update-revisions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import json
import re
import subprocess
from pathlib import Path

SKIP = [
r"Cargo.lock",
r"Cargo.Bazel.lock",
r"target/.*",
r".*\.png$",
r".*\.jpeg$",
r".*\.md$",
r".*\.ico$",
r"\.git/.*",
"release-index.yaml",
"__pycache__/.*",
"release-controller/test_reconciler.py",
]

SKIP_REGEX = [re.compile(f".*/{pattern}") for pattern in SKIP]


def get_toplevel() -> str:
return subprocess.run(
["git", "rev-parse", "--show-toplevel"], capture_output=True, text=True, check=True
).stdout.strip()


def get_latest_commit(repo_url: str, ref: str) -> str:
return (
subprocess.run(["git", "ls-remote", repo_url, ref], capture_output=True, text=True, check=True)
.stdout.strip()
.split()[0]
)


def get_files(top_level: str):
path = Path(top_level)
for file_path in path.rglob("**/*"):
if file_path.is_file() and not any([bool(regex.fullmatch(str(file_path))) for regex in SKIP_REGEX]):
yield file_path


def update_files(top_level: str, to_update: list[(str, str)]):
for file_path in get_files(top_level):
try:
with open(file_path, "r") as file:
content = file.read()

for from_commit, to_commit in to_update:
content = content.replace(from_commit, to_commit)

with open(file_path, "w") as file:
file.write(content)
except Exception as e:
print(f"Error on path '{file_path}': {e}")
pass


def main():
top_level = get_toplevel()
deps = json.load(open(f"{top_level}/ic-revisions.json"))
to_update = []

for key in deps:
dep = deps[key]
print(f"Updating {key} from {dep['commit']} and ref {dep['ref']}")
new_commit = get_latest_commit(key, dep["ref"])
if dep["commit"] == new_commit:
print("Nothing new... skipping")
continue

print(f"Will update {key}: {dep['commit']} > {new_commit}")
to_update.append((dep["commit"], new_commit))
dep["commit"] = new_commit

print("Updating files")
update_files(top_level, to_update)

print("Updating revisions file")
json.dump(deps, open(f"{top_level}/ic-revisions.json", mode="w"), indent=4)
print("Done")


if __name__ == "__main__":
main()
Loading