Skip to content

Commit

Permalink
fix: github token not required (will not throw exception)
Browse files Browse the repository at this point in the history
  • Loading branch information
shazron committed Sep 14, 2020
1 parent 59d1a11 commit 7a33670
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 45 deletions.
24 changes: 21 additions & 3 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,28 @@ const globby = require('globby')
const { githubFetchContributorsForPage } = require('./src/gql')
const path = require('path')

exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type Contributors implements Node {
date: String,
login: String,
name: String
}
type GithubContributors implements Node {
contributors: [Contributors]
}
`
createTypes(typeDefs)
}

exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }, options = {}) => {
const root = options.root ? options.root : ''
const { paths: pages = ['src/pages'], extensions = ['md', 'mdx'] } = options.pages ? options.pages : {}
const { token, owner, name, branch } = options.repo ? options.repo : {}
const { token, owner, name, branch = 'main' } = options.repo ? options.repo : {}

if (!token) {
throw new Error('token is required (GITHUB_TOKEN environment variable)')
console.warn('To get Github Contributors, a Github token is required (GITHUB_TOKEN environment variable)')
}

const paths = await globby(pages.map(page => path.resolve(process.cwd(), page)), {
Expand All @@ -46,7 +61,10 @@ exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }, opt
let githubPath = path.join(root, _path.replace(process.cwd(), ''))
if (githubPath.charAt(0) === '/') githubPath = githubPath.substr(1)

const contributors = await githubFetchContributorsForPage(owner, name, branch, githubPath, token)
let contributors = []
if (token) {
contributors = await githubFetchContributorsForPage(owner, name, branch, githubPath, token)
}

actions.createNode({
contributors,
Expand Down
117 changes: 78 additions & 39 deletions test/gatsby-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,26 @@ jest.mock('../src/gql', () => ({
githubFetch: mockGithubFetch
}))

const originalWarn = console.warn
const mockedWarn = jest.fn()

const mockGlobby = jest.fn()
jest.mock('globby', () => mockGlobby)

const gatsbyNode = require('../gatsby-node')

const gatsbyHelpers = {
actions: {
createNode: jest.fn()
createNode: jest.fn(),
createTypes: jest.fn()
},
createNodeId: jest.fn(),
createContentDigest: jest.fn()
}

beforeEach(() => {
jest.restoreAllMocks()
})

test('exists', () => {
expect(typeof gatsbyNode.sourceNodes).toEqual('function')
})
// ///////////////////////////////////////

test('sourceNodes, no token', async () => {
await expect(gatsbyNode.sourceNodes(gatsbyHelpers)).rejects.toThrowError('token is required (GITHUB_TOKEN environment variable)')
})

test('sourceNodes', async () => {
describe('gatsby-node', () => {
const options = {
pages: {
},
Expand All @@ -53,37 +47,82 @@ test('sourceNodes', async () => {
token: 'dummy-token'
}
}

const pages = ['foo.md', 'bar.md']
mockGlobby.mockResolvedValueOnce(pages)

const contributors = [
[{ name: 'John Doe', login: 'johndoe2020', date: new Date() }],
[{ name: 'Jane Austen', login: 'janeausten2020', date: new Date() }]
]

mockGithubFetchContributors
.mockResolvedValueOnce(contributors[0])
.mockResolvedValueOnce(contributors[1])

await expect(gatsbyNode.sourceNodes(gatsbyHelpers, options)).resolves.toEqual(undefined)
expect(gatsbyHelpers.actions.createNode).toHaveBeenCalledTimes(pages.length + 1)
expect(mockGithubFetchContributors).toHaveBeenCalledTimes(pages.length)

options.root = 'my-root' // coverage
mockGlobby.mockResolvedValueOnce([])
await expect(gatsbyNode.sourceNodes(gatsbyHelpers, options)).resolves.toEqual(undefined)
expect(gatsbyHelpers.actions.createNode).toHaveBeenCalledTimes(pages.length + 2)
expect(mockGithubFetchContributors).toHaveBeenCalledTimes(pages.length)

pages.forEach((page, index) => {
const { owner, name, branch, token } = options.repo
expect(mockGithubFetchContributors).toHaveBeenNthCalledWith(index + 1, owner, name, branch, page, token)
expect(gatsbyHelpers.actions.createNode).toHaveBeenNthCalledWith(index + 2, expect.objectContaining({
contributors: contributors[index],
internal: expect.objectContaining({
type: 'GithubContributors'
})
}))
beforeEach(() => {
jest.resetAllMocks()
console.warn = mockedWarn
})

afterEach(() => {
console.warn = originalWarn
})

test('exists', () => {
expect(typeof gatsbyNode.sourceNodes).toEqual('function')
})

test('no token', async () => {
mockGlobby.mockResolvedValueOnce(pages)

await expect(gatsbyNode.sourceNodes(gatsbyHelpers)).resolves.toEqual(undefined)
await expect(mockedWarn).toHaveBeenCalledWith('To get Github Contributors, a Github token is required (GITHUB_TOKEN environment variable)')
})

test('createSchemaCustomization', () => {
expect(gatsbyNode.createSchemaCustomization(gatsbyHelpers)).toEqual(undefined)
expect(gatsbyHelpers.actions.createTypes).toHaveBeenCalledTimes(1)
})

test('no root', async () => {
mockGlobby.mockResolvedValue(pages)
mockGithubFetchContributors
.mockResolvedValueOnce(contributors[0])
.mockResolvedValueOnce(contributors[1])

await expect(gatsbyNode.sourceNodes(gatsbyHelpers, options)).resolves.toEqual(undefined)
expect(gatsbyHelpers.actions.createNode).toHaveBeenCalledTimes(pages.length + 1)
expect(mockGithubFetchContributors).toHaveBeenCalledTimes(pages.length)

pages.forEach((page, index) => {
const { owner, name, branch, token } = options.repo
expect(mockGithubFetchContributors).toHaveBeenNthCalledWith(index + 1, owner, name, branch, page, token)
// skip the very first createNode, which is a Github object
expect(gatsbyHelpers.actions.createNode).toHaveBeenNthCalledWith(index + 2, expect.objectContaining({
contributors: contributors[index],
internal: expect.objectContaining({
type: 'GithubContributors'
})
}))
})
})

test('with root', async () => {
mockGlobby.mockResolvedValue(pages)
mockGithubFetchContributors
.mockResolvedValueOnce(contributors[0])
.mockResolvedValueOnce(contributors[1])

const root = 'my-root'
options.root = `/${root}` // coverage
await expect(gatsbyNode.sourceNodes(gatsbyHelpers, options)).resolves.toEqual(undefined)
expect(gatsbyHelpers.actions.createNode).toHaveBeenCalledTimes(pages.length + 1)
expect(mockGithubFetchContributors).toHaveBeenCalledTimes(pages.length)

pages.forEach((page, index) => {
const { owner, name, branch, token } = options.repo
expect(mockGithubFetchContributors).toHaveBeenNthCalledWith(index + 1, owner, name, branch, `${root}/${page}`, token)
// skip the very first createNode, which is a Github object
expect(gatsbyHelpers.actions.createNode).toHaveBeenNthCalledWith(index + 2, expect.objectContaining({
contributors: contributors[index],
internal: expect.objectContaining({
type: 'GithubContributors'
})
}))
})
})
})
6 changes: 3 additions & 3 deletions test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ module.exports = {
],
coverageThreshold: {
global: {
branches: 90,
lines: 90,
statements: 90
branches: 100,
lines: 100,
statements: 100
}
},
reporters: [
Expand Down

0 comments on commit 7a33670

Please sign in to comment.