From bf14341b369fd65a4dc7789f53987062e4937b5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Konopski?= Date: Fri, 1 Nov 2024 08:48:00 +0100 Subject: [PATCH] fix(ci): check if milestone exists before trying to create new one related to #https://github.com/bpmn-io/internal-docs/issues/1052 --- .github/workflows/CREATE_MILESTONE.yml | 35 ++++++++++++++------------ 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/.github/workflows/CREATE_MILESTONE.yml b/.github/workflows/CREATE_MILESTONE.yml index 442548fb1..fde97575f 100644 --- a/.github/workflows/CREATE_MILESTONE.yml +++ b/.github/workflows/CREATE_MILESTONE.yml @@ -12,32 +12,35 @@ jobs: steps: - name: Get new Milestone Title id: getTitle - env: + env: TITLE: ${{github.event.milestone.title}} run: | MILESTONE_NUMBER=${TITLE:1} INCREMENTED_NUMBER=$((MILESTONE_NUMBER + 1)) echo "MILESTONE_NAME=M$INCREMENTED_NUMBER" >> $GITHUB_OUTPUT + - name: Check if Milestone already exists + id: checkIfMilestoneExists + env: + TITLE: ${{steps.getTitle.outputs.MILESTONE_NAME}} + run: | + existing_milestones=$( + gh api -H "Accept: application/vnd.github.v3+json" \ + /repos/${{ github.repository }}/milestones \ + --jq '[.[] | select(.title | startswith("'$TITLE'")) | .number ][0]' + ) + if [ -z "$existing_milestones" ]; then + echo "MILESTONE_EXISTS=false" >> $GITHUB_OUTPUT + else + echo "MILESTONE_EXISTS=true" >> $GITHUB_OUTPUT + fi - name: Create new Milestone + if: steps.checkIfMilestoneExists.outputs.MILESTONE_EXISTS == 'false' env: TITLE: ${{steps.getTitle.outputs.MILESTONE_NAME}} GH_TOKEN: ${{ github.token }} shell: bash run: | - response=$(gh api \ + gh api \ --method POST \ /repos/${{ github.repository }}/milestones \ - -f "title=$TITLE" -f "state=open") - - # if response has a errors[].code === "already_exists" in a JSON response, we do nothing - if echo "$response" | jq -e '.errors | map(select(.code == "already_exists")) | length > 0' > /dev/null; then - echo "Milestone $TITLE already exists, no action needed" - # if there is .title === $TITLE in the response, we successfully created the milestone - elif echo "$response" | jq -e ".title == \"$TITLE\"" >/dev/null; then - echo "Milestone $TITLE created successfully" - # otherwise, we print the error - else - echo "Error creating milestone $TITLE" - echo $response - exit 1 - fi + -f "title=$TITLE" -f "state=open"