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 on missing token #172

Merged
merged 2 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions __tests__/changelog-enforcer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ describe('the changelog-enforcer', () => {
})
})

it('should throw an error when token is missing', (done) => {
inputs['token'] = ''

changelogEnforcer.enforce()
.then(() => {
expect(infoSpy).not.toHaveBeenCalled()
expect(failureSpy).toHaveBeenCalled()
expect(outputSpy).toHaveBeenCalled()

done()
})
})

it('should enforce when label is not present; changelog is changed', (done) => {
inputs['skipLabels'] = 'A different label'

Expand Down Expand Up @@ -95,7 +108,7 @@ describe('the changelog-enforcer', () => {
"raw_url": "/path/to/AnotherFile.md"
}
]


fetch.mockImplementation((url, options) => {
return prepareResponse(JSON.stringify(files))
Expand Down Expand Up @@ -155,8 +168,8 @@ describe('the changelog-enforcer', () => {
}
]

const changelog =
`## [v2.1.0]
const changelog =
`## [v2.1.0]
- Changelog
`

Expand Down
12 changes: 11 additions & 1 deletion __tests__/client.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
jest.mock('node-fetch');

const core = require('@actions/core')
const fetch = require('node-fetch')
const { Response } = jest.requireActual('node-fetch');
const client = require('../src/client')
Expand Down Expand Up @@ -58,4 +57,15 @@ describe('the client', () => {
expect(fetch).toHaveBeenCalledTimes(2)
expect(changelogFile).toBeUndefined()
})

it('should get an error with bad response code', async () => {
fetch
.mockReturnValueOnce(Promise.resolve(new Response("", { status: 401 })))

try {
await client.findChangelog('token', 'repo', 1, 1, 'CHANGELOG.md')
} catch (err) {
expect(fetch).toHaveBeenCalled()
}
})
})
2 changes: 1 addition & 1 deletion coverage/badge.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6248,7 +6248,7 @@ module.exports.enforce = async function () {
const missingUpdateErrorMessage = getMissingUpdateErrorMessage(changeLogPath)
const expectedLatestVersion = core.getInput(IN_EXPECTED_LATEST_VERSION)
const versionPattern = core.getInput(IN_VERSION_PATTERN)
const token = core.getInput(IN_TOKEN)
const token = getToken()

core.info(`Skip Labels: ${skipLabelList}`)
core.info(`Changelog Path: ${changeLogPath}`)
Expand Down Expand Up @@ -6291,6 +6291,14 @@ function getMissingUpdateErrorMessage(changeLogPath) {
return `No update to ${changeLogPath} found!`
}

function getToken() {
const token = core.getInput(IN_TOKEN)
if (!token) {
throw new Error("Did not find token for using the GitHub API")
}
return token
}

function shouldEnforceChangelog(labelNames, skipLabelList) {
return !labelNames.some(l => skipLabelList.includes(l))
}
Expand Down Expand Up @@ -6343,6 +6351,9 @@ module.exports.findChangelog = async function (token, repository, pullRequestNum
core.debug(`Downloading page ${page} of pull request files from /repos/${repository}/pulls/${pullRequestNumber}/files`)
const options = addAuth(token, {})
const response = await fetch(`https://api.github.com/repos/${repository}/pulls/${pullRequestNumber}/files?per_page=${pageSize}&page=${page}`, options)
if (!response.ok) {
throw new Error(`Got a ${response.status} response from GitHub API`)
}
const files = await response.json()
core.debug(`Downloaded page ${page} of pull request files`)

Expand All @@ -6366,6 +6377,9 @@ module.exports.downloadChangelog = async function (token, changelogUrl) {
core.debug(`Downloading changelog from ${changelogUrl}`)
const options = addAuth(token, {})
const response = await fetch(`${changelogUrl}`, options)
if (!response.ok) {
throw new Error(`Got a ${response.status} response from GitHub API`)
}
const changelog = await response.text()
core.debug("Downloaded changelog")
return changelog
Expand Down
10 changes: 9 additions & 1 deletion src/changelog-enforcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports.enforce = async function () {
const missingUpdateErrorMessage = getMissingUpdateErrorMessage(changeLogPath)
const expectedLatestVersion = core.getInput(IN_EXPECTED_LATEST_VERSION)
const versionPattern = core.getInput(IN_VERSION_PATTERN)
const token = core.getInput(IN_TOKEN)
const token = getToken()

core.info(`Skip Labels: ${skipLabelList}`)
core.info(`Changelog Path: ${changeLogPath}`)
Expand Down Expand Up @@ -66,6 +66,14 @@ function getMissingUpdateErrorMessage(changeLogPath) {
return `No update to ${changeLogPath} found!`
}

function getToken() {
const token = core.getInput(IN_TOKEN)
if (!token) {
throw new Error("Did not find token for using the GitHub API")
}
return token
}

function shouldEnforceChangelog(labelNames, skipLabelList) {
return !labelNames.some(l => skipLabelList.includes(l))
}
Expand Down
6 changes: 6 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports.findChangelog = async function (token, repository, pullRequestNum
core.debug(`Downloading page ${page} of pull request files from /repos/${repository}/pulls/${pullRequestNumber}/files`)
const options = addAuth(token, {})
const response = await fetch(`https://api.github.com/repos/${repository}/pulls/${pullRequestNumber}/files?per_page=${pageSize}&page=${page}`, options)
if (!response.ok) {
throw new Error(`Got a ${response.status} response from GitHub API`)
}
const files = await response.json()
core.debug(`Downloaded page ${page} of pull request files`)

Expand All @@ -31,6 +34,9 @@ module.exports.downloadChangelog = async function (token, changelogUrl) {
core.debug(`Downloading changelog from ${changelogUrl}`)
const options = addAuth(token, {})
const response = await fetch(`${changelogUrl}`, options)
if (!response.ok) {
throw new Error(`Got a ${response.status} response from GitHub API`)
}
const changelog = await response.text()
core.debug("Downloaded changelog")
return changelog
Expand Down