Mohammed Naser | bd398ea | 2024-06-01 13:38:16 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | # Check if the correct number of arguments are provided |
| 6 | if [ "$#" -ne 1 ]; then |
| 7 | echo "Usage: $0 <issue_number>" |
| 8 | exit 1 |
| 9 | fi |
| 10 | |
| 11 | # Variables |
| 12 | REPO="vexxhost/atmosphere" |
| 13 | ISSUE_NUMBER=$1 |
| 14 | LOCAL_REPO_PATH=$(mktemp -d -t repo-XXXXXXXXXX) |
| 15 | |
| 16 | # Function to clean up the local repository path |
| 17 | cleanup() { |
| 18 | echo "Cleaning up..." |
| 19 | rm -rf "$LOCAL_REPO_PATH" |
| 20 | } |
| 21 | trap cleanup EXIT |
| 22 | |
| 23 | # Clone the repo into a dynamically generated path |
| 24 | git clone "https://github.com/$REPO.git" "$LOCAL_REPO_PATH" |
| 25 | |
| 26 | cd "$LOCAL_REPO_PATH" || exit |
| 27 | |
| 28 | # Fetch the latest changes |
| 29 | git fetch origin |
| 30 | |
| 31 | # Get the issue details |
| 32 | ISSUE_DETAILS=$(gh issue view $ISSUE_NUMBER --json title,body --jq '{title: .title, body: .body}') |
| 33 | ISSUE_TITLE=$(echo "$ISSUE_DETAILS" | jq -r .title) |
| 34 | ISSUE_BODY=$(echo "$ISSUE_DETAILS" | jq -r .body) |
| 35 | |
| 36 | # Extract the target branch from the issue title |
| 37 | TARGET_BRANCH=$(echo "$ISSUE_TITLE" | grep -oP '(?<=\[).+?(?=\])') |
| 38 | |
| 39 | if [ -z "$TARGET_BRANCH" ]; then |
| 40 | echo "Failed to extract the target branch from the issue title." |
| 41 | exit 1 |
| 42 | fi |
| 43 | |
| 44 | # Extract the PR number from the issue body |
| 45 | PR_NUMBER=$(echo "$ISSUE_BODY" | grep -oP '(?<=#)\d+') |
| 46 | |
| 47 | if [ -z "$PR_NUMBER" ]; then |
| 48 | echo "Failed to extract the PR number from the issue body." |
| 49 | exit 1 |
| 50 | fi |
| 51 | |
| 52 | echo "Extracted PR number: $PR_NUMBER" |
| 53 | echo "Extracted target branch: $TARGET_BRANCH" |
| 54 | |
| 55 | # Get the merged commit ID |
| 56 | MERGED_COMMIT_ID=$(gh pr view $PR_NUMBER --json mergeCommit --jq .mergeCommit.oid) |
| 57 | |
| 58 | # Check if the commit ID was retrieved |
| 59 | if [ -z "$MERGED_COMMIT_ID" ]; then |
| 60 | echo "Failed to get the merged commit ID." |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
| 64 | # Checkout the target branch |
| 65 | git checkout $TARGET_BRANCH |
| 66 | git pull origin $TARGET_BRANCH |
| 67 | |
| 68 | # Cherry-pick the merged commit |
| 69 | if ! git cherry-pick $MERGED_COMMIT_ID; then |
| 70 | echo "Error during cherry-pick" |
| 71 | exit 1 |
| 72 | fi |
| 73 | |
| 74 | # Create a new branch |
| 75 | NEW_BRANCH_NAME="cherry-pick-$MERGED_COMMIT_ID-$TARGET_BRANCH" |
| 76 | git checkout -b $NEW_BRANCH_NAME |
| 77 | git push origin $NEW_BRANCH_NAME |
| 78 | |
| 79 | # Get the original PR title and body |
| 80 | PR_TITLE=$(gh pr view $PR_NUMBER --json title --jq .title) |
| 81 | PR_BODY=$(gh pr view $PR_NUMBER --json body --jq .body) |
| 82 | |
| 83 | # Create a new PR with the same title, prefixed with the target branch name |
| 84 | NEW_PR_TITLE="[$TARGET_BRANCH] $PR_TITLE" |
| 85 | NEW_PR_BODY="${PR_BODY}\n\nCloses #$ISSUE_NUMBER" |
Mohammed Naser | f2e3b27 | 2024-06-02 14:15:50 -0400 | [diff] [blame] | 86 | gh pr create --title "$NEW_PR_TITLE" --body "$NEW_PR_BODY" --head "$NEW_BRANCH_NAME" --base "$TARGET_BRANCH" |
Mohammed Naser | bd398ea | 2024-06-01 13:38:16 -0400 | [diff] [blame] | 87 | |
Mohammed Naser | f2e3b27 | 2024-06-02 14:15:50 -0400 | [diff] [blame] | 88 | echo "New pull request created." |