This repository was archived by the owner on Feb 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfrom-kokoro.sh
More file actions
executable file
·191 lines (164 loc) · 6.22 KB
/
from-kokoro.sh
File metadata and controls
executable file
·191 lines (164 loc) · 6.22 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
#!/bin/bash
#
# Copyright 2018-present The 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.
#
# Run clang-format and post suggested changes back to the pull request.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
# clang-format releases pulled from https://github.com/material-foundation/clang-format/releases
CLANG_FORMAT_TAG="r352957"
CLANG_FORMAT_SHA="bfd106536cb99207073b0626b41f3fd302ca127773c5a05f24e3daef9ae85d9b"
# git-clang-format commit pulled from
# https://github.com/llvm-mirror/clang/blob/master/tools/clang-format/git-clang-format
GIT_CLANG_FORMAT_COMMIT="c510fac5695e904b43d5bf0feee31cc9550f110e"
GIT_CLANG_FORMAT_SHA="1f6cfad79f90ea202dcf2d52a360186341a589cdbfdee05b0e7694f912aa9820"
usage() {
echo "Usage: $0 <repo>"
echo
echo "Will apply clang-format to changes made on the current branch from the merge-base of"
echo "the target branch. The result will be posted to GitHub as a series of inline comments."
echo
echo "Must set the following environment variables to run locally:"
echo
echo "GITHUB_API_TOKEN -> Create a token here: https://github.com/settings/tokens."
echo " Must have public_repo scope."
echo
echo "KOKORO_GITHUB_PULL_REQUEST_NUMBER=\"###\""
echo " The pull request # you want to post the API diff results to."
echo
echo "KOKORO_GITHUB_PULL_REQUEST_COMMIT=\"###\""
echo " The last commit of the pull request."
echo
echo "KOKORO_GITHUB_PULL_REQUEST_TARGET_BRANCH=\"###\""
echo " The branch that this pull request will be merged into."
}
REPO="$1"
if [ -z "$REPO" ]; then
usage
exit 1
fi
version_as_number() {
padded_version="${1%.}" # Strip any trailing dots
# Pad with .0 until we get a M.m.p version string.
while [ $(grep -o "\." <<< "$padded_version" | wc -l) -lt "2" ]; do
padded_version=${padded_version}.0
done
echo "${padded_version//.}"
}
# xcode-select's the provided xcode version.
# Usage example:
# select_xcode 9.2.0
select_xcode() {
desired_version="$1"
if [ -z "$desired_version" ]; then
return # No Xcode version to select.
fi
xcodes=$(ls /Applications/ | grep "Xcode")
for xcode_path in $xcodes; do
xcode_version=$(cat /Applications/$xcode_path/Contents/version.plist \
| grep "CFBundleShortVersionString" -A1 \
| grep string \
| cut -d'>' -f2 \
| cut -d'<' -f1)
xcode_version_as_number="$(version_as_number $xcode_version)"
if [ "$xcode_version_as_number" -ne "$(version_as_number $desired_version)" ]; then
continue
fi
sudo xcode-select --switch /Applications/$xcode_path/Contents/Developer
xcodebuild -version
# Resolves the following crash when switching Xcode versions:
# "Failed to locate a valid instance of CoreSimulatorService in the bootstrap"
launchctl remove com.apple.CoreSimulator.CoreSimulatorService || true
break
done
}
# Will run git clang-format on the branch's changes, reporting a failure if the linter generated any
# stylistic changes.
#
# For local runs, you must set the following environment variables:
#
# GITHUB_API_TOKEN -> Create a token here: https://github.com/settings/tokens.
# Must have public_repo scope.
# KOKORO_GITHUB_PULL_REQUEST_NUMBER="###" -> The PR # you want to post the API diff results to.
# KOKORO_GITHUB_PULL_REQUEST_COMMIT="..." -> The PR commit you want to post to.
#
# And install the following tools:
#
# - clang-format
# - git-clang-format
lint_clang_format() {
repo="$1"
if [ -z "$GITHUB_API_TOKEN" ]; then
echo "GITHUB_API_TOKEN must be set to a github token with public_repo scope."
usage
exit 1
fi
if [ -z "$KOKORO_GITHUB_PULL_REQUEST_NUMBER" ]; then
echo "KOKORO_GITHUB_PULL_REQUEST_NUMBER must be set to a github pull request number."
usage
exit 1
fi
if [ -z "$KOKORO_GITHUB_PULL_REQUEST_COMMIT" ]; then
echo "KOKORO_GITHUB_PULL_REQUEST_COMMIT must be set to a commit."
usage
exit 1
fi
if [ -z "$KOKORO_GITHUB_PULL_REQUEST_TARGET_BRANCH" ]; then
echo "$KOKORO_GITHUB_PULL_REQUEST_TARGET_BRANCH must be set to the target branch."
usage
exit 1
fi
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
select_xcode "$XCODE_VERSION"
mkdir bin
pushd bin >> /dev/null
# Install clang-format
echo "Downloading clang-format..."
curl -Ls "https://github.com/material-foundation/clang-format/releases/download/$CLANG_FORMAT_TAG/clang-format" -o "clang-format"
if openssl sha256 -sha256 "clang-format" | grep -q "$CLANG_FORMAT_SHA"; then
echo "SHAs match. Proceeding."
else
echo "clang-format does not match sha. Aborting."
exit 1
fi
chmod +x "clang-format"
echo "Downloading git-clang-format..."
# Install git-clang-format
curl -Ls "https://raw.githubusercontent.com/llvm-mirror/clang/$GIT_CLANG_FORMAT_COMMIT/tools/clang-format/git-clang-format" -o "git-clang-format"
if openssl sha256 -sha256 "git-clang-format" | grep -q "$GIT_CLANG_FORMAT_SHA"; then
echo "SHAs match. Proceeding."
else
echo "git-clang-format does not match sha. Aborting."
exit 1
fi
chmod +x "git-clang-format"
export PATH="$(pwd):$PATH"
popd >> /dev/null
# Move into our cloned repo
cd github/repo
fi
if ! git clang-format -h > /dev/null 2> /dev/null; then
echo
echo "git clang-format is not configured correctly."
echo "Please ensure that the git-clang-format command is in your PATH and that it is executable."
exit 1
fi
"$DIR/check-pull-request.sh" \
--api_token "$GITHUB_API_TOKEN" \
--repo "$repo" \
--pr "$KOKORO_GITHUB_PULL_REQUEST_NUMBER" \
--commit "$KOKORO_GITHUB_PULL_REQUEST_COMMIT" \
--target_branch "$KOKORO_GITHUB_PULL_REQUEST_TARGET_BRANCH"
}
lint_clang_format "$REPO"