create-dr/entrypoint.py
Shrikanth Upadhayaya b953c700bf
Initialize
2024-09-17 12:12:32 -04:00

93 lines
2.4 KiB
Python
Executable File

#! /usr/bin/env -S python3
"""
create_design_review.py: Create a design review with the changes at HEAD.
"""
import argparse
import logging
import os
import subprocess
from allspice import AllSpice, Repository
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--title", help="Title of the design review.", required=True)
parser.add_argument(
"--description",
help="Description of the design review.",
required=False,
)
parser.add_argument(
"--repository",
help="The repository to create the design review for.",
required=True,
)
parser.add_argument(
"--allspice-hub-url",
help="The URL of the AllSpice hub.",
required=True,
)
parser.add_argument(
"--base-branch-name",
help="The name of the branch to use as the base for the DR. If not provided, the default branch is used.",
required=False,
)
parser.add_argument(
"--log-level",
required=False,
default="INFO",
help="Level of logger to use, default INFO.",
)
args = parser.parse_args()
logger.setLevel(args.log_level.upper())
logger.info("Creating design review, with args %s", vars(args))
token_text = os.getenv("ALLSPICE_AUTH_TOKEN")
if not token_text:
raise ValueError("ALLSPICE_AUTH_TOKEN environment variable not set.")
client = AllSpice(
allspice_hub_url=args.allspice_hub_url,
token_text=token_text,
log_level=args.log_level.upper(),
)
owner_name, repo_name = args.repository.split("/")
repository = Repository.request(client, owner_name, repo_name)
current_branch = (
subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"])
.decode()
.strip()
)
base_branch = args.base_branch_name or repository.default_branch
logger.info(
"Creating design review at branch %s with base branch %s",
current_branch,
base_branch,
)
if base_branch == current_branch:
raise ValueError("Base branch cannot be the same as the current branch.")
repository.create_design_review(
title=args.title,
head=current_branch,
base=base_branch,
body=args.description,
)
logger.info("Design review created successfully.")
if __name__ == "__main__":
main()