Skip to content

Commit

Permalink
adds github actions to add the label
Browse files Browse the repository at this point in the history
  • Loading branch information
satyampsoni committed Nov 25, 2024
1 parent eeaa468 commit a133113
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/auto-label.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Devtron-auto-labeller

on:
issue_comment:
types: [created]
pull_request_comment:
types: [created]

jobs:
manage-labels:
runs-on: ubuntu-latest

steps:
- name: Parse and manage labels
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
#comment body and issue/PR number
COMMENT_BODY=$(jq -r '.comment.body' "$GITHUB_EVENT_PATH")
ISSUE_NUMBER=$(jq -r '.issue.number // .pull_request.number' "$GITHUB_EVENT_PATH")
# check for existing labels on issues and prs
EXISTING_LABELS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER" | jq -r '.labels[].name')
if [[ "$COMMENT_BODY" =~ ^/([^ ]+)$ ]]; then
LABEL_NAME="${COMMENT_BODY:1}"
# Existence of label in the repository
REPO_LABELS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/labels")
if echo "$REPO_LABELS" | jq -e ".[] | select(.name == \"$LABEL_NAME\")" > /dev/null; then
# Add the requested label, keeping existing ones intact
UPDATED_LABELS=$(echo "$EXISTING_LABELS" | jq -R 'split("\n") | . + ["'"$LABEL_NAME"'"] | unique')
curl -s -X PATCH -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-d "{\"labels\": $UPDATED_LABELS}" \
"https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER"
echo "Successfully added label '$LABEL_NAME' to issue/PR #$ISSUE_NUMBER."
else
echo "The label '$LABEL_NAME' doesn't exist in the repository. Please create it first."
fi
fi
# Remove labels when asked
if [[ "$COMMENT_BODY" =~ ^/remove[[:space:]](.+)$ ]]; then
LABEL_NAME_TO_REMOVE=$(echo "$COMMENT_BODY" | sed -n 's|/remove ||p')
# Remove the specified label
if echo "$EXISTING_LABELS" | grep -q "^$LABEL_NAME_TO_REMOVE$"; then
curl -s -X DELETE -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER/labels/$LABEL_NAME_TO_REMOVE"
echo "Successfully removed label '$LABEL_NAME_TO_REMOVE' from issue/PR #$ISSUE_NUMBER."
else
echo "The label '$LABEL_NAME_TO_REMOVE' is not attached to issue/PR #$ISSUE_NUMBER."
fi
fi

0 comments on commit a133113

Please sign in to comment.