markdown-linter.yml 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright Broadcom, Inc. All Rights Reserved.
  2. # SPDX-License-Identifier: APACHE-2.0
  3. name: '[CI/CD] Markdown linter'
  4. on:
  5. pull_request:
  6. branches:
  7. - main
  8. paths:
  9. - '**.md'
  10. - '!**/CHANGELOG.md'
  11. # Remove all permissions by default
  12. permissions: {}
  13. jobs:
  14. markdown-linter:
  15. runs-on: ubuntu-latest
  16. permissions:
  17. contents: read
  18. steps:
  19. - name: Install mardownlint
  20. run: npm install -g markdownlint-cli@0.33.0
  21. - name: Checkout project
  22. uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29
  23. - name: Execute markdownlint
  24. env:
  25. DIFF_URL: "${{github.event.pull_request.diff_url}}"
  26. TEMP_FILE: "${{runner.temp}}/pr-${{github.event.number}}.diff"
  27. TEMP_OUTPUT: "${{runner.temp}}/output"
  28. run: |
  29. # This request doesn't consume API calls.
  30. curl -Lkso $TEMP_FILE $DIFF_URL
  31. files_changed="$(sed -nr 's/[\-\+]{3} [ab]\/(.*)/\1/p' $TEMP_FILE | sort | uniq)"
  32. md_files="$(echo "$files_changed" | grep -v "CHANGELOG.md" | grep -o ".*\.md$" | sort | uniq || true)"
  33. # Create an empty file, useful when the PR changes ignored files
  34. touch "${TEMP_OUTPUT}"
  35. exit_code=0
  36. for f in ${md_files}; do
  37. # Looking for links that do not start with https, # or img
  38. if grep --quiet --perl-regexp '\]\((?!(http|#|img))[^\)]*' $f; then
  39. echo "::error:: Please ensure all links in $f starts with http(s), # or img"
  40. exit 1
  41. fi
  42. done
  43. markdownlint -o "${TEMP_OUTPUT}" ${md_files[@]} || exit_code=$?
  44. while read -r line; do
  45. # line format:
  46. # file:row[:column] message
  47. # white space inside brackets is intentional to detect the message for the notice.
  48. message="${line#*[ ]}"
  49. file_row_column="${line%%[ ]*}"
  50. # Split by ':'
  51. readarray -d : -t strarr < <(printf '%s' "$file_row_column")
  52. if [[ "${#strarr[@]}" -eq 3 ]]; then
  53. echo "::warning file=${strarr[0]},line=${strarr[1]},col=${strarr[2]}::${message}"
  54. elif [[ "${#strarr[@]}" -eq 2 ]]; then
  55. echo "::warning file=${strarr[0]},line=${strarr[1]}::${message}"
  56. else
  57. echo "::warning:: Error processing: ${line}"
  58. fi
  59. done < "${TEMP_OUTPUT}"
  60. if [[ $exit_code -ne 0 ]]; then
  61. echo "::error:: Please review linter messages"
  62. exit "$exit_code"
  63. fi