mirror of
https://github.com/actions/configure-pages.git
synced 2025-12-08 08:06:09 +00:00
Merge branch 'main' into dist-check-workflow
This commit is contained in:
21
.github/workflows/test.yml
vendored
21
.github/workflows/test.yml
vendored
@@ -10,16 +10,17 @@ jobs:
|
|||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Set Node.JS
|
- name: Setup Node.JS
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: 16.x
|
node-version: 16.x
|
||||||
|
cache: 'npm'
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: npm test
|
run: npm test
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ inputs:
|
|||||||
description: "GitHub token"
|
description: "GitHub token"
|
||||||
default: ${{ github.token }}
|
default: ${{ github.token }}
|
||||||
required: true
|
required: true
|
||||||
|
enablement:
|
||||||
|
description: "Should a Pages site be enabled for the repository if not so already?"
|
||||||
|
default: "true"
|
||||||
|
required: false
|
||||||
outputs:
|
outputs:
|
||||||
base_url:
|
base_url:
|
||||||
description: 'GitHub Pages site full base URL. Examples: "https://octocat.github.io/my-repo/", "https://octocat.github.io/", "https://www.example.com/"'
|
description: 'GitHub Pages site full base URL. Examples: "https://octocat.github.io/my-repo/", "https://octocat.github.io/", "https://www.example.com/"'
|
||||||
|
|||||||
202
dist/index.js
vendored
202
dist/index.js
vendored
@@ -14423,6 +14423,104 @@ if (process.env.NODE_DEBUG && /\btunnel\b/.test(process.env.NODE_DEBUG)) {
|
|||||||
exports.debug = debug; // for test
|
exports.debug = debug; // for test
|
||||||
|
|
||||||
|
|
||||||
|
/***/ }),
|
||||||
|
|
||||||
|
/***/ 9432:
|
||||||
|
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
|
||||||
|
|
||||||
|
const axios = __nccwpck_require__(6545)
|
||||||
|
const core = __nccwpck_require__(2186)
|
||||||
|
|
||||||
|
function getApiBaseUrl() {
|
||||||
|
return process.env.GITHUB_API_URL || 'https://api.github.com'
|
||||||
|
}
|
||||||
|
|
||||||
|
async function enablePagesSite({ repositoryNwo, githubToken }) {
|
||||||
|
const pagesEndpoint = `${getApiBaseUrl()}/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'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const pageObject = response.data
|
||||||
|
return pageObject
|
||||||
|
} catch (error) {
|
||||||
|
if (error.response && error.response.status === 409) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getPagesSite({ repositoryNwo, githubToken }) {
|
||||||
|
try {
|
||||||
|
const pagesEndpoint = `${getApiBaseUrl()}/repos/${repositoryNwo}/pages`
|
||||||
|
|
||||||
|
const response = await axios.get(pagesEndpoint, {
|
||||||
|
headers: {
|
||||||
|
Accept: 'application/vnd.github.v3+json',
|
||||||
|
Authorization: `Bearer ${githubToken}`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const pageObject = response.data
|
||||||
|
return pageObject
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findOrCreatePagesSite({ repositoryNwo, githubToken, enablement = true }) {
|
||||||
|
let pageObject
|
||||||
|
|
||||||
|
// Try to find an existing Pages site first
|
||||||
|
try {
|
||||||
|
pageObject = await getPagesSite({ repositoryNwo, githubToken })
|
||||||
|
} catch (error) {
|
||||||
|
if (!enablement) {
|
||||||
|
core.error('Get Pages site failed', error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
core.warning('Get Pages site failed', error)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pageObject && enablement) {
|
||||||
|
// Create a new Pages site if one doesn't exist
|
||||||
|
try {
|
||||||
|
pageObject = await enablePagesSite({ repositoryNwo, githubToken })
|
||||||
|
} catch (error) {
|
||||||
|
core.error('Create Pages site failed', error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
// This somehow implies that the Pages site was already created but initially failed to be retrieved.
|
||||||
|
// Try one more time for this extreme edge case!
|
||||||
|
if (pageObject == null) {
|
||||||
|
try {
|
||||||
|
pageObject = await getPagesSite({ repositoryNwo, githubToken })
|
||||||
|
} catch (error) {
|
||||||
|
core.error('Get Pages site still failed', error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pageObject
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { findOrCreatePagesSite, enablePagesSite, getPagesSite, getApiBaseUrl }
|
||||||
|
|
||||||
|
|
||||||
/***/ }),
|
/***/ }),
|
||||||
|
|
||||||
/***/ 8395:
|
/***/ 8395:
|
||||||
@@ -14718,7 +14816,8 @@ function getRequiredVars() {
|
|||||||
return {
|
return {
|
||||||
repositoryNwo: process.env.GITHUB_REPOSITORY,
|
repositoryNwo: process.env.GITHUB_REPOSITORY,
|
||||||
githubToken: core.getInput('token'),
|
githubToken: core.getInput('token'),
|
||||||
staticSiteGenerator: core.getInput('static_site_generator')
|
staticSiteGenerator: core.getInput('static_site_generator'),
|
||||||
|
enablement: core.getInput('enablement') !== 'false'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -14739,85 +14838,19 @@ module.exports = {getContext}
|
|||||||
|
|
||||||
/***/ }),
|
/***/ }),
|
||||||
|
|
||||||
/***/ 5424:
|
/***/ 7527:
|
||||||
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
|
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
|
||||||
|
|
||||||
const core = __nccwpck_require__(2186)
|
const core = __nccwpck_require__(2186)
|
||||||
const axios = __nccwpck_require__(6545)
|
|
||||||
|
|
||||||
async function enablePages({repositoryNwo, githubToken}) {
|
function outputPagesBaseUrl(siteUrl) {
|
||||||
const pagesEndpoint = `https://api.github.com/repos/${repositoryNwo}/pages`
|
core.setOutput('base_url', siteUrl.href)
|
||||||
|
core.setOutput('origin', siteUrl.origin)
|
||||||
try {
|
core.setOutput('host', siteUrl.host)
|
||||||
const response = await axios.post(
|
core.setOutput('base_path', siteUrl.pathname)
|
||||||
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
|
module.exports = outputPagesBaseUrl
|
||||||
|
|
||||||
|
|
||||||
/***/ }),
|
|
||||||
|
|
||||||
/***/ 9965:
|
|
||||||
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
|
|
||||||
|
|
||||||
const core = __nccwpck_require__(2186)
|
|
||||||
const axios = __nccwpck_require__(6545)
|
|
||||||
const {setPagesPath} = __nccwpck_require__(4770)
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
/***/ }),
|
/***/ }),
|
||||||
@@ -16375,17 +16408,24 @@ var __webpack_exports__ = {};
|
|||||||
(() => {
|
(() => {
|
||||||
const core = __nccwpck_require__(2186)
|
const core = __nccwpck_require__(2186)
|
||||||
|
|
||||||
const enablePages = __nccwpck_require__(5424)
|
|
||||||
const getPagesBaseUrl = __nccwpck_require__(9965)
|
|
||||||
|
|
||||||
// All variables we need from the runtime are loaded here
|
// All variables we need from the runtime are loaded here
|
||||||
const {getContext} = __nccwpck_require__(1319)
|
const { getContext } = __nccwpck_require__(1319)
|
||||||
|
|
||||||
|
const { findOrCreatePagesSite } = __nccwpck_require__(9432)
|
||||||
|
const { setPagesPath } = __nccwpck_require__(4770)
|
||||||
|
const outputPagesBaseUrl = __nccwpck_require__(7527)
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
const context = getContext()
|
const { repositoryNwo, githubToken, enablement, staticSiteGenerator } = getContext()
|
||||||
await enablePages(context)
|
|
||||||
await getPagesBaseUrl(context)
|
const pageObject = await findOrCreatePagesSite({ repositoryNwo, githubToken, enablement })
|
||||||
|
const siteUrl = new URL(pageObject.html_url)
|
||||||
|
|
||||||
|
if (staticSiteGenerator) {
|
||||||
|
setPagesPath({ staticSiteGenerator, path: siteUrl.pathname })
|
||||||
|
}
|
||||||
|
outputPagesBaseUrl(siteUrl)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(error)
|
core.setFailed(error)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
|
|||||||
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
11
package.json
11
package.json
@@ -1,22 +1,23 @@
|
|||||||
{
|
{
|
||||||
|
"private": true,
|
||||||
"name": "configure-pages",
|
"name": "configure-pages",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "An action to enable Pages and extract various metadata about a site. It can also be used to configure various static site generators we support as starter workflows.",
|
"description": "A GitHub Action to enable Pages and extract various metadata about a site. It can also be used to configure various static site generators we support as starter workflows.",
|
||||||
"main": "src/index.js",
|
"main": "./dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepare": "ncc build src/index.js -o dist --source-map --license licenses.txt",
|
"prepare": "ncc build src/index.js -o dist --source-map --license licenses.txt",
|
||||||
"test": "jest"
|
"test": "jest"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/paper-spa/configure-pages.git"
|
"url": "git+https://github.com/actions/configure-pages.git"
|
||||||
},
|
},
|
||||||
"author": "GitHub",
|
"author": "GitHub",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/paper-spa/configure-pages/issues"
|
"url": "https://github.com/actions/configure-pages/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/paper-spa/configure-pages#readme",
|
"homepage": "https://github.com/actions/configure-pages#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.8.2",
|
"@actions/core": "^1.8.2",
|
||||||
"axios": "^0.27.2",
|
"axios": "^0.27.2",
|
||||||
|
|||||||
91
src/api-client.js
Normal file
91
src/api-client.js
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
const axios = require('axios')
|
||||||
|
const core = require('@actions/core')
|
||||||
|
|
||||||
|
function getApiBaseUrl() {
|
||||||
|
return process.env.GITHUB_API_URL || 'https://api.github.com'
|
||||||
|
}
|
||||||
|
|
||||||
|
async function enablePagesSite({ repositoryNwo, githubToken }) {
|
||||||
|
const pagesEndpoint = `${getApiBaseUrl()}/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'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const pageObject = response.data
|
||||||
|
return pageObject
|
||||||
|
} catch (error) {
|
||||||
|
if (error.response && error.response.status === 409) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getPagesSite({ repositoryNwo, githubToken }) {
|
||||||
|
try {
|
||||||
|
const pagesEndpoint = `${getApiBaseUrl()}/repos/${repositoryNwo}/pages`
|
||||||
|
|
||||||
|
const response = await axios.get(pagesEndpoint, {
|
||||||
|
headers: {
|
||||||
|
Accept: 'application/vnd.github.v3+json',
|
||||||
|
Authorization: `Bearer ${githubToken}`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const pageObject = response.data
|
||||||
|
return pageObject
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findOrCreatePagesSite({ repositoryNwo, githubToken, enablement = true }) {
|
||||||
|
let pageObject
|
||||||
|
|
||||||
|
// Try to find an existing Pages site first
|
||||||
|
try {
|
||||||
|
pageObject = await getPagesSite({ repositoryNwo, githubToken })
|
||||||
|
} catch (error) {
|
||||||
|
if (!enablement) {
|
||||||
|
core.error('Get Pages site failed', error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
core.warning('Get Pages site failed', error)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pageObject && enablement) {
|
||||||
|
// Create a new Pages site if one doesn't exist
|
||||||
|
try {
|
||||||
|
pageObject = await enablePagesSite({ repositoryNwo, githubToken })
|
||||||
|
} catch (error) {
|
||||||
|
core.error('Create Pages site failed', error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
// This somehow implies that the Pages site was already created but initially failed to be retrieved.
|
||||||
|
// Try one more time for this extreme edge case!
|
||||||
|
if (pageObject == null) {
|
||||||
|
try {
|
||||||
|
pageObject = await getPagesSite({ repositoryNwo, githubToken })
|
||||||
|
} catch (error) {
|
||||||
|
core.error('Get Pages site still failed', error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pageObject
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { findOrCreatePagesSite, enablePagesSite, getPagesSite, getApiBaseUrl }
|
||||||
221
src/api-client.test.js
Normal file
221
src/api-client.test.js
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
const core = require('@actions/core')
|
||||||
|
const axios = require('axios')
|
||||||
|
|
||||||
|
const apiClient = require('./api-client')
|
||||||
|
|
||||||
|
describe('apiClient', () => {
|
||||||
|
const GITHUB_REPOSITORY = 'actions/is-awesome'
|
||||||
|
const GITHUB_TOKEN = 'gha-token'
|
||||||
|
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
|
||||||
|
|
||||||
|
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())
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('getApiBaseUrl', () => {
|
||||||
|
it('returns GITHUB_API_URL environment variable when set', async () => {
|
||||||
|
const expectedBaseUrl = 'https://api.ghe.com'
|
||||||
|
process.env.GITHUB_API_URL = expectedBaseUrl
|
||||||
|
const result = apiClient.getApiBaseUrl()
|
||||||
|
delete process.env.GITHUB_API_URL
|
||||||
|
expect(result).toEqual(expectedBaseUrl)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('defaults to GitHub API if GITHUB_API_URL environment variable is empty', async () => {
|
||||||
|
process.env.GITHUB_API_URL = ''
|
||||||
|
const result = apiClient.getApiBaseUrl()
|
||||||
|
delete process.env.GITHUB_API_URL
|
||||||
|
expect(result).toEqual('https://api.github.com')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('defaults to GitHub API if GITHUB_API_URL environment variable is not set', async () => {
|
||||||
|
delete process.env.GITHUB_API_URL
|
||||||
|
const result = apiClient.getApiBaseUrl()
|
||||||
|
expect(result).toEqual('https://api.github.com')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('enablePagesSite', () => {
|
||||||
|
it('makes a request to create a page', async () => {
|
||||||
|
jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.resolve({ status: 201, data: PAGE_OBJECT }))
|
||||||
|
|
||||||
|
const result = await apiClient.enablePagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN
|
||||||
|
})
|
||||||
|
expect(result).toEqual(PAGE_OBJECT)
|
||||||
|
})
|
||||||
|
|
||||||
|
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
|
||||||
|
const result = await apiClient.enablePagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result).toBe(null)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('re-raises errors on failure status codes', async () => {
|
||||||
|
jest
|
||||||
|
.spyOn(axios, 'post')
|
||||||
|
.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
|
||||||
|
|
||||||
|
let erred = false
|
||||||
|
try {
|
||||||
|
await apiClient.enablePagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
erred = true
|
||||||
|
expect(error.response.status).toEqual(404)
|
||||||
|
}
|
||||||
|
expect(erred).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('getPagesSite', () => {
|
||||||
|
it('makes a request to get a page', async () => {
|
||||||
|
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
|
||||||
|
jest.spyOn(axios, 'get').mockImplementationOnce(() => Promise.resolve({ status: 200, data: PAGE_OBJECT }))
|
||||||
|
|
||||||
|
const result = await apiClient.getPagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN
|
||||||
|
})
|
||||||
|
expect(result).toEqual(PAGE_OBJECT)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('re-raises errors on failure status codes', async () => {
|
||||||
|
jest
|
||||||
|
.spyOn(axios, 'get')
|
||||||
|
.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
|
||||||
|
|
||||||
|
let erred = false
|
||||||
|
try {
|
||||||
|
await apiClient.getPagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
erred = true
|
||||||
|
expect(error.response.status).toEqual(404)
|
||||||
|
}
|
||||||
|
expect(erred).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('findOrCreatePagesSite', () => {
|
||||||
|
it('does not make a request to create a page if it already exists', async () => {
|
||||||
|
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
|
||||||
|
jest.spyOn(axios, 'get').mockImplementationOnce(() => Promise.resolve({ status: 200, data: PAGE_OBJECT }))
|
||||||
|
jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
|
||||||
|
|
||||||
|
const result = await apiClient.findOrCreatePagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN
|
||||||
|
})
|
||||||
|
expect(result).toEqual(PAGE_OBJECT)
|
||||||
|
expect(axios.get).toHaveBeenCalledTimes(1)
|
||||||
|
expect(axios.post).toHaveBeenCalledTimes(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('makes request to create a page by default if it does not exist', async () => {
|
||||||
|
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
|
||||||
|
jest.spyOn(axios, 'get').mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
|
||||||
|
jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.resolve({ status: 201, data: PAGE_OBJECT }))
|
||||||
|
|
||||||
|
const result = await apiClient.findOrCreatePagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN
|
||||||
|
})
|
||||||
|
expect(result).toEqual(PAGE_OBJECT)
|
||||||
|
expect(axios.get).toHaveBeenCalledTimes(1)
|
||||||
|
expect(axios.post).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('makes a request to create a page when explicitly enabled if it does not exist', async () => {
|
||||||
|
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
|
||||||
|
jest.spyOn(axios, 'get').mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
|
||||||
|
jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.resolve({ status: 201, data: PAGE_OBJECT }))
|
||||||
|
|
||||||
|
const result = await apiClient.findOrCreatePagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN,
|
||||||
|
enablement: true
|
||||||
|
})
|
||||||
|
expect(result).toEqual(PAGE_OBJECT)
|
||||||
|
expect(axios.get).toHaveBeenCalledTimes(1)
|
||||||
|
expect(axios.post).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not make a request to create a page when explicitly disabled even if it does not exist', async () => {
|
||||||
|
jest.spyOn(axios, 'get').mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
|
||||||
|
jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.reject({ response: { status: 500 } })) // just so they both aren't 404
|
||||||
|
|
||||||
|
let erred = false
|
||||||
|
try {
|
||||||
|
await apiClient.findOrCreatePagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN,
|
||||||
|
enablement: false
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
erred = true
|
||||||
|
// re-raised error
|
||||||
|
expect(error.response.status).toEqual(404)
|
||||||
|
}
|
||||||
|
expect(erred).toBe(true)
|
||||||
|
expect(axios.get).toHaveBeenCalledTimes(1)
|
||||||
|
expect(axios.post).toHaveBeenCalledTimes(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not make a second request to get page if create fails for reason other than existence', async () => {
|
||||||
|
jest.spyOn(axios, 'get').mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
|
||||||
|
jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.reject({ response: { status: 500 } })) // just so they both aren't 404
|
||||||
|
|
||||||
|
let erred = false
|
||||||
|
try {
|
||||||
|
await apiClient.findOrCreatePagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
erred = true
|
||||||
|
// re-raised error
|
||||||
|
expect(error.response.status).toEqual(500)
|
||||||
|
}
|
||||||
|
expect(erred).toBe(true)
|
||||||
|
expect(axios.get).toHaveBeenCalledTimes(1)
|
||||||
|
expect(axios.post).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('makes second request to get page if create fails because of existence', async () => {
|
||||||
|
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
|
||||||
|
jest.spyOn(axios, 'get')
|
||||||
|
.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
|
||||||
|
.mockImplementationOnce(() => Promise.resolve({ status: 200, data: PAGE_OBJECT }))
|
||||||
|
jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.reject({ response: { status: 409 } }))
|
||||||
|
|
||||||
|
const result = await apiClient.findOrCreatePagesSite({
|
||||||
|
repositoryNwo: GITHUB_REPOSITORY,
|
||||||
|
githubToken: GITHUB_TOKEN
|
||||||
|
})
|
||||||
|
expect(result).toEqual(PAGE_OBJECT)
|
||||||
|
expect(axios.get).toHaveBeenCalledTimes(2)
|
||||||
|
expect(axios.post).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
@@ -5,7 +5,8 @@ function getRequiredVars() {
|
|||||||
return {
|
return {
|
||||||
repositoryNwo: process.env.GITHUB_REPOSITORY,
|
repositoryNwo: process.env.GITHUB_REPOSITORY,
|
||||||
githubToken: core.getInput('token'),
|
githubToken: core.getInput('token'),
|
||||||
staticSiteGenerator: core.getInput('static_site_generator')
|
staticSiteGenerator: core.getInput('static_site_generator'),
|
||||||
|
enablement: core.getInput('enablement') !== 'false'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -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
|
|
||||||
21
src/index.js
21
src/index.js
@@ -1,16 +1,23 @@
|
|||||||
const core = require('@actions/core')
|
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
|
// All variables we need from the runtime are loaded here
|
||||||
const {getContext} = require('./context')
|
const { getContext } = require('./context')
|
||||||
|
|
||||||
|
const { findOrCreatePagesSite } = require('./api-client')
|
||||||
|
const { setPagesPath } = require('./set-pages-path')
|
||||||
|
const outputPagesBaseUrl = require('./output-pages-base-url')
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
const context = getContext()
|
const { repositoryNwo, githubToken, enablement, staticSiteGenerator } = getContext()
|
||||||
await enablePages(context)
|
|
||||||
await getPagesBaseUrl(context)
|
const pageObject = await findOrCreatePagesSite({ repositoryNwo, githubToken, enablement })
|
||||||
|
const siteUrl = new URL(pageObject.html_url)
|
||||||
|
|
||||||
|
if (staticSiteGenerator) {
|
||||||
|
setPagesPath({ staticSiteGenerator, path: siteUrl.pathname })
|
||||||
|
}
|
||||||
|
outputPagesBaseUrl(siteUrl)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(error)
|
core.setFailed(error)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
|
|||||||
10
src/output-pages-base-url.js
Normal file
10
src/output-pages-base-url.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
const core = require('@actions/core')
|
||||||
|
|
||||||
|
function outputPagesBaseUrl(siteUrl) {
|
||||||
|
core.setOutput('base_url', siteUrl.href)
|
||||||
|
core.setOutput('origin', siteUrl.origin)
|
||||||
|
core.setOutput('host', siteUrl.host)
|
||||||
|
core.setOutput('base_path', siteUrl.pathname)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = outputPagesBaseUrl
|
||||||
@@ -1,12 +1,8 @@
|
|||||||
const core = require('@actions/core')
|
const core = require('@actions/core')
|
||||||
const axios = require('axios')
|
|
||||||
|
|
||||||
const getPagesBaseUrl = require('./get-pages-base-url')
|
const outputPagesBaseUrl = require('./output-pages-base-url')
|
||||||
|
|
||||||
describe('getPagesBaseUrl', () => {
|
|
||||||
const GITHUB_REPOSITORY = 'paper-spa/is-awesome'
|
|
||||||
const GITHUB_TOKEN = 'gha-token'
|
|
||||||
|
|
||||||
|
describe('outputPagesBaseUrl', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.restoreAllMocks()
|
jest.restoreAllMocks()
|
||||||
|
|
||||||
@@ -25,16 +21,7 @@ describe('getPagesBaseUrl', () => {
|
|||||||
it('gets expected outputs for profile site', async () => {
|
it('gets expected outputs for profile site', async () => {
|
||||||
const baseUrl = 'https://octocat.github.io/'
|
const baseUrl = 'https://octocat.github.io/'
|
||||||
|
|
||||||
jest
|
outputPagesBaseUrl(new URL(baseUrl))
|
||||||
.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('base_url', baseUrl)
|
||||||
expect(core.setOutput).toHaveBeenCalledWith(
|
expect(core.setOutput).toHaveBeenCalledWith(
|
||||||
@@ -48,16 +35,7 @@ describe('getPagesBaseUrl', () => {
|
|||||||
it('gets expected outputs for project site', async () => {
|
it('gets expected outputs for project site', async () => {
|
||||||
const baseUrl = 'https://octocat.github.io/my-repo/'
|
const baseUrl = 'https://octocat.github.io/my-repo/'
|
||||||
|
|
||||||
jest
|
outputPagesBaseUrl(new URL(baseUrl))
|
||||||
.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('base_url', baseUrl)
|
||||||
expect(core.setOutput).toHaveBeenCalledWith(
|
expect(core.setOutput).toHaveBeenCalledWith(
|
||||||
@@ -71,16 +49,7 @@ describe('getPagesBaseUrl', () => {
|
|||||||
it('gets expected outputs for site with custom domain name', async () => {
|
it('gets expected outputs for site with custom domain name', async () => {
|
||||||
const baseUrl = 'https://www.example.com/'
|
const baseUrl = 'https://www.example.com/'
|
||||||
|
|
||||||
jest
|
outputPagesBaseUrl(new URL(baseUrl))
|
||||||
.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('base_url', baseUrl)
|
||||||
expect(core.setOutput).toHaveBeenCalledWith(
|
expect(core.setOutput).toHaveBeenCalledWith(
|
||||||
Reference in New Issue
Block a user