Skip to content

Commit

Permalink
Enhance lambda_handler: ensure repository has a default branch by cre…
Browse files Browse the repository at this point in the history
…ating a README if needed, improve error handling during repository initialization, and update feature branch creation to reference the default branch.
  • Loading branch information
Your Name committed May 14, 2025
1 parent 43b5b3d commit 7b7eb3c
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions template_automation/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ def lambda_handler(event: dict, context) -> dict:
dict: Creation results containing:
repository_url (str): URL of the created repository
pull_request_url (str, optional): URL of the config pull request
Raises:
ValueError: If input validation fails
ClientError: On AWS Secrets Manager errors
GithubException: On GitHub API errors.
"""
try:
logger.info(f"Processing template request: {event}")
Expand Down Expand Up @@ -158,10 +153,32 @@ def lambda_handler(event: dict, context) -> dict:
# Create repository from template
repo_name = template_input.project_name
repo = github.get_repository(repo_name, create=True, owning_team=template_input.owning_team)

# Ensure the repository has a default branch by creating a README if needed
default_branch = None
try:
# Try to get the default branch
default_branch = repo.get("default_branch", "main")
github.get_branch(repo_name, default_branch)
logger.info(f"Repository has default branch: {default_branch}")
except requests.exceptions.HTTPError:
# No default branch found, create a README to initialize the repository
logger.info(f"No default branch found, initializing repository with README.md")
github.create_readme_file(repo_name)

# Wait for branch to be created and get the repository again
time.sleep(2)
try:
repo = github.get_repository(repo_name)
default_branch = repo.get("default_branch", "main")
logger.info(f"Repository initialized with default branch: {default_branch}")
except Exception as e:
logger.error(f"Failed to get updated repository info: {str(e)}")
raise ValueError("Repository was created but could not be initialized with a default branch")

# Create feature branch for template configuration
feature_branch = f"template-config-{int(time.time())}"
github.create_branch(repo_name, feature_branch)
github.create_branch(repo_name, feature_branch, from_ref=default_branch)

# Write template configuration
github.write_file(
Expand All @@ -186,7 +203,7 @@ def lambda_handler(event: dict, context) -> dict:
title=pr_details["title"],
body=pr_details["body"],
head_branch=feature_branch,
base_branch=github.get_default_branch(repo_name)
base_branch=default_branch
)

# Optionally trigger initialization workflow
Expand Down

0 comments on commit 7b7eb3c

Please sign in to comment.