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

Update check logic #93

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 1 addition & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@
"html.format.templating": true,
"markdown.extension.list.indentationSize": "adaptive",
"markdown.extension.italic.indicator": "_",
"markdown.extension.orderedList.marker": "one",
"java.checkstyle.configuration": "usps-common-configuration/linting_rules/java/sun-checks.xml",
"java.configuration.updateBuildConfiguration": "automatic",
"java.compile.nullAnalysis.mode": "automatic"
"markdown.extension.orderedList.marker": "one"
}
10 changes: 7 additions & 3 deletions __fixtures__/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export const context = {
job: 'job-name',
payload: {},
repo: {
owner: 'USPS',
repo: 'fast-track-deployment-manifest-action'
}
owner: 'issue-ops',
repo: 'semver'
},
issue: {
number: 1
},
workflow: 'workflow-name'
}
3 changes: 2 additions & 1 deletion __fixtures__/octokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const rest = {
get: jest.fn(),
listComments: jest.fn(),
removeLabel: jest.fn(),
update: jest.fn()
update: jest.fn(),
updateComment: jest.fn()
},
orgs: {
checkMembershipForUser: jest.fn()
Expand Down
8 changes: 7 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ jest.unstable_mockModule('../src/version.js', async () => {
Version
}
})
jest.unstable_mockModule('../src/messages.js', async () => {
return {
getCommentId: jest.fn(),
versionCheckComment: jest.fn()
}
})

const main = await import('../src/main.js')
const { Version } = await import('../src/version.js')
Expand Down Expand Up @@ -243,7 +249,7 @@ describe('main', () => {
await main.run()

expect(core.setFailed).toHaveBeenCalledWith(
"Version already exists and 'check-only' is true"
"Version exists and 'check-only' is true"
)
})
})
102 changes: 102 additions & 0 deletions __tests__/messages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { jest } from '@jest/globals'
import * as github from '../__fixtures__/github.js'

jest.unstable_mockModule('@actions/github', () => github)

const messages = await import('../src/messages.js')

const { getOctokit } = await import('@actions/github')
const mocktokit = jest.mocked(getOctokit(process.env.GITHUB_TOKEN as string))

describe('messages', () => {
beforeEach(() => {
process.env.GITHUB_REPOSITORY = 'issue-ops/semver'
})

afterEach(() => {
jest.resetAllMocks()
})

describe('getCommentId()', () => {
it('returns the ID of the existing comment', async () => {
mocktokit.rest.issues.listComments.mockResolvedValue({
data: [
{
id: 1,
body: 'test comment\n\n<!-- semver: workflow=workflow-name -->'
}
]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)

const commentId = await messages.getCommentId()
expect(commentId).toBe(1)
})

it('returns undefined if no existing comment is present', async () => {
mocktokit.rest.issues.listComments.mockResolvedValue({
data: []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)

const commentId = await messages.getCommentId()
expect(commentId).toBe(undefined)
})
})

describe('versionCheckComment()', () => {
it('updates an existing comment (success)', async () => {
mocktokit.rest.issues.listComments.mockResolvedValue({
data: [
{
id: 1,
body: 'test comment\n\n<!-- semver: workflow=workflow-name -->'
}
]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)

await messages.versionCheckComment(true, 'package.json')

expect(mocktokit.rest.issues.updateComment).toHaveBeenCalled()
})

it('updates an existing comment (failure)', async () => {
mocktokit.rest.issues.listComments.mockResolvedValue({
data: [
{
id: 1,
body: 'test comment\n\n<!-- semver: workflow=workflow-name -->'
}
]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)

await messages.versionCheckComment(false, 'package.json')

expect(mocktokit.rest.issues.updateComment).toHaveBeenCalled()
})

it('creates a new comment (success)', async () => {
mocktokit.rest.issues.listComments.mockResolvedValue({
data: []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)

await messages.versionCheckComment(true, 'package.json')

expect(mocktokit.rest.issues.createComment).toHaveBeenCalled()
})

it('creates a new comment (failure)', async () => {
mocktokit.rest.issues.listComments.mockResolvedValue({
data: []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)

await messages.versionCheckComment(false, 'package.json')

expect(mocktokit.rest.issues.createComment).toHaveBeenCalled()
})
})
})
Loading