Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
arnol377 committed Apr 21, 2025
2 parents 5cbce3a + ebbbdd9 commit b738c3f
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 191 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
GITHUB_API: "https://api.github.com" # Can be overridden with vars if needed
GITHUB_ORG: ${{ github.repository_owner }}
SECRET_NAME: /dev/secret/ssh/dont/tell
run: |
cd eks_automation
python -m pytest tests/ -v -m integration
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: install test test-unit test-integration clean
.PHONY: install test test-unit test-integration clean clean-test-repos

# Variables
PYTHON = python3
Expand Down Expand Up @@ -37,3 +37,8 @@ clean:
find . -type d -name '__pycache__' -exec rm -rf {} +
rm -rf .pytest_cache
rm -rf .coverage

# Clean up temporary test repositories on GitHub
clean-test-repos:
echo "Cleaning up temporary test repositories..."
$(PYTHON) scripts/cleanup_test_repos.py
82 changes: 47 additions & 35 deletions eks_automation/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,25 @@ def _create_headers(self):
}

def get_repository(self, repo_name, create=False):
"""Get or create a repository in the GitHub organization
"""Get or create a repository
Args:
repo_name (str): Name of the repository
create (bool): Whether to create the repo if it doesn't exist
create (bool, optional): Create the repository if it doesn't exist
Returns:
dict: Repository information
dict: Repository information from GitHub API
"""
repo_api_url = f"{self.api_base_url}/repos/{self.org_name}/{repo_name}"

# Try to get the repository
logger.info(f"Checking if repository {repo_name} exists")
response = requests.get(repo_api_url, headers=self.headers, verify=False)

if response.status_code == 200:
# Repository exists
return response.json()
elif response.status_code == 404:
if create:
# Repository doesn't exist, create it
get_url = f"{self.api_base_url}/repos/{self.org_name}/{repo_name}"
try:
response = requests.get(get_url, headers=self.headers, verify=False)
if response.status_code == 200:
return response.json()
elif response.status_code == 404 and create:
logger.info(f"Creating repository {repo_name}")
create_url = f"{self.api_base_url}/orgs/{self.org_name}/repos"
repo_data = {
"name": repo_name,
"description": "EKS Automation CI/CD Pipeline Repo",
"private": True,
"auto_init": True, # Initialize with README
"default_branch": "main",
Expand All @@ -112,9 +105,21 @@ def get_repository(self, repo_name, create=False):
)

if create_response.status_code in (201, 200):
# Wait briefly for repository initialization
time.sleep(2)
return create_response.json()
# Wait for repository initialization
repo = create_response.json()
max_retries = 10
retry_delay = 1
for _ in range(max_retries):
try:
# Try to get the main branch's reference
self.get_reference_sha(repo_name, "heads/main")
return repo
except Exception:
# If reference doesn't exist yet, wait and retry
time.sleep(retry_delay)
continue
# If we got here, initialization failed
raise Exception(f"Repository {repo_name} initialization timed out")
else:
error_message = f"Failed to create repository: {create_response.status_code} - {create_response.text}"
logger.error(error_message)
Expand All @@ -123,8 +128,8 @@ def get_repository(self, repo_name, create=False):
error_message = f"Repository {repo_name} not found and create=False"
logger.error(error_message)
raise Exception(error_message)
else:
error_message = f"Unexpected response when getting repository: {response.status_code} - {response.text}"
except requests.exceptions.RequestException as e:
error_message = f"Error accessing GitHub API: {str(e)}"
logger.error(error_message)
raise Exception(error_message)

Expand Down Expand Up @@ -479,12 +484,12 @@ def commit_repository_contents(self, repo_name, work_dir, commit_message, branch
"sha": blob_sha
})

# Try to get the latest commit SHA for the branch
# If it doesn't exist, we'll create it
# Try to get the latest commit SHA from the base branch
base_branch = "main" # Always use main as base when creating new branches
try:
latest_commit_sha = self.get_reference_sha(repo_name, f"heads/{target_branch}")
latest_commit = self.get_commit(repo_name, latest_commit_sha)
base_tree_sha = latest_commit["tree"]["sha"]
base_commit_sha = self.get_reference_sha(repo_name, f"heads/{base_branch}")
base_commit = self.get_commit(repo_name, base_commit_sha)
base_tree_sha = base_commit["tree"]["sha"]
except Exception:
# If we can't get the reference, assume it's a new repo with no commits
base_tree_sha = None
Expand All @@ -499,7 +504,7 @@ def commit_repository_contents(self, repo_name, work_dir, commit_message, branch
repo_name,
commit_message,
new_tree_sha,
[latest_commit_sha]
[base_commit_sha]
)
else:
# If it's a new repo, create the first commit
Expand All @@ -512,18 +517,25 @@ def commit_repository_contents(self, repo_name, work_dir, commit_message, branch

# Update or create the reference to point to the new commit
try:
# Try to update existing branch
self.update_reference(
repo_name,
f"heads/{target_branch}",
new_commit_sha
)
except Exception:
# If the reference doesn't exist, create it
self.create_reference(
repo_name,
f"refs/heads/{target_branch}",
new_commit_sha
)
# If the branch doesn't exist, create it
try:
self.create_reference(
repo_name,
f"refs/heads/{target_branch}",
new_commit_sha
)
except Exception as e:
# If we still can't create the branch, something is wrong
error_message = f"Failed to create or update branch {target_branch} for {repo_name}: {str(e)}"
logger.error(error_message)
raise Exception(error_message)

return target_branch

Expand Down
Loading

0 comments on commit b738c3f

Please sign in to comment.