diff --git a/src/enable-pages.js b/src/enable-pages.js deleted file mode 100644 index 68f47e3..0000000 --- a/src/enable-pages.js +++ /dev/null @@ -1,31 +0,0 @@ -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 diff --git a/src/enable-pages.test.js b/src/enable-pages.test.js deleted file mode 100644 index 359f552..0000000 --- a/src/enable-pages.test.js +++ /dev/null @@ -1,55 +0,0 @@ -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) - } - }) -}) diff --git a/src/get-pages-base-url.js b/src/get-pages-base-url.js deleted file mode 100644 index bd5f933..0000000 --- a/src/get-pages-base-url.js +++ /dev/null @@ -1,38 +0,0 @@ -const core = require('@actions/core') -const axios = require('axios') -const {setPagesPath} = require('./set-pages-path') - -async function getPagesBaseUrl({ - repositoryNwo, - githubToken, - staticSiteGenerator -}) { - try { - const pagesEndpoint = `https://api.github.com/repos/${repositoryNwo}/pages` - - core.info(`Get the Base URL to the page with endpoint ${pagesEndpoint}`) - const response = await axios.get(pagesEndpoint, { - headers: { - Accept: 'application/vnd.github.v3+json', - Authorization: `Bearer ${githubToken}` - } - }) - - pageObject = response.data - core.info(JSON.stringify(pageObject)) - - const siteUrl = new URL(pageObject.html_url) - if (staticSiteGenerator) { - setPagesPath({staticSiteGenerator, path: siteUrl.pathname}) - } - core.setOutput('base_url', siteUrl.href) - core.setOutput('origin', siteUrl.origin) - core.setOutput('host', siteUrl.host) - core.setOutput('base_path', siteUrl.pathname) - } catch (error) { - core.error('Get on the Page failed', error) - throw error - } -} - -module.exports = getPagesBaseUrl diff --git a/src/get-pages-base-url.test.js b/src/get-pages-base-url.test.js deleted file mode 100644 index 28512ca..0000000 --- a/src/get-pages-base-url.test.js +++ /dev/null @@ -1,93 +0,0 @@ -const core = require('@actions/core') -const axios = require('axios') - -const getPagesBaseUrl = require('./get-pages-base-url') - -describe('getPagesBaseUrl', () => { - const GITHUB_REPOSITORY = 'paper-spa/is-awesome' - const GITHUB_TOKEN = 'gha-token' - - beforeEach(() => { - jest.restoreAllMocks() - - jest.spyOn(core, 'setOutput').mockImplementation((key, value) => { - key, value - }) - jest.spyOn(core, 'setFailed').mockImplementation(param => param) - - // 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('gets expected outputs for profile site', async () => { - const baseUrl = 'https://octocat.github.io/' - - jest - .spyOn(axios, 'get') - .mockImplementationOnce(() => - Promise.resolve({data: {html_url: baseUrl}}) - ) - - await getPagesBaseUrl({ - repositoryNwo: GITHUB_REPOSITORY, - githubToken: GITHUB_TOKEN - }) - - expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl) - expect(core.setOutput).toHaveBeenCalledWith( - 'origin', - 'https://octocat.github.io' - ) - expect(core.setOutput).toHaveBeenCalledWith('host', 'octocat.github.io') - expect(core.setOutput).toHaveBeenCalledWith('base_path', '/') - }) - - it('gets expected outputs for project site', async () => { - const baseUrl = 'https://octocat.github.io/my-repo/' - - jest - .spyOn(axios, 'get') - .mockImplementationOnce(() => - Promise.resolve({data: {html_url: baseUrl}}) - ) - - await getPagesBaseUrl({ - repositoryNwo: GITHUB_REPOSITORY, - githubToken: GITHUB_TOKEN - }) - - expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl) - expect(core.setOutput).toHaveBeenCalledWith( - 'origin', - 'https://octocat.github.io' - ) - expect(core.setOutput).toHaveBeenCalledWith('host', 'octocat.github.io') - expect(core.setOutput).toHaveBeenCalledWith('base_path', '/my-repo/') - }) - - it('gets expected outputs for site with custom domain name', async () => { - const baseUrl = 'https://www.example.com/' - - jest - .spyOn(axios, 'get') - .mockImplementationOnce(() => - Promise.resolve({data: {html_url: baseUrl}}) - ) - - await getPagesBaseUrl({ - repositoryNwo: GITHUB_REPOSITORY, - githubToken: GITHUB_TOKEN - }) - - expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl) - expect(core.setOutput).toHaveBeenCalledWith( - 'origin', - 'https://www.example.com' - ) - expect(core.setOutput).toHaveBeenCalledWith('host', 'www.example.com') - expect(core.setOutput).toHaveBeenCalledWith('base_path', '/') - }) -})