func_Milestones.ps1

# ---------------------------------------------------------------------
# Project milestones API
# https://docs.gitlab.com/ee/api/milestones.html
function Get-GitlabMilestoneID( [Parameter(Mandatory=$true)] [string] $project
                              , [Parameter(Mandatory=$false)][string] $title
                              )
{
  if ([string]::IsNullOrWhiteSpace($title)) {
    return 0
  }

  # query project
  [string] $GAPI_MILESTONES = "$CI_API_V4_URL/projects/$project/milestones?title=$title"
  $milestone = Invoke-RestMethod -headers $GLPT -uri $GAPI_MILESTONES -method GET
  if ($milestone) { return $milestone[0].id }

  # query groups in reverse order
  [string] $group = $project
  do {
    # remove last subgroup entry
    $group = $group -replace '%2F[^%]+$'

    $GAPI_MILESTONES = "$CI_API_V4_URL/groups/$group/milestones?title=$title"
    $milestone = Invoke-RestMethod -headers $GLPT -uri $GAPI_MILESTONES -method GET

    if ($milestone) { return $milestone[0].id }
  } while ($group -cmatch '%2F')  # stop after top level group

  return 0
}