This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
backport-prs
executable file
·128 lines (104 loc) · 4.82 KB
/
backport-prs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
# Backport JF PRs to release branch
org="jellyfin"
if [[ ! -f ~/.config/gh/hosts.yml || ! -f ~/.config/hub ]]; then
# Remove any existing gh configs
rm -r ~/.config/gh
# Copy the gh/hub configs from Joshua (which uses Jellyfin Bot acct)
sudo cp -a /var/home/joshua/.config/{hub,gh} ~/.config/
sudo chown -R $USER ~/.config/
fi
echo "Please select the repo you want to work on:"
echo -e " jellyfin"
echo -e " jellyfin-web"
echo -n "> "
read repo
if [[ ${repo} != 'jellyfin' && ${repo} != 'jellyfin-web' ]]; then
echo "Invalid repo!"
exit 1
fi
pushd projects/server/${repo}
# Ensure we're in group mode and fix perms
sudo -u build git config core.sharedRepository group
sudo chgrp -R build .
sudo chmod -R g+rwX .
sudo find . -type d -exec chmod g+s '{}' +
upstream_repo="${org}/${repo}"
remote="$( grep -w --color=none "${upstream_repo}" <<<"$( git remote -v )" | grep 'fetch' | awk '{ print $1 }' )"
echo "Project remote: ${remote}"
current_branch="$( git branch | grep '^*' | awk '{ print $NF }' )"
echo "Current branch: ${current_branch}"
echo "Please select the release branch you want to apply backports from:"
git branch | grep --color=none 'release-'
echo -n "> "
read release_branch
if ! grep -qwo "${release_branch}" <<<"$( git branch )"; then
echo "Invalid branch!"
exit 1
fi
echo "Please enter the release project name (e.g. 'Release 10.7.0')"
echo -n "> "
read gh_project
### REAL MEAT ###
echo "Synchronizing local repository with remote release branch"
git checkout ${release_branch}
git fetch --all
git reset --hard
git rebase ${remote}/${release_branch}
if [[ $? -ne 0 ]]; then
echo "Failed to rebase current master onto working directory! Stash or revert changes."
exit 1
fi
echo "Synchronizing local repository with remote master"
git checkout master
git fetch --all
git reset --hard
git rebase ${remote}/master
if [[ $? -ne 0 ]]; then
echo "Failed to rebase current master onto working directory! Stash or revert changes."
exit 1
fi
git checkout ${release_branch}
# Get all the stable-backport PRs
backport_pr_list="$( gh pr list --limit 999 --state merged --label "stable backport" 2>/dev/null | sort -n | awk '{ print $1 }' | tr -d '#' )"
backport_merges=()
echo "We will backport the following $( wc -w <<<"${backport_pr_list}" ) PRs from the branch ${release_branch} to master:"
echo "$( tr '\n' ' ' <<<"${backport_pr_list}" )"
echo
echo -n "Press <Enter> to continue. "
read
echo
git checkout master
for pr in ${backport_pr_list}; do
merge_commit="$( hub pr show -f '%sm' ${pr} 2>/dev/null )"
merge_title="$( hub pr show -f '%t' ${pr} 2>/dev/null )"
merge_author="$( git show -s --format='%an <%ae>' ${merge_commit}^2 )"
merge_committer="$( git show -s --format='%an <%ae>' ${merge_commit} )"
merge_backporter="$( git config --global user.name ) <$( git config --global user.email )>"
echo "Parsing ${merge_commit} for ${pr}..."
git cherry-pick -sxn -m1 ${merge_commit}
if [[ $? -ne 0 ]]; then
echo "Issue with merge! Review the changes in another terminal, run 'git add', then press <Enter>."
read
fi
git commit -m "Backport pull request #${pr} from jellyfin/${release_branch}" --author="${merge_author}" -m "${merge_title}" -m "Original-merge: ${merge_commit}" -m "Merged-by: ${merge_committer}" -m "Backported-by: ${merge_backporter}"
done
echo
echo "Make sure everything is OK, then press <Enter> to push changes, remove labels, and update projects."
read
git push -u ${remote}
export gh_project
gh_project_id="$( gh api -XGET -H'Accept:application/vnd.github.inertia-preview+json' "orgs/${org}/projects" 2>/dev/null | jq '.[] | select(.name==env.gh_project) | .id' )"
gh_project_completed_column_id="$( gh api -XGET -H'Accept:application/vnd.github.inertia-preview+json' "projects/${gh_project_id}/columns" 2>/dev/null | jq '.[] | select(.name=="Completed PRs") | .id' )"
gh_project_jellyfinished_column_id="$( gh api -XGET -H'Accept:application/vnd.github.inertia-preview+json' "projects/${gh_project_id}/columns" 2>/dev/null | jq '.[] | select(.name=="Jellyfinished") | .id' )"
for pr in ${backport_pr_list}; do
echo "Moving project card for PR ${pr}..."
export pr_url="https://api.github.com/repos/${org}/${repo}/issues/${pr}"
gh_project_card_id="$( gh api -XGET -H'Accept:application/vnd.github.inertia-preview+json' "projects/columns/${gh_project_completed_column_id}/cards" 2>/dev/null | jq '.[] | select(.content_url==env.pr_url) | .id' )"
gh api -XPOST -H'Accept:application/vnd.github.inertia-preview+json' /projects/columns/cards/${gh_project_card_id}/moves --field column_id=${gh_project_jellyfinished_column_id} --field position=bottom 2>/dev/null
echo "Removing backport label for PR ${pr}..."
gh api -XDELETE "repos/${org}/${repo}/issues/${pr}/labels/stable%20backport" 2>/dev/null
done
sudo chown -R build:build .
popd
exit 0