-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-pr-reviewed-by.sh
executable file
·132 lines (110 loc) · 3.42 KB
/
add-pr-reviewed-by.sh
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
129
130
131
132
#!/bin/bash
#
# This script lets you automatically get the approvals from a github Pull Request
# and add the Reviewed-by tag to all the commits.
#
# Pre-requirements:
# 1. sudo apt-get install jq
# 2. snap install hub
# 3. Generate a personal access token on github (select repo and user) and save it somewhere.
#
# Usage:
# ./add-pr-reviewed-by.sh PR_NUMBER GITHUB_TOKEN
PR_NUM=$1
#get emails of users who approved the PR
url="https://api.github.com/repos/thesofproject/linux/pulls/"$PR_NUM"/reviews"
curl -H "Authorization: token $2" $url | jq -r '[.[] | select(.state=="APPROVED") | {user: .user.login}]' > users.json
userarray=`cat users.json | jq -r '[.[] | join(",")] | @csv'`
IFS=',' read -r -a hubuser <<< "$userarray"
num_approvals=${#hubuser[@]}
if [ $num_approvals == 0 ]
then
echo "No approvals for PR"$1
exit
fi
emails=""
fullnames=""
for element in "${hubuser[@]}"
do
github_name="${element%\"}"
github_name="${github_name#\"}"
url="https://api.github.com/users/"$github_name
email=`curl -H "Authorization: token $2" $url | jq '.email'`
fullname=`curl -H "Authorization: token $2" $url | jq '.name'`
#remove the quotes
github_email="${email%\"}"
github_email="${github_email#\"}"
fullname="${fullname%\"}"
fullname="${fullname#\"}"
#skip if name or github email is not set
if [[ "$github_email" == "null" ]] || [[ $fullname == "null" ]]
then
continue
fi
if [[ -z "$github_email" ]] || [[ -z $fullname ]]
then
continue
fi
emails="${emails},${github_email}"
fullnames="${fullnames},${fullname}"
done
#remove the comma at the beginning
emails="${emails%\"}"
fullnames="${fullnames%\"}"
#split emails into array
IFS=',' read -r -a email_array <<< "$emails"
IFS=',' read -r -a fullname_array <<< "$fullnames"
#create the Reviewed-by string to be added to commits
reviewed=""
i=0
for element in "${email_array[@]}"
do
if [ ! -z "$element" ]
then
if [ ! -z "$reviewed" ]
then
reviewed=${reviewed}$"\nReviewed-by: "${fullname_array[$i]}" <"$element">"
else
reviewed="\"\nReviewed-by: "${fullname_array[$i]}" <"$element">"
fi
fi
i=$i+1
done
reviewed=$reviewed"\""
#count the number of commits
url="https://api.github.com/repos/thesofproject/linux/pulls/"$1"/commits"
curl -H "Authorization: token $2" $url | jq -r '[.[] | {sha: .sha}]' > commits.json
num_commits=`cat commits.json | jq '. | length'`
#checkout pull request
branch_name="pr"$PR_NUM
pull_head="pull/"$PR_NUM"/head"
git fetch thesofproject $pull_head:$branch_name
git checkout $branch_name
#add reviewed-by
git rebase HEAD~$num_commits -x 'git commit --amend -m "$(git log --format=%B -n1)$(echo '"$reviewed"')"'
#get the PR branch
url="https://api.github.com/repos/thesofproject/linux/pulls/"$PR_NUM
repo_name=`curl -H "Authorization: token $2" $url | jq '.head.repo.full_name'`
pr_branch=`curl -H "Authorization: token $2" $url | jq '.head.ref'`
#remove the quotes
repo_name="${repo_name%\"}"
repo_name="${repo_name#\"}"
pr_branch="${pr_branch%\"}"
pr_branch="${pr_branch#\"}"
branch_url="https://github.com/"$repo_name"/"$pr_branch
repo_url="https://github.com/"$repo_name
#force push the changes
if `git push -f $repo_url $branch_name:$pr_branch`
then
echo "Updated PR branch "$pr_branch" successfully"
else
echo "Permissed denied to update "$repo_url". Aborting merge"
exit
fi
sleep 5
#merge the PR now
export GITHUB_TOKEN=$2
hub api -XPUT "repos/thesofproject/linux/pulls/$PR_NUM/merge" -f merge_method=rebase
#remove the json files
rm commits.json
rm users.json