agent: |
VYUrl6HnTludBnlqJsnmSwitch to a branch and list the repo files
Switch to a branch and list the repo files
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This script switches to the specified branch and lists the files in the repository.
inputs
outputs
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 files in a branch
def list_files_in_branch(owner, repo_name, branch_name):
token = getEnvVar('GITHUB_TOKEN')
url = f'https://api.github.com/repos/{owner}/{repo_name}/git/trees/{branch_name}?recursive=1'
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}')
print(f'Input branch_name: {branch_name}')
# Check token validity
is_valid = check_github_token_validity()
if is_valid:
# List files in the branch
response = list_files_in_branch(owner, repo_name, branch_name)
if response.status_code == 200:
files_info = response.json()
file_paths = [file['path'] for file in files_info.get('tree', [])]
explanation = json.dumps(file_paths, indent=4)
elif response.status_code == 404:
explanation = 'The branch or repository was not found. Please check the owner, repository name, and branch 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 file_paths:')
print(explanation)
copied