@@ -3,62 +3,62 @@ name: Devtron-auto-labeller
3
3
on :
4
4
issue_comment :
5
5
types : [created]
6
- pull_request_comment :
7
- types : [created]
6
+
8
7
9
8
jobs :
10
9
manage-labels :
11
10
runs-on : ubuntu-latest
12
11
13
12
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
+
14
27
- name : Parse and manage labels
15
- env :
16
- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
17
28
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
19
33
COMMENT_BODY=$(jq -r '.comment.body' "$GITHUB_EVENT_PATH")
20
34
ISSUE_NUMBER=$(jq -r '.issue.number // .pull_request.number' "$GITHUB_EVENT_PATH")
21
35
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')
26
38
27
-
39
+ # Add Label
28
40
if [[ "$COMMENT_BODY" =~ ^/([^ ]+)$ ]]; then
29
41
LABEL_NAME="${COMMENT_BODY:1}"
30
42
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
37
45
# 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."
45
48
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."
47
50
fi
48
51
fi
49
52
50
- # Remove labels when asked
53
+ # Remove Label Logic
51
54
if [[ "$COMMENT_BODY" =~ ^/remove[[:space:]](.+)$ ]]; then
52
55
LABEL_NAME_TO_REMOVE=$(echo "$COMMENT_BODY" | sed -n 's|/remove ||p')
53
56
54
- # Remove the specified label
57
+ # Remove the specified label
55
58
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."
61
61
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."
63
63
fi
64
- fi
64
+ fi
0 commit comments