|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import tempfile |
| 7 | +from pathlib import Path |
| 8 | +from typing import List, Optional |
| 9 | + |
| 10 | +from github import Github, GithubException |
| 11 | + |
| 12 | +from lib.base_logger import logger |
| 13 | + |
| 14 | +REPO_URL = "https://github.com/mongodb/helm-charts.git" |
| 15 | +REPO_NAME = "mongodb/helm-charts" |
| 16 | +TARGET_CHART_SUBDIR = "charts/mongodb-kubernetes" |
| 17 | +BASE_BRANCH = "main" |
| 18 | + |
| 19 | + |
| 20 | +# run_command runs the command `command` from dir cwd |
| 21 | +def run_command(command: List[str], cwd: Optional[str] = None): |
| 22 | + logger.debug(f"Running command: {' '.join(command)} in directory {cwd}") |
| 23 | + result = subprocess.run(command, capture_output=True, text=True, cwd=cwd) |
| 24 | + if result.returncode != 0: |
| 25 | + raise RuntimeError(f"Command {' '.join(command)} failed. Stdout: {result.stdout}, stderr: {result.stderr}") |
| 26 | + logger.debug("Command succeeded") |
| 27 | + return result.stdout |
| 28 | + |
| 29 | + |
| 30 | +# create_pull_request creates the pull request to the helm-charts repo |
| 31 | +def create_pull_request(branch_name, chart_version, github_token): |
| 32 | + logger.info("Creating the pull request in the helm-charts repo.") |
| 33 | + |
| 34 | + try: |
| 35 | + g = Github(github_token) |
| 36 | + repo = g.get_repo(REPO_NAME) |
| 37 | + pr_title = f"Release MCK {chart_version}" |
| 38 | + body = f"This PR publishes the MCK chart version {chart_version}." |
| 39 | + |
| 40 | + pr = repo.create_pull( |
| 41 | + title=pr_title, |
| 42 | + body=body, |
| 43 | + head=branch_name, |
| 44 | + base=BASE_BRANCH, |
| 45 | + ) |
| 46 | + logger.info(f"Successfully created Pull Request {pr.html_url}") |
| 47 | + except Exception as e: |
| 48 | + pr_url = f"https://github.com/{REPO_NAME}/pull/new/{branch_name}" |
| 49 | + raise Exception( |
| 50 | + f"An unexpected error occurred while creating the PR: {e}. Please create the PR manually by following this link {pr_url}" |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def commit_and_push_chart(chart_version): |
| 55 | + branch_name = f"mck-release-{chart_version}" |
| 56 | + |
| 57 | + mck_dir = Path(".").resolve() |
| 58 | + # source_chart_path is local helm chart in MCK repo |
| 59 | + source_chart_path = os.path.join(mck_dir, "helm_chart") |
| 60 | + |
| 61 | + if not os.path.isdir(source_chart_path): |
| 62 | + raise Exception(f"The source chart path '{source_chart_path}' is not a valid directory.") |
| 63 | + |
| 64 | + github_token = os.environ.get("GH_TOKEN") |
| 65 | + if not github_token: |
| 66 | + raise Exception("github token not found, git push will fail.") |
| 67 | + |
| 68 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 69 | + helm_repo_path = os.path.join(temp_dir, "helm-charts") |
| 70 | + logger.debug(f"Working in a temporary directory: {temp_dir}") |
| 71 | + |
| 72 | + try: |
| 73 | + run_command(["git", "clone", REPO_URL, helm_repo_path]) |
| 74 | + run_command(["git", "checkout", "-b", branch_name], cwd=helm_repo_path) |
| 75 | + |
| 76 | + target_dir = os.path.join(helm_repo_path, TARGET_CHART_SUBDIR) |
| 77 | + logger.debug(f"Clearing content from dir '{target_dir}'") |
| 78 | + if os.path.exists(target_dir): |
| 79 | + for item in os.listdir(target_dir): |
| 80 | + item_path = os.path.join(target_dir, item) |
| 81 | + if os.path.isdir(item_path): |
| 82 | + shutil.rmtree(item_path) |
| 83 | + else: |
| 84 | + os.remove(item_path) |
| 85 | + |
| 86 | + logger.debug(f"Copying local MCK chart from '{source_chart_path}' to helm repo chart path {target_dir}") |
| 87 | + shutil.copytree(source_chart_path, target_dir, dirs_exist_ok=True) |
| 88 | + |
| 89 | + commit_message = f"Release MCK {chart_version}" |
| 90 | + run_command(["git", "add", "."], cwd=helm_repo_path) |
| 91 | + run_command(["git", "commit", "-m", commit_message], cwd=helm_repo_path) |
| 92 | + |
| 93 | + logger.debug("Configuring remote URL for authenticated push...") |
| 94 | + # Constructs a URL like https://x-access-token:YOUR_TOKEN@github.com/owner/repo.git |
| 95 | + authenticated_url = f"https://x-access-token:{github_token}@{REPO_URL.split('//')[1]}" |
| 96 | + run_command(["git", "remote", "set-url", "origin", authenticated_url], cwd=helm_repo_path) |
| 97 | + run_command(["git", "push", "-u", "origin", branch_name], cwd=helm_repo_path) |
| 98 | + |
| 99 | + create_pull_request(branch_name, chart_version, github_token) |
| 100 | + |
| 101 | + except Exception as e: |
| 102 | + raise Exception(f"An error occurred while performing git commit and push, error: {e}") |
| 103 | + |
| 104 | + |
| 105 | +def main(): |
| 106 | + parser = argparse.ArgumentParser( |
| 107 | + description="Automate PR creation to release MCK helm chart to github helm chart repo." |
| 108 | + ) |
| 109 | + parser.add_argument( |
| 110 | + "--chart_version", help="The version of the chart to be released (e.g., '1.3.0').", required=True |
| 111 | + ) |
| 112 | + args = parser.parse_args() |
| 113 | + |
| 114 | + chart_version = args.chart_version |
| 115 | + try: |
| 116 | + commit_and_push_chart(chart_version) |
| 117 | + except Exception as e: |
| 118 | + logger.error(f"Failed releasing helm chart, error: {e}") |
| 119 | + raise e |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + sys.exit(main()) |
0 commit comments