Skip to content

Commit

Permalink
Merge pull request #93 from issue-ops/ncalteen/check
Browse files Browse the repository at this point in the history
Update check logic
  • Loading branch information
ncalteen authored Oct 31, 2024
2 parents e1b983a + 5c5e4fc commit df8a60d
Show file tree
Hide file tree
Showing 13 changed files with 29,557 additions and 1,384 deletions.
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
10 changes: 8 additions & 2 deletions __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 @@ -210,7 +216,7 @@ describe('main', () => {
await main.run()

expect(core.setFailed).toHaveBeenCalledWith(
"Version already exists and 'overwrite' is false"
"Version exists and 'overwrite' is false"
)
})

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

0 comments on commit df8a60d

Please sign in to comment.