Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dataclasses import dataclass, fields
from enum import Enum
from pathlib import Path
from datetime import datetime, timezone
import re
import boto3
import jinja2
Expand Down Expand Up @@ -234,6 +235,31 @@ def generate_commit_input(
return output


def branch_exists(repo_name, branch_name):
try:
CC_CLIENT.get_branch(
repositoryName=repo_name,
branchName=branch_name,
)
return True
except CC_CLIENT.exceptions.BranchDoesNotExistException:
return False


def determine_unique_branch_name(repo_name, branch_name):
for index in range(0, 10):
new_branch_name = (
branch_name
if index == 0 else
f"{branch_name}-no-{index}"
)
if not branch_exists(repo_name, new_branch_name):
return new_branch_name
# Fallback, use the unix timestamp in the branch name
timestamp = round(datetime.now(timezone.utc).timestamp())
return f"{branch_name}-at-{timestamp}"


def generate_commits(event, repo_name, directory, parent_commit_id=None):
"""
Generate the commits for the specified repository.
Expand All @@ -256,6 +282,10 @@ def generate_commits(event, repo_name, directory, parent_commit_id=None):
default_branch_name = event.ResourceProperties.DefaultBranchName
branch_name = version
if parent_commit_id:
branch_name = determine_unique_branch_name(
repo_name=repo_name,
branch_name=branch_name,
)
CC_CLIENT.create_branch(
repositoryName=repo_name,
branchName=branch_name,
Expand Down
30 changes: 30 additions & 0 deletions src/lambda_codebase/initial_commit/initial_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dataclasses import dataclass, fields
from enum import Enum
from pathlib import Path
from datetime import datetime, timezone
import re
import boto3
import jinja2
Expand Down Expand Up @@ -253,6 +254,31 @@ def generate_commit_input(
return output


def branch_exists(repo_name, branch_name):
try:
CC_CLIENT.get_branch(
repositoryName=repo_name,
branchName=branch_name,
)
return True
except CC_CLIENT.exceptions.BranchDoesNotExistException:
return False


def determine_unique_branch_name(repo_name, branch_name):
for index in range(0, 10):
new_branch_name = (
branch_name
if index == 0 else
f"{branch_name}-no-{index}"
)
if not branch_exists(repo_name, new_branch_name):
return new_branch_name
# Fallback, use the unix timestamp in the branch name
timestamp = round(datetime.now(timezone.utc).timestamp())
return f"{branch_name}-at-{timestamp}"


def generate_commits(event, repo_name, directory, parent_commit_id=None):
"""
Generate the commits for the specified repository.
Expand All @@ -275,6 +301,10 @@ def generate_commits(event, repo_name, directory, parent_commit_id=None):
default_branch_name = event.ResourceProperties.DefaultBranchName
branch_name = version
if parent_commit_id:
branch_name = determine_unique_branch_name(
repo_name=repo_name,
branch_name=branch_name,
)
CC_CLIENT.create_branch(
repositoryName=repo_name,
branchName=branch_name,
Expand Down