Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hardcoded api endpoint and artifact name lookup #46

Merged
merged 12 commits into from
Aug 5, 2022
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ inputs:
description: 'Time in milliseconds between two deployment status report (default: 5 seconds)'
required: false
default: "5000"
artifact_name:
description: 'Name of the artifact to deploy'
required: false
default: "github-pages"
outputs:
page_url:
description: 'URL to deployed GitHub Pages'
22 changes: 14 additions & 8 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

20 changes: 13 additions & 7 deletions pre/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pre/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ function getRequiredVars() {
buildVersion: process.env.GITHUB_SHA,
buildActor: process.env.GITHUB_ACTOR,
actionsId: process.env.GITHUB_ACTION,
githubToken: core.getInput('token')
githubApiUrl: process.env.GITHUB_API_URL ?? `https://api.github.com`,
githubToken: core.getInput('token'),
artifactName: core.getInput('artifact_name'),
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Deployment {
this.workflowRun = context.workflowRun
this.requestedDeployment = false
this.deploymentInfo = null
this.githubApiUrl = context.githubApiUrl
this.artifactName = context.artifactName
}

// Ask the runtime for the unsigned artifact URL and deploy to GitHub Pages
Expand All @@ -33,7 +35,7 @@ class Deployment {
try {
core.info(`Actor: ${this.buildActor}`)
core.info(`Action ID: ${this.actionsId}`)
const pagesDeployEndpoint = `https://api.github.com/repos/${this.repositoryNwo}/pages/deployment`
const pagesDeployEndpoint = `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment`
const artifactExgUrl = `${this.runTimeUrl}_apis/pipelines/workflows/${this.workflowRun}/artifacts?api-version=6.0-preview`
core.info(`Artifact URL: ${artifactExgUrl}`)
const {data} = await axios.get(artifactExgUrl, {
Expand All @@ -43,10 +45,12 @@ class Deployment {
}
})
core.info(JSON.stringify(data))
if (data.value.length == 0) {
throw new Error('No uploaded artifact was found! Please check if there are any errors at build step.')
const artifactRawUrl = data?.value?.find(artifact => artifact.name === this.artifactName)?.url
if (!artifactRawUrl) {
throw new Error('No uploaded artifact was found! Please check if there are any errors at build step, or uploaded artifact name is correct.')
}
const artifactUrl = `${data.value[0].url}&%24expand=SignedContent`

const artifactUrl = `${artifactRawUrl}&%24expand=SignedContent`
const payload = {
artifact_url: artifactUrl,
pages_build_version: this.buildVersion,
Expand Down Expand Up @@ -105,7 +109,7 @@ class Deployment {
try {
const statusUrl = this.deploymentInfo != null ?
this.deploymentInfo["status_url"] :
`https://api.github.com/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}`
`${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}`
core.setOutput('page_url', this.deploymentInfo != null ? this.deploymentInfo["page_url"] : "")
const timeout = Number(core.getInput('timeout'))
const reportingInterval = Number(core.getInput('reporting_interval'))
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const deployment = new Deployment()
async function cancelHandler(evtOrExitCodeOrError) {
try {
if (deployment.requestedDeployment) {
const pagesCancelDeployEndpoint = `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/cancel/${process.env.GITHUB_SHA}`
const pagesCancelDeployEndpoint = `${deployment.githubApiUrl}/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/cancel/${process.env.GITHUB_SHA}`
await axios.put(
pagesCancelDeployEndpoint,
{},
Expand Down
18 changes: 14 additions & 4 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('with all environment variables set', () => {
process.env.GITHUB_ACTOR = 'monalisa'
process.env.GITHUB_ACTION = '__monalisa/octocat'
process.env.GITHUB_ACTION_PATH = 'something'
process.env.ARTIFACT_NAME = 'github-pages'
})

it('Executes cleanly', done => {
Expand Down Expand Up @@ -55,6 +56,14 @@ describe('create', () => {
process.env.GITHUB_ACTOR = 'monalisa'
process.env.GITHUB_ACTION = '__monalisa/octocat'
process.env.GITHUB_ACTION_PATH = 'something'
jest.spyOn(core, 'getInput').mockImplementation(param => {
switch (param) {
case 'artifact_name':
return 'github-pages'
case 'token':
return process.env.GITHUB_TOKEN
}
})

jest.spyOn(core, 'setOutput').mockImplementation(param => {
return param
Expand All @@ -75,7 +84,7 @@ describe('create', () => {
const fakeJwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiNjllMWIxOC1jOGFiLTRhZGQtOGYxOC03MzVlMzVjZGJhZjAiLCJzdWIiOiJyZXBvOnBhcGVyLXNwYS9taW55aTplbnZpcm9ubWVudDpQcm9kdWN0aW9uIiwiYXVkIjoiaHR0cHM6Ly9naXRodWIuY29tL3BhcGVyLXNwYSIsInJlZiI6InJlZnMvaGVhZHMvbWFpbiIsInNoYSI6ImEyODU1MWJmODdiZDk3NTFiMzdiMmM0YjM3M2MxZjU3NjFmYWM2MjYiLCJyZXBvc2l0b3J5IjoicGFwZXItc3BhL21pbnlpIiwicmVwb3NpdG9yeV9vd25lciI6InBhcGVyLXNwYSIsInJ1bl9pZCI6IjE1NDY0NTkzNjQiLCJydW5fbnVtYmVyIjoiMzQiLCJydW5fYXR0ZW1wdCI6IjIiLCJhY3RvciI6IllpTXlzdHkiLCJ3b3JrZmxvdyI6IkNJIiwiaGVhZF9yZWYiOiIiLCJiYXNlX3JlZiI6IiIsImV2ZW50X25hbWUiOiJwdXNoIiwicmVmX3R5cGUiOiJicmFuY2giLCJlbnZpcm9ubWVudCI6IlByb2R1Y3Rpb24iLCJqb2Jfd29ya2Zsb3dfcmVmIjoicGFwZXItc3BhL21pbnlpLy5naXRodWIvd29ya2Zsb3dzL2JsYW5rLnltbEByZWZzL2hlYWRzL21haW4iLCJpc3MiOiJodHRwczovL3Rva2VuLmFjdGlvbnMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwibmJmIjoxNjM4ODI4MDI4LCJleHAiOjE2Mzg4Mjg5MjgsImlhdCI6MTYzODgyODYyOH0.1wyupfxu1HGoTyIqatYg0hIxy2-0bMO-yVlmLSMuu2w'
const scope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, { value: [{ url: 'https://fake-artifact.com' }] })
.reply(200, { value: [ {url: 'https://another-artifact.com', name: 'another-artifact'}, { url: 'https://fake-artifact.com', name: 'github-pages' }] })

core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)
axios.post = jest.fn().mockResolvedValue('test')
Expand All @@ -94,7 +103,7 @@ describe('create', () => {
{
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: 'Bearer ',
Authorization: `Bearer gha-token`,

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "Bearer gha-token" is used as [authorization header](1).
'Content-type': 'application/json'
}
}
Expand All @@ -112,7 +121,7 @@ describe('create', () => {
process.env.GITHUB_SHA = 'invalid-build-version'
const scope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, { value: [{ url: 'https://invalid-artifact.com' }] })
.reply(200, { value: [{ url: 'https://invalid-artifact.com', name: 'github-pages' }] })

axios.post = jest.fn().mockRejectedValue({
status: 400
Expand Down Expand Up @@ -161,6 +170,7 @@ describe('check', () => {
process.env.GITHUB_ACTOR = 'monalisa'
process.env.GITHUB_ACTION = '__monalisa/octocat'
process.env.GITHUB_ACTION_PATH = 'something'
process.env.ARTIFACT_NAME = 'github-pages'

jest.spyOn(core, 'setOutput').mockImplementation(param => {
return param
Expand Down Expand Up @@ -209,7 +219,7 @@ describe('check', () => {
`https://api.github.com/repos/${repositoryNwo}/pages/deployment/status/${buildVersion}`,
{
headers: {
Authorization: 'token '
Authorization: 'token gha-token'

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "token gha-token" is used as [authorization header](1).
}
}
)
Expand Down
2 changes: 1 addition & 1 deletion src/pre.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const {Deployment} = require('./deployment')
async function emitTelemetry() {
// All variables we need from the runtime are set in the Deployment constructor
const deployment = new Deployment()
const telemetryUrl = `https://api.github.com/repos/${deployment.repositoryNwo}/pages/telemetry`
const telemetryUrl = `${deployment.githubApiUrl}/repos/${deployment.repositoryNwo}/pages/telemetry`
core.info(`Sending telemetry for run id ${deployment.workflowRun}`)
await axios
.post(
Expand Down