check-pr.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 outdated pages. A page is marked as outdated when the number of
  13. # commands differ from the number of commands in the English page or the
  14. # contents of the commands differ from the English page.
  15. # 6. Detect other miscellaneous anomalies in the pages folder.
  16. #
  17. # Results are printed to stdout, logs and errors to stderr.
  18. #
  19. # NOTE: must be run from the repository root directory to correctly work!
  20. # NOTE: no `set -e`, failure of this script should not invalidate the build.
  21. # Check for duplicated pages.
  22. function check_duplicates {
  23. local page=$1 # page path in the format 'platform/pagename.md'
  24. local parts
  25. local other
  26. readarray -td'/' parts < <(echo -n "$page")
  27. local platform=${parts[0]}
  28. local file=${parts[1]}
  29. case "$platform" in
  30. common) # check if page already exists in other platforms
  31. for other in ${PLATFORMS/common/}; do
  32. if [[ -f "pages/$other/$file" ]]; then
  33. printf "\x2d $MSG_EXISTS" "$page" "$other"
  34. fi
  35. done
  36. ;;
  37. *) # check if page already exists under common
  38. if [[ -f "pages/common/$file" ]]; then
  39. printf "\x2d $MSG_EXISTS" "$page" 'common'
  40. fi
  41. ;;
  42. esac
  43. }
  44. function check_missing_english_page() {
  45. local page=$1
  46. local english_page="pages/${page#pages*\/}"
  47. if [[ "$page" = "$english_page" ]]; then
  48. return 1
  49. fi
  50. if [[ ! -f "$english_page" ]]; then
  51. printf "\x2d $MSG_NOT_EXISTS" "$page" "$english_page"
  52. fi
  53. }
  54. function check_outdated_page() {
  55. local page=$1
  56. local english_page="pages/${page#pages*\/}"
  57. local command_regex='^`[^`]\+`$'
  58. if [[ "$page" = "$english_page" ]] || [[ ! -f "$english_page" ]]; then
  59. return 1
  60. fi
  61. local english_commands=$(grep -c $command_regex "$english_page")
  62. mapfile -t stripped_english_commands < <(grep $command_regex "$english_page" | sed 's/{{[^}]*}}/{{}}/g' | sed 's/"[^"]*"/""/g' | sed "s/'[^']*'//g" | sed 's/`//g')
  63. local commands=$(grep -c $command_regex $page)
  64. mapfile -t stripped_commands < <(grep $command_regex "$page" | sed 's/{{[^}]*}}/{{}}/g' | sed 's/"[^"]*"/""/g' | sed "s/'[^']*'//g" | sed 's/`//g')
  65. local english_commands_as_string=$(printf "%s\n" "${stripped_english_commands[*]}")
  66. local commands_as_string=$(printf "%s\n" "${stripped_commands[*]}")
  67. if [[ $english_commands != $commands ]]; then
  68. printf "\x2d $MSG_OUTDATED" "$page" "based on number of commands"
  69. elif [[ "$english_commands_as_string" != "$commands_as_string" ]]; then
  70. printf "\x2d $MSG_OUTDATED" "$page" "based on the command contents itself"
  71. fi
  72. }
  73. # Look at git diff and check for copied/duplicated pages.
  74. function check_diff {
  75. local git_diff
  76. local line
  77. local entry
  78. git_diff=$(git diff --name-status --find-copies-harder --diff-filter=ACM origin/main -- pages*/)
  79. if [[ -n $git_diff ]]; then
  80. echo -e "Check PR: git diff:\n$git_diff" >&2
  81. else
  82. echo 'Check PR: git diff looks fine, no interesting changes detected.' >&2
  83. return 0
  84. fi
  85. while read line; do
  86. readarray -td$'\t' entry < <(echo -n "$line")
  87. local change="${entry[0]}"
  88. local file1="${entry[1]}"
  89. local file2="${entry[2]}"
  90. case "$change" in
  91. C*) # file2 is a copy of file1
  92. local percentage=${change#C}
  93. percentage=${percentage#0}
  94. percentage=${percentage#0}
  95. printf "\x2d $MSG_IS_COPY" "$file2" "$file1" "$percentage"
  96. ;;
  97. A|M) # file1 was newly added or modified
  98. check_duplicates "$file1"
  99. check_missing_english_page "$file1"
  100. check_outdated_page "$file1"
  101. ;;
  102. esac
  103. done <<< "$git_diff"
  104. }
  105. # Recursively check the pages/ folder for anomalies.
  106. function check_structure {
  107. for platform in $PLATFORMS; do
  108. if [[ ! -d "pages/$platform" ]]; then
  109. printf "\x2d $MSG_NOT_DIR" "pages/$platform"
  110. else
  111. for page in "pages/$platform"/*; do
  112. if [[ ! -f $page ]]; then
  113. printf "\x2d $MSG_NOT_FILE" "$page"
  114. elif [[ ${page:(-3)} != ".md" ]]; then
  115. printf "\x2d $MSG_NOT_MD" "$page"
  116. fi
  117. done
  118. fi
  119. done
  120. }
  121. ###################################
  122. # MAIN
  123. ###################################
  124. MSG_EXISTS='The page `%s` already exists under the `%s` platform.\n'
  125. MSG_NOT_EXISTS='The page `%s` does not exists as English page `%s` yet.\n'
  126. MSG_OUTDATED='The page `%s` is outdated, %s.\n'
  127. MSG_IS_COPY='The page `%s` seems to be a copy of `%s` (%d%% matching).\n'
  128. MSG_NOT_DIR='The file `%s` does not look like a directory.\n'
  129. MSG_NOT_FILE='The file `%s` does not look like a regular file.\n'
  130. MSG_NOT_MD='The file `%s` does not have a `.md` extension.\n'
  131. PLATFORMS=$(ls pages/)
  132. if [[ $CI == true && $GITHUB_REPOSITORY == "tldr-pages/tldr" && $PULL_REQUEST_ID != "" ]]; then
  133. check_diff
  134. check_structure
  135. else
  136. echo 'Not a pull request, refusing to run.' >&2
  137. exit 0
  138. fi