Merge pull request #1 from paper-spa/url_parts

Add outputs for origin, host, and base path
This commit is contained in:
James M. Greene
2022-06-08 15:07:17 -05:00
committed by GitHub
7 changed files with 157 additions and 96 deletions

View File

@@ -10,4 +10,10 @@ inputs:
required: true
outputs:
base_url:
description: 'URL to deployed GitHub Pages'
description: 'GitHub Pages site full base URL. Examples: "https://octocat.github.io/my-repo/", "https://octocat.github.io/", "https://www.example.com/"'
origin:
description: 'GitHub Pages site origin. Examples: "https://octocat.github.io", "https://www.example.com"'
host:
description: 'GitHub Pages site host. Examples: "octocat.github.io", "www.example.com"'
base_path:
description: 'GitHub Pages site full base path. Examples: "/my-repo/" or "/"'

75
dist/index.js vendored
View File

@@ -8078,6 +8078,46 @@ module.exports = function getContext() {
}
/***/ }),
/***/ 9965:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
const core = __nccwpck_require__(2186)
const axios = __nccwpck_require__(6545)
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
/***/ }),
/***/ 9491:
@@ -8242,45 +8282,18 @@ var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
const core = __nccwpck_require__(2186)
const axios = __nccwpck_require__(6545)
const getPagesBaseUrl = __nccwpck_require__(9965)
// All variables we need from the runtime are loaded here
const getContext = __nccwpck_require__(1319)
async function getPageBaseUrl() {
try {
const context = getContext()
const pagesEndpoint = `https://api.github.com/repos/${context.repositoryNwo}/pages`
core.info("GITHUB_TOKEN : " + context.githubToken)
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))
core.setOutput('base_url', pageObject.html_url)
core.info(`Get the Base URL to the page with endpoint ${pagesEndpoint}`)
} 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)
}
}

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

33
src/get-pages-base-url.js Normal file
View File

@@ -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

View File

@@ -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', '/')
})
})

View File

@@ -1,43 +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)
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))
core.setOutput('base_url', pageObject.html_url)
core.info(`Get the Base URL to the page with endpoint ${pagesEndpoint}`)
} 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)
}
}

View File

@@ -1,32 +0,0 @@
const core = require('@actions/core')
const path = require('path')
const cp = require('child_process')
const nock = require('nock')
const axios = require('axios')
const { expect, jest } = require('@jest/globals')
describe('GET', () => {
beforeAll(() => {
process.env.GITHUB_REPOSITORY = 'paper-spa/is-awesome'
process.env.GITHUB_TOKEN = 'gha-token'
jest.spyOn(core, 'setOutput').mockImplementation(param => {
return param
})
jest.spyOn(core, 'setFailed').mockImplementation(param => {
return 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('can successfully get the page', async () => {
axios.get = jest.fn().mockResolvedValue({ data: { html_url: "blah" } ))
}