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

Fix duplicate reviewer identities causing "500 Internal Server Error" when creating pull requests #1457

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class AzureDevOpsWebApiClient {
if (pr.assignees?.length > 0) {
for (const assignee of pr.assignees) {
const identityId = isGuid(assignee) ? assignee : await this.resolveIdentityId(assignee);
if (identityId) {
if (identityId && !allReviewers.some((r) => r.id === identityId)) {
allReviewers.push({
id: identityId,
isRequired: true,
Expand All @@ -203,7 +203,7 @@ export class AzureDevOpsWebApiClient {
if (pr.reviewers?.length > 0) {
for (const reviewer of pr.reviewers) {
const identityId = isGuid(reviewer) ? reviewer : await this.resolveIdentityId(reviewer);
if (identityId) {
if (identityId && !allReviewers.some((r) => r.id === identityId)) {
allReviewers.push({
id: identityId,
});
Expand Down