Search code examples
bashgitlabgitlab-api

How to delete every closed milestone with gitlab api


Since we're a small team, we set the milestone with the number of the week. ex: week - 12.

But then, when started a new year, we cannot create those milestone anymore because they already exists and are closed.

I would like to delete every closed milestone and tried to use the giltab API for this purpose.

I've created the follwing script :

requesting every closed milestone working

# global.sh
get_closed_milestones(){
  curl -s -H "$private_token" \
  -X GET \
  "${base_url}groups/${group_name}/milestones?state=closed"
}

remove_milestone() {
 # echo result: https://gitlab.entepriseName.ch/api/v4/groups/entepriseName/milestones/279  
  curl -H "$private_token" \
  -X DELETE \
  "${base_url}groups/${group_name}/milestones/$1"
}

making a loop and deleting it

# milestone-remover.sh
#!/bin/bash

# Importes
source ./includes/global.sh

issues=$(get_closed_milestones)

  for milestone_id in $(jq -c '.[] | .id'<<< "$issues"); do

  milestone_id=${milestone_id%/r}


  # If not null
    if [ ! "$milestone_id" == "null/r" ];
    then
     remove_milestone "$milestone_id"
    fi

  done

I had to use the ! "$milestone_id" == "null/r" cause I realize I did get null like this.
It is looking a bit stang but it did send the request. I'll then had this response

+ curl -H 'PRIVATE-TOKEN: xxxxx' -X DELETE $'https://gitlab.entepriseName.ch/api/v4/groups/entepriseName/milestones/293\r'
curl: (3) URL using bad/illegal format or missing URL

Does somebody see an error?


Solution

  • I had to remove to remove the /r that was in the $milestone_id variable with the following code milestone_id="${milestone_id%%[[:cntrl:]]}"

    This is the final code

    issues=$(get_closed_milestones)
    
      for milestone_id in $(jq -c '.[] | .id'<<< "$issues"); do
    
      milestone_id="${milestone_id%%[[:cntrl:]]}"
    
    
      # If not null
        if [ ! "$milestone_id" == "null/r" ];
        then
         remove_milestone "$milestone_id"
        fi
    
      done