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: Adding syncing of NNS released wasm versions #3148

Merged
merged 40 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a8ba8f6
started work on the script
NikolaMilosa Dec 12, 2024
7e23547
refactoring
NikolaMilosa Dec 12, 2024
d61b442
refactoring
NikolaMilosa Dec 12, 2024
9ad0d44
making git lib runnable
NikolaMilosa Dec 12, 2024
79155ef
adding ci
NikolaMilosa Dec 12, 2024
218e506
adding syncing to tmp dir
NikolaMilosa Dec 12, 2024
de8aa0d
fixing type
NikolaMilosa Dec 12, 2024
0c64f69
removing debug code
NikolaMilosa Dec 12, 2024
0f74779
adding output
NikolaMilosa Dec 12, 2024
90a9c12
correcting token
NikolaMilosa Dec 12, 2024
8dc9d8b
adding debug print if token is set
NikolaMilosa Dec 12, 2024
d1b722a
changing name if its taken
NikolaMilosa Dec 12, 2024
1859dfe
updating workflow
NikolaMilosa Dec 12, 2024
1986ecd
removing needs
NikolaMilosa Dec 12, 2024
6449945
adding the print of what is uncommited
NikolaMilosa Dec 12, 2024
d8e6f22
deleting gh download tar
NikolaMilosa Dec 12, 2024
dbb6fcf
adding bazel config
NikolaMilosa Dec 12, 2024
e87113b
linting
NikolaMilosa Dec 12, 2024
b315f06
suggestions
NikolaMilosa Dec 12, 2024
b1f453d
separating concerns
NikolaMilosa Dec 13, 2024
8860cc1
linting
NikolaMilosa Dec 13, 2024
0ca0c49
typo
NikolaMilosa Dec 13, 2024
7fc3667
missing arg
NikolaMilosa Dec 13, 2024
8f09f24
moving to if else
NikolaMilosa Dec 13, 2024
72e0d74
fixing typo
NikolaMilosa Dec 13, 2024
4abfb55
merging back into one script
NikolaMilosa Dec 16, 2024
a4f9b1f
formatting
NikolaMilosa Dec 16, 2024
f2bb8c9
unifying cache placings
NikolaMilosa Dec 16, 2024
304d95b
adding debug print for home dir
NikolaMilosa Dec 16, 2024
116e0a4
adding to bot approved files
NikolaMilosa Dec 16, 2024
a2628ae
linting
NikolaMilosa Dec 16, 2024
9693ead
adding correct cache path
NikolaMilosa Dec 16, 2024
efbaa94
fixing path
NikolaMilosa Dec 16, 2024
0537895
fixing typos
NikolaMilosa Dec 16, 2024
7d1e76e
adding the pr description
NikolaMilosa Dec 18, 2024
bad1c63
fixing paths
NikolaMilosa Dec 18, 2024
164b958
specifying title
NikolaMilosa Dec 18, 2024
5d9b194
removing extra dot
NikolaMilosa Dec 18, 2024
0e54c17
formatting
NikolaMilosa Dec 18, 2024
145ce64
Merge branch 'master' into nm-syncing-released-wasms
NikolaMilosa Dec 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion .github/workflows/update-mainnet-revisions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,52 @@ jobs:
run: |
set -eEuxo pipefail

time python ci/src/mainnet_revisions/mainnet_revisions.py
time python ci/src/revisions/mainnet_revisions.py
shell: bash

update-nervous-system-wasms:
runs-on:
labels: dind-small
container:
image: ghcr.io/dfinity/ic-build@sha256:4fd13b47285e783c3a6f35aadd9559d097c0de162a1cf221ead66ab1598d5d45
options: >-
-e NODE_NAME --privileged --cgroupns host -v /cache:/cache -v /var/sysimage:/var/sysimage -v /var/tmp:/var/tmp -v /ceph-s3-info:/ceph-s3-info
steps:
- name: Create GitHub App Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.PR_AUTOMATION_BOT_PUBLIC_APP_ID }}
private-key: ${{ secrets.PR_AUTOMATION_BOT_PUBLIC_PRIVATE_KEY }}

- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1
token: ${{ steps.app-token.outputs.token }}

- name: Setup gh cli
uses: ksivamuthu/actions-setup-gh-cli@v3
with:
version: 2.53.0

- name: Update Mainnet canisters file
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
set -eEuxo pipefail

# Leftover from previous step of setting up gh cli
rm gh_tar

REPO_ROOT=$(git rev-parse --show-toplevel)
BRANCH="ic-nervous-system-wasms"
FILE="mainnet-canisters.json"

# Cache the git_lib.py so it doesn't change between branch changes
cp ci/src/revisions/git_lib.py /tmp/git_lib.py

python /tmp/git_lib.py --repo-root $REPO_ROOT sync-and-checkout --branch $BRANCH --main-branch master
bazel run --config=ci --repository_cache=/cache/bazel //rs/nervous_system/tools/sync-with-released-nervous-system-wasms
python /tmp/git_lib.py --repo-root $REPO_ROOT commit-and-create-pr --branch $BRANCH --file $FILE
shell: bash
177 changes: 0 additions & 177 deletions ci/src/mainnet_revisions/mainnet_revisions.py

This file was deleted.

163 changes: 163 additions & 0 deletions ci/src/revisions/git_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import argparse
import logging
import pathlib
import subprocess
import sys
from typing import List


class HelpfulParser(argparse.ArgumentParser):
"""An argparse parser that prints usage on any error."""

def error(self, message):
sys.stderr.write("error: %s\n" % message)
self.print_help()
sys.exit(2)


def init_helpful_parser() -> HelpfulParser:
parser = HelpfulParser()
parser.add_argument("--verbose", "-v", action="store_true", help="Verbose mode")

return parser


def get_logger(level) -> logging.Logger:
FORMAT = "[%(asctime)s] %(levelname)-8s %(message)s"
logging.basicConfig(format=FORMAT, level=level)
return logging.getLogger("logger")


def get_repo_root() -> pathlib.Path:
return pathlib.Path(
subprocess.run(["git", "rev-parse", "--show-toplevel"], text=True, stdout=subprocess.PIPE).stdout.strip()
)


def sync_main_branch_and_checkout_branch(
repo_root: pathlib.Path, main_branch: str, branch_to_checkout: str, logger: logging.Logger
):
if not repo_root.exists():
raise Exception("Expected dir %s to exist", repo_root.name)

subprocess.call(["git", "fetch", "origin", f"{main_branch}:{main_branch}"], cwd=repo_root)

result = subprocess.run(["git", "status", "--porcelain"], stdout=subprocess.PIPE, text=True, check=True)
if result.stdout.strip():
logger.warn("Uncommited work:\n%s", result.stdout.strip())
logger.error("Found uncommited work! Commit and then proceed.")
exit(2)

if subprocess.call(["git", "checkout", branch_to_checkout], cwd=repo_root) == 0:
# The branch already exists, update the existing MR
logger.info("Found an already existing target branch")
else:
subprocess.check_call(["git", "checkout", "-b", branch_to_checkout], cwd=repo_root)
subprocess.check_call(["git", "reset", "--hard", f"origin/{main_branch}"], cwd=repo_root)


def commit_and_create_pr(
repo: str, repo_root: pathlib.Path, branch: str, check_for_updates_in_paths: List[str], logger: logging.Logger
):
git_modified_files = subprocess.check_output(["git", "ls-files", "--modified", "--others"], cwd=repo_root).decode(
"utf8"
)

paths_to_add = [path for path in check_for_updates_in_paths if path in git_modified_files]

if len(paths_to_add) > 0:
logger.info("Creating/updating a MR that updates the saved NNS subnet revision")
cmd = ["git", "add"] + paths_to_add
logger.info("Running command '%s'", " ".join(cmd))
subprocess.check_call(cmd, cwd=repo_root)
cmd = [
"git",
"-c",
"user.name=CI Automation",
"-c",
"[email protected]",
"commit",
"-m",
"chore: Update Mainnet IC revisions file",
] + paths_to_add
logger.info("Running command '%s'", " ".join(cmd))
subprocess.check_call(
cmd,
cwd=repo_root,
)
subprocess.check_call(["git", "push", "origin", branch, "-f"], cwd=repo_root)

if not subprocess.check_output(
["gh", "pr", "list", "--head", branch, "--repo", repo],
cwd=repo_root,
).decode("utf8"):
subprocess.check_call(
[
"gh",
"pr",
"create",
"--head",
branch,
"--repo",
repo,
"--fill",
],
cwd=repo_root,
)


def run_sync_main_branch_and_checkout_branch(args, logger):
repo_root = args.repo_root
branch = args.branch
main_branch = args.main_branch
sync_main_branch_and_checkout_branch(repo_root, main_branch, branch, logger)


def run_commit_and_create_pr(args, logger):
repo = args.repo
repo_root = args.repo_root
branch = args.branch
files = args.files
commit_and_create_pr(repo, repo_root, branch, files, logger)


if __name__ == "__main__":
parser = init_helpful_parser()
parser.add_argument(
"--repo-root", help="Root of the repository", default=pathlib.Path("."), type=pathlib.Path, dest="repo_root"
)

subparsers = parser.add_subparsers(title="subcommands", description="valid commands", help="sub-command help")

parser_sync_main_branch_and_checkout_branch = subparsers.add_parser(
"sync-and-checkout", help="Sync with the main branch and checkout a different branch"
)
parser_sync_main_branch_and_checkout_branch.add_argument(
"--branch", help="Branch to checkout after syncing with main branch", type=str
)
parser_sync_main_branch_and_checkout_branch.add_argument(
"--main-branch", help="Main branch of the repository", type=str, default="master", dest="main_branch"
)
parser_sync_main_branch_and_checkout_branch.set_defaults(func=run_sync_main_branch_and_checkout_branch)

parser_commit_and_create_pr = subparsers.add_parser(
"commit-and-create-pr", help="Commit and create a pull request against the repository"
)
parser_commit_and_create_pr.add_argument(
"--branch", help="Branch to checkout after syncing with main branch", type=str
)
parser_commit_and_create_pr.add_argument(
"--file", help="Check if the file is modified before commiting", type=str, action="append", dest="files"
)
parser_commit_and_create_pr.add_argument(
"--repo", help="Github repository, `<owner>/<repo>`", type=str, default="dfinity/ic"
)
parser_commit_and_create_pr.set_defaults(func=run_commit_and_create_pr)

args = parser.parse_args()
logger = get_logger(logging.DEBUG if args.verbose else logging.INFO)

if hasattr(args, "func"):
args.func(args, logger)
else:
parser.print_help()
Loading
Loading