Skip to content

Commit

Permalink
Merge pull request #111 from reviewdog/reviewdog-17-rdjson
Browse files Browse the repository at this point in the history
Upgraded to ReviewDog 0.17.0, implemented RDJSON
  • Loading branch information
haya14busa authored Feb 3, 2024
2 parents dfce857 + dc8734f commit 2b4d11b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ runs:
- run: $GITHUB_ACTION_PATH/script.sh
shell: bash
env:
REVIEWDOG_VERSION: v0.15.0
REVIEWDOG_VERSION: v0.17.0
INPUT_GITHUB_TOKEN: ${{ inputs.github_token }}
INPUT_LEVEL: ${{ inputs.level }}
INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }}
Expand Down
33 changes: 9 additions & 24 deletions script.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/bin/sh

TEMP_PATH="$(mktemp -d)"
PATH="${TEMP_PATH}:$PATH"
export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"
STYLELINT_FORMATTER="${GITHUB_ACTION_PATH}/stylelint-formatter-rdjson/index.js"

__run_stylelint() {
cmd="stylelint ${INPUT_STYLELINT_INPUT} --formatter json"
cmd="stylelint ${INPUT_STYLELINT_INPUT} --custom-formatter ${STYLELINT_FORMATTER}"

if [ -n "${INPUT_STYLELINT_CONFIG}" ]; then
cmd="${cmd} --config='${INPUT_STYLELINT_CONFIG}'"
Expand All @@ -14,25 +19,8 @@ __run_stylelint() {
npx --no-install -c "${cmd}"
}

__rdformat_filter() {
input_filter='.[] | {source: .source, warnings:.warnings[]}'
output_filter='\(.source):\(.warnings.line):\(.warnings.column):\(.warnings.severity): \(.warnings.text)'
output_links_filter='[\(.warnings.rule)](\(if .warnings.rule | startswith("scss/") then "https://github.com/stylelint-scss/stylelint-scss/blob/master/src/rules/\(.warnings.rule | split("scss/") | .[1])/README.md" else "https://stylelint.io/user-guide/rules/\(.warnings.rule)" end))'

if [ "${INPUT_REPORTER}" = 'github-pr-review' ]; then
# Format results to include link to rule page.
echo "${input_filter} | \"${output_filter} ${output_links_filter}\""
else
echo "${input_filter} | \"${output_filter}\""
fi
}

__filter_json() {
jq "$(__rdformat_filter)"
}

__run_reviewdog() {
reviewdog -efm="%f:%l:%c:%t%*[^:]: %m" \
reviewdog -f=rdjson \
-name="${INPUT_NAME}" \
-reporter="${INPUT_REPORTER}" \
-level="${INPUT_LEVEL}" \
Expand All @@ -42,10 +30,6 @@ __run_reviewdog() {

cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit 1

TEMP_PATH="$(mktemp -d)"
PATH="${TEMP_PATH}:$PATH"
export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"

echo '::group:: Installing reviewdog 🐶 ... https://github.com/reviewdog/reviewdog'
curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh | sh -s -- -b "${TEMP_PATH}" "${REVIEWDOG_VERSION}" 2>&1
echo '::endgroup::'
Expand All @@ -66,7 +50,8 @@ fi
echo "stylelint version: $(npx --no-install -c 'stylelint --version')"

echo '::group:: Running stylelint with reviewdog 🐶 ...'
__run_stylelint | __filter_json | __run_reviewdog
stylelint_results=$(__run_stylelint)
echo "${stylelint_results}" | __run_reviewdog

reviewdog_rc=$?
echo '::endgroup::'
Expand Down
45 changes: 45 additions & 0 deletions stylelint-formatter-rdjson/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Stylelint Formatter to Output Reviewdog Diagnostic Format (RDFormat)
// https://github.com/reviewdog/reviewdog/tree/7ab09a1158ed4abd0eb0395ab0f1af6cfcdf513e/proto/rdf#rdjson
// https://stylelint.io/developer-guide/formatters

module.exports = function (results, returnValue) {
const rdjson = {
source: {
name: 'stylelint',
url: 'https://stylelint.io/'
},
diagnostics: []
};

results.filter(r => !r.ignored).forEach(result => {
const filePath = result.source;

result.warnings.forEach(warning => {
const diagnostic = {
message: warning.text,
location: {
path: filePath,
range: {
start: {
line: warning.line,
column: warning.column
},
end: warning.endLine && warning.endColumn ? {
line: warning.endLine,
column: warning.endColumn
} : undefined
}
},
severity: warning.severity.toUpperCase(),
code: {
value: warning.rule,
url: returnValue.ruleMetadata[warning.rule]?.url
}
};

rdjson.diagnostics.push(diagnostic);
});
});

return JSON.stringify(rdjson);
};

0 comments on commit 2b4d11b

Please sign in to comment.