Skip to content

Commit

Permalink
Extract getPagesBaseUrl to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
James M. Greene authored and James M. Greene committed Jun 8, 2022
1 parent 772a668 commit f996195
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 68 deletions.
33 changes: 33 additions & 0 deletions src/get-pages-base-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const core = require('@actions/core')
const axios = require('axios')

async function getPagesBaseUrl({ repositoryNwo, githubToken}) {
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)
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
68 changes: 68 additions & 0 deletions src/get-pages-base-url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const core = require('@actions/core')
const axios = require('axios')
//const { expect, jest } = require('@jest/globals')

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', '/')
})
})
40 changes: 4 additions & 36 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,16 @@
const core = require('@actions/core')
const axios = require('axios')

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

// All variables we need from the runtime are loaded here
const getContext = require('./context')

async function getPageBaseUrl() {
try {
const context = getContext()

const pagesEndpoint = `https://api.github.com/repos/${context.repositoryNwo}/pages`

core.info("GITHUB_TOKEN : " + context.githubToken)

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 ${context.githubToken}`
}
}
)

pageObject = response.data
core.info(JSON.stringify(pageObject))

const siteUrl = new URL(pageObject.html_url)
core.setOutput('base_url', siteUrl.href)
core.setOutput('origin', siteUrl.origin)
core.setOutput('host', siteUrl.host)
core.setOutput('base_path', siteUrl.pathname)
} catch (e) {
console.info('Get on the Page failed', e)
process.exit(1)
}
}


async function main() {
try {
await getPageBaseUrl()
await getPagesBaseUrl(context)
} catch (error) {
core.setFailed(error)
process.exit(1)
}
}

Expand Down
32 changes: 0 additions & 32 deletions src/index.test.js

This file was deleted.

0 comments on commit f996195

Please sign in to comment.