Using the Gitlab API

Updated on 2021-08-09

I have build a small script gitlab-api.sh to ease the handling of gitlab groups. Yet, to use it for assignments, a little bit more specific magic is needed, using “jq” to parse the JSON-objects provided by the gitlab API.

The script has the HS Bremerhaven Gitlab Server hardcoded and uses “pass” to retrieve my API key.

The script provides sub-commands for “project”, “group”, and “uid” as first options. Best you take a look at the switch-case-tree at the end.

Getting Lists of Project Members

I use gitlab also to collect assignments — and, by the way, have my students do their work using collaborative versioning control. I use a wild mix of jq, bash and related tools here.

The following script first retrieves the list of projects I am member of at our gitlab server and sends it along the shell-pipe. First jq selects only those projects that are somehow named “assignment03” or a variation of that name. Of course, the same selection could be done in bash.

Then, after some parameter juggling to retrieve identifier and name of the project, I retrieve the members of the project. After removing lines that contain my name — or maybe any other tutor or teacher involved — I give an output as CSV (well semicolon-structured-value-list). Of course this filter could be implemented in jq instead of bash.

gitlab-api.sh p list | jq -c '.[]| select(.repo,.name | test("[aA]ssign(ment)?0?3"))' \
  |sort | uniq | \
  while read p; do 
    i=$(jq '.id' <<<$p); 
    n=$(jq '.name' <<<$p); 
    proj=${n##*/ }; 
    grp=${n%% /*};  
    gitlab-api.sh p $i m | jq '.[]|.name' | grep -Ev 'lafischer' |\
    while read stud; do 
      echo "$stud;$grp\";\"$proj";
    done ;
  done