summaryrefslogtreecommitdiff
path: root/gitlablib.sh
blob: 8f57b7dfe8fd4601ebf5a871f4d50634567a6256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh
# Startdate: 2020-05-29
# Dependencies:
#    jq
#    my private token
# Library for interacting with Gitlab API
# For manual work:
#    curl --header "${authheader}" "https://git.devuan.org/api/v4/projects/devuan%2Fdevuan-project/issues"
# References:
#    https://docs.gitlab.com/ee/api/README.html#pagination
#    handle transforming the / in the path_with_namespace to %2F per https://docs.gitlab.com/ee/api/README.html#namespaced-path-encoding https://docs.gitlab.com/ee/api/README.html#namespaced-path-encoding
#    https://docs.gitlab.com/ee/api/issues.html

export token="$( cat /mnt/public/work/devuan/git.devuan.org.token.txt )"
export authheader="Private-Token: ${token}"

export server=git.devuan.org

export GLL_TMPDIR="$( mktemp -d )"

clean_gitlablib() {
   rm -rf "${GLL_TMPDIR:-NOTHINGTODELETE}"/*
}

# PRIVATE
_handle_gitlab_pagination() {
   # call: list_all_projects "${startUri}"
   ___hgp_starturi="${1}"
   test -n "${GLL_DEBUG}" && set -x
   # BEGIN
   rhfile="$( TMPDIR="${GLL_TMPDIR}" mktemp -t "headers.XXXXXXXXXX" )"
   done=0
   size=-1
   uri="${___hgp_starturi}"

   # LOOP
   while test ${done} -eq 0 ;
   do
      response="$( curl -v -L --header "${authheader}" "${uri}" 2>"${rhfile}" )" 
      #grep -iE "^< link" "${rhfile}"
      # determine size
      if test "${size}" = "-1" ; then # run only if size is still undefined
         tmpsize="$( awk '$2 == "x-total:" {print $3}' "${rhfile}" 2>/dev/null )"
         test -n "${tmpsize}" && size="${tmpsize}"
         echo "Number of items: ${size}" 1>&2
      fi

      tmpnextpage="$( awk '$2 == "x-next-page:" {print $3}' "${rhfile}" 2>/dev/null )"
      # if x-next-page is blank, that means we are on the last page. Also, we could try x-total-pages compared to x-page.
      test -z "${tmpnextpage}" && done=1
      # so if we have a next page, get that link
      nextUri="$( awk '{$1="";$2="";print}' "${rhfile}" | tr ',' '\n' | awk -F';' '/rel="next"/{print $1}' | sed -r -e 's/^\s*<//;' -e 's/>\s*$//;' )"
      if test -n "${nextUri}" ; then
         uri="${nextUri}"
      else
         echo "No next page provided! Error." 1>&2
         done=1
      fi

      # show contents
      echo "${response}"
   done

   # cleanup
   rm "${rhfile}"
   set +x
}

list_all_projects() {
   _handle_gitlab_pagination "https://${server}/api/v4/projects"
}

list_all_issues() {
   _handle_gitlab_pagination "https://${server}/api/v4/issues?scope=all&status=all"
}

list_issues_for_project() {
   ___lifp_project="${1}"
   ___lifp_htmlencode_bool="${2}"
   istruthy "${___lifp_htmlencode_bool}" && ___lifp_project="$( echo "${___lifp_project}" | sed -r -e 's/\//%2F/g;' )"
   _handle_gitlab_pagination "https://${server}/api/v4/projects/${___lifp_project}/issues"
}

list_issues_for_all_projects_pipe() {
   # call: <projects.path_with_namespace.txt list_issues_for_all_projects_pipe
   echo "STUB"
}

bgstack15