Skip to content

Commit

Permalink
Merge pull request #2 from paper-spa/jess/create-pages
Browse files Browse the repository at this point in the history
Create Page if it does not already exist
  • Loading branch information
Mingzi authored and GitHub committed Jun 17, 2022
2 parents dabdcba + 39ec7ad commit e1a914e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
40 changes: 40 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions src/enable-pages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const core = require('@actions/core')
const axios = require('axios')

async function enablePages({ repositoryNwo, githubToken }) {
const pagesEndpoint = `https://api.github.com/repos/${repositoryNwo}/pages`

try {
const response = await axios.post(
pagesEndpoint,
{ build_type: 'workflow' },
{
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${githubToken}`,
'Content-type': 'application/json',
},
}
)
core.info('Created pages site')
} catch (error) {
if (error.response && error.response.status === 409) {
core.info('Pages site exists')
return
}

core.error('Couldn\'t create pages site', error)
throw error
}
}

module.exports = enablePages
48 changes: 48 additions & 0 deletions src/enable-pages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const core = require('@actions/core')
const axios = require('axios')

const enablePages = require('./enable-pages')

describe('enablePages', () => {
const GITHUB_REPOSITORY = 'paper-spa/is-awesome'
const GITHUB_TOKEN = 'gha-token'

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

// Mock error/warning/info/debug
jest.spyOn(core, 'error').mockImplementation(jest.fn())
jest.spyOn(core, 'warning').mockImplementation(jest.fn())
jest.spyOn(core, 'info').mockImplementation(jest.fn())
jest.spyOn(core, 'debug').mockImplementation(jest.fn())
})

it('makes a request to create a page', async () => {
jest
.spyOn(axios, 'post')
.mockImplementationOnce(() => Promise.resolve({ }))

await enablePages({ repositoryNwo: GITHUB_REPOSITORY, githubToken: GITHUB_TOKEN })
})

it('handles a 409 response when the page already exists', async () => {
jest
.spyOn(axios, 'post')
.mockImplementationOnce(() => Promise.reject({ response: { status: 409 } }))

// Simply assert that no error is raised
await enablePages({ repositoryNwo: GITHUB_REPOSITORY, githubToken: GITHUB_TOKEN })
})

it('re-raises errors on failure status codes', async () => {
jest
.spyOn(axios, 'post')
.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))

try {
await enablePages({ repositoryNwo: GITHUB_REPOSITORY, githubToken: GITHUB_TOKEN })
} catch (error) {
expect(error.response.status).toEqual(404)
}
})
})
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const core = require('@actions/core')

const enablePages = require('./enable-pages')
const getPagesBaseUrl = require('./get-pages-base-url')

// All variables we need from the runtime are loaded here
Expand All @@ -8,6 +9,7 @@ const getContext = require('./context')
async function main() {
try {
const context = getContext()
await enablePages(context)
await getPagesBaseUrl(context)
} catch (error) {
core.setFailed(error)
Expand Down

0 comments on commit e1a914e

Please sign in to comment.