Sign in
agent:

Create a pull request to merge a feature branch to main branch for a repo

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

This script creates a pull request to merge the branch head_branch (like feature branch) to the base_branch (like main) for the 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 create a pull request def create_pull_request(owner, repo_name, head_branch, base_branch, title, body): token = getEnvVar('GITHUB_TOKEN') url = f'https://api.github.com/repos/{owner}/{repo_name}/pulls' headers = { 'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json' } data = { 'title': title, 'head': head_branch, 'base': base_branch, 'body': body } response = requests.post(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 head_branch: {head_branch}') print(f'Input base_branch: {base_branch}') print(f'Input title: {title}') print(f'Input body: {body}') # Check token validity is_valid = check_github_token_validity() if is_valid: # Create pull request response = create_pull_request(owner, repo_name, head_branch, base_branch, title, body) print('Raw response:') print(response.text) print(f'Status code: {response.status_code}') if response.status_code == 201: pull_request_info = response.json() pull_request_url = pull_request_info['html_url'] explanation = f'Pull request created successfully: {pull_request_url}' elif response.status_code == 422: explanation = 'Validation failed. Please check if the pull request already exists or if there are any conflicts.' 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 explanation:') print(explanation)
copied