|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Set your GitHub access token and repository details |
| 4 | +TOKEN="YOUR_GITHUB_ACCESS_TOKEN" |
| 5 | +REPO_OWNER="OWNER" |
| 6 | +REPO_NAME="REPO_NAME" |
| 7 | +BASE_BRANCH="base_branch" # The branch you want to merge into |
| 8 | +HEAD_BRANCH="head_branch" # The branch you want to merge from |
| 9 | +PR_TITLE="Pull Request Title" |
| 10 | +PR_BODY="Pull Request Body" |
| 11 | + |
| 12 | +# Method 1: Using GitHub API with cURL |
| 13 | +create_pull_request_with_curl() { |
| 14 | + # Create a pull request using GitHub API |
| 15 | + pull_request=$(curl -X POST -H "Authorization: token $TOKEN" \ |
| 16 | + -d '{"title": "'"$PR_TITLE"'", "body": "'"$PR_BODY"'", "head": "'"$REPO_OWNER:$HEAD_BRANCH"'", "base": "'"$BASE_BRANCH"'"}' \ |
| 17 | + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls") |
| 18 | + |
| 19 | + # Extract the pull request number from the response |
| 20 | + pr_number=$(echo "$pull_request" | jq -r '.number') |
| 21 | + |
| 22 | + # Check if the pull request was successfully created |
| 23 | + if [ -n "$pr_number" ] && [ "$pr_number" != "null" ]; then |
| 24 | + echo "Pull request created successfully using cURL. PR Number: $pr_number" |
| 25 | + else |
| 26 | + echo "Failed to create pull request using cURL. Error message:" |
| 27 | + echo "$pull_request" | jq -r '.message' |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +# Method 2: Using hub command-line tool |
| 32 | +create_pull_request_with_hub() { |
| 33 | + # Install hub if not already installed |
| 34 | + if ! command -v hub &> /dev/null; then |
| 35 | + echo "hub command-line tool is not installed. Installing..." |
| 36 | + sudo apt-get update |
| 37 | + sudo apt-get install hub -y |
| 38 | + fi |
| 39 | + |
| 40 | + # Create a pull request using hub |
| 41 | + hub pull-request -b "$REPO_OWNER:$BASE_BRANCH" -h "$REPO_OWNER:$HEAD_BRANCH" -m "$PR_TITLE" -m "$PR_BODY" |
| 42 | +} |
| 43 | + |
| 44 | +# Method 3: Using gh command-line tool |
| 45 | +create_pull_request_with_gh() { |
| 46 | + # Install gh if not already installed |
| 47 | + if ! command -v gh &> /dev/null; then |
| 48 | + echo "gh command-line tool is not installed. Installing..." |
| 49 | + sudo apt-get update |
| 50 | + sudo apt-get install gh -y |
| 51 | + fi |
| 52 | + |
| 53 | + # Create a pull request using gh |
| 54 | + gh pr create --base "$BASE_BRANCH" --head "$HEAD_BRANCH" --title "$PR_TITLE" --body "$PR_BODY" |
| 55 | +} |
| 56 | + |
| 57 | +# Execute the desired method to create a pull request |
| 58 | +# Uncomment the method you want to use, and comment out the others |
| 59 | + |
| 60 | +# Method 1: Using GitHub API with cURL |
| 61 | +# create_pull_request_with_curl |
| 62 | + |
| 63 | +# Method 2: Using hub command-line tool |
| 64 | +# create_pull_request_with_hub |
| 65 | + |
| 66 | +# Method 3: Using gh command-line tool |
| 67 | +# create_pull_request_with_gh |
0 commit comments