-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix null error in getReviewers #10229
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
238 changes: 238 additions & 0 deletions
238
services/github/pod-github/src/sync/__tests__/pullrequests.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| /* eslint-disable import/first */ | ||
| import { PersonId } from '@hcengineering/core' | ||
| import { PullRequestExternalData } from '../githubTypes' | ||
| import type { UserInfo } from '../../types' | ||
|
|
||
| // Mock dependencies before imports | ||
| jest.mock('@octokit/webhooks-types', () => ({}), { virtual: true }) | ||
| jest.mock('octokit', () => ({}), { virtual: true }) | ||
| jest.mock('../../config', () => ({ | ||
| default: { | ||
| AccountsURL: 'http://localhost:3000', | ||
| ServerSecret: 'test-secret', | ||
| AppId: 'test-app-id', | ||
| ClientId: 'test-client-id', | ||
| ClientSecret: 'test-client-secret', | ||
| PrivateKey: 'test-private-key', | ||
| CollaboratorURL: 'http://localhost:3078', | ||
| BotName: 'test-bot' | ||
| } | ||
| })) | ||
|
|
||
| import { PullRequestSyncManager } from '../pullrequests' | ||
| /* eslint-enable import/first */ | ||
|
|
||
| describe('PullRequestSyncManager', () => { | ||
| describe('getReviewers', () => { | ||
| let manager: PullRequestSyncManager | ||
| let mockProvider: any | ||
|
|
||
| beforeEach(async () => { | ||
| mockProvider = { | ||
| getAccount: jest.fn() | ||
| } | ||
| manager = new PullRequestSyncManager(null as any, null as any, null as any) | ||
| await manager.init(mockProvider) | ||
| }) | ||
|
|
||
| it('should handle null reviewRequests gracefully', async () => { | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: null as any, | ||
| latestReviews: { nodes: [], totalCount: 0 } | ||
| } | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual([]) | ||
| expect(mockProvider.getAccount).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should handle undefined reviewRequests gracefully', async () => { | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: undefined as any, | ||
| latestReviews: { nodes: [], totalCount: 0 } | ||
| } | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual([]) | ||
| expect(mockProvider.getAccount).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should handle null items in reviewRequests.nodes', async () => { | ||
| const user1: UserInfo = { id: 'user1', login: 'user1' } | ||
| const user2: UserInfo = { id: 'user2', login: 'user2' } | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: { | ||
| nodes: [null, { requestedReviewer: user1 }, null, { requestedReviewer: user2 }] as any, | ||
| totalCount: 4 | ||
| }, | ||
| latestReviews: { nodes: [], totalCount: 0 } | ||
| } | ||
|
|
||
| mockProvider.getAccount.mockImplementation((user: UserInfo) => { | ||
| return Promise.resolve(user.id as PersonId) | ||
| }) | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual(['user1', 'user2']) | ||
| expect(mockProvider.getAccount).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| it('should handle null requestedReviewer in nodes', async () => { | ||
| const user1: UserInfo = { id: 'user1', login: 'user1' } | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: { | ||
| nodes: [{ requestedReviewer: null }, { requestedReviewer: user1 }, { requestedReviewer: null }] as any, | ||
| totalCount: 3 | ||
| }, | ||
| latestReviews: { nodes: [], totalCount: 0 } | ||
| } | ||
|
|
||
| mockProvider.getAccount.mockImplementation((user: UserInfo) => { | ||
| return Promise.resolve(user.id as PersonId) | ||
| }) | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual(['user1']) | ||
| expect(mockProvider.getAccount).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('should filter out reviewers when getAccount returns undefined', async () => { | ||
| const user1: UserInfo = { id: 'user1', login: 'user1' } | ||
| const user2: UserInfo = { id: 'user2', login: 'user2' } | ||
| const user3: UserInfo = { id: 'user3', login: 'user3' } | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: { | ||
| nodes: [{ requestedReviewer: user1 }, { requestedReviewer: user2 }, { requestedReviewer: user3 }] as any, | ||
| totalCount: 3 | ||
| }, | ||
| latestReviews: { nodes: [], totalCount: 0 } | ||
| } | ||
|
|
||
| mockProvider.getAccount.mockImplementation((user: UserInfo) => { | ||
| if (user.id === 'user2') { | ||
| return Promise.resolve(undefined) | ||
| } | ||
| return Promise.resolve(user.id as PersonId) | ||
| }) | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual(['user1', 'user3']) | ||
| expect(mockProvider.getAccount).toHaveBeenCalledTimes(3) | ||
| }) | ||
|
|
||
| it('should handle null latestReviews', async () => { | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: { nodes: [], totalCount: 0 }, | ||
| latestReviews: null as any | ||
| } | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual([]) | ||
| expect(mockProvider.getAccount).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should handle undefined latestReviews', async () => { | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: { nodes: [], totalCount: 0 }, | ||
| latestReviews: undefined as any | ||
| } | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual([]) | ||
| expect(mockProvider.getAccount).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should skip reviews with null author', async () => { | ||
| const user1: UserInfo = { id: 'user1', login: 'user1' } | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: { nodes: [], totalCount: 0 }, | ||
| latestReviews: { | ||
| nodes: [ | ||
| { author: null, state: 'APPROVED' } as any, | ||
| { author: user1, state: 'APPROVED' } as any, | ||
| { author: null, state: 'CHANGES_REQUESTED' } as any | ||
| ], | ||
| totalCount: 3 | ||
| } | ||
| } | ||
|
|
||
| mockProvider.getAccount.mockImplementation((user: UserInfo) => { | ||
| return Promise.resolve(user.id as PersonId) | ||
| }) | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual(['user1']) | ||
| expect(mockProvider.getAccount).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('should combine reviewRequests and latestReviews', async () => { | ||
| const user1: UserInfo = { id: 'user1', login: 'user1' } | ||
| const user2: UserInfo = { id: 'user2', login: 'user2' } | ||
| const user3: UserInfo = { id: 'user3', login: 'user3' } | ||
| const user4: UserInfo = { id: 'user4', login: 'user4' } | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: { | ||
| nodes: [{ requestedReviewer: user1 }, { requestedReviewer: user2 }] as any, | ||
| totalCount: 2 | ||
| }, | ||
| latestReviews: { | ||
| nodes: [{ author: user3, state: 'APPROVED' } as any, { author: user4, state: 'CHANGES_REQUESTED' } as any], | ||
| totalCount: 2 | ||
| } | ||
| } | ||
|
|
||
| mockProvider.getAccount.mockImplementation((user: UserInfo) => { | ||
| return Promise.resolve(user.id as PersonId) | ||
| }) | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual(['user1', 'user2', 'user3', 'user4']) | ||
| expect(mockProvider.getAccount).toHaveBeenCalledTimes(4) | ||
| }) | ||
|
|
||
| it('should handle complex null cases', async () => { | ||
| const user1: UserInfo = { id: 'user1', login: 'user1' } | ||
| const user2: UserInfo = { id: 'user2', login: 'user2' } | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: { | ||
| nodes: [null, { requestedReviewer: null }, { requestedReviewer: user1 }, null] as any, | ||
| totalCount: 4 | ||
| }, | ||
| latestReviews: { | ||
| nodes: [null as any, { author: null, state: 'APPROVED' } as any, { author: user2, state: 'APPROVED' } as any], | ||
| totalCount: 3 | ||
| } | ||
| } | ||
|
|
||
| mockProvider.getAccount.mockImplementation((user: UserInfo) => { | ||
| return Promise.resolve(user.id as PersonId) | ||
| }) | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual(['user1', 'user2']) | ||
| expect(mockProvider.getAccount).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| it('should handle empty arrays', async () => { | ||
| const prData: Partial<PullRequestExternalData> = { | ||
| reviewRequests: { nodes: [], totalCount: 0 }, | ||
| latestReviews: { nodes: [], totalCount: 0 } | ||
| } | ||
|
|
||
| const result = await manager.getReviewers(prData as PullRequestExternalData) | ||
|
|
||
| expect(result).toEqual([]) | ||
| expect(mockProvider.getAccount).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.