forked from dora-rs/dora
-
Notifications
You must be signed in to change notification settings - Fork 0
130 lines (116 loc) · 5.85 KB
/
Copy pathdora-bot-assign.yml
File metadata and controls
130 lines (116 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
name: "Dora Bot"
on:
issue_comment:
types: [created]
schedule:
- cron: "0 0 * * *" # Midnight(UTC)
jobs:
assign-unassign:
runs-on: ubuntu-latest
permissions:
issues: write
if: github.event_name == 'issue_comment'
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Parses comment then assign/unassign user
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMENT_BODY: "${{ github.event.comment.body }}"
ISSUE_NUMBER: "${{ github.event.issue.number }}"
COMMENT_AUTHOR: "${{ github.event.comment.user.login }}"
AUTHOR_ASSOCIATION: "${{ github.event.comment.author_association }}"
run: |
# For assigning
if [[ "$COMMENT_BODY" == "@dora-bot assign me" ]]; then
echo "Assigning $COMMENT_AUTHOR to issue #$ISSUE_NUMBER"
curl -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER/assignees \
-d "{\"assignees\":[\"$COMMENT_AUTHOR\"]}"
# Returns a comment back
curl -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER/comments \
-d "{\"body\":\"Hello @$COMMENT_AUTHOR, this issue is now assigned to you!\"}"
# for unassigning(self)
elif [[ "$COMMENT_BODY" == "@dora-bot unassign me" ]]; then
echo "Unassigning $COMMENT_AUTHOR from issue #$ISSUE_NUMBER"
curl -X DELETE \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER/assignees \
-d "{\"assignees\":[\"$COMMENT_AUTHOR\"]}"
# Returns a comment back
curl -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER/comments \
-d "{\"body\":\"Hello @$COMMENT_AUTHOR, you have been unassigned from this issue.\"}"
# Command to help maintainers to unassign
elif [[ "$COMMENT_BODY" =~ @dora-bot\ unassign\ [@]?([a-zA-Z0-9_-]+) ]]; then
TARGET_USER="${BASH_REMATCH[1]}"
# Checking that the comment author has proper permissions
if [[ "$AUTHOR_ASSOCIATION" == "NONE" || "$AUTHOR_ASSOCIATION" == "CONTRIBUTOR" ]]; then
echo "Unauthorized unassign command by $COMMENT_AUTHOR. Only maintainers or collaborators may unassign others."
exit 1
fi
echo "Maintainer $COMMENT_AUTHOR is unassigning $TARGET_USER from issue #$ISSUE_NUMBER"
curl -X DELETE \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER/assignees \
-d "{\"assignees\":[\"$TARGET_USER\"]}"
curl -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER/comments \
-d "{\"body\":\"Hello @$TARGET_USER, you have been unassigned from this issue by @$COMMENT_AUTHOR.\"}"
else
echo "No matching command found in comment: $COMMENT_BODY"
fi
stale-unassign:
runs-on: ubuntu-latest
permissions:
issues: write
if: github.event_name == 'schedule'
steps:
- name: Unassign stale issues
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Calculate the timestamp for 14 days ago
TWO_WEEKS_AGO=$(date -d "14 days ago" +%s)
repo="${{ github.repository }}"
echo "Fetching open issues for $repo"
issues=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/${repo}/issues?state=open&per_page=100")
issue_count=$(echo "$issues" | jq '. | length')
echo "Found $issue_count open issues"
for (( i=0; i<$issue_count; i++ )); do
issue_number=$(echo "$issues" | jq -r ".[$i].number")
updated_at=$(echo "$issues" | jq -r ".[$i].updated_at")
updated_ts=$(date -d "$updated_at" +%s)
# If the issue hasn't been updated within 2 weeks, consider it stale.
if [[ $updated_ts -lt $TWO_WEEKS_AGO ]]; then
assignees=$(echo "$issues" | jq -r ".[$i].assignees | .[].login")
if [[ -n "$assignees" ]]; then
echo "Issue #$issue_number is stale. Unassigning users: $assignees"
for user in $assignees; do
curl -X DELETE \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${repo}/issues/$issue_number/assignees \
-d "{\"assignees\":[\"$user\"]}"
curl -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${repo}/issues/$issue_number/comments \
-d "{\"body\":\"@${user} has been automatically unassigned from this stale issue after 2 weeks of inactivity.\"}"
done
fi
fi
done