From f6cc74de77b7c60794840cc9a4f44efeabe28a44 Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Sun, 31 Oct 2021 01:00:54 -0400 Subject: [PATCH] feat(plugins/reactions): add support for discussions and discussions comments (#637) --- source/plugins/reactions/index.mjs | 48 +++++++++++-------- source/plugins/reactions/metadata.yml | 18 ++++++- .../reactions/queries/reactions.graphql | 2 +- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/source/plugins/reactions/index.mjs b/source/plugins/reactions/index.mjs index affcb5ec..3f4ef431 100644 --- a/source/plugins/reactions/index.mjs +++ b/source/plugins/reactions/index.mjs @@ -7,32 +7,38 @@ export default async function({login, q, imports, data, graphql, queries, accoun return null //Load inputs - let {limit:_limit1, "limit.issues":_limit2, days, details, display, ignored} = imports.metadata.plugins.reactions.inputs({data, account, q}) + 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}) //Load issue comments const comments = [] - for (const {type, limit} of [{type:"issueComments", limit:_limit1}, {type:"issues", limit:_limit2}].filter(({limit}) => limit)) { + for (const {type, limit} of [{type:"issueComments", limit:_limit1}, {type:"issues", limit:_limit2}, {type:"repositoryDiscussionComments", limit:_limit3}, {type:"repositoryDiscussions", limit:_limit4}].filter(({limit}) => limit)) { let cursor = null, pushed = 0 const fetched = [] - do { - //Load issue comments - console.debug(`metrics/compute/${login}/plugins > reactions > retrieving ${type} after ${cursor}`) - const {user:{[type]:{edges}}} = await graphql(queries.reactions({login, type, after:cursor ? `after: "${cursor}"` : ""})) - cursor = edges?.[edges?.length - 1]?.cursor - //Save issue comments - const filtered = edges - .flatMap(({node:{createdAt:created, reactions:{nodes:reactions}}}) => ({created:new Date(created), reactions:reactions.filter(({user = {}}) => !ignored.includes(user.login)).map(({content}) => content)})) - .filter(comment => Number.isFinite(days) ? comment.created < new Date(Date.now() - days * 24 * 60 * 60 * 1000) : true) - pushed = filtered.length - fetched.push(...filtered) - console.debug(`metrics/compute/${login}/plugins > reactions > currently at ${fetched.length} ${type} comments`) - //Applying limit - if ((fetched.length >= limit) || (filtered.length < edges.length)) { - fetched.splice(limit) - console.debug(`metrics/compute/${login}/plugins > reactions > keeping only ${fetched.length} ${type} comments`) - } - } while ((cursor) && (pushed) && (fetched.length < limit)) - comments.push(...fetched) + try { + do { + //Load issue comments + console.debug(`metrics/compute/${login}/plugins > reactions > retrieving ${type} before ${cursor}`) + const {user:{[type]:{edges}}} = await graphql(queries.reactions({login, type, before:cursor ? `before: "${cursor}"` : ""})) + cursor = edges?.[0]?.cursor + //Save issue comments + const filtered = edges + .flatMap(({node:{createdAt:created, reactions:{nodes:reactions}}}) => ({created:new Date(created), reactions:reactions.filter(({user = {}}) => !ignored.includes(user.login)).map(({content}) => content)})) + .filter(comment => Number.isFinite(days) ? comment.created < new Date(Date.now() - days * 24 * 60 * 60 * 1000) : true) + pushed = filtered.length + fetched.push(...filtered) + console.debug(`metrics/compute/${login}/plugins > reactions > currently at ${fetched.length} ${type} comments`) + //Applying limit + if ((fetched.length >= limit) || (filtered.length < edges.length)) { + fetched.splice(limit) + console.debug(`metrics/compute/${login}/plugins > reactions > keeping only ${fetched.length} ${type} comments`) + } + } while ((cursor) && (pushed) && (fetched.length < limit)) + comments.push(...fetched) + } + catch (error) { + console.debug(error) + console.debug(`metrics/compute/${login}/plugins > reactions > an error occured while retrieving ${type}`) + } } console.debug(`metrics/compute/${login}/plugins > reactions > fetched ${comments.length} comments`) diff --git a/source/plugins/reactions/metadata.yml b/source/plugins/reactions/metadata.yml index 0f573988..5ef4c041 100644 --- a/source/plugins/reactions/metadata.yml +++ b/source/plugins/reactions/metadata.yml @@ -28,10 +28,26 @@ inputs: min: 0 max: 1000 + # Maximum number of discussions opened to parse + plugin_reactions_limit_discussions: + description: Maximum number of discussions opened to parse + type: number + default: 100 + min: 0 + max: 1000 + + # Maximum number of discussions comments opened to parse + plugin_reactions_limit_discussions_comments: + description: Maximum number of discussions comments opened to parse + type: number + default: 100 + min: 0 + max: 1000 + # Filter reactions by issue comments age # Set to 0 to disable age filtering plugin_reactions_days: - description: Maximum issue comments age + description: Maximum comments age type: number default: 0 min: 0 diff --git a/source/plugins/reactions/queries/reactions.graphql b/source/plugins/reactions/queries/reactions.graphql index b1d9c704..7d023243 100644 --- a/source/plugins/reactions/queries/reactions.graphql +++ b/source/plugins/reactions/queries/reactions.graphql @@ -1,7 +1,7 @@ query ReactionsDefault { user(login: "$login") { login - $type($after first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) { + $type($before last: 50) { edges { cursor node {