|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +pull_request=$1 |
| 4 | + |
| 5 | +#pr_data=$(gh pr view $pull_request --json title,body,headRefName,headRepositoryOwner,headRepository | jq '.headRepository = .headRepositoryOwner.login + "/" + .headRepository.name | del(.headRepositoryOwner)') |
| 6 | +pr_data=$(gh pr view $pull_request --json title,body,headRefName,headRepositoryOwner,headRepository | jq '.headRepositoryOwner = .headRepositoryOwner.login | .headRepository = .headRepository.name') |
| 7 | + |
| 8 | +pr_title=$(echo $pr_data | jq '.title') |
| 9 | +pr_body=$(echo $pr_data | jq \""(migrated from $pull_request)\r\n\r\n\""' + .body') |
| 10 | +pr_head_repo=$(echo $pr_data | jq -r '.headRepository') |
| 11 | +pr_head_repo_owner=$(echo $pr_data | jq -r '.headRepositoryOwner') |
| 12 | +pr_head_ref=$(echo $pr_data | jq -r '.headRefName') |
| 13 | + |
| 14 | +# Assumes this script's directory has a sibling directory called `git-filter-repo` |
| 15 | +# which contains a copy of the `git-filter-repo` script. |
| 16 | +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 17 | +GIT_FILTER_REPO_DIR="$(realpath "$SCRIPT_DIR/../git-filter-repo")" |
| 18 | + |
| 19 | +current_branch="$(git rev-parse --abbrev-ref HEAD)" |
| 20 | +migrated_ref="$pr_head_repo/$pr_head_ref" |
| 21 | + |
| 22 | +case $pr_head_repo in |
| 23 | + "worker" | "codecov-api") |
| 24 | + subdirectory="apps/$pr_head_repo" |
| 25 | + ;; |
| 26 | + "shared") |
| 27 | + subdirectory="libs/$pr_head_repo" |
| 28 | + ;; |
| 29 | + *) |
| 30 | + echo "Uh oh" |
| 31 | + exit 1 |
| 32 | + ;; |
| 33 | +esac |
| 34 | + |
| 35 | +if ! $(git remote | grep $pr_head_repo); then |
| 36 | + echo "Adding remote for $pr_head_repo_owner/$pr_head_repo..." |
| 37 | + git remote add $pr_head_repo git@github.com:$pr_head_repo_owner/$pr_head_repo |
| 38 | + echo "Done" |
| 39 | +fi |
| 40 | + |
| 41 | +echo "Checking out $pr_head_repo/$pr_head_ref as $migrated_ref..." |
| 42 | +git fetch $pr_head_repo $pr_head_ref |
| 43 | +git checkout -b "$migrated_ref" $pr_head_repo/$pr_head_ref |
| 44 | + |
| 45 | +# Going back to the starting branch to run branch mutation |
| 46 | +git checkout "$current_branch" |
| 47 | +python "$GIT_FILTER_REPO_DIR/git-filter-repo" --force --refs "$migrated_ref" --to-subdirectory-filter "$subdirectory" |
| 48 | + |
| 49 | +# Try to create a PR |
| 50 | +git checkout $migrated_ref |
| 51 | +gh pr create --title $pr_title --body $pr_body |
0 commit comments