The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

91 lines
2.1 KiB

  1. #!/bin/bash
  2. set -euo pipefail
  3. GIT_URLS_MODE='--git-urls'
  4. MODES=("${GIT_URLS_MODE}")
  5. function usage {
  6. echo "Usage: $0 [$(printf " | %s" "${MODES[@]}" | sed 's,^ | ,,')] [--instance URL] [USER_OR_GROUP_NAME...]" >&2
  7. echo "The instance URL defaults to https://gitlab.com/" >&2
  8. exit 1
  9. }
  10. mode=''
  11. if [[ $# -ge 1 && " ${MODES[@]} " =~ " ${1} " ]]
  12. then
  13. mode="$1"
  14. shift
  15. fi
  16. instance=https://gitlab.com/
  17. if [[ $# -ge 2 && "$1" == '--instance' ]]
  18. then
  19. instance="$2"
  20. shift
  21. shift
  22. if [[ "${instance}" != */ ]]
  23. then
  24. instance="${instance}/"
  25. fi
  26. fi
  27. if [[ $# -ge 1 && "$1" == --* ]]; then usage; fi
  28. if [[ $# -eq 0 ]]
  29. then
  30. target=instance
  31. set ''
  32. else
  33. target=user-or-group
  34. fi
  35. scriptpath="$(cd "$(dirname "$0")"; pwd -P)"
  36. for name
  37. do
  38. if [[ "${target}" == instance ]]
  39. then
  40. url="${instance}api/v4/projects?per_page=100"
  41. else
  42. page="$("${scriptpath}/curl-ua" firefox -s "${instance}${name}")"
  43. if grep -q -F '<a data-target="div#activity" data-action="activity" data-toggle="tab" href="' <<<"${page}"
  44. then
  45. echo "${name}: User" >&2
  46. url="${instance}api/v4/users/${name}/projects?per_page=100"
  47. elif grep -q -F 'data-page="groups:show"' <<<"${page}"
  48. then
  49. echo "${name}: Group" >&2
  50. url="${instance}api/v4/groups/${name}/projects?per_page=100&include_subgroups=true"
  51. else
  52. echo "Error: unknown page type for ${name}" >&2
  53. exit 1
  54. fi
  55. fi
  56. {
  57. pipe=$(mktemp -u); mkfifo "${pipe}"; exec 3<>"${pipe}"; rm "${pipe}"; unset pipe # For the disgusting HTTP header treatment
  58. "${scriptpath}/curl-ua" firefox -s --fail -D >({ grep -i -m 1 '^x-total-pages: ' || echo 'x-total-pages: 1'; } | sed 's,^.*: ,,' | tr -d '\r' >&3) "${url}"
  59. declare -i nPages=$(head -1 <&3)
  60. if [[ ${nPages} -ge 2 ]]
  61. then
  62. for ((page=2; page<=${nPages}; ++page))
  63. do
  64. echo -n $'\r'"Requesting page ${page} of ${nPages}" >&2
  65. "${scriptpath}/curl-ua" firefox -s --fail "${url}&page=${page}"
  66. done
  67. echo >&2
  68. fi
  69. }
  70. done | \
  71. grep -Po '"path_with_namespace":\s*"\K[^"]+' | \
  72. awk -v instance="${instance}" '{ print instance $0; }' | \
  73. {
  74. if [[ -z "${mode}" ]]
  75. then
  76. cat
  77. elif [[ "${mode}" == "${GIT_URLS_MODE}" ]]
  78. then
  79. awk '{ print $0 ".git"; print $0 ".wiki.git"; }'
  80. fi
  81. }