-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Before return site domain & path, create the site
- Loading branch information
Jess Bees
committed
Jun 15, 2022
1 parent
dabdcba
commit a0e3ad5
Showing
5 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| { | ||
| headers: { | ||
| Accept: 'application/vnd.github.v3+json', | ||
| Authorization: `Bearer ${githubToken}`, | ||
| 'Content-type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ build_type: 'workflow' }), | ||
| } | ||
| ) | ||
| 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters