Skip to content

Commit

Permalink
fix(ci): check if milestone exists before trying to create new one
Browse files Browse the repository at this point in the history
  • Loading branch information
misiekhardcore authored and nikku committed Nov 1, 2024
1 parent 4764a5c commit bf14341
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions .github/workflows/CREATE_MILESTONE.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit bf14341

Please sign in to comment.