-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub-graphql-query.sh
More file actions
executable file
·217 lines (188 loc) · 5.94 KB
/
github-graphql-query.sh
File metadata and controls
executable file
·217 lines (188 loc) · 5.94 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/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 in the range requested.
# This script should be run before the weekly upstream merge while aligning
# the sof-dev and sof-dev-rebase branches. It cherrypicks the patches in the commit
# range to sof-dev-rebase and amends their commit message to add the Reviewed-by tags
# based on the the github PR info.
#
# 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:
#commit range should be start_commit..end_commit meaning the start_commit is not included
# ./github-graphql-query.sh GITHUB_TOKEN commit_range
# List of developers whose email address should be fixed up
linux_developers=(
"pierre-louis.bossart@intel.com" "pierre-louis.bossart@linux.intel.com"
"ranjani.sridharan@intel.com" "ranjani.sridharan@linux.intel.com"
"fred.oh@intel.com" "fred.oh@linux.intel.com"
"guennadi.liakhovetski@intel.com" "guennadi.liakhovetski@linux.intel.com"
"kai.vehmanen@intel.com" "kai.vehmanen@linux.intel.com"
"jaska.uimonen@intel.com" "jaska.uimonen@linux.intel.com"
"seppo.ingalsuo@intel.com" "seppo.ingalsuo@linux.intel.com"
"peter.ujfalusi@intel.com" "peter.ujfalusi@linux.intel.com"
"bard.liao@intel.com" "yung-chuan.liao@linux.intel.com"
"curtis@malainey.com" "cujomalainey@chromium.org"
)
github_email_fixup() {
let devs="${#linux_developers[@]} / 2"
declare -n ret=$2
for (( i=0; i<${devs}; i++ ));
do
let "dev_idx=$i*2"
if [[ $1 == ${linux_developers[$dev_idx]} ]]
then
ret=${linux_developers[$dev_idx+1]}
return 0
fi
done
}
if [[ $# != 2 ]]
then
echo "Missing commit or commit range."
exit 1
fi
# get commit SHA1s in an array
if [[ $2 == *".."* ]]
then
# a range is given
git log --reverse --oneline $2 > log.txt
echo "Commits to pick:"
else
# single commit to pick
git log -1 --oneline $2 > log.txt
echo "Commit to pick:"
fi
cat log.txt
cat log.txt | awk '{print $1}' > commits.txt
IFS=$'\n' read -d '' -r -a commits < commits.txt
#cherry-pick each commit while getting their reviewed-by tags
for SHA in "${commits[@]}"
do
script='query {
repository(name: \"linux\", owner: \"thesofproject\") {
commit: object(expression: \"'${SHA}'\") {
... on Commit {
associatedPullRequests(first: 1) {
edges {
node {
title
number
}
}
}
}
}
}
}'
script="$(echo $script)"
#get PR number from SHA
curl -s -H "Authorization: token $1" -X POST \
-d "{ \"query\": \"$script\"}" \
https://api.github.com/graphql | jq '.data.repository.commit.associatedPullRequests.edges' > node.json
PR_NUM=`cat node.json | jq -r '[.[] | .node.number] | .[]'`
PR_TITLE=`cat node.json | jq -r '[.[] | .node.title] | .[]'`
echo ""
echo "-----------------------------------------------"
git log --pretty='short' -1 ${SHA} | cat
echo ""
echo "from Github PR#${PR_NUM} - ${PR_TITLE}"
# Results could be paginated. So get the link head info and find the last page number to construct the URL
url="https://api.github.com/repos/thesofproject/linux/pulls/"$PR_NUM"/reviews"
curl -s -I -H "Authorization: token $1" $url > info.txt
IFS=$'\n' read -d '' -r -a info_lines < info.txt
found_link=0
for info_line in "${info_lines[@]}"
do
if [[ $info_line == *"link:"* ]]; then
found_link=1
break
fi
done
if [[ $found_link -eq 1 ]]; then
IFS=$',' read -d '' -r -a link_header_fields <<< $info_line
IFS=$';' read -d '' -r -a link_header <<< ${link_header_fields[1]}
# remove the brackets
url=`echo "${link_header[0]:2: -1}"`
fi
#get emails of users who approved the PR
#url="https://api.github.com/repos/thesofproject/linux/pulls/"$PR_NUM"/reviews"
curl -s -H "Authorization: token $1" $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"
git cherry-pick $SHA >&/dev/null
continue
fi
echo "Number of approvals: '$num_approvals'"
emails=""
fullnames=""
for element in "${hubuser[@]}"
do
github_name="${element%\"}"
github_name="${github_name#\"}"
url="https://api.github.com/users/"$github_name
email=`curl -s -H "Authorization: token $1" $url | jq '.email'`
fullname=`curl -s -H "Authorization: token $1" $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
github_email_fixup ${github_email} github_email
emails="${emails},${github_email}"
fullnames="${fullnames},${fullname}"
done
# just cherry-pick if there're no valid emails/full names
if [[ -z "$emails" ]] || [[ -z $fullnames ]]
then
git cherry-pick $SHA >&/dev/null
continue
fi
#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"\""
#cherry-pick and amend commit to add reviewed-by tags
git cherry-pick $SHA >&/dev/null
git rebase HEAD~1 -x 'git commit --amend -m "$(git log --format=%B -n1)$(/bin/echo -e '"$reviewed"')"' >&/dev/null
done
rm users.json
rm node.json
rm log.txt
rm commits.txt