Skip to content

Commit eaaccf9

Browse files
authored
Merge pull request #46 from actions/bug-fixing
Fix hardcoded api endpoint and artifact name lookup
2 parents 38e42fe + 8daa1e0 commit eaaccf9

File tree

11 files changed

+61
-30
lines changed

11 files changed

+61
-30
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Set Node.JS
1616
uses: actions/setup-node@v2
1717
with:
18-
node-version: 12.x
18+
node-version: 16.x
1919

2020
- name: Install dependencies
2121
run: npm install

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ inputs:
2424
description: 'Time in milliseconds between two deployment status report (default: 5 seconds)'
2525
required: false
2626
default: "5000"
27+
artifact_name:
28+
description: 'Name of the artifact to deploy'
29+
required: false
30+
default: "github-pages"
2731
outputs:
2832
page_url:
2933
description: 'URL to deployed GitHub Pages'

dist/index.js

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pre/index.js

Lines changed: 13 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pre/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ function getRequiredVars() {
1010
buildVersion: process.env.GITHUB_SHA,
1111
buildActor: process.env.GITHUB_ACTOR,
1212
actionsId: process.env.GITHUB_ACTION,
13-
githubToken: core.getInput('token')
13+
githubToken: core.getInput('token'),
14+
githubApiUrl: process.env.GITHUB_API_URL ?? 'https://api.github.com',
15+
artifactName: core.getInput('artifact_name') ?? 'github-pages'
1416
}
1517
}
1618

src/deployment.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Deployment {
2525
this.workflowRun = context.workflowRun
2626
this.requestedDeployment = false
2727
this.deploymentInfo = null
28+
this.githubApiUrl = context.githubApiUrl
29+
this.artifactName = context.artifactName
2830
}
2931

3032
// Ask the runtime for the unsigned artifact URL and deploy to GitHub Pages
@@ -33,7 +35,7 @@ class Deployment {
3335
try {
3436
core.info(`Actor: ${this.buildActor}`)
3537
core.info(`Action ID: ${this.actionsId}`)
36-
const pagesDeployEndpoint = `https://api.github.com/repos/${this.repositoryNwo}/pages/deployment`
38+
const pagesDeployEndpoint = `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment`
3739
const artifactExgUrl = `${this.runTimeUrl}_apis/pipelines/workflows/${this.workflowRun}/artifacts?api-version=6.0-preview`
3840
core.info(`Artifact URL: ${artifactExgUrl}`)
3941
const {data} = await axios.get(artifactExgUrl, {
@@ -43,10 +45,12 @@ class Deployment {
4345
}
4446
})
4547
core.info(JSON.stringify(data))
46-
if (data.value.length == 0) {
47-
throw new Error('No uploaded artifact was found! Please check if there are any errors at build step.')
48+
const artifactRawUrl = data?.value?.find(artifact => artifact.name === this.artifactName)?.url
49+
if (!artifactRawUrl) {
50+
throw new Error('No uploaded artifact was found! Please check if there are any errors at build step, or uploaded artifact name is correct.')
4851
}
49-
const artifactUrl = `${data.value[0].url}&%24expand=SignedContent`
52+
53+
const artifactUrl = `${artifactRawUrl}&%24expand=SignedContent`
5054
const payload = {
5155
artifact_url: artifactUrl,
5256
pages_build_version: this.buildVersion,
@@ -105,7 +109,7 @@ class Deployment {
105109
try {
106110
const statusUrl = this.deploymentInfo != null ?
107111
this.deploymentInfo["status_url"] :
108-
`https://api.github.com/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}`
112+
`${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}`
109113
core.setOutput('page_url', this.deploymentInfo != null ? this.deploymentInfo["page_url"] : "")
110114
const timeout = Number(core.getInput('timeout'))
111115
const reportingInterval = Number(core.getInput('reporting_interval'))

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const deployment = new Deployment()
1717
async function cancelHandler(evtOrExitCodeOrError) {
1818
try {
1919
if (deployment.requestedDeployment) {
20-
const pagesCancelDeployEndpoint = `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/cancel/${process.env.GITHUB_SHA}`
20+
const pagesCancelDeployEndpoint = `${deployment.githubApiUrl}/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/cancel/${process.env.GITHUB_SHA}`
2121
await axios.put(
2222
pagesCancelDeployEndpoint,
2323
{},

src/index.test.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ describe('create', () => {
5555
process.env.GITHUB_ACTOR = 'monalisa'
5656
process.env.GITHUB_ACTION = '__monalisa/octocat'
5757
process.env.GITHUB_ACTION_PATH = 'something'
58+
jest.spyOn(core, 'getInput').mockImplementation(param => {
59+
switch (param) {
60+
case 'artifact_name':
61+
return 'github-pages'
62+
case 'token':
63+
return process.env.GITHUB_TOKEN
64+
}
65+
})
5866

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

8088
core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)
8189
axios.post = jest.fn().mockResolvedValue('test')
@@ -94,7 +102,7 @@ describe('create', () => {
94102
{
95103
headers: {
96104
Accept: 'application/vnd.github.v3+json',
97-
Authorization: 'Bearer ',
105+
Authorization: `Bearer gha-token`,
98106
'Content-type': 'application/json'
99107
}
100108
}
@@ -112,7 +120,7 @@ describe('create', () => {
112120
process.env.GITHUB_SHA = 'invalid-build-version'
113121
const scope = nock(`http://my-url`)
114122
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
115-
.reply(200, { value: [{ url: 'https://invalid-artifact.com' }] })
123+
.reply(200, { value: [{ url: 'https://invalid-artifact.com', name: 'github-pages' }] })
116124

117125
axios.post = jest.fn().mockRejectedValue({
118126
status: 400
@@ -161,6 +169,7 @@ describe('check', () => {
161169
process.env.GITHUB_ACTOR = 'monalisa'
162170
process.env.GITHUB_ACTION = '__monalisa/octocat'
163171
process.env.GITHUB_ACTION_PATH = 'something'
172+
process.env.ARTIFACT_NAME = 'github-pages'
164173

165174
jest.spyOn(core, 'setOutput').mockImplementation(param => {
166175
return param
@@ -209,7 +218,7 @@ describe('check', () => {
209218
`https://api.github.com/repos/${repositoryNwo}/pages/deployment/status/${buildVersion}`,
210219
{
211220
headers: {
212-
Authorization: 'token '
221+
Authorization: 'token gha-token'
213222
}
214223
}
215224
)

0 commit comments

Comments
 (0)