Skip to content

Commit f6cc74d

Browse files
authored
feat(plugins/reactions): add support for discussions and discussions comments (lowlighter#637)
1 parent 4d4842f commit f6cc74d

File tree

3 files changed

+45
-23
lines changed

3 files changed

+45
-23
lines changed

source/plugins/reactions/index.mjs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,38 @@ export default async function({login, q, imports, data, graphql, queries, accoun
77
return null
88

99
//Load inputs
10-
let {limit:_limit1, "limit.issues":_limit2, days, details, display, ignored} = imports.metadata.plugins.reactions.inputs({data, account, q})
10+
let {limit:_limit1, "limit.issues":_limit2, "limit.discussions":_limit3, "limit.discussions.comments":_limit4, days, details, display, ignored} = imports.metadata.plugins.reactions.inputs({data, account, q})
1111

1212
//Load issue comments
1313
const comments = []
14-
for (const {type, limit} of [{type:"issueComments", limit:_limit1}, {type:"issues", limit:_limit2}].filter(({limit}) => limit)) {
14+
for (const {type, limit} of [{type:"issueComments", limit:_limit1}, {type:"issues", limit:_limit2}, {type:"repositoryDiscussionComments", limit:_limit3}, {type:"repositoryDiscussions", limit:_limit4}].filter(({limit}) => limit)) {
1515
let cursor = null, pushed = 0
1616
const fetched = []
17-
do {
18-
//Load issue comments
19-
console.debug(`metrics/compute/${login}/plugins > reactions > retrieving ${type} after ${cursor}`)
20-
const {user:{[type]:{edges}}} = await graphql(queries.reactions({login, type, after:cursor ? `after: "${cursor}"` : ""}))
21-
cursor = edges?.[edges?.length - 1]?.cursor
22-
//Save issue comments
23-
const filtered = edges
24-
.flatMap(({node:{createdAt:created, reactions:{nodes:reactions}}}) => ({created:new Date(created), reactions:reactions.filter(({user = {}}) => !ignored.includes(user.login)).map(({content}) => content)}))
25-
.filter(comment => Number.isFinite(days) ? comment.created < new Date(Date.now() - days * 24 * 60 * 60 * 1000) : true)
26-
pushed = filtered.length
27-
fetched.push(...filtered)
28-
console.debug(`metrics/compute/${login}/plugins > reactions > currently at ${fetched.length} ${type} comments`)
29-
//Applying limit
30-
if ((fetched.length >= limit) || (filtered.length < edges.length)) {
31-
fetched.splice(limit)
32-
console.debug(`metrics/compute/${login}/plugins > reactions > keeping only ${fetched.length} ${type} comments`)
33-
}
34-
} while ((cursor) && (pushed) && (fetched.length < limit))
35-
comments.push(...fetched)
17+
try {
18+
do {
19+
//Load issue comments
20+
console.debug(`metrics/compute/${login}/plugins > reactions > retrieving ${type} before ${cursor}`)
21+
const {user:{[type]:{edges}}} = await graphql(queries.reactions({login, type, before:cursor ? `before: "${cursor}"` : ""}))
22+
cursor = edges?.[0]?.cursor
23+
//Save issue comments
24+
const filtered = edges
25+
.flatMap(({node:{createdAt:created, reactions:{nodes:reactions}}}) => ({created:new Date(created), reactions:reactions.filter(({user = {}}) => !ignored.includes(user.login)).map(({content}) => content)}))
26+
.filter(comment => Number.isFinite(days) ? comment.created < new Date(Date.now() - days * 24 * 60 * 60 * 1000) : true)
27+
pushed = filtered.length
28+
fetched.push(...filtered)
29+
console.debug(`metrics/compute/${login}/plugins > reactions > currently at ${fetched.length} ${type} comments`)
30+
//Applying limit
31+
if ((fetched.length >= limit) || (filtered.length < edges.length)) {
32+
fetched.splice(limit)
33+
console.debug(`metrics/compute/${login}/plugins > reactions > keeping only ${fetched.length} ${type} comments`)
34+
}
35+
} while ((cursor) && (pushed) && (fetched.length < limit))
36+
comments.push(...fetched)
37+
}
38+
catch (error) {
39+
console.debug(error)
40+
console.debug(`metrics/compute/${login}/plugins > reactions > an error occured while retrieving ${type}`)
41+
}
3642
}
3743
console.debug(`metrics/compute/${login}/plugins > reactions > fetched ${comments.length} comments`)
3844

source/plugins/reactions/metadata.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,26 @@ inputs:
2828
min: 0
2929
max: 1000
3030

31+
# Maximum number of discussions opened to parse
32+
plugin_reactions_limit_discussions:
33+
description: Maximum number of discussions opened to parse
34+
type: number
35+
default: 100
36+
min: 0
37+
max: 1000
38+
39+
# Maximum number of discussions comments opened to parse
40+
plugin_reactions_limit_discussions_comments:
41+
description: Maximum number of discussions comments opened to parse
42+
type: number
43+
default: 100
44+
min: 0
45+
max: 1000
46+
3147
# Filter reactions by issue comments age
3248
# Set to 0 to disable age filtering
3349
plugin_reactions_days:
34-
description: Maximum issue comments age
50+
description: Maximum comments age
3551
type: number
3652
default: 0
3753
min: 0

source/plugins/reactions/queries/reactions.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
query ReactionsDefault {
22
user(login: "$login") {
33
login
4-
$type($after first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) {
4+
$type($before last: 50) {
55
edges {
66
cursor
77
node {

0 commit comments

Comments
 (0)