import requests import base64 import os # GitHub repository information repository_owner = os.environ['GIT_USER_NAME'] repository_name = os.environ['GIT_REPO_NAME'] branch_name = 'main' # GitHub API token (generate one in your GitHub account settings) github_token = os.environ['GIT_TOKEN'] def push(new_content, path, file_name): file_path = f'{path}/{file_name}' # Get the current file content url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/contents/{file_path}?ref={branch_name}' headers = {'Authorization': f'token {github_token}'} response = requests.get(url, headers=headers) response_json = response.json() # Extract necessary information current_sha = response_json['sha'] encoded_content = base64.b64encode(new_content.encode()).decode() # Update the file content update_data = { 'message': 'Update file via API', 'content': encoded_content, 'sha': current_sha, 'branch': branch_name } update_url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/contents/{file_path}' response = requests.put(update_url, json=update_data, headers=headers) response_json = response.json() return response_json