This repository has been archived by the owner on Oct 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
184 lines (156 loc) · 6.68 KB
/
Comment_command.yml
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
name: Comment command
on:
issue_comment:
types: [created]
jobs:
comment_command:
name: Comment command ${{ github.event.issue.number }}
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ github.token }}
steps:
- name: Debug github context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
shell: bash
- name: Check if member
id: member
run: |
gh api \
-H "Accept: application/vnd.github+json" \
/orgs/${{ github.repository_owner }}/members/${{ github.actor }} || echo "member=false" >> $GITHUB_OUTPUT
- name: Get Actor
if: ${{ steps.member.outputs.member != 'false' }}
id: get_actor
run: |
echo "actor=${{ github.actor }}" >> $GITHUB_OUTPUT
- name: Comment command
env:
COMMENT_BODY: ${{ github.event.comment.body }}
COMMENT_ID: ${{ github.event.comment.id }}
ISSUE_BODY: ${{ github.event.issue.body }}
# https://regex101.com/r/vHEc61/1
YT_REGEX: '^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$' # yamllint disable-line rule:line-length
if: >-
steps.member.outputs.member != 'false' &&
steps.get_actor.outputs.actor != 'LizardByte-bot'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_BOT_TOKEN }}
script: |
const {COMMENT_BODY, ISSUE_BODY} = process.env
const COMMENT_ID = parseInt(process.env.COMMENT_ID)
const YT_REGEX = new RegExp(process.env.YT_REGEX, 'gim') // global, case-insensitive, multiline
// get comment body
let comment = `${COMMENT_BODY}`
console.log(`comment: ${comment}`)
// strip any whitespace
comment = comment.trim()
// remove any double spaces
do {
comment = comment.replace(" ", " ")
} while (comment.includes(" "))
// return if comment does not starts with @LizardByte-bot
if (!comment.startsWith("@LizardByte-bot")) {
console.log("the comment is not a @LizardByte-bot command, exiting")
return
}
// split comment to get positional arguments
let args = comment.split(" ")
let command_ran = false
// command is args[1], parameters are args[2:]
// approve
if (args[1] === "approve") {
console.log("approve command running")
let labelsToAdd = ["approve-queue"]
// get list of open issues with label "approve-theme"
const running_issues = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
labels: 'approve-theme'
})
// add approve-theme label if no other issues have it
if (running_issues.data.length === 0) {
console.log("no other issues have approve-theme label, adding label")
labelsToAdd.push("approve-theme")
}
else {
console.log("other issues have approve-theme label, not adding label")
}
console.log(`Adding labels: ${labelsToAdd}`)
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: labelsToAdd
})
command_ran = true
}
// edit
if (args[1] === "edit") {
console.log("edit command running")
// replace youtube url in issue body
let og_issue_body = `${ISSUE_BODY}`
console.log(`og issue_body: ${og_issue_body}`)
let current_url = og_issue_body.match(YT_REGEX)
console.log(`current_url: ${current_url}`)
// if current_url is not null
if (current_url !== null) {
// replace current_url with args[2]
let issue_body = og_issue_body.replace(current_url[0], args[2])
console.log(`updated issue_body: ${issue_body}`)
if (issue_body !== og_issue_body) {
// update issue body
github.rest.issues.update({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: issue_body
})
// re-apply labels since the relabeler ignores edits from LizardByte-bot
// get labels
const labels = await github.rest.issues.listLabelsOnIssue({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
})
// list of labels to permanently remove
const labels_remove = ["approve-theme", "approve-queue", "request-theme"]
// check if labels list contains any of the labels to remove
for (var label_name of labels_remove) {
var label_remove = labels.data.some(label => label.name === label_name)
if (label_remove) {
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: [label_name]
})
}
}
// add a delay to allow the label to be removed before trying to re-add it
await new Promise(r => setTimeout(r, 10000))
// re-add request-theme label
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ["request-theme"]
})
}
}
command_ran = true
}
if (command_ran) {
console.log("command ran, adding reaction")
// add reaction to issue comment
github.rest.reactions.createForIssueComment({
comment_id: COMMENT_ID,
owner: context.repo.owner,
repo: context.repo.repo,
content: "+1"
})
}