Sign in
agent:

Update the contents of a file in the repo and commit the changes to a branch.

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

This script updates the README.md file with 'Hello folks!' at the top and commits the changes to the specified branch.

import requests import json import base64 # 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 # Function to update the contents of a file def update_file_content(owner, repo_name, branch_name, file_path, content, sha, commit_message): token = getEnvVar('GITHUB_TOKEN') url = f'https://api.github.com/repos/{owner}/{repo_name}/contents/{file_path}' headers = { 'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json' } data = { 'message': commit_message, 'content': base64.b64encode(content.encode()).decode(), 'sha': sha, 'branch': branch_name } response = requests.put(url, headers=headers, data=json.dumps(data)) 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}') print(f'Input commit_message: {commit_message}') # 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) print('Raw response:') print(response.text) print(f'Status code: {response.status_code}') if response.status_code == 200: file_content = response.text new_content = f'Hello folks!\n{file_content}' sha = response.headers.get('ETag').strip('"') # Update the file content update_response = update_file_content(owner, repo_name, branch_name, file_path, new_content, sha, commit_message) print('Update response:') print(update_response.text) print(f'Update status code: {update_response.status_code}') if update_response.status_code == 200 or update_response.status_code == 201: explanation = 'File updated successfully.' else: explanation = f'Error: {update_response.status_code} - {update_response.text}' 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 explanation:') print(explanation)
copied