agent: |
fuV0hKvoJ5F0JuNCw6vaGet the contents of a file in the github repo
Get the contents of a file in the github repo
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This script shows the contents of the specified file in the specified branch of the GitHub 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 get the contents of a file
def get_file_content(owner, repo_name, branch_name, file_path):
token = getEnvVar('GITHUB_TOKEN')
url = f'https://api.github.com/repos/{owner}/{repo_name}/contents/{file_path}?ref={branch_name}'
headers = {
'Authorization': f'token {token}',
'Accept': 'application/vnd.github.v3.raw'
}
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}')
print(f'Input file_path: {file_path}')
# Check token validity
is_valid = check_github_token_validity()
if is_valid:
# Get the file content
response = get_file_content(owner, repo_name, branch_name, file_path)
if response.status_code == 200:
file_content = response.text
explanation = file_content
elif response.status_code == 404:
explanation = 'The file or repository was not found. Please check the owner, repository name, branch name, and file path.'
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_content:')
print(explanation)
copied