|
| 1 | +//Setup |
| 2 | + export default async function({login, q, imports, graphql, queries, data, account}, {enabled = false} = {}) { |
| 3 | + //Plugin execution |
| 4 | + try { |
| 5 | + //Check if plugin is enabled and requirements are met |
| 6 | + if ((!enabled)||(!q.discussions)) |
| 7 | + return null |
| 8 | + |
| 9 | + //Load inputs |
| 10 | + imports.metadata.plugins.discussions.inputs({data, account, q}) |
| 11 | + const discussions = {categories:{}} |
| 12 | + |
| 13 | + //Fetch general statistics |
| 14 | + const stats = Object.fromEntries(Object.entries((await graphql(queries.discussions.statistics({login}))).user).map(([key, value]) => [key, value.totalCount])) |
| 15 | + Object.assign(discussions, stats) |
| 16 | + |
| 17 | + //Load started discussions |
| 18 | + { |
| 19 | + const fetched = [] |
| 20 | + const categories = {} |
| 21 | + let cursor = null |
| 22 | + let pushed = 0 |
| 23 | + do { |
| 24 | + console.debug(`metrics/compute/${login}/discussions > retrieving discussions after ${cursor}`) |
| 25 | + const {user:{repositoryDiscussions:{edges = [], nodes = []} = {}}} = await graphql(queries.discussions.categories({login, after:cursor ? `after: "${cursor}"` : ""})) |
| 26 | + cursor = edges?.[edges?.length - 1]?.cursor |
| 27 | + fetched.push(...nodes) |
| 28 | + pushed = nodes.length |
| 29 | + console.debug(`metrics/compute/${login}/discussions > retrieved ${pushed} discussions after ${cursor}`) |
| 30 | + } while ((pushed) && (cursor)) |
| 31 | + |
| 32 | + //Compute favorite category |
| 33 | + for (const category of [...fetched.map(({category:{emoji, name}}) => `${imports.emoji.get(emoji)} ${name}`)]) |
| 34 | + categories[category] = (categories[category] ?? 0) + 1 |
| 35 | + discussions.categories.stats = categories |
| 36 | + discussions.categories.favorite = Object.entries(categories).sort((a, b) => b[1] - a[1]).map(([name]) => name).shift() ?? null |
| 37 | + } |
| 38 | + |
| 39 | + //Results |
| 40 | + return discussions |
| 41 | + } |
| 42 | + //Handle errors |
| 43 | + catch (error) { |
| 44 | + throw {error:{message:"An error occured", instance:error}} |
| 45 | + } |
| 46 | + } |
0 commit comments