Sign in
agent:

Create a new deployment branch for the repo

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This code creates a new branch in the specified GitHub repository using the GitHub API.

import json import requests source_branch = "main" latest_branch_id = int(latest_deployment_branch.split("deployment-branch-")[1]) new_branch_id = latest_branch_id + 1 new_branch_name = f"deployment-branch-{new_branch_id}" # Get environment variables GITHUB_TOKEN = getEnvVar('GITHUB_TOKEN') GITHUB_REPO_OWNER = getEnvVar('GITHUB_REPO_OWNER') # Print input parameters print(f"Input Parameters:\nrepo_name: {repo_name}\nnew_branch_name: {new_branch_name}\nsource_branch: {source_branch}") # GitHub API URL to get the latest commit of the source branch url = f"https://api.github.com/repos/{GITHUB_REPO_OWNER}/{repo_name}/git/refs/heads/{source_branch}" # Headers for authentication headers = { 'Authorization': f'Bearer {GITHUB_TOKEN}', 'Accept': 'application/vnd.github.v3+json' } # Make the request to GitHub API to get the latest commit SHA of the source branch response = requests.get(url, headers=headers) if response.status_code == 200: source_branch_info = response.json() latest_commit_sha = source_branch_info['object']['sha'] # GitHub API URL to create a new branch url = f"https://api.github.com/repos/{GITHUB_REPO_OWNER}/{repo_name}/git/refs" # Payload to create a new branch payload = { 'ref': f'refs/heads/{new_branch_name}', 'sha': latest_commit_sha } # Make the request to GitHub API to create the new branch response = requests.post(url, headers=headers, json=payload) if response.status_code == 201: new_branch = response.json()['ref'] print(f"Output:\n{json.dumps(new_branch, indent=4, default=str)}") else: new_branch = None print(f"Failed to create new branch. Status code: {response.status_code}") else: new_branch = None print(f"Failed to get the latest commit of the source branch. Status code: {response.status_code}")
copied