Sign in
agent:

List just the names of the branches of a github repo.

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

This script lists just the names of the branches of a specified GitHub repository.

import requests import json # Function to check if the GitHub token is valid def check_github_token_validity(): token = getEnvVar('GITHUB_TOKEN') url = 'https://api.github.com/user' headers = { 'Authorization': f'token {token}' } response = requests.get(url, headers=headers) return response.status_code == 200 # Function to list branches of a repo def list_branches(owner, repo_name): token = getEnvVar('GITHUB_TOKEN') url = f'https://api.github.com/repos/{owner}/{repo_name}/branches' headers = { 'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json' } response = requests.get(url, headers=headers) return response # Input print(f'Input owner: {owner}') print(f'Input repo_name: {repo_name}') # Check token validity is_valid = check_github_token_validity() if is_valid: # List branches response = list_branches(owner, repo_name) if response.status_code == 200: branches = response.json() branch_names = [branch['name'] for branch in branches] explanation = json.dumps(branch_names, indent=4) elif response.status_code == 404: explanation = 'The repository was not found. Please check the owner and repository name.' elif response.status_code == 403: explanation = 'Access to the repository is forbidden. Please check your permissions.' else: explanation = f'Error: {response.status_code} - {response.text}' else: explanation = 'The GitHub token is not valid.' # Output print('Output branch_names:') print(explanation)
copied