Skip to content

Commit 74ecc57

Browse files
authored
Merge pull request #6126 from satyampsoni/label
feat: Adds Github actions to add the label using slash command
2 parents 632f20c + da4a624 commit 74ecc57

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

.github/workflows/auto-label.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Devtron-auto-labeller
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
8+
jobs:
9+
manage-labels:
10+
if: ${{ !github.event.issue.pull_request }}
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout Repository
15+
uses: actions/checkout@v3
16+
17+
- name: Parse and manage labels
18+
env:
19+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20+
run: |
21+
set -e
22+
set -x # Enable debugging
23+
24+
# Extract comment on body and issue number
25+
COMMENT_BODY=$(jq -r '.comment.body' "$GITHUB_EVENT_PATH")
26+
ISSUE_NUMBER=$(jq -r '.issue.number // .pull_request.number' "$GITHUB_EVENT_PATH")
27+
28+
ORG_NAME="devtron-labs"
29+
30+
# checks if the person is authorized to add labels or not
31+
ORG_MEMBERSHIP_STATUS=$(gh api "orgs/$ORG_NAME/members/$COMMENT_AUTHOR" --silent --exit-status)
32+
33+
if [[ $? -ne 0 ]]; then
34+
gh issue comment "$ISSUE_NUMBER" --body "Hi @$COMMENT_AUTHOR, you must be a member of the organization '$ORG_NAME' to add or remove labels."
35+
36+
echo "User '$COMMENT_AUTHOR' is not a member of the organization '$ORG_NAME'. Exiting."
37+
exit 1
38+
fi
39+
40+
echo "User '$COMMENT_AUTHOR' is a verified member of the organization '$ORG_NAME'. Adding label"
41+
42+
43+
# Get the existing labels on the issue
44+
EXISTING_LABELS=$(gh issue view "$ISSUE_NUMBER" --json labels -q '.labels[].name')
45+
46+
# Add Label
47+
if [[ "$COMMENT_BODY" =~ ^/([^ ]+)$ ]]; then
48+
LABEL_NAME="${COMMENT_BODY:1}"
49+
50+
# check for already existing labels in reppo
51+
if gh label list --json name -q '.[].name' | grep -q "^$LABEL_NAME$"; then
52+
# Add the requested label, keeping existing ones intact
53+
gh issue edit "$ISSUE_NUMBER" --add-label "$LABEL_NAME"
54+
echo "Successfully added label '$LABEL_NAME' to issue #$ISSUE_NUMBER."
55+
else
56+
echo "The label '$LABEL_NAME' doesn't exist in the repository. You may need to create a label first."
57+
fi
58+
fi
59+
60+
# Remove Label Logic
61+
if [[ "$COMMENT_BODY" =~ ^/remove[[:space:]](.+)$ ]]; then
62+
LABEL_NAME_TO_REMOVE=$(echo "$COMMENT_BODY" | sed -n 's|/remove ||p')
63+
64+
# Remove the specified label
65+
if echo "$EXISTING_LABELS" | grep -q "^$LABEL_NAME_TO_REMOVE$"; then
66+
gh issue edit "$ISSUE_NUMBER" --remove-label "$LABEL_NAME_TO_REMOVE"
67+
echo "Successfully removed label '$LABEL_NAME_TO_REMOVE' from issue #$ISSUE_NUMBER."
68+
else
69+
echo "The label '$LABEL_NAME_TO_REMOVE' is not attached to issue #$ISSUE_NUMBER."
70+
fi
71+
fi

0 commit comments

Comments
 (0)