|
| 1 | +# Copyright 2020 The Knative Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# This file is automagically synced here from github.com/knative-sandbox/.github |
| 16 | +# repo by knobots: https://github.com/knative-sandbox/knobots and will be overwritten. |
| 17 | + |
| 18 | +name: Code Style |
| 19 | + |
| 20 | +on: |
| 21 | + pull_request: |
| 22 | + branches: [ 'main', 'master', 'release-*' ] |
| 23 | + |
| 24 | +jobs: |
| 25 | + |
| 26 | + autoformat: |
| 27 | + name: Auto-format and Check |
| 28 | + runs-on: ubuntu-latest |
| 29 | + strategy: |
| 30 | + fail-fast: false # Keep running if one leg fails. |
| 31 | + matrix: |
| 32 | + tool: |
| 33 | + - goimports |
| 34 | + - gofmt |
| 35 | + |
| 36 | + include: |
| 37 | + - tool: gofmt |
| 38 | + options: -s |
| 39 | + - tool: goimports |
| 40 | + importpath: golang.org/x/tools/cmd/goimports@latest |
| 41 | + |
| 42 | + steps: |
| 43 | + - name: Set up Go 1.18.x |
| 44 | + uses: actions/setup-go@v2 |
| 45 | + with: |
| 46 | + go-version: 1.18.x |
| 47 | + id: go |
| 48 | + |
| 49 | + - name: Check out code |
| 50 | + uses: actions/checkout@v2 |
| 51 | + |
| 52 | + - name: Install Dependencies |
| 53 | + if: ${{ matrix.importpath != '' }} |
| 54 | + run: | |
| 55 | + cd $(mktemp -d) |
| 56 | + GO111MODULE=on go install ${{ matrix.importpath }} |
| 57 | +
|
| 58 | + - name: ${{ matrix.tool }} ${{ matrix.options }} |
| 59 | + shell: bash |
| 60 | + run: > |
| 61 | + ${{ matrix.tool }} ${{ matrix.options }} -w |
| 62 | + $(find . |
| 63 | + -path './vendor' -prune |
| 64 | + -o -path './third_party' -prune |
| 65 | + -o -name '*.pb.go' -prune |
| 66 | + -o -name 'wire_gen.go' -prune |
| 67 | + -o -name '*.deepcopy.go' -prune |
| 68 | + -o -type f -name '*.go' -print) |
| 69 | +
|
| 70 | + - name: Verify ${{ matrix.tool }} |
| 71 | + shell: bash |
| 72 | + run: | |
| 73 | + # From: https://backreference.org/2009/12/23/how-to-match-newlines-in-sed/ |
| 74 | + # This is to leverage this workaround: |
| 75 | + # https://github.com/actions/toolkit/issues/193#issuecomment-605394935 |
| 76 | + function urlencode() { |
| 77 | + sed ':begin;$!N;s/\n/%0A/;tbegin' |
| 78 | + } |
| 79 | + if [[ $(git diff-index --name-only HEAD --) ]]; then |
| 80 | + for x in $(git diff-index --name-only HEAD --); do |
| 81 | + echo "::error file=$x::Please run ${{ matrix.tool }} ${{ matrix.options }}.%0A$(git diff $x | urlencode)" |
| 82 | + done |
| 83 | + echo "${{ github.repository }} is out of style. Please run ${{ matrix.tool }} ${{ matrix.options }}." |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | + echo "${{ github.repository }} is formatted correctly." |
| 87 | +
|
| 88 | + lint: |
| 89 | + name: Lint |
| 90 | + runs-on: ubuntu-latest |
| 91 | + |
| 92 | + steps: |
| 93 | + - name: Set up Go 1.18.x |
| 94 | + uses: actions/setup-go@v2 |
| 95 | + with: |
| 96 | + go-version: 1.18.x |
| 97 | + id: go |
| 98 | + |
| 99 | + - name: Check out code |
| 100 | + uses: actions/checkout@v2 |
| 101 | + |
| 102 | + - name: Install Tools |
| 103 | + env: |
| 104 | + WOKE_VERSION: v0.5.0 |
| 105 | + run: | |
| 106 | + TEMP_PATH="$(mktemp -d)" |
| 107 | + cd $TEMP_PATH |
| 108 | +
|
| 109 | + echo '::group::🐶 Installing reviewdog ... https://github.com/reviewdog/reviewdog' |
| 110 | + curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh | sh -s -- -b "${TEMP_PATH}" 2>&1 |
| 111 | + echo '::endgroup::' |
| 112 | +
|
| 113 | + echo '::group:: Installing misspell ... https://github.com/client9/misspell' |
| 114 | + go install github.com/client9/misspell/cmd/misspell@latest |
| 115 | + echo '::endgroup::' |
| 116 | +
|
| 117 | + echo '::group:: Installing woke ... https://github.com/get-woke/woke' |
| 118 | + curl -sfL https://raw.githubusercontent.com/get-woke/woke/main/install.sh | sh -s -- -b "${TEMP_PATH}" "${WOKE_VERSION}" 2>&1 |
| 119 | + echo '::endgroup::' |
| 120 | +
|
| 121 | + echo "${TEMP_PATH}" >> $GITHUB_PATH |
| 122 | +
|
| 123 | + - id: golangci_configuration |
| 124 | + uses: andstor/file-existence-action@v1 |
| 125 | + with: |
| 126 | + files: .golangci.yaml |
| 127 | + - name: Go Lint |
| 128 | + if: steps.golangci_configuration.outputs.files_exists == 'true' |
| 129 | + run: | |
| 130 | + make run-lint |
| 131 | +
|
| 132 | + - name: misspell |
| 133 | + shell: bash |
| 134 | + if: ${{ always() }} |
| 135 | + env: |
| 136 | + REVIEWDOG_GITHUB_API_TOKEN: ${{ github.token }} |
| 137 | + run: | |
| 138 | + set -e |
| 139 | + cd "${GITHUB_WORKSPACE}" || exit 1 |
| 140 | +
|
| 141 | + echo '::group:: Running github.com/client9/misspell with reviewdog 🐶 ...' |
| 142 | + # Don't fail because of misspell |
| 143 | + set +o pipefail |
| 144 | + # Exclude generated and vendored files, plus some legacy |
| 145 | + # paths until we update all .gitattributes |
| 146 | + git ls-files | |
| 147 | + git check-attr --stdin linguist-generated | grep -Ev ': (set|true)$' | cut -d: -f1 | |
| 148 | + git check-attr --stdin linguist-vendored | grep -Ev ': (set|true)$' | cut -d: -f1 | |
| 149 | + grep -Ev '^(vendor/|third_party/|.git|utils/)' | |
| 150 | + xargs misspell -error | |
| 151 | + reviewdog -efm="%f:%l:%c: %m" \ |
| 152 | + -name="github.com/client9/misspell" \ |
| 153 | + -reporter="github-pr-check" \ |
| 154 | + -filter-mode="added" \ |
| 155 | + -fail-on-error="true" \ |
| 156 | + -level="error" |
| 157 | +
|
| 158 | + echo '::endgroup::' |
| 159 | +
|
| 160 | +# - name: trailing whitespace |
| 161 | +# shell: bash |
| 162 | +# if: ${{ always() }} |
| 163 | +# env: |
| 164 | +# REVIEWDOG_GITHUB_API_TOKEN: ${{ github.token }} |
| 165 | +# run: | |
| 166 | +# set -e |
| 167 | +# cd "${GITHUB_WORKSPACE}" || exit 1 |
| 168 | +# |
| 169 | +# echo '::group:: Flagging trailing whitespace with reviewdog 🐶 ...' |
| 170 | +# # Don't fail because of grep |
| 171 | +# set +o pipefail |
| 172 | +# |
| 173 | +# # Exclude generated and vendored files, plus some legacy |
| 174 | +# # paths until we update all .gitattributes |
| 175 | +# git ls-files | |
| 176 | +# git check-attr --stdin linguist-generated | grep -Ev ': (set|true)$' | cut -d: -f1 | |
| 177 | +# git check-attr --stdin linguist-vendored | grep -Ev ': (set|true)$' | cut -d: -f1 | |
| 178 | +# grep -Ev '^(vendor/|third_party/|.git|utils/)' | |
| 179 | +# xargs grep -nE " +$" | |
| 180 | +# reviewdog -efm="%f:%l:%m" \ |
| 181 | +# -name="trailing whitespace" \ |
| 182 | +# -reporter="github-pr-check" \ |
| 183 | +# -filter-mode="added" \ |
| 184 | +# -fail-on-error="true" \ |
| 185 | +# -level="error" |
| 186 | +# |
| 187 | +# echo '::endgroup::' |
| 188 | + |
| 189 | + - name: EOF newline |
| 190 | + shell: bash |
| 191 | + if: ${{ always() }} |
| 192 | + env: |
| 193 | + REVIEWDOG_GITHUB_API_TOKEN: ${{ github.token }} |
| 194 | + run: | |
| 195 | + set -e |
| 196 | + cd "${GITHUB_WORKSPACE}" || exit 1 |
| 197 | +
|
| 198 | + echo '::group:: Flagging missing EOF newlines with reviewdog 🐶 ...' |
| 199 | + # Don't fail because of misspell |
| 200 | + set +o pipefail |
| 201 | + # Lint exclude rule: |
| 202 | + # - nothing in vendor/ |
| 203 | + # - nothing in third_party |
| 204 | + # - nothing in .git/ |
| 205 | + # - no *.ai (Adobe Illustrator) files. |
| 206 | + LINT_FILES=$(git ls-files | |
| 207 | + git check-attr --stdin linguist-generated | grep -Ev ': (set|true)$' | cut -d: -f1 | |
| 208 | + git check-attr --stdin linguist-vendored | grep -Ev ': (set|true)$' | cut -d: -f1 | |
| 209 | + grep -Ev '^(vendor/|third_party/|.git|utils/)' | |
| 210 | + grep -v '\.ai$' | |
| 211 | + grep -v '\.svg$') |
| 212 | +
|
| 213 | + for x in $LINT_FILES; do |
| 214 | + # Based on https://stackoverflow.com/questions/34943632/linux-check-if-there-is-an-empty-line-at-the-end-of-a-file |
| 215 | + if [[ -f $x && ! ( -s "$x" && -z "$(tail -c 1 $x)" ) ]]; then |
| 216 | + # We add 1 to `wc -l` here because of this limitation (from the man page): |
| 217 | + # Characters beyond the final <newline> character will not be included in the line count. |
| 218 | + echo $x:$((1 + $(wc -l $x | tr -s ' ' | cut -d' ' -f 1))): Missing newline |
| 219 | + fi |
| 220 | + done | |
| 221 | + reviewdog -efm="%f:%l: %m" \ |
| 222 | + -name="EOF Newline" \ |
| 223 | + -reporter="github-pr-check" \ |
| 224 | + -filter-mode="added" \ |
| 225 | + -fail-on-error="true" \ |
| 226 | + -level="error" |
| 227 | +
|
| 228 | + echo '::endgroup::' |
| 229 | +
|
| 230 | + # This is mostly copied from https://github.com/get-woke/woke-action-reviewdog/blob/main/entrypoint.sh |
| 231 | + # since their action is not yet released under a stable version. |
| 232 | + - name: Language |
| 233 | + if: ${{ always() && github.event_name == 'pull_request' }} |
| 234 | + shell: bash |
| 235 | + env: |
| 236 | + REVIEWDOG_GITHUB_API_TOKEN: ${{ github.token }} |
| 237 | + run: | |
| 238 | + set -e |
| 239 | + cd "${GITHUB_WORKSPACE}" || exit 1 |
| 240 | +
|
| 241 | + # Create a minimal .wokeignore if none already exist. |
| 242 | + if [ ! -f .wokeignore ]; then |
| 243 | + cat > .wokeignore <<EOF |
| 244 | + vendor/* |
| 245 | + third_party/* |
| 246 | + EOF |
| 247 | + fi |
| 248 | +
|
| 249 | + echo '::group:: Running woke with reviewdog 🐶 ...' |
| 250 | + woke --output simple \ |
| 251 | + | reviewdog -efm="%f:%l:%c: %m" \ |
| 252 | + -name="woke" \ |
| 253 | + -reporter="github-pr-check" \ |
| 254 | + -filter-mode="added" \ |
| 255 | + -fail-on-error="true" \ |
| 256 | + -level="error" |
| 257 | + echo '::endgroup::' |
0 commit comments