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: handle no release #43

Merged
merged 1 commit into from
May 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 32 additions & 36 deletions dist/index.js

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

14 changes: 4 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
'use strict'
const core = require('@actions/core')
const { logInfo } = require('./log')

const { runAction } = require('./release-notify-action')

async function run() {
try {
const token = core.getInput('github-token', { required: true })
const staleDays = Number(core.getInput('stale-days'))
const commitMessageLines = Number(core.getInput('commit-messages-lines'))
const token = core.getInput('github-token', { required: true })
const staleDays = Number(core.getInput('stale-days'))
const commitMessageLines = Number(core.getInput('commit-messages-lines'))

return await runAction(token, staleDays, commitMessageLines)
} catch (error) {
logInfo(error.message)
core.setFailed(error.message)
}
await runAction(token, staleDays, commitMessageLines)
}

run()
41 changes: 24 additions & 17 deletions src/release-notify-action.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { logInfo } = require('./log')
const { logInfo, logWarning } = require('./log')
const { getLatestRelease, getUnreleasedCommits } = require('./release')
const {
createOrUpdateIssue,
Expand All @@ -9,39 +9,46 @@ const {
} = require('./issue')

async function runAction(token, staleDays, commitMessageLines) {
const latestRelease = await getLatestRelease(token)
let latestRelease

if (!latestRelease) {
return logInfo('Could not find latest release')
try {
latestRelease = await getLatestRelease(token)
} catch (err) {
return logWarning('Could not find latest release')
}

logInfo(`Latest release - name:${latestRelease.name}, published:${latestRelease.published_at},
Tag:${latestRelease.tag_name}, author:${latestRelease.author.login}`)
logInfo(`Latest release:
- name:${latestRelease.name}
- published:${latestRelease.published_at}
- tag:${latestRelease.tag_name}
- author:${latestRelease.author.login}
`)

const pendingIssue = await getLastOpenPendingIssue(token)

let pendingIssue = await getLastOpenPendingIssue(token)
const unreleasedCommits = await getUnreleasedCommits(
token,
latestRelease.published_at,
staleDays
)

if (unreleasedCommits.length) {
await createOrUpdateIssue(
return createOrUpdateIssue(
token,
unreleasedCommits,
pendingIssue,
latestRelease,
commitMessageLines
)
} else {
logInfo('No pending commits found')
if (
pendingIssue &&
Date.parse(latestRelease.published_at) >
Date.parse(pendingIssue.updated_at)
) {
await closeIssue(token, pendingIssue.number)
}
}

logInfo('No pending commits found')

if (
pendingIssue &&
Date.parse(latestRelease.published_at) > Date.parse(pendingIssue.updated_at)
) {
return closeIssue(token, pendingIssue.number)
}
}

Expand Down
13 changes: 4 additions & 9 deletions src/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ async function getLatestRelease(token) {
const octokit = github.getOctokit(token)
const { owner, repo } = github.context.repo

const latestReleaseResponse = await octokit.request(
`GET /repos/{owner}/{repo}/releases/latest`,
{
owner,
repo,
}
)

return latestReleaseResponse.data
return octokit.rest.repos.getLatestRelease({
owner,
repo,
})
}

async function getUnreleasedCommits(token, latestReleaseDate, staleDays) {
Expand Down
12 changes: 12 additions & 0 deletions test/log.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

jest.mock('@actions/core')

const log = require('../src/log')

describe('log', () => {
it.each([['logDebug'], ['logInfo'], ['logWarning'], ['logError']])(
'it should log with %s',
(method) => log[method]()
)
})
18 changes: 8 additions & 10 deletions test/release-notify-action.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ const {
pendingIssues,
} = require('./testData')

jest.mock('../src/log', () => ({
logInfo: jest.fn(),
}))
jest.mock('../src/log')

jest.mock('../src/release', () => ({
getLatestRelease: jest.fn(),
Expand All @@ -36,7 +34,7 @@ beforeEach(() => {
const token = 'dummyToken'

test('Create issue for unreleased commits (no existing issues)', async () => {
release.getLatestRelease.mockResolvedValue(allReleases.data[0])
release.getLatestRelease.mockResolvedValue(allReleases[0])
issue.getLastOpenPendingIssue.mockResolvedValue(null)
release.getUnreleasedCommits.mockResolvedValue(unreleasedCommitsData1)

Expand All @@ -48,14 +46,14 @@ test('Create issue for unreleased commits (no existing issues)', async () => {
token,
unreleasedCommitsData1,
null,
allReleases.data[0],
allReleases[0],
1
)
expect(issue.closeIssue).not.toHaveBeenCalled()
})

test('Update issue for unreleased commits (issue already exists)', async () => {
release.getLatestRelease.mockResolvedValue(allReleases.data[0])
release.getLatestRelease.mockResolvedValue(allReleases[0])
issue.getLastOpenPendingIssue.mockResolvedValue(pendingIssues[0])
release.getUnreleasedCommits.mockResolvedValue(unreleasedCommitsData1)

Expand All @@ -67,14 +65,14 @@ test('Update issue for unreleased commits (issue already exists)', async () => {
token,
unreleasedCommitsData1,
pendingIssues[0],
allReleases.data[0],
allReleases[0],
1
)
expect(issue.closeIssue).not.toHaveBeenCalled()
})

test('Close issue when there is one pending and no unreleased commits', async () => {
release.getLatestRelease.mockResolvedValue(allReleases.data[0])
release.getLatestRelease.mockResolvedValue(allReleases[0])
issue.getLastOpenPendingIssue.mockResolvedValue(pendingIssues[0])
release.getUnreleasedCommits.mockResolvedValue([])

Expand All @@ -87,7 +85,7 @@ test('Close issue when there is one pending and no unreleased commits', async ()
})

test('Do nothing when there is one issue pending and no new releases', async () => {
release.getLatestRelease.mockResolvedValue(allReleases.data[1])
release.getLatestRelease.mockResolvedValue(allReleases[1])
issue.getLastOpenPendingIssue.mockResolvedValue(pendingIssues[0])
release.getUnreleasedCommits.mockResolvedValue([])

Expand All @@ -100,7 +98,7 @@ test('Do nothing when there is one issue pending and no new releases', async ()
})

test('Do nothing when no releases found', async () => {
release.getLatestRelease.mockResolvedValue(null)
release.getLatestRelease.mockRejectedValue()

await runAction(token, 1, 1)

Expand Down
23 changes: 13 additions & 10 deletions test/release.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
allReleasesData: allReleases,
unreleasedCommitsData0,
unreleasedCommitsData1,
allReleasesNoData,
} = require('./testData')

const token = 'dummytoken'
Expand All @@ -19,24 +18,28 @@ jest.mock('@actions/github', () => ({

test('Gets the latest release of the repository', async () => {
getOctokit.mockReturnValue({
request: async () => ({ data: allReleases.data[0] }),
rest: {
repos: { getLatestRelease: async () => allReleases[0] },
},
})
const latestReleaseResponse = await getLatestRelease(token)
expect(latestReleaseResponse).toStrictEqual(allReleases.data[0])
expect(latestReleaseResponse).toStrictEqual(allReleases[0])
})

test('Retuns null if no releases found', async () => {
test('Throws if no releases found', async () => {
getOctokit.mockReturnValue({
request: async () => ({ data: allReleasesNoData.data[0] }),
rest: {
repos: { getLatestRelease: () => Promise.reject(new Error()) },
},
})
const latestReleaseResponse = await getLatestRelease(token)
expect(latestReleaseResponse).toBeUndefined()

await expect(getLatestRelease(token)).rejects.toBeDefined()
})

test('Gets the unreleased commits with stale-days as 0', async () => {
getOctokit.mockReturnValue({ request: async () => allCommits })
const daysToIgnore = 0
const latestReleaseDate = allReleases.data[0].created_at
const latestReleaseDate = allReleases[0].created_at
const allCommitsResponse = await getUnreleasedCommits(
token,
latestReleaseDate,
Expand All @@ -48,7 +51,7 @@ test('Gets the unreleased commits with stale-days as 0', async () => {
test('Gets the unreleased commits with stale-days as non zero', async () => {
getOctokit.mockReturnValue({ request: async () => allCommits })
const daysToIgnore = 3
const latestReleaseDate = allReleases.data[0].created_at
const latestReleaseDate = allReleases[0].created_at
const allCommitsResponse = await getUnreleasedCommits(
token,
latestReleaseDate,
Expand All @@ -60,7 +63,7 @@ test('Gets the unreleased commits with stale-days as non zero', async () => {
test('Gets the unreleased commits and uses default value of stale-days', async () => {
getOctokit.mockReturnValue({ request: async () => allCommits })
const daysToIgnore = undefined
const latestReleaseDate = allReleases.data[0].created_at
const latestReleaseDate = allReleases[0].created_at
const allCommitsResponse = await getUnreleasedCommits(
token,
latestReleaseDate,
Expand Down
Loading