Last active
October 30, 2025 09:14
-
-
Save matt-FFFFFF/9717a7b1a003746ccd7024485de69559 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Script to migrate a PR from a fork to a local branch | |
| # This preserves original commit authorship and allows CI/CD with secrets to run | |
| # Usage: migpr <PR_NUMBER> | |
| set -e | |
| # Colors for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| # Check if PR number is provided | |
| if [ -z "$1" ]; then | |
| echo -e "${RED}Error: PR number is required${NC}" | |
| echo "Usage: $0 <PR_NUMBER>" | |
| exit 1 | |
| fi | |
| PR_NUMBER=$1 | |
| # Check if gh CLI is installed | |
| if ! command -v gh &> /dev/null; then | |
| echo -e "${RED}Error: GitHub CLI (gh) is not installed${NC}" | |
| echo "Please install it from https://cli.github.com/" | |
| exit 1 | |
| fi | |
| # Check if user is authenticated | |
| if ! gh auth status &> /dev/null; then | |
| echo -e "${RED}Error: Not authenticated with GitHub CLI${NC}" | |
| echo "Please run: gh auth login" | |
| exit 1 | |
| fi | |
| echo -e "${GREEN}Fetching PR #${PR_NUMBER} details...${NC}" | |
| # Get PR details | |
| PR_JSON=$(gh pr view "$PR_NUMBER" --json number,headRefName,headRepository,headRepositoryOwner,author,title,body,state) | |
| PR_STATE=$(echo "$PR_JSON" | jq -r '.state') | |
| if [ "$PR_STATE" != "OPEN" ]; then | |
| echo -e "${RED}Error: PR #${PR_NUMBER} is not open (state: ${PR_STATE})${NC}" | |
| exit 1 | |
| fi | |
| PR_TITLE=$(echo "$PR_JSON" | jq -r '.title') | |
| PR_BODY=$(echo "$PR_JSON" | jq -r '.body') | |
| PR_AUTHOR=$(echo "$PR_JSON" | jq -r '.author.login') | |
| PR_HEAD_REF=$(echo "$PR_JSON" | jq -r '.headRefName') | |
| PR_HEAD_REPO=$(echo "$PR_JSON" | jq -r '.headRepository.name') | |
| PR_HEAD_OWNER=$(echo "$PR_JSON" | jq -r '.headRepositoryOwner.login') | |
| echo -e "${YELLOW}PR Details:${NC}" | |
| echo " Title: $PR_TITLE" | |
| echo " Author: @$PR_AUTHOR" | |
| echo " Branch: $PR_HEAD_REF" | |
| echo " Repository: $PR_HEAD_OWNER/$PR_HEAD_REPO" | |
| echo "" | |
| # Create a new branch name | |
| NEW_BRANCH="fork-pr-${PR_NUMBER}-${PR_HEAD_REF}" | |
| # Check if branch already exists | |
| if git show-ref --verify --quiet "refs/heads/$NEW_BRANCH"; then | |
| echo -e "${RED}Error: Branch '$NEW_BRANCH' already exists locally${NC}" | |
| echo "Please delete it first: git branch -D $NEW_BRANCH" | |
| exit 1 | |
| fi | |
| echo -e "${GREEN}Fetching PR commits from fork...${NC}" | |
| # Fetch the PR ref | |
| git fetch origin "pull/${PR_NUMBER}/head:${NEW_BRANCH}" | |
| echo -e "${GREEN}Creating local branch: ${NEW_BRANCH}${NC}" | |
| # Checkout the new branch | |
| git checkout "$NEW_BRANCH" | |
| # Verify commits and authorship | |
| echo "" | |
| echo -e "${YELLOW}Commits in this PR (with original authorship preserved):${NC}" | |
| git log --oneline --format="%h %an <%ae> - %s" origin/main.."$NEW_BRANCH" | |
| echo "" | |
| # Push the branch to origin | |
| echo -e "${GREEN}Pushing branch to origin...${NC}" | |
| git push -u origin "$NEW_BRANCH" | |
| # Create new PR body with reference to original | |
| NEW_PR_BODY="This PR replaces #${PR_NUMBER} to enable CI/CD checks with required secrets. | |
| Original PR by @${PR_AUTHOR} | |
| --- | |
| ${PR_BODY}" | |
| echo "" | |
| echo -e "${GREEN}Creating new PR...${NC}" | |
| # Create new PR | |
| NEW_PR_URL=$(gh pr create \ | |
| --title "$PR_TITLE" \ | |
| --body "$NEW_PR_BODY" \ | |
| --head "$NEW_BRANCH" \ | |
| --base main) | |
| # Extract new PR number from URL | |
| NEW_PR_NUMBER=$(echo "$NEW_PR_URL" | grep -oE '[0-9]+$') | |
| echo -e "${GREEN}New PR created: ${NEW_PR_URL}${NC}" | |
| echo "" | |
| # Close the original PR with a comment | |
| CLOSE_COMMENT="Thank you for your contribution @${PR_AUTHOR}! | |
| Due to CI/CD limitations with fork PRs and secrets access, I've migrated your changes to #${NEW_PR_NUMBER}. All your commits and authorship have been preserved. Please follow the new PR for any updates or requested changes. | |
| The new PR will run all required checks and can be merged once approved." | |
| echo -e "${YELLOW}Closing original PR #${PR_NUMBER}...${NC}" | |
| gh pr close "$PR_NUMBER" --comment "$CLOSE_COMMENT" | |
| echo "" | |
| echo -e "${GREEN}✅ Migration complete!${NC}" | |
| echo "" | |
| echo -e "${YELLOW}Summary:${NC}" | |
| echo " • Original PR #${PR_NUMBER} has been closed" | |
| echo " • New PR #${NEW_PR_NUMBER} has been created: ${NEW_PR_URL}" | |
| echo " • Local branch: ${NEW_BRANCH}" | |
| echo " • All commits preserved with original authorship" | |
| echo "" | |
| echo -e "${YELLOW}Next steps:${NC}" | |
| echo " 1. Review the new PR: gh pr view ${NEW_PR_NUMBER}" | |
| echo " 2. CI/CD checks should now run with access to secrets" | |
| echo " 3. Notify @${PR_AUTHOR} if any changes are needed" | |
| echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment