|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +pull_request=$1 |
| 4 | + |
| 5 | + |
| 6 | +_jq_head_repo_owner=".headRepositoryOwner = .headRepositoryOwner.login" |
| 7 | +_jq_head_repo_name=".headRepository = .headRepository.name" |
| 8 | + |
| 9 | +__jq_reviewed_by="with_entries(if .key == \"reviews\" then .value = [.value[].author.login] else . end)" |
| 10 | +__jq_review_requested_from="with_entries(if .key == \"reviewRequests\" then .value = [.value[].slug] end)" |
| 11 | +_jq_reviewers="$__jq_reviewed_by | $__jq_review_requested_from | .reviewers = ([.reviews, .reviewRequests] | flatten) | del(.reviews) | del(.reviewRequests)" |
| 12 | + |
| 13 | +jq_query="$_jq_head_repo_owner | $_jq_head_repo_name | $_jq_reviewers" |
| 14 | + |
| 15 | +pr_data=$(gh pr view $pull_request --json title,body,headRefName,headRepositoryOwner,headRepository,reviews,reviewRequests | jq "$jq_query") |
| 16 | + |
| 17 | +pr_title=$(echo $pr_data | jq -r '.title') |
| 18 | +pr_body=$(echo $pr_data | jq -r \""(migrated from $pull_request)\r\n\r\n\""' + .body') |
| 19 | +pr_head_repo=$(echo $pr_data | jq -r '.headRepository') |
| 20 | +pr_head_repo_owner=$(echo $pr_data | jq -r '.headRepositoryOwner') |
| 21 | +pr_head_ref=$(echo $pr_data | jq -r '.headRefName') |
| 22 | +pr_reviewers=$(echo $pr_data | jq -r '.reviewers' | sed 's/\[/-r /' | sed 's/,/-r /g' | sed 's/\]//') |
| 23 | + |
| 24 | +# Assumes this script's directory has a sibling directory called `git-filter-repo` |
| 25 | +# which contains a copy of the `git-filter-repo` script. |
| 26 | +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 27 | +GIT_FILTER_REPO_DIR="$(realpath "$SCRIPT_DIR/../git-filter-repo")" |
| 28 | + |
| 29 | +current_branch="$(git rev-parse --abbrev-ref HEAD)" |
| 30 | +migrated_ref="$pr_head_repo/$pr_head_ref" |
| 31 | +remote_name="$pr_head_repo-orig" |
| 32 | + |
| 33 | +case $pr_head_repo in |
| 34 | + "worker" | "codecov-api") |
| 35 | + subdirectory="apps/$pr_head_repo" |
| 36 | + ;; |
| 37 | + "shared") |
| 38 | + subdirectory="libs/$pr_head_repo" |
| 39 | + ;; |
| 40 | + *) |
| 41 | + echo "Uh oh" |
| 42 | + exit 1 |
| 43 | + ;; |
| 44 | +esac |
| 45 | + |
| 46 | +if ! $(git remote | grep $remote_name); then |
| 47 | + echo "Adding remote for $pr_head_repo_owner/$pr_head_repo..." |
| 48 | + git remote add $remote_name git@github.com:$pr_head_repo_owner/$pr_head_repo || true |
| 49 | + echo "Done" |
| 50 | +fi |
| 51 | + |
| 52 | +echo "Checking out $pr_head_repo/$pr_head_ref as _$migrated_ref..." |
| 53 | +git fetch $remote_name $pr_head_ref |
| 54 | +git checkout -b "_$migrated_ref" $remote_name/$pr_head_ref |
| 55 | + |
| 56 | +# Going back to the starting branch to run branch mutation |
| 57 | +git checkout "$current_branch" |
| 58 | +python "$GIT_FILTER_REPO_DIR/git-filter-repo" --force --refs "_$migrated_ref" --to-subdirectory-filter "$subdirectory" |
| 59 | + |
| 60 | +# Create a branch in this repository to merge into |
| 61 | +git checkout -b $migrated_ref |
| 62 | +git merge "_$migrated_ref" --allow-unrelated-histories --no-edit |
| 63 | +git branch -D _$migrated_ref |
| 64 | +git remote remove $remote_name |
| 65 | + |
| 66 | +git push origin $migrated_ref |
| 67 | +gh pr create --title "$pr_title" --body "$pr_body" $pr_reviewers |
0 commit comments