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

Error count #186

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coverage_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 18 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.

132 changes: 131 additions & 1 deletion src/__tests__/internal/deployment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,101 @@ describe('Deployment', () => {
jest.spyOn(core, 'debug').mockImplementation(jest.fn())
})

describe('#setOptionalUserInput', () => {
it('warns when the timeout is greater than the maximum allowed', async () => {
jest.spyOn(core, 'getInput').mockImplementation(param => {
switch (param) {
case 'timeout':
return MAX_TIMEOUT + 1
default:
return process.env[`INPUT_${param.toUpperCase()}`] || ''
}
})

const deployment = new Deployment()
deployment.setOptionalUserInput()

expect(deployment.timeout).toBe(MAX_TIMEOUT)
expect(core.warning).toBeCalledWith(
`Warning: timeout value is greater than the allowed maximum - timeout set to the maximum of ${MAX_TIMEOUT} milliseconds.`
)
})

it('sets the error_count input when valid', async () => {
jest.spyOn(core, 'getInput').mockImplementation(param => {
switch (param) {
case 'error_count':
return '1'
default:
return process.env[`INPUT_${param.toUpperCase()}`] || ''
}
})

// Create the deployment
const deployment = new Deployment()
deployment.setOptionalUserInput()

expect(deployment.maxErrorCount).toBe(1)
})

it('sets the error_count input to null if zero and warns user', async () => {
jest.spyOn(core, 'getInput').mockImplementation(param => {
switch (param) {
case 'error_count':
return '0'
default:
return process.env[`INPUT_${param.toUpperCase()}`] || ''
}
})

const deployment = new Deployment()
deployment.setOptionalUserInput()

expect(deployment.maxErrorCount).toBe(null)
expect(core.warning).toHaveBeenCalledWith(
'Invalid error_count value will be ignored. Please ensure the value is a positive integer.'
)
})

it('sets the error_count input to null if negative and warns user', async () => {
jest.spyOn(core, 'getInput').mockImplementation(param => {
switch (param) {
case 'error_count':
return '-1'
default:
return process.env[`INPUT_${param.toUpperCase()}`] || ''
}
})

const deployment = new Deployment()
deployment.setOptionalUserInput()

expect(deployment.maxErrorCount).toBe(null)
expect(core.warning).toHaveBeenCalledWith(
'Invalid error_count value will be ignored. Please ensure the value is a positive integer.'
)
})

it('sets the error_count input to null if not a number and warns user', async () => {
jest.spyOn(core, 'getInput').mockImplementation(param => {
switch (param) {
case 'error_count':
return 'not a number'
default:
return process.env[`INPUT_${param.toUpperCase()}`] || ''
}
})

const deployment = new Deployment()
deployment.setOptionalUserInput()

expect(deployment.maxErrorCount).toBe(null)
expect(core.warning).toHaveBeenCalledWith(
'Invalid error_count value will be ignored. Please ensure the value is a positive integer.'
)
})
})

describe('#create', () => {
afterEach(() => {
// Remove mock for `core.getInput('preview')`
Expand Down Expand Up @@ -88,6 +183,42 @@ describe('Deployment', () => {
createDeploymentScope.done()
})

it('invokes #setOptionalUserInput', async () => {
process.env.GITHUB_SHA = 'valid-build-version'

const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, {
value: [
{ url: 'https://another-artifact.com', name: 'another-artifact' },
{ url: 'https://fake-artifact.com', name: 'github-pages' }
]
})

const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments`, {
artifact_url: 'https://fake-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA,
oidc_token: fakeJwt
})
.reply(200, {
status_url: `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments/${process.env.GITHUB_SHA}`,
page_url: 'https://actions.github.io/is-awesome'
})

core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)

// Create the deployment
const deployment = new Deployment()
deployment.setOptionalUserInput = jest.fn()
await deployment.create(fakeJwt)

expect(deployment.setOptionalUserInput).toHaveBeenCalled()

artifactExchangeScope.done()
createDeploymentScope.done()
})

it('can successfully create a preview deployment', async () => {
process.env.GITHUB_SHA = 'valid-build-version'

Expand Down Expand Up @@ -587,7 +718,6 @@ describe('Deployment', () => {

core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)

// Set timeout to great than max
jest.spyOn(core, 'getInput').mockImplementation(param => {
switch (param) {
case 'artifact_name':
Expand Down
26 changes: 18 additions & 8 deletions src/internal/deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,32 @@ class Deployment {
this.isPreview = context.isPreview === true
this.timeout = MAX_TIMEOUT
this.startTime = null
this.maxErrorCount = null
}

// Ask the runtime for the unsigned artifact URL and deploy to GitHub Pages
// by creating a deployment with that artifact
async create(idToken) {
if (Number(core.getInput('timeout')) > MAX_TIMEOUT) {
setOptionalUserInput() {
const timeoutInput = Number(core.getInput('timeout'))
if (timeoutInput > MAX_TIMEOUT) {
core.warning(
`Warning: timeout value is greater than the allowed maximum - timeout set to the maximum of ${MAX_TIMEOUT} milliseconds.`
)
}

const timeoutInput = Number(core.getInput('timeout'))
this.timeout = !timeoutInput || timeoutInput <= 0 ? MAX_TIMEOUT : Math.min(timeoutInput, MAX_TIMEOUT)

const maxErrorCountInput = Number(core.getInput('error_count'))
if (!maxErrorCountInput || maxErrorCountInput <= 0) {
core.warning('Invalid error_count value will be ignored. Please ensure the value is a positive integer.')
} else {
this.maxErrorCount = maxErrorCountInput
}
}

// Ask the runtime for the unsigned artifact URL and deploy to GitHub Pages
// by creating a deployment with that artifact
async create(idToken) {
try {
this.setOptionalUserInput()

core.debug(`Actor: ${this.buildActor}`)
core.debug(`Action ID: ${this.actionsId}`)
core.debug(`Actions Workflow Run ID: ${this.workflowRun}`)
Expand Down Expand Up @@ -145,7 +156,6 @@ class Deployment {

const deploymentId = this.deploymentInfo.id || this.buildVersion
const reportingInterval = Number(core.getInput('reporting_interval'))
const maxErrorCount = Number(core.getInput('error_count'))

let errorCount = 0

Expand Down Expand Up @@ -204,7 +214,7 @@ class Deployment {
}
}

if (errorCount >= maxErrorCount) {
if (errorCount >= this.maxErrorCount) {
core.error('Too many errors, aborting!')
core.setFailed('Failed with status code: ' + errorStatus)

Expand Down