Skip to content

Commit 15cceee

Browse files
authored
add migrate-pr.sh script to help migration (#33)
1 parent 1b3753f commit 15cceee

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

tools/absorb-repo/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,23 @@ The above invocation will create one or two commits on the `absorb-worker` branc
1414
- a merge commit that merges the `absorb-worker` branch with the rewritten default branch of `git@github.com:codecov/worker.git`
1515

1616
You can run it multiple times to absorb multiple repositories in a single branch.
17+
18+
### Migrating existing PRs
19+
20+
Pull requests that weren't ready to be merged before the cutover can be migrated to `umbrella` using `migrate-pr.sh`. In order to use it, you need to have `gh` and `jq` installed. The simplest way to get them is with homebrew:
21+
```
22+
# Assuming you already have homebrew
23+
$ brew install gh jq
24+
25+
# Log into the GH CLI
26+
$ gh auth login
27+
```
28+
29+
Script usage:
30+
```
31+
./migrate-pr.sh https://github.com/codecov/worker/pull/1300
32+
```
33+
34+
The above invocation will:
35+
- apply the same transformation to the PR's branch that `absorb-repo.sh` does
36+
- create a PR with the same title and body (with a link to the original PR) for the transformed branch

tools/absorb-repo/migrate-pr.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)