|
| 1 | +import { graphql as githubGraphQL } from '@octokit/graphql'; |
| 2 | + |
| 3 | +import { GITHUB_API_KEY } from '#site/next.constants.mjs'; |
| 4 | + |
| 5 | +// This is used to ensure that URLs are always in the correct format |
| 6 | +function fixUrl(url) { |
| 7 | + if (!url) { |
| 8 | + return null; |
| 9 | + } |
| 10 | + |
| 11 | + if (!url.startsWith('http://') && !url.startsWith('https://')) { |
| 12 | + return `https://${url}`; |
| 13 | + } |
| 14 | + |
| 15 | + return url; |
| 16 | +} |
| 17 | + |
| 18 | +async function fetchOpenCollectiveData() { |
| 19 | + const endpoint = 'https://api.opencollective.com/graphql/v2'; |
| 20 | + |
| 21 | + const query = `{ |
| 22 | + account(slug: "nodejs") { |
| 23 | + orders(status: ACTIVE, filter: INCOMING) { |
| 24 | + totalCount |
| 25 | + nodes { |
| 26 | + fromAccount { |
| 27 | + name |
| 28 | + website |
| 29 | + imageUrl |
| 30 | + } |
| 31 | + amount { |
| 32 | + value |
| 33 | + } |
| 34 | + tier { |
| 35 | + slug |
| 36 | + } |
| 37 | + frequency |
| 38 | + totalDonations { |
| 39 | + value |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + donations: orders( |
| 45 | + account: { slug: "nodejs" } |
| 46 | + frequency: ONETIME |
| 47 | + status: PAID |
| 48 | + filter: INCOMING |
| 49 | + ) { |
| 50 | + totalCount |
| 51 | + nodes { |
| 52 | + id |
| 53 | + updatedAt |
| 54 | + frequency |
| 55 | + status |
| 56 | + amount { |
| 57 | + value |
| 58 | + currency |
| 59 | + } |
| 60 | + fromAccount { |
| 61 | + name |
| 62 | + website |
| 63 | + imageUrl |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + }`; |
| 68 | + |
| 69 | + const response = await fetch(endpoint, { |
| 70 | + method: 'POST', |
| 71 | + headers: { 'Content-Type': 'application/json' }, |
| 72 | + body: JSON.stringify({ query }), |
| 73 | + }); |
| 74 | + |
| 75 | + const payload = await response.json(); |
| 76 | + |
| 77 | + const sponsors = payload.data.account.orders.nodes.map(order => ({ |
| 78 | + name: order.fromAccount.name, |
| 79 | + url: fixUrl(order.fromAccount.website), |
| 80 | + image: order.fromAccount.imageUrl, |
| 81 | + source: 'opencollective', |
| 82 | + })); |
| 83 | + |
| 84 | + const donations = payload.data.donations.nodes.map(transaction => ({ |
| 85 | + name: transaction.fromAccount.name, |
| 86 | + url: fixUrl(transaction.fromAccount.website), |
| 87 | + image: transaction.fromAccount.imageUrl, |
| 88 | + source: 'opencollective', |
| 89 | + })); |
| 90 | + |
| 91 | + sponsors.push(...donations); |
| 92 | + |
| 93 | + return sponsors; |
| 94 | +} |
| 95 | + |
| 96 | +async function fetchGitHubSponsors() { |
| 97 | + if (GITHUB_API_KEY === null) { |
| 98 | + return []; |
| 99 | + } |
| 100 | + |
| 101 | + function sponsorshipsQuery(cursor = null) { |
| 102 | + return ` |
| 103 | + query { |
| 104 | + organization(login: "nodejs") { |
| 105 | + sponsorshipsAsMaintainer (first:100, includePrivate: false, after: "${cursor}") { |
| 106 | + nodes { |
| 107 | + sponsor: sponsorEntity { |
| 108 | + ...on User { |
| 109 | + id: databaseId, |
| 110 | + name, |
| 111 | + login, |
| 112 | + avatarUrl, |
| 113 | + url, |
| 114 | + websiteUrl |
| 115 | + } |
| 116 | + ...on Organization { |
| 117 | + id: databaseId, |
| 118 | + name, |
| 119 | + login, |
| 120 | + avatarUrl, |
| 121 | + url, |
| 122 | + websiteUrl |
| 123 | + } |
| 124 | + }, |
| 125 | + } |
| 126 | + pageInfo { |
| 127 | + endCursor |
| 128 | + startCursor |
| 129 | + hasNextPage |
| 130 | + hasPreviousPage |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + `; |
| 136 | + } |
| 137 | + |
| 138 | + const sponsorshipsResponse = await githubGraphQL(sponsorshipsQuery(), { |
| 139 | + headers: { |
| 140 | + authorization: `token ${GITHUB_API_KEY}`, |
| 141 | + }, |
| 142 | + }); |
| 143 | + |
| 144 | + let pageInfo = |
| 145 | + sponsorshipsResponse.organization.sponsorshipsAsMaintainer.pageInfo; |
| 146 | + const sponsorships = |
| 147 | + sponsorshipsResponse.organization.sponsorshipsAsMaintainer.nodes; |
| 148 | + |
| 149 | + while (pageInfo.hasNextPage) { |
| 150 | + const cursor = pageInfo.endCursor; |
| 151 | + |
| 152 | + const pagedResponse = await githubGraphQL(sponsorshipsQuery(cursor), { |
| 153 | + headers: { |
| 154 | + authorization: `token ${GITHUB_API_KEY}`, |
| 155 | + }, |
| 156 | + }); |
| 157 | + |
| 158 | + pageInfo = pagedResponse.organization.sponsorshipsAsMaintainer.pageInfo; |
| 159 | + sponsorships.push( |
| 160 | + ...pagedResponse.organization.sponsorshipsAsMaintainer.nodes |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + const sponsors = sponsorships |
| 165 | + // return an array in the same format as Open Collective |
| 166 | + .map(({ sponsor }) => ({ |
| 167 | + name: sponsor.name || sponsor.login, |
| 168 | + image: `https://avatars.githubusercontent.com/u/${sponsor.id}`, |
| 169 | + url: fixUrl(sponsor.websiteUrl || sponsor.url), |
| 170 | + source: 'github', |
| 171 | + })); |
| 172 | + |
| 173 | + return sponsors; |
| 174 | +} |
| 175 | + |
| 176 | +export { fetchOpenCollectiveData, fetchGitHubSponsors }; |
0 commit comments