check-pr.sh 3.4 KB

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