|
1 | 1 | //Setup |
2 | | - export default async function ({login, graphql, q, queries, imports}, {enabled = false} = {}) { |
| 2 | + export default async function ({login, data, graphql, rest, q, queries, imports}, {enabled = false} = {}) { |
3 | 3 | //Plugin execution |
4 | 4 | try { |
5 | 5 | //Check if plugin is enabled and requirements are met |
6 | 6 | if ((!enabled)||(!q.people)) |
7 | 7 | return null |
8 | 8 |
|
| 9 | + //Context |
| 10 | + let context = { |
| 11 | + mode:"user", |
| 12 | + types:["followers", "following", "sponsorshipsAsMaintainer", "sponsorshipsAsSponsor", "thanks"], |
| 13 | + default:"followers, following", |
| 14 | + alias:{followed:"following", sponsors:"sponsorshipsAsMaintainer", sponsored:"sponsorshipsAsSponsor", sponsoring:"sponsorshipsAsSponsor"}, |
| 15 | + sponsorships:{sponsorshipsAsMaintainer:"sponsorEntity", sponsorshipsAsSponsor:"sponsorable"} |
| 16 | + } |
| 17 | + if (q.repo) { |
| 18 | + console.debug(`metrics/compute/${login}/plugins > people > switched to repository mode`) |
| 19 | + const {owner, repo} = data.user.repositories.nodes.map(({name:repo, owner:{login:owner}}) => ({repo, owner})).shift() |
| 20 | + context = {...context, mode:"repo", types:["contributors", "stargazers", "watchers", "sponsorshipsAsMaintainer", "thanks"], default:"stargazers, watchers", owner, repo} |
| 21 | + } |
| 22 | + |
9 | 23 | //Parameters override |
10 | | - let {"people.limit":limit = 28, "people.types":types = "followers, following", "people.size":size = 28, "people.identicons":identicons = false} = q |
| 24 | + let {"people.limit":limit = 28, "people.types":types = context.default, "people.size":size = 28, "people.identicons":identicons = false, "people.thanks":thanks = []} = q |
11 | 25 | //Limit |
12 | 26 | limit = Math.max(1, limit) |
13 | 27 | //Repositories projects |
14 | | - types = decodeURIComponent(types ?? "").split(",").map(type => type.trim()).filter(type => ["followers", "following"].includes(type)) ?? [] |
| 28 | + types = [...new Set(decodeURIComponent(types ?? "").split(",").map(type => type.trim()).map(type => (context.alias[type] ?? type)).filter(type => context.types.includes(type)) ?? [])] |
| 29 | + //Special thanks |
| 30 | + thanks = decodeURIComponent(thanks ?? "").split(",").map(user => user.trim()).filter(user => user) |
15 | 31 |
|
16 | 32 | //Retrieve followers from graphql api |
17 | 33 | console.debug(`metrics/compute/${login}/plugins > people > querying api`) |
18 | | - const result = {followers:[], following:[]} |
| 34 | + const result = Object.fromEntries(types.map(type => [type, []])) |
19 | 35 | for (const type of types) { |
20 | 36 | //Iterate through people |
21 | 37 | console.debug(`metrics/compute/${login}/plugins > people > retrieving ${type}`) |
22 | | - let cursor = null |
23 | | - let pushed = 0 |
24 | | - do { |
25 | | - console.debug(`metrics/compute/${login}/plugins > people > retrieving ${type} after ${cursor}`) |
26 | | - const {user:{[type]:{edges}}} = await graphql(queries.people({login, type, size, after:cursor ? `after: "${cursor}"` : ""})) |
27 | | - cursor = edges?.[edges?.length-1]?.cursor |
28 | | - result[type].push(...edges.map(({node}) => node)) |
29 | | - pushed = edges.length |
30 | | - } while ((pushed)&&(cursor)) |
| 38 | + //Rest |
| 39 | + if (type === "contributors") { |
| 40 | + const {owner, repo} = context |
| 41 | + const {data:nodes} = await rest.repos.listContributors({owner, repo}) |
| 42 | + result[type].push(...nodes.map(({login, avatar_url}) => ({login, avatarUrl:avatar_url}))) |
| 43 | + } |
| 44 | + else if (type === "thanks") { |
| 45 | + const nodes = await Promise.all(thanks.map(async username => (await rest.users.getByUsername({username})).data)) |
| 46 | + result[type].push(...nodes.map(({login, avatar_url}) => ({login, avatarUrl:avatar_url}))) |
| 47 | + } |
| 48 | + //GraphQL |
| 49 | + else { |
| 50 | + let cursor = null |
| 51 | + let pushed = 0 |
| 52 | + do { |
| 53 | + console.debug(`metrics/compute/${login}/plugins > people > retrieving ${type} after ${cursor}`) |
| 54 | + const {[type]:{edges}} = ( |
| 55 | + type in context.sponsorships ? (await graphql(queries["people.sponsors"]({login:context.owner ?? login, type, size, after:cursor ? `after: "${cursor}"` : "", target:context.sponsorships[type]}))).user : |
| 56 | + context.mode === "repo" ? (await graphql(queries["people.repository"]({login:context.owner, repository:context.repo, type, size, after:cursor ? `after: "${cursor}"` : ""}))).user.repository : |
| 57 | + (await graphql(queries.people({login, type, size, after:cursor ? `after: "${cursor}"` : ""}))).user |
| 58 | + ) |
| 59 | + cursor = edges?.[edges?.length-1]?.cursor |
| 60 | + result[type].push(...edges.map(({node}) => node[context.sponsorships[type]] ?? node)) |
| 61 | + pushed = edges.length |
| 62 | + } while ((pushed)&&(cursor)&&(result[type].length <= limit)) |
| 63 | + } |
31 | 64 | //Limit people |
32 | 65 | if (limit > 0) { |
33 | 66 | console.debug(`metrics/compute/${login}/plugins > people > keeping only ${limit} ${type}`) |
|
0 commit comments