From 0ce0613ee760983f976a14f0f4c760be3cb83c07 Mon Sep 17 00:00:00 2001 From: haya14busa Date: Mon, 20 Sep 2021 16:00:16 +0000 Subject: [PATCH 1/6] Add install_shfmt.sh script $ godownloader --source=raw --repo='mvdan/sh' --exe=shfmt > install_shfmt.sh --- install_shfmt.sh | 337 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100755 install_shfmt.sh diff --git a/install_shfmt.sh b/install_shfmt.sh new file mode 100755 index 0000000..894ee88 --- /dev/null +++ b/install_shfmt.sh @@ -0,0 +1,337 @@ +#!/bin/sh +set -e +# Code generated by godownloader on 2021-09-20T15:59:56Z. DO NOT EDIT. +# + +usage() { + this=$1 + cat </dev/null +} +echoerr() { + echo "$@" 1>&2 +} +log_prefix() { + echo "$0" +} +_logp=6 +log_set_priority() { + _logp="$1" +} +log_priority() { + if test -z "$1"; then + echo "$_logp" + return + fi + [ "$1" -le "$_logp" ] +} +log_tag() { + case $1 in + 0) echo "emerg" ;; + 1) echo "alert" ;; + 2) echo "crit" ;; + 3) echo "err" ;; + 4) echo "warning" ;; + 5) echo "notice" ;; + 6) echo "info" ;; + 7) echo "debug" ;; + *) echo "$1" ;; + esac +} +log_debug() { + log_priority 7 || return 0 + echoerr "$(log_prefix)" "$(log_tag 7)" "$@" +} +log_info() { + log_priority 6 || return 0 + echoerr "$(log_prefix)" "$(log_tag 6)" "$@" +} +log_err() { + log_priority 3 || return 0 + echoerr "$(log_prefix)" "$(log_tag 3)" "$@" +} +log_crit() { + log_priority 2 || return 0 + echoerr "$(log_prefix)" "$(log_tag 2)" "$@" +} +uname_os() { + os=$(uname -s | tr '[:upper:]' '[:lower:]') + case "$os" in + cygwin_nt*) os="windows" ;; + mingw*) os="windows" ;; + msys_nt*) os="windows" ;; + esac + echo "$os" +} +uname_arch() { + arch=$(uname -m) + case $arch in + x86_64) arch="amd64" ;; + x86) arch="386" ;; + i686) arch="386" ;; + i386) arch="386" ;; + aarch64) arch="arm64" ;; + armv5*) arch="armv5" ;; + armv6*) arch="armv6" ;; + armv7*) arch="armv7" ;; + esac + echo ${arch} +} +uname_os_check() { + os=$(uname_os) + case "$os" in + darwin) return 0 ;; + dragonfly) return 0 ;; + freebsd) return 0 ;; + linux) return 0 ;; + android) return 0 ;; + nacl) return 0 ;; + netbsd) return 0 ;; + openbsd) return 0 ;; + plan9) return 0 ;; + solaris) return 0 ;; + windows) return 0 ;; + esac + log_crit "uname_os_check '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/client9/shlib" + return 1 +} +uname_arch_check() { + arch=$(uname_arch) + case "$arch" in + 386) return 0 ;; + amd64) return 0 ;; + arm64) return 0 ;; + armv5) return 0 ;; + armv6) return 0 ;; + armv7) return 0 ;; + ppc64) return 0 ;; + ppc64le) return 0 ;; + mips) return 0 ;; + mipsle) return 0 ;; + mips64) return 0 ;; + mips64le) return 0 ;; + s390x) return 0 ;; + amd64p32) return 0 ;; + esac + log_crit "uname_arch_check '$(uname -m)' got converted to '$arch' which is not a GOARCH value. Please file bug report at https://github.com/client9/shlib" + return 1 +} +untar() { + tarball=$1 + case "${tarball}" in + *.tar.gz | *.tgz) tar --no-same-owner -xzf "${tarball}" ;; + *.tar) tar --no-same-owner -xf "${tarball}" ;; + *.zip) unzip "${tarball}" ;; + *) + log_err "untar unknown archive format for ${tarball}" + return 1 + ;; + esac +} +http_download_curl() { + local_file=$1 + source_url=$2 + header=$3 + if [ -z "$header" ]; then + code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url") + else + code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url") + fi + if [ "$code" != "200" ]; then + log_debug "http_download_curl received HTTP status $code" + return 1 + fi + return 0 +} +http_download_wget() { + local_file=$1 + source_url=$2 + header=$3 + if [ -z "$header" ]; then + wget -q -O "$local_file" "$source_url" + else + wget -q --header "$header" -O "$local_file" "$source_url" + fi +} +http_download() { + log_debug "http_download $2" + if is_command curl; then + http_download_curl "$@" + return + elif is_command wget; then + http_download_wget "$@" + return + fi + log_crit "http_download unable to find wget or curl" + return 1 +} +http_copy() { + tmp=$(mktemp) + http_download "${tmp}" "$1" "$2" || return 1 + body=$(cat "$tmp") + rm -f "${tmp}" + echo "$body" +} +github_release() { + owner_repo=$1 + version=$2 + test -z "$version" && version="latest" + giturl="https://github.com/${owner_repo}/releases/${version}" + json=$(http_copy "$giturl" "Accept:application/json") + test -z "$json" && return 1 + version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//') + test -z "$version" && return 1 + echo "$version" +} +hash_sha256() { + TARGET=${1:-/dev/stdin} + if is_command gsha256sum; then + hash=$(gsha256sum "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f 1 + elif is_command sha256sum; then + hash=$(sha256sum "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f 1 + elif is_command shasum; then + hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1 + echo "$hash" | cut -d ' ' -f 1 + elif is_command openssl; then + hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f a + else + log_crit "hash_sha256 unable to find command to compute sha-256 hash" + return 1 + fi +} +hash_sha256_verify() { + TARGET=$1 + checksums=$2 + if [ -z "$checksums" ]; then + log_err "hash_sha256_verify checksum file not specified in arg2" + return 1 + fi + BASENAME=${TARGET##*/} + want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1) + if [ -z "$want" ]; then + log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'" + return 1 + fi + got=$(hash_sha256 "$TARGET") + if [ "$want" != "$got" ]; then + log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got" + return 1 + fi +} +cat /dev/null < Date: Mon, 20 Sep 2021 16:03:47 +0000 Subject: [PATCH 2/6] Install shfmt --- script.sh | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/script.sh b/script.sh index 8575258..4a18666 100755 --- a/script.sh +++ b/script.sh @@ -1,28 +1,8 @@ #!/bin/sh set -e -if [ -n "${GITHUB_WORKSPACE}" ] ; then - cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit -fi - -export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}" - -echo '::group::🐶 Installing misspell ... https://github.com/client9/misspell' +echo '::group::🐶 Installing shfmt ... https://github.com/mvdan/sh' TEMP_PATH="$(mktemp -d)" -PATH="${TEMP_PATH}:$PATH" -wget -O - -q https://git.io/misspell | sh -s -- -b "${TEMP_PATH}" -echo '::endgroup::' - -echo '::group:: Running misspell with reviewdog 🐶 ...' -# shellcheck disable=SC2086 -misspell -locale="${INPUT_LOCALE}" . \ - | reviewdog -efm="%f:%l:%c: %m" \ - -name="linter-name (misspell)" \ - -reporter="${INPUT_REPORTER}" \ - -filter-mode="${INPUT_FILTER_MODE}" \ - -fail-on-error="${INPUT_FAIL_ON_ERROR}" \ - -level="${INPUT_LEVEL}" \ - ${INPUT_REVIEWDOG_FLAGS} -exit_code=$? +$GITHUB_ACTION_PATH/install_shfmt.sh -b "${TEMP_PATH}" echo '::endgroup::' -exit $exit_code +echo "${TEMP_PATH}" >>"${GITHUB_PATH}" From f7516b767c54dfdd57094d9586c5cee9fe982646 Mon Sep 17 00:00:00 2001 From: haya14busa Date: Mon, 20 Sep 2021 16:27:54 +0000 Subject: [PATCH 3/6] implement the action --- action.yml | 41 ++++++++++++++++------------------------- script.sh | 8 +++++++- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/action.yml b/action.yml index 25b1f5d..2f89ed1 100644 --- a/action.yml +++ b/action.yml @@ -1,6 +1,6 @@ -name: 'TODO: Run with reviewdog' -description: 'TODO: 🐶 Run with reviewdog on pull requests to improve code review experience.' -author: 'TODO: ' +name: 'Run shfmt with reviewdog' +description: '🐶 Run shfmt with reviewdog on pull requests to improve code review experience.' +author: 'haya14busa' inputs: github_token: description: 'GITHUB_TOKEN' @@ -12,9 +12,6 @@ inputs: level: description: 'Report level for reviewdog [info,warning,error]' default: 'error' - reporter: - description: 'Reporter of reviewdog command [github-check,github-pr-review].' - default: 'github-check' filter_mode: description: | Filtering mode for the reviewdog command [added,diff_context,file,nofilter]. @@ -28,33 +25,27 @@ inputs: reviewdog_flags: description: 'Additional reviewdog flags' default: '' - ### Flags for ### - locale: - description: '-locale flag of misspell. (US/UK)' - default: '' + ### Flags for shfmt ### + shfmt_flags: + description: 'flags for shfmt' + default: '-i 2 -ci' runs: using: 'composite' steps: - - uses: reviewdog/action-setup@v1 - with: - reviewdog_version: v0.13.0 - run: $GITHUB_ACTION_PATH/script.sh shell: bash env: - # INPUT_ is not available in Composite run steps - # https://github.community/t/input-variable-name-is-not-available-in-composite-run-steps/127611 - INPUT_GITHUB_TOKEN: ${{ inputs.github_token }} - INPUT_LEVEL: ${{ inputs.level }} - INPUT_REPORTER: ${{ inputs.reporter }} - INPUT_FILTER_MODE: ${{ inputs.filter_mode }} - INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }} - INPUT_REVIEWDOG_FLAGS: ${{ inputs.reviewdog_flags }} - INPUT_ESLINT_FLAGS: ${{ inputs.eslint_flags }} - INPUT_WORKDIR: ${{ inputs.workdir }} - INPUT_TOOL_NAME: ${{ inputs.tool_name }} + INPUT_SHFMT_FLAGS: ${{ inputs.shfmt_flags }} + - uses: reviewdog/action-suggester@v1 + with: + tool_name: shfmt + github_token: ${{ inputs.github_token }} + level: ${{ inputs.level }} + filter_mode: ${{ inputs.filter_mode }} + fail_on_error: ${{ inputs.fail_on_error }} + reviewdog_flags: ${{ inputs.reviewdog_flags }} # Ref: https://haya14busa.github.io/github-action-brandings/ -# TODO: update branding if you want. branding: icon: 'check' color: 'blue' diff --git a/script.sh b/script.sh index 4a18666..3fb3a8d 100755 --- a/script.sh +++ b/script.sh @@ -1,8 +1,14 @@ #!/bin/sh set -e +if [ -n "${GITHUB_WORKSPACE}" ] ; then + cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit +fi + echo '::group::🐶 Installing shfmt ... https://github.com/mvdan/sh' TEMP_PATH="$(mktemp -d)" $GITHUB_ACTION_PATH/install_shfmt.sh -b "${TEMP_PATH}" echo '::endgroup::' -echo "${TEMP_PATH}" >>"${GITHUB_PATH}" + +# shellcheck disable=SC2086 +"${TEMP_PATH}/shfmt" ${INPUT_SHFMT_FLAGS} -w . From f92399bc7ee873f172c10145b9ee3d84b43c7194 Mon Sep 17 00:00:00 2001 From: haya14busa Date: Mon, 20 Sep 2021 16:36:02 +0000 Subject: [PATCH 4/6] add tests --- .github/workflows/test.yml | 39 +++----------------------------------- testdata/subdir/test.sh | 1 + testdata/subdir/text.md | 2 -- testdata/test.sh | 1 + testdata/text.md | 5 ----- 5 files changed, 5 insertions(+), 43 deletions(-) create mode 100644 testdata/subdir/test.sh delete mode 100644 testdata/subdir/text.md create mode 100644 testdata/test.sh delete mode 100644 testdata/text.md diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index adfd4ce..4aedeaf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,50 +1,17 @@ name: Test on: - push: - branches: - - main pull_request: jobs: - test-check: - name: runner / (github-check) - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: ./ - with: - github_token: ${{ secrets.github_token }} - reporter: github-check - level: info - locale: "US" - - test-pr-check: - if: github.event_name == 'pull_request' - name: runner / (github-pr-check) - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: ./ - with: - github_token: ${{ secrets.github_token }} - reporter: github-pr-check - level: warning - locale: "US" - workdir: ./testdata/subdir/ - test-pr-review: - if: github.event_name == 'pull_request' - name: runner / (github-pr-review) + name: runner / shfmt runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: ./ continue-on-error: true with: - github_token: ${{ secrets.github_token }} - reporter: github-pr-review - level: error - locale: "US" - reviewdog_flags: -filter-mode=file -fail-on-error + filter_mode: file + reviewdog_flags: -fail-on-error - name: check the exit code if: ${{ !success() }} run: echo 'The previous step should fail' && exit 1 diff --git a/testdata/subdir/test.sh b/testdata/subdir/test.sh new file mode 100644 index 0000000..1a553b9 --- /dev/null +++ b/testdata/subdir/test.sh @@ -0,0 +1 @@ +echo 'subdir test' diff --git a/testdata/subdir/text.md b/testdata/subdir/text.md deleted file mode 100644 index 5b33346..0000000 --- a/testdata/subdir/text.md +++ /dev/null @@ -1,2 +0,0 @@ -Determinisitic result is important! - diff --git a/testdata/test.sh b/testdata/test.sh new file mode 100644 index 0000000..ae42db3 --- /dev/null +++ b/testdata/test.sh @@ -0,0 +1 @@ +echo 'test' diff --git a/testdata/text.md b/testdata/text.md deleted file mode 100644 index 5025db8..0000000 --- a/testdata/text.md +++ /dev/null @@ -1,5 +0,0 @@ -Determinisitic result is important. - -colour # <= Check -locale - -langauge From ec99aad0f7f48bc453efc8a3ad57c8c2491a504f Mon Sep 17 00:00:00 2001 From: haya14busa Date: Mon, 20 Sep 2021 16:39:04 +0000 Subject: [PATCH 5/6] shellcheck: Double quote to prevent globbing and word splitting. SC2086 --- script.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script.sh b/script.sh index 3fb3a8d..e93d5d8 100755 --- a/script.sh +++ b/script.sh @@ -7,7 +7,7 @@ fi echo '::group::🐶 Installing shfmt ... https://github.com/mvdan/sh' TEMP_PATH="$(mktemp -d)" -$GITHUB_ACTION_PATH/install_shfmt.sh -b "${TEMP_PATH}" +"${GITHUB_ACTION_PATH}/install_shfmt.sh" -b "${TEMP_PATH}" echo '::endgroup::' # shellcheck disable=SC2086 From e5b1b53aa8c2591ac5b33de04a02b2116c71790a Mon Sep 17 00:00:00 2001 From: haya14busa Date: Tue, 21 Sep 2021 01:40:11 +0900 Subject: [PATCH 6/6] Update install_shfmt.sh Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- install_shfmt.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_shfmt.sh b/install_shfmt.sh index 894ee88..2fd0713 100755 --- a/install_shfmt.sh +++ b/install_shfmt.sh @@ -307,7 +307,7 @@ BINDIR=${BINDIR:-./bin} PREFIX="$OWNER/$REPO" # use in logging routines log_prefix() { - echo "$PREFIX" + echo "$PREFIX" } OS=$(uname_os) ARCH=$(uname_arch)