-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck-pull-request.sh
executable file
·232 lines (199 loc) · 6.08 KB
/
check-pull-request.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
#
# Copyright 2018-present Material Foundation Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Fail on any error.
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
# The tagged version of https://github.com/material-foundation/github-comment to check out.
# A * wildcard can be used to check out the latest release of a given version.
GITHUB_COMMENT_VERSION="v1.*"
GITHUB_COMMENT_SRC_DIR="$DIR/.github-comment"
usage() {
echo "Usage: $0"
echo
echo "Runs clang-format against the current pull request's changes."
echo
echo "If clang-format suggested changes, the changes are posted to the pull"
echo "request as line-by-line comments and the script returns a non-zero exit"
echo "code. Otherwise, the script returns an exit code of 0."
echo
echo "If the git clang-format plugin is not installed, then it will be installed."
echo
echo "All arguments are required."
echo
echo " --api_token <token> A GitHub API token with a scope that matches the visibility"
echo " of the repo."
echo " Create a token at https://github.com/settings/tokens/new"
echo
echo " --repo <repo> The GitHub repo to which pull request comments should be"
echo " posted."
echo " E.g. material-components/material-components-ios"
echo
echo " --pr <number> The GitHub number of the pull request to which comments"
echo " should be posted."
echo
echo " --commit <SHA> The pull request's commit to which comments should be posted."
echo
echo " --target_branch <branch> The pull request's target branch."
echo " E.g. master"
echo
}
suggest_diff_changes() {
echo
echo "clang-format requires the following stylistic changes to be made:"
echo
git --no-pager diff
COMMENT_TMP_PATH=$(mktemp -d)
COMMENT_TMP_FILE="$COMMENT_TMP_PATH/comment"
DIFF_TMP_FILE="$COMMENT_TMP_PATH/diff"
echo "clang-format suggested the following change:" > "$COMMENT_TMP_FILE"
git --no-pager diff -U0 > "$DIFF_TMP_FILE"
echo "Posting results to GitHub..."
pushd "$GITHUB_COMMENT_SRC_DIR" >> /dev/null
swift run github-comment \
--repo="$REPO" \
--github_token="$API_TOKEN" \
--pull_request_number="$PULL_REQUEST" \
--commit="$COMMIT" \
--identifier=clang-format \
--comment_body="$COMMENT_TMP_FILE" \
--diff="$DIFF_TMP_FILE"
cat > "$COMMENT_TMP_FILE" <<EOL
clang-format recommended changes.
Consider installing [git-clang-format](https://github.com/llvm-mirror/clang/blob/master/tools/clang-format/git-clang-format) and running the following command:
\`\`\`bash
git clang-format \$(git merge-base origin/develop HEAD)
\`\`\`
EOL
swift run github-comment \
--repo="$REPO" \
--github_token="$API_TOKEN" \
--pull_request_number="$PULL_REQUEST" \
--identifier=clang-format \
--comment_body="$COMMENT_TMP_FILE"
popd >> /dev/null
exit 1
}
delete_comment() {
pushd "$GITHUB_COMMENT_SRC_DIR" >> /dev/null
# No recommended changes, so delete any existing comment
swift run github-comment \
--repo="$REPO" \
--github_token="$API_TOKEN" \
--pull_request_number="$PULL_REQUEST" \
--identifier=clang-format \
--delete
popd >> /dev/null
}
main() {
if ! git diff-index --quiet HEAD --; then
echo "Changes were already detected on the local branch prior to"
echo "running git clang-format. Refusing to continue."
exit 1
fi
if [ ! -d "$GITHUB_COMMENT_SRC_DIR" ]; then
git clone --recurse-submodules https://github.com/material-foundation/github-comment.git "$GITHUB_COMMENT_SRC_DIR"
fi
pushd "$GITHUB_COMMENT_SRC_DIR"
git fetch > /dev/null
TAG=$(git tag --sort=v:refname -l "$GITHUB_COMMENT_VERSION" | tail -n1)
if [ -z "$TAG" ]; then
echo "No tag matching $GITHUB_COMMENT_VERSION found in https://github.com/material-foundation/github-comment"
exit 1
fi
git checkout "$TAG" > /dev/null
echo "Using github-comment $TAG"
popd
if ! git clang-format -h > /dev/null 2> /dev/null; then
echo "git clang-format is not available. Please install clang-format"
echo "and git-clang-format and try again."
exit 1
fi
# If the target branch doesn't exist locally, it likely exists upstream.
if [ $(git branch --list "$TARGET_BRANCH") ]; then
base_sha=$(git merge-base "$TARGET_BRANCH" HEAD)
else
base_sha="HEAD^"
fi
echo "Running clang-format on changes from $base_sha to HEAD..."
git clang-format "$base_sha"
if ! git diff-index --quiet HEAD --; then
suggest_diff_changes
else
delete_comment
fi
}
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--api_token)
API_TOKEN="$2"
shift
shift
;;
--repo)
REPO="$2"
shift
shift
;;
--pr)
PULL_REQUEST="$2"
shift
shift
;;
--commit)
COMMIT="$2"
shift
shift
;;
--target_branch)
TARGET_BRANCH="$2"
shift
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [ -z "$API_TOKEN" ]; then
echo "--api_token is required."
usage
exit 1
fi
if [ -z "$REPO" ]; then
echo "--repo is required."
usage
exit 1
fi
if [ -z "$PULL_REQUEST" ]; then
echo "--pr is required."
usage
exit 1
fi
if [ -z "$COMMIT" ]; then
echo "--commit is required."
usage
exit 1
fi
if [ -z "$TARGET_BRANCH" ]; then
echo "--target_branch is required."
usage
exit 1
fi
main