From 2fc9652ee95e52fcef5361f232fe3f3e31788bbb Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Sat, 11 Sep 2021 18:39:51 +0000 Subject: [PATCH] Add support for other instances and full-instance listing --- gitlab-list-repos | 90 +++++++++++++++++++++++++++++++++++++++++++ gitlab.com-list-repos | 59 ---------------------------- 2 files changed, 90 insertions(+), 59 deletions(-) create mode 100755 gitlab-list-repos delete mode 100755 gitlab.com-list-repos diff --git a/gitlab-list-repos b/gitlab-list-repos new file mode 100755 index 0000000..fbbe468 --- /dev/null +++ b/gitlab-list-repos @@ -0,0 +1,90 @@ +#!/bin/bash +set -euo pipefail + +GIT_URLS_MODE='--git-urls' +MODES=("${GIT_URLS_MODE}") + +function usage { + echo "Usage: $0 [$(printf " | %s" "${MODES[@]}" | sed 's,^ | ,,')] [--instance URL] [USER_OR_GROUP_NAME...]" >&2 + echo "The instance URL defaults to https://gitlab.com/" >&2 + exit 1 +} + +mode='' +if [[ $# -ge 1 && " ${MODES[@]} " =~ " ${1} " ]] +then + mode="$1" + shift +fi + +instance=https://gitlab.com/ +if [[ $# -ge 2 && "$1" == '--instance' ]] +then + instance="$2" + shift + shift + if [[ "${instance}" != */ ]] + then + instance="${instance}/" + fi +fi + +if [[ $# -ge 1 && "$1" == --* ]]; then usage; fi + +if [[ $# -eq 0 ]] +then + target=instance + set '' +else + target=user-or-group +fi + +scriptpath="$(cd "$(dirname "$0")"; pwd -P)" + +for name +do + if [[ "${target}" == instance ]] + then + url="${instance}api/v4/projects?per_page=100" + else + page="$("${scriptpath}/curl-ua" firefox -s "${instance}${name}")" + if grep -q -F '&2 + url="${instance}api/v4/users/${name}/projects?per_page=100" + elif grep -q -F 'data-page="groups:show"' <<<"${page}" + then + echo "${name}: Group" >&2 + url="${instance}api/v4/groups/${name}/projects?per_page=100&include_subgroups=true" + else + echo "Error: unknown page type for ${name}" >&2 + exit 1 + fi + fi + + { + pipe=$(mktemp -u); mkfifo "${pipe}"; exec 3<>"${pipe}"; rm "${pipe}"; unset pipe # For the disgusting HTTP header treatment + "${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}" + declare -i nPages=$(head -1 <&3) + if [[ ${nPages} -ge 2 ]] + then + for ((page=2; page<=${nPages}; ++page)) + do + echo -n $'\r'"Requesting page ${page} of ${nPages}" >&2 + "${scriptpath}/curl-ua" firefox -s --fail "${url}&page=${page}" + done + echo >&2 + fi + } +done | \ + grep -Po '"path_with_namespace":\s*"\K[^"]+' | \ + awk -v instance="${instance}" '{ print instance $0; }' | \ + { + if [[ -z "${mode}" ]] + then + cat + elif [[ "${mode}" == "${GIT_URLS_MODE}" ]] + then + awk '{ print $0 ".git"; print $0 ".wiki.git"; }' + fi + } diff --git a/gitlab.com-list-repos b/gitlab.com-list-repos deleted file mode 100755 index 05ff821..0000000 --- a/gitlab.com-list-repos +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash -set -euo pipefail - -GIT_URLS_MODE='--git-urls' -MODES=("${GIT_URLS_MODE}") - -if [[ $# -lt 1 || ( $# -ge 2 && "$1" == --* && ! " ${MODES[@]} " =~ " ${1} " ) || "$1" == '-h' || "$1" == '--help' ]] -then - echo "Usage: $0 [$(printf " | %s" "${MODES[@]}" | sed 's,^ | ,,')] USER_OR_GROUP_NAME [USER_OR_GROUP_NAME...]" >&2 - exit 1 -fi -mode='' -if [[ $# -ge 2 && "$1" == --* ]] -then - mode="$1" - shift -fi - -scriptpath="$(cd "$(dirname "$0")"; pwd -P)" - -for name -do - page="$("${scriptpath}/curl-ua" firefox -s "https://gitlab.com/${name}")" - if grep -q -F '&2 - url="https://gitlab.com/api/v4/users/${name}/projects?per_page=100" - elif grep -q -F '
  • &2 - url="https://gitlab.com/api/v4/groups/${name}/projects?per_page=100&include_subgroups=true" - else - echo "Error: unknown page type for ${name}" >&2 - exit 1 - fi - - { - pipe=$(mktemp -u); mkfifo "${pipe}"; exec 3<>"${pipe}"; rm "${pipe}"; unset pipe # For the disgusting HTTP header treatment - "${scriptpath}/curl-ua" firefox -sv "${url}" 2> >(grep '^< x-total-pages: ' | sed 's,^.*: ,,' | tr -d '\r' >&3) - declare -i nPages=$(head -1 <&3) - if [[ ${nPages} -ge 2 ]] - then - for ((page=2; page<=${nPages}; ++page)) - do - echo -n $'\r'"Requesting page ${page} of ${nPages}" >&2 - "${scriptpath}/curl-ua" firefox -s "${url}&page=${page}" - done - echo >&2 - fi - } -done | grep -Po '"path_with_namespace":\s*"\K[^"]+' | sed 's,^,https://gitlab.com/,' | { - if [[ -z "${mode}" ]] - then - cat - elif [[ "${mode}" == "${GIT_URLS_MODE}" ]] - then - awk '{ print $0 ".git"; print $0 ".wiki.git"; }' - fi -}