-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove duplicate reviewer identities before creating pull requests (#…
- Loading branch information
1 parent
f0790a3
commit e88189d
Showing
2 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
extension/tasks/dependabotV2/utils/azure-devops/AzureDevOpsWebApiClient.test.ts
This file contains 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,95 @@ | ||
import { jest } from '@jest/globals'; | ||
|
||
import { VersionControlChangeType } from 'azure-devops-node-api/interfaces/TfvcInterfaces'; | ||
|
||
import { AzureDevOpsWebApiClient } from './AzureDevOpsWebApiClient'; | ||
import { ICreatePullRequest } from './interfaces/IPullRequest'; | ||
import exp = require('constants'); | ||
|
||
jest.mock('azure-devops-node-api'); | ||
jest.mock('azure-pipelines-task-lib/task'); | ||
|
||
describe('AzureDevOpsWebApiClient', () => { | ||
const organisationApiUrl = 'https://dev.azure.com/mock-organization'; | ||
const accessToken = 'mock-access-token'; | ||
let client: AzureDevOpsWebApiClient; | ||
|
||
beforeEach(() => { | ||
client = new AzureDevOpsWebApiClient(organisationApiUrl, accessToken); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('createPullRequest', () => { | ||
let pr: ICreatePullRequest; | ||
|
||
beforeEach(() => { | ||
pr = { | ||
project: 'project', | ||
repository: 'repository', | ||
source: { | ||
branch: 'update-branch', | ||
commit: 'commit-id', | ||
}, | ||
target: { | ||
branch: 'main', | ||
}, | ||
title: 'PR Title', | ||
description: 'PR Description', | ||
commitMessage: 'Commit Message', | ||
changes: [ | ||
{ | ||
path: 'file.txt', | ||
content: 'hello world', | ||
encoding: 'utf-8', | ||
changeType: VersionControlChangeType.Add, | ||
}, | ||
], | ||
}; | ||
}); | ||
|
||
it('should create a pull request without duplicate reviewer and assignee identities', async () => { | ||
// Arange | ||
const mockGetUserId = jest.spyOn(client, 'getUserId').mockResolvedValue('my-user-id'); | ||
const mockResolveIdentityId = jest | ||
.spyOn(client, 'resolveIdentityId') | ||
.mockImplementation(async (identity?: string) => { | ||
return identity || ''; | ||
}); | ||
const mockRestApiPost = jest | ||
.spyOn(client as any, 'restApiPost') | ||
.mockResolvedValueOnce({ | ||
commits: [{ commitId: 'new-commit-id' }], | ||
}) | ||
.mockResolvedValueOnce({ | ||
pullRequestId: 1, | ||
}); | ||
const mockRestApiPatch = jest.spyOn(client as any, 'restApiPatch').mockResolvedValueOnce({ | ||
count: 1, | ||
}); | ||
|
||
// Act | ||
pr.assignees = ['user1', 'user2']; | ||
pr.reviewers = ['user1', 'user3']; | ||
const pullRequestId = await client.createPullRequest(pr); | ||
|
||
// Assert | ||
expect(mockRestApiPost).toHaveBeenCalledTimes(2); | ||
expect((mockRestApiPost.mock.calls[1] as any)[1].reviewers.length).toBe(3); | ||
expect((mockRestApiPost.mock.calls[1] as any)[1].reviewers).toContainEqual({ | ||
id: 'user1', | ||
isRequired: true, | ||
isFlagged: true, | ||
}); | ||
expect((mockRestApiPost.mock.calls[1] as any)[1].reviewers).toContainEqual({ | ||
id: 'user2', | ||
isRequired: true, | ||
isFlagged: true, | ||
}); | ||
expect((mockRestApiPost.mock.calls[1] as any)[1].reviewers).toContainEqual({ id: 'user3' }); | ||
expect(pullRequestId).toBe(1); | ||
}); | ||
}); | ||
}); |
This file contains 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