Skip to content

Commit eb6e6bd

Browse files
committed
modified the actions to use CLI
1 parent a133113 commit eb6e6bd

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

.github/workflows/auto-label.yml

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,62 @@ name: Devtron-auto-labeller
33
on:
44
issue_comment:
55
types: [created]
6-
pull_request_comment:
7-
types: [created]
6+
87

98
jobs:
109
manage-labels:
1110
runs-on: ubuntu-latest
1211

1312
steps:
13+
- name: Checkout Repository
14+
uses: actions/checkout@v3
15+
16+
- name: Install GitHub CLI
17+
run: |
18+
sudo apt-get install -y jq
19+
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
20+
echo "deb [signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
21+
sudo apt update && sudo apt install gh -y
22+
23+
- name: Authenticate with GitHub CLI
24+
run: |
25+
echo ${{ secrets.GITHUB_TOKEN }} | gh auth login --with-token
26+
1427
- name: Parse and manage labels
15-
env:
16-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1728
run: |
18-
#comment body and issue/PR number
29+
set -e
30+
set -x # Enable debugging
31+
32+
# Extract comment on body and issue number
1933
COMMENT_BODY=$(jq -r '.comment.body' "$GITHUB_EVENT_PATH")
2034
ISSUE_NUMBER=$(jq -r '.issue.number // .pull_request.number' "$GITHUB_EVENT_PATH")
2135
22-
# check for existing labels on issues and prs
23-
EXISTING_LABELS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
24-
-H "Accept: application/vnd.github.v3+json" \
25-
"https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER" | jq -r '.labels[].name')
36+
# Get the existing labels on the issue
37+
EXISTING_LABELS=$(gh issue view "$ISSUE_NUMBER" --json labels -q '.labels[].name')
2638
27-
39+
# Add Label
2840
if [[ "$COMMENT_BODY" =~ ^/([^ ]+)$ ]]; then
2941
LABEL_NAME="${COMMENT_BODY:1}"
3042
31-
# Existence of label in the repository
32-
REPO_LABELS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
33-
-H "Accept: application/vnd.github.v3+json" \
34-
"https://api.github.com/repos/${{ github.repository }}/labels")
35-
36-
if echo "$REPO_LABELS" | jq -e ".[] | select(.name == \"$LABEL_NAME\")" > /dev/null; then
43+
# check for already existing labels in reppo
44+
if gh label list --json name -q '.[].name' | grep -q "^$LABEL_NAME$"; then
3745
# Add the requested label, keeping existing ones intact
38-
UPDATED_LABELS=$(echo "$EXISTING_LABELS" | jq -R 'split("\n") | . + ["'"$LABEL_NAME"'"] | unique')
39-
curl -s -X PATCH -H "Authorization: token $GITHUB_TOKEN" \
40-
-H "Accept: application/vnd.github.v3+json" \
41-
-d "{\"labels\": $UPDATED_LABELS}" \
42-
"https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER"
43-
44-
echo "Successfully added label '$LABEL_NAME' to issue/PR #$ISSUE_NUMBER."
46+
gh issue edit "$ISSUE_NUMBER" --add-label "$LABEL_NAME"
47+
echo "Successfully added label '$LABEL_NAME' to issue #$ISSUE_NUMBER."
4548
else
46-
echo "The label '$LABEL_NAME' doesn't exist in the repository. Please create it first."
49+
echo "The label '$LABEL_NAME' doesn't exist in the repository. You may need to create a label first."
4750
fi
4851
fi
4952
50-
# Remove labels when asked
53+
# Remove Label Logic
5154
if [[ "$COMMENT_BODY" =~ ^/remove[[:space:]](.+)$ ]]; then
5255
LABEL_NAME_TO_REMOVE=$(echo "$COMMENT_BODY" | sed -n 's|/remove ||p')
5356
54-
# Remove the specified label
57+
# Remove the specified label
5558
if echo "$EXISTING_LABELS" | grep -q "^$LABEL_NAME_TO_REMOVE$"; then
56-
curl -s -X DELETE -H "Authorization: token $GITHUB_TOKEN" \
57-
-H "Accept: application/vnd.github.v3+json" \
58-
"https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER/labels/$LABEL_NAME_TO_REMOVE"
59-
60-
echo "Successfully removed label '$LABEL_NAME_TO_REMOVE' from issue/PR #$ISSUE_NUMBER."
59+
gh issue edit "$ISSUE_NUMBER" --remove-label "$LABEL_NAME_TO_REMOVE"
60+
echo "Successfully removed label '$LABEL_NAME_TO_REMOVE' from issue #$ISSUE_NUMBER."
6161
else
62-
echo "The label '$LABEL_NAME_TO_REMOVE' is not attached to issue/PR #$ISSUE_NUMBER."
62+
echo "The label '$LABEL_NAME_TO_REMOVE' is not attached to issue #$ISSUE_NUMBER."
6363
fi
64-
fi
64+
fi

0 commit comments

Comments
 (0)