Skip to content

Commit

Permalink
Cleanup logging and duplicate test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Harding <[email protected]>
  • Loading branch information
klutchell committed Oct 16, 2024
1 parent 2c30fe3 commit e6da12b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 144 deletions.
6 changes: 2 additions & 4 deletions __tests__/approval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ describe('ApprovalProcess', () => {
getPullRequestMergeRef: jest.fn(),
getRefSha: jest.fn(),
getAuthenticatedUser: jest.fn(),
getPullRequestRepository: jest.fn(),
findIssueComment: jest.fn(),
throwOnContextMismatch: jest.fn(),
createIssueComment: jest.fn(),
deleteStaleIssueComments: jest.fn(),
getWorkflowRunUrl: jest.fn()
Expand Down Expand Up @@ -56,7 +55,7 @@ describe('ApprovalProcess', () => {
mockGitHubClient.getAuthenticatedUser.mockResolvedValue({
id: 'test-user-id'
})
mockGitHubClient.getPullRequestRepository.mockReturnValue(null)
mockGitHubClient.throwOnContextMismatch.mockReturnValue(null)
mockGitHubClient.createIssueComment.mockResolvedValue({
id: 'test-comment-id',
html_url: 'http://test-url.com'
Expand Down Expand Up @@ -159,7 +158,6 @@ describe('ApprovalProcess', () => {
await waitPromise

expect(mockReactionManager.getEligibleReactions).toHaveBeenCalledTimes(3)
expect(core.debug).toHaveBeenCalledWith('Waiting for reactions...')
expect(core.setOutput).toHaveBeenCalledWith('approved-by', 'approver')
}, 2000) // Set test timeout to 2 seconds
})
Expand Down
80 changes: 7 additions & 73 deletions __tests__/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,18 @@ describe('GitHubClient', () => {
)
})

test('getPullRequestRepository returns the correct repository', () => {
expect(gitHubClient.getPullRequestRepository()).toEqual({
owner: 'testOwner',
repo: 'testRepo'
})
})

test('getPullRequestRepository throws an error when the context repo does not match the payload base repo', () => {
test('throwOnContextMismatch throws an error when the context repo does not match the payload base repo', () => {
gitHubClient.context.payload.pull_request.base.repo.owner.login =
'otherOwner'
expect(() => gitHubClient.getPullRequestRepository()).toThrow(
expect(() => gitHubClient.throwOnContextMismatch()).toThrow(
'Context repo does not match payload pull request base repo!'
)
})

test('throwOnContextMismatch does not throw an error when the context repo matches the payload base repo', () => {
expect(() => gitHubClient.throwOnContextMismatch()).not.toThrow()
})

test('getPullRequestAuthors returns the correct author IDs', async () => {
const mockCommits = [
{
Expand Down Expand Up @@ -122,69 +119,6 @@ describe('GitHubClient', () => {
})

test('createIssueComment creates a new comment', async () => {
const mockComment = { id: 1, html_url: 'http://test.com/comment' }
mockOctokit.rest.issues.createComment.mockResolvedValue({
data: mockComment
})

const comment = await gitHubClient.createIssueComment('Test comment body')
expect(comment).toEqual(mockComment)
expect(mockOctokit.rest.issues.createComment).toHaveBeenCalledWith({
owner: 'testOwner',
repo: 'testRepo',
body: 'Test comment body',
issue_number: 1
})
expect(core.info).toHaveBeenCalledWith(
`Created new issue comment: ${mockComment.url}`
)
})

test('findIssueComment finds an existing comment', async () => {
const mockComments = [
{
id: 1,
body: 'Test PR comment',
created_at: '2023-01-01',
updated_at: '2023-01-01',
user: { id: 'testUserId' }
},
{
id: 2,
body: 'Another PR comment',
created_at: '2023-01-02',
updated_at: '2023-01-03',
user: { id: 'testUserId' }
}
]
mockOctokit.rest.issues.listComments.mockResolvedValue({
data: mockComments
})

const comment = await gitHubClient.findIssueComment(
'testUserId',
'Test PR comment'
)
expect(comment).toEqual(mockComments[0])
expect(mockOctokit.rest.issues.listComments).toHaveBeenCalledWith({
owner: 'testOwner',
repo: 'testRepo',
issue_number: 1
})
})

test('findIssueComment returns null when no matching comment is found', async () => {
mockOctokit.rest.issues.listComments.mockResolvedValue({ data: [] })

const comment = await gitHubClient.findIssueComment(
'testUserId',
'Non-existent PR comment'
)
expect(comment).toBeNull()
expect(core.info).toHaveBeenCalledWith('No matching issue comment found.')
})

test('createIssueComment creates a new PR comment', async () => {
const mockComment = { id: 1, url: 'http://test.com/pr-comment' }
mockOctokit.rest.issues.createComment.mockResolvedValue({
data: mockComment
Expand All @@ -201,7 +135,7 @@ describe('GitHubClient', () => {
body: 'Test PR comment body'
})
expect(core.info).toHaveBeenCalledWith(
`Created new issue comment: ${mockComment.url}`
`Created new issue comment with ID: ${mockComment.id}`
)
})

Expand Down
61 changes: 28 additions & 33 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.

7 changes: 3 additions & 4 deletions src/approval.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class ApprovalProcess {
async run() {
const tokenUser = await this.gitHubClient.getAuthenticatedUser()

// used for validation purposes only
this.gitHubClient.getPullRequestRepository()
// FIXME: remove this once we manually test reviewer permissions with PRs from forks
this.gitHubClient.throwOnContextMismatch()

const runUrl = await this.gitHubClient.getWorkflowRunUrl()

Expand Down Expand Up @@ -54,7 +54,7 @@ class ApprovalProcess {

// Wait for approval by checking reactions on a comment
async waitForApproval(commentId, interval = 30) {
core.info('Waiting for reactions on comment ID:', commentId)
core.info(`Waiting for reactions at ${interval}-second intervals...`)
for (;;) {
const reactions = await this.reactionManager.getEligibleReactions(
commentId,
Expand Down Expand Up @@ -82,7 +82,6 @@ class ApprovalProcess {
return
}

core.debug('Waiting for reactions...')
await new Promise(resolve => setTimeout(resolve, interval * 1000))
}
}
Expand Down
Loading

0 comments on commit e6da12b

Please sign in to comment.