fix: better warning message when no github token is set

Luke Karrys committed Sep 24, 2023 at 11:01 UTC a55507f91576ace389242cb1bd1bfff6710c14b1
1 file changed +11 -15
theme/gatsby-node.js
+11 -15
@@ -59,10 +59,6 @@ exports.createPages = async ({graphql, actions}, themeOptions) => {
59 }
60 `)
61
62 - if (!process.env.GITHUB_TOKEN && !process.env.NOW_GITHUB_DEPLOYMENT) {
63 - console.error(`Non-deploy build and no GITHUB_TOKEN environment variable set; skipping GitHub API calls`)
64 - }
65 -
62 // Turn every MDX file into a page.
63 return Promise.all(
64 data.allMdx.nodes.map(async node => {
@@ -74,10 +70,8 @@ exports.createPages = async ({graphql, actions}, themeOptions) => {
70
71 const editUrl = getEditUrl(themeOptions, repo, fileRelativePath, node.frontmatter)
72
77 - let contributors = []
78 - if (themeOptions.showContributors !== false && (process.env.GITHUB_TOKEN || process.env.NOW_GITHUB_DEPLOYMENT)) {
79 - contributors = await fetchContributors(repo, fileRelativePath, node.frontmatter, process.env.GITHUB_TOKEN)
80 - }
73 + const contributors =
74 + themeOptions.showContributors !== false ? await fetchContributors(repo, fileRelativePath, node.frontmatte) : []
75
76 actions.createPage({
77 path: pagePath,
@@ -177,7 +171,12 @@ function getEditUrl(themeOptions, repo, filePath, overrideData = {}) {
171 return `https://github.com/${gh.nwo}/edit/${gh.branch}/${gh.path}`
172 }
173
180 -async function fetchContributors(repo, filePath, overrideData = {}, accessToken = '') {
174 +async function fetchContributors(repo, filePath, overrideData = {}) {
175 + if (!process.env.GITHUB_TOKEN) {
176 + console.warn('Skipping fetching contributors because no github token was set')
177 + return
178 + }
179 +
180 const gh = getGitHubData(repo, overrideData, filePath)
181
182 const cached = CONTRIBUTOR_CACHE.get(gh)
@@ -190,12 +189,9 @@ async function fetchContributors(repo, filePath, overrideData = {}, accessToken
189 method: 'get',
190 baseURL: 'https://api.github.com/',
191 url: `/repos/${gh.nwo}/commits?path=${gh.path}&sha=${gh.branch}&per_page=100`,
193 - }
194 -
195 - if (accessToken && accessToken.length) {
196 - req.headers = {
197 - Authorization: `token ${accessToken}`,
198 - }
192 + headers: {
193 + Authorization: `token ${process.env.GITHUB_TOKEN}`,
194 + },
195 }
196
197 const {data} = await axios.request(req)