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