check-pr.sh 3.5 KB

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