diff --git a/.gitlab/ci/download/select_jobs.gitlab-ci.yml b/.gitlab/ci/download/select_jobs.gitlab-ci.yml index 610c45a1b14314fd3384766aee3854419aae4094..41b51e87ba63dbea71ed0aa906254568a28f564c 100644 --- a/.gitlab/ci/download/select_jobs.gitlab-ci.yml +++ b/.gitlab/ci/download/select_jobs.gitlab-ci.yml @@ -94,7 +94,7 @@ .[] | sort_by(.commit.created_at) | reverse - ' <<<$(curl -s "$DEP_API_URL/repository/tags")) + ' <<<$(curl -s "$DEP_API_URL/repository/tags?per_page=100")) TAGS_BEFORE_DATE=$(jq --slurp --arg before_date "$PROJECT_CURR_TAG_DATE" ' .[] | map(select(.commit.created_at != null and (.commit.created_at <= $before_date))) @@ -302,6 +302,7 @@ $DEP_ID=$(Invoke-RestMethod -Uri "$API_URL/users/$USER_ID/projects?search=${DEP_NAME}" -Method Get)[0].id } Write-Host "DEP_ID = $DEP_ID" + $DEP_URL="$DEP_API_URL" ###################################################################################################### @@ -316,14 +317,17 @@ Write-Host "*************************************************************************" $DEFAULT_BRANCH="dev" $BRANCH_TO_PULL=$DEFAULT_BRANCH # default branch to pull - Write-Host "Retrieving branches of $DEP_NAME $API_URL/projects/$DEP_ID/repository/branches" - $DEP_BRANCHES=$($(Invoke-RestMethod -Uri "$API_URL/projects/$DEP_ID/repository/branches?per_page=100" -Method Get) | Select-Object name) - Write-Host "Found $($DEP_BRANCHES.Count) branches." - Write-Host "$($DEP_BRANCHES | Out-String)" Write-Host "Choosing a branch to pull dependending if we are in a Non draft merge request or not." Write-Host "CI_MERGE_REQUEST_TITLE = $CI_MERGE_REQUEST_TITLE" + + ################################ + # CASE NON DRAFT MERGE REQUEST if ( "$CI_MERGE_REQUEST_ID" -and ! ("$CI_MERGE_REQUEST_TITLE" -match 'Draft.*') ) { # case we are in a merge request # If the MR is not in draft: pulling from target branch (if exists otherwise dev) + Write-Host "Retrieving branches of $DEP_NAME $DEP_API_URL/repository/branches?per_page=100" + $DEP_BRANCHES=$($(Invoke-RestMethod -Uri "$DEP_API_URL/repository/branches?per_page=100" -Method Get) | Select-Object name) + Write-Host "Found $($DEP_BRANCHES.Count) branches." + Write-Host "$($DEP_BRANCHES | Out-String)" Write-Host "NON DRAFT MERGE REQUEST Pipeline detected : Pulling from CI_MERGE_REQUEST_TARGET_BRANCH_NAME : $CI_MERGE_REQUEST_TARGET_BRANCH_NAME." Write-Host "Checking if CI_MERGE_REQUEST_TARGET_BRANCH_NAME "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" branch exists, otherwise pull from default." $FILTERED_BRANCHES=@($DEP_BRANCHES | Where-Object { $_.name -eq "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"}) @@ -335,7 +339,42 @@ Write-Host "Branch $CI_MERGE_REQUEST_TARGET_BRANCH_NAME not found on repo $DEP_NAME." Write-Host "Pulling from default branch." } - } else { # CASE CASUAL COMMIT or Draft MR PIPELINE + ###################################### + # CASE WHERE A COMMIT TAG IS CREATED + } else if ( "$CI_COMMIT_TAG" ) { + $REPO_CURR_TAG_DATE=$($(Invoke-RestMethod -Uri "$DEP_API_URL/repository/tags/$CI_COMMIT_TAG" -Method Get) | Select-Object commit.created_at) + # PROJECT_CURR_TAG_DATE=$(jq '.commit.created_at' <<<$(curl -s "$API_URL/projects/$CI_PROJECT_ID/repository/tags/$CI_COMMIT_TAG")) + + Write-Host "Looking for latest release in $DEP_NAME before $CI_COMMIT_TAG was released on project $CI_PROJECT_NAME." + $DEP_TAGS_BEFORE_DATE=$($(Invoke-RestMethod -Uri "$DEP_API_URL/repository/tags?per_page=100" -Method Get) | + Sort-Object -Property commit.created_at -Descending | + Where-Object { $_.commit.created_at -eq "$CI_COMMIT_REF_NAME"} + $DEP_TAGS_BEFORE_DATE=$() + # TAGS_BEFORE_DATE=$(jq --slurp --arg before_date "$PROJECT_CURR_TAG_DATE" ' + # .[] + # | map(select(.commit.created_at != null and (.commit.created_at <= $before_date))) + # ' <<<"$DEP_TAGS") + + # SELECTED_TAG="" + # if [[ $(jq length <<<"$TAGS_BEFORE_DATE") != 0 ]]; then + # BRANCH_TO_PULL=$(jq -r '.[0].name' <<<"$TAGS_BEFORE_DATE") + # echo "Found release : $BRANCH_TO_PULL. Using it as \$BRANCH_TO_PULL." + # else + # echo "Found no release predating $CI_PROJECT_NAME $CI_COMMIT_TAG's date : $CI_COMMIT_TAG." + # echo "Retrieving latest repo tag." + # BRANCH_TO_PULL=$(jq -r '.[0].name' <<<"$DEP_TAGS") + # echo "\$BRANCH_TO_PULL = $BRANCH_TO_PULL" + # fi + + + + ########################################### + # CASE CASUAL COMMIT or Draft MR PIPELINE + } else { + Write-Host "Retrieving branches of $DEP_NAME $DEP_API_URL/repository/branches?per_page=100" + $DEP_BRANCHES=$($(Invoke-RestMethod -Uri "$DEP_API_URL/repository/branches?per_page=100" -Method Get) | Select-Object name) + Write-Host "Found $($DEP_BRANCHES.Count) branches." + Write-Host "$($DEP_BRANCHES | Out-String)" # checking if a branch with same name exists in $DEP_NAME, otherwise, pulling from dev Write-Host "No non draft merge request, looking for branch CI_COMMIT_REF_NAME : $CI_COMMIT_REF_NAME" $FILTERED_BRANCHES=@($DEP_BRANCHES | Where-Object { $_.name -eq "$CI_COMMIT_REF_NAME"}) @@ -361,14 +400,14 @@ Write-Host "**" Write-Host "*************************************************************************" Write-Host "*************************************************************************" - Write-Host "Retrieving regular pipelines from $API_URL/projects/$DEP_ID/pipelines?ref=$BRANCH_TO_PULL" - $PIPELINES=$(Invoke-RestMethod -Uri "$API_URL/projects/$DEP_ID/pipelines?ref=$BRANCH_TO_PULL" -Method Get) + Write-Host "Retrieving regular pipelines from $DEP_API_URL/pipelines?ref=$BRANCH_TO_PULL" + $PIPELINES=$(Invoke-RestMethod -Uri "$DEP_API_URL/pipelines?ref=$BRANCH_TO_PULL" -Method Get) Write-Host "Found $($PIPELINES.Count) pipelines linked with $BRANCH_TO_PULL as ref." - Write-Host "Retrieving Merge requests pipelines from $API_URL/projects/$DEP_ID/merge_requests?source_branch=$BRANCH_TO_PULL&state=opened" - $MR_ID=(Invoke-RestMethod -Uri "$API_URL/projects/$DEP_ID/merge_requests?source_branch=$BRANCH_TO_PULL&state=opened" -Method Get) + Write-Host "Retrieving Merge requests pipelines from $DEP_API_URL/merge_requests?source_branch=$BRANCH_TO_PULL&state=opened" + $MR_ID=(Invoke-RestMethod -Uri "$DEP_API_URL/merge_requests?source_branch=$BRANCH_TO_PULL&state=opened" -Method Get) Write-Host "Found $($MR_ID.Count) opened MR with $BRANCH_TO_PULL as source branch." if ( $MR_ID.Count -ne 0 ){ - $MR_PIPELINES=(Invoke-RestMethod -Uri "$API_URL/projects/$DEP_ID/merge_requests/$($MR_ID[0].iid)/pipelines" -Method Get) + $MR_PIPELINES=(Invoke-RestMethod -Uri "$DEP_API_URL/merge_requests/$($MR_ID[0].iid)/pipelines" -Method Get) $PIPELINES+=$MR_PIPELINES } ## Sort pipelines by "updated_at" from earliest to latest and only retrieve the ones whose url contain our project namespaec. @@ -389,8 +428,8 @@ for($i = 0; $i -lt $PIPELINES.Count ; $i++){ $PIPELINE = $PIPELINES[$i] Write-Host "current pipeline : $i = $PIPELINE" - Write-Host "curling from $API_URL/projects/$DEP_ID/pipelines/$($PIPELINE.id)/jobs" - $JOBS=$($(Invoke-RestMethod -Uri "$API_URL/projects/$DEP_ID/pipelines/$($PIPELINE.id)/jobs?name=$DEPENDENCY_JOB" -Method Get)) + Write-Host "curling from $DEP_API_URL/pipelines/$($PIPELINE.id)/jobs" + $JOBS=$($(Invoke-RestMethod -Uri "$DEP_API_URL/pipelines/$($PIPELINE.id)/jobs?name=$DEPENDENCY_JOB" -Method Get)) # Filter jobs by name $JOBS = @( $JOBS | @@ -443,3 +482,4 @@ Write-Host "*************************************************************************" Write-Host "SELECTED_JOBS = $($DEPS_SELECTED_JOBS | Out-String)" } +