check-pr.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: MIT
  3. # This script is executed by GitHub Actions for every pull request opened.
  4. # It currently accomplishes the following objectives:
  5. #
  6. # 1. Detect pages that were just copied (i.e. cp pages/{common,linux}/7z.md).
  7. # 2. Detect pages that were added in a platform specific directory although
  8. # they already exist under 'common'.
  9. # 3. Detect pages that were added in the 'common' platform although they
  10. # already exist under a platform specific directory.
  11. # 4. Detect pages that do not exist as English pages yet.
  12. # 5. Detect other miscellaneous anomalies in the pages folder.
  13. #
  14. # Results are printed to stdout, logs and errors to stderr.
  15. #
  16. # NOTE: must be run from the repository root directory to correctly work!
  17. # NOTE: no `set -e`, failure of this script should not invalidate the build.
  18. # Check for duplicated pages.
  19. function check_duplicates {
  20. local page=$1 # page path in the format 'platform/pagename.md'
  21. local parts
  22. local other
  23. readarray -td'/' parts < <(echo -n "$page")
  24. local platform=${parts[0]}
  25. local file=${parts[1]}
  26. case "$platform" in
  27. common) # check if page already exists in other platforms
  28. for other in ${PLATFORMS/common/}; do
  29. if [[ -f "pages/$other/$file" ]]; then
  30. printf "\x2d $MSG_EXISTS" "$page" "$other"
  31. fi
  32. done
  33. ;;
  34. *) # check if page already exists under common
  35. if [[ -f "pages/common/$file" ]]; then
  36. printf "\x2d $MSG_EXISTS" "$page" 'common'
  37. fi
  38. ;;
  39. esac
  40. }
  41. function check_missing_english_page() {
  42. local page="$1"
  43. local english_page="pages/${page#pages*\/}"
  44. if [[ ! -f "$english_page" ]]; then
  45. printf "\x2d $MSG_NOT_EXISTS" "$page" "$english_page"
  46. fi
  47. }
  48. # Look at git diff and check for copied/duplicated pages.
  49. function check_diff {
  50. local git_diff
  51. local line
  52. local entry
  53. git_diff=$(git diff --name-status --find-copies-harder --diff-filter=AC origin/main -- pages*/)
  54. if [[ -n $git_diff ]]; then
  55. echo -e "Check PR: git diff:\n$git_diff" >&2
  56. else
  57. echo 'Check PR: git diff looks fine, no interesting changes detected.' >&2
  58. return 0
  59. fi
  60. while read line; do
  61. readarray -td$'\t' entry < <(echo -n "$line")
  62. local change="${entry[0]}"
  63. local file1="${entry[1]}"
  64. local file2="${entry[2]}"
  65. case "$change" in
  66. C*) # file2 is a copy of file1
  67. local percentage=${change#C}
  68. percentage=${percentage#0}
  69. percentage=${percentage#0}
  70. printf "\x2d $MSG_IS_COPY" "$file2" "$file1" "$percentage"
  71. ;;
  72. A) # file1 was newly added
  73. check_duplicates "$file1"
  74. check_missing_english_page "$file1"
  75. ;;
  76. esac
  77. done <<< "$git_diff"
  78. }
  79. # Recursively check the pages/ folder for anomalies.
  80. function check_structure {
  81. for platform in $PLATFORMS; do
  82. if [[ ! -d "pages/$platform" ]]; then
  83. printf "\x2d $MSG_NOT_DIR" "pages/$platform"
  84. else
  85. for page in "pages/$platform"/*; do
  86. if [[ ! -f $page ]]; then
  87. printf "\x2d $MSG_NOT_FILE" "$page"
  88. elif [[ ${page:(-3)} != ".md" ]]; then
  89. printf "\x2d $MSG_NOT_MD" "$page"
  90. fi
  91. done
  92. fi
  93. done
  94. }
  95. ###################################
  96. # MAIN
  97. ###################################
  98. MSG_EXISTS='The page `%s` already exists under the `%s` platform.\n'
  99. MSG_NOT_EXISTS='The page `%s` does not exists as English page `%s` yet.\n'
  100. MSG_IS_COPY='The page `%s` seems to be a copy of `%s` (%d%% matching).\n'
  101. MSG_NOT_DIR='The file `%s` does not look like a directory.\n'
  102. MSG_NOT_FILE='The file `%s` does not look like a regular file.\n'
  103. MSG_NOT_MD='The file `%s` does not have a `.md` extension.\n'
  104. PLATFORMS=$(ls pages/)
  105. if [[ $CI == true && $GITHUB_REPOSITORY == "tldr-pages/tldr" && $PULL_REQUEST_ID != "" ]]; then
  106. check_diff
  107. check_structure
  108. else
  109. echo 'Not a pull request, refusing to run.' >&2
  110. exit 0
  111. fi