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

feat(azure): small improvements for PR assignees and reviewers (corre… #5601

Merged
merged 3 commits into from
Mar 25, 2020
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
19 changes: 16 additions & 3 deletions lib/platform/azure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,12 +820,25 @@ describe('platform/azure', () => {
azureApi.gitApi.mockImplementation(
() =>
({
getRepositories: jest.fn(() => [{ id: '1', project: { id: 2 } }]),
createThread: jest.fn(() => [{ id: 123 }]),
getThreads: jest.fn(() => []),
} as any)
);
await azure.addAssignees(123, ['test@bonjour.fr']);
expect(azureApi.gitApi).toHaveBeenCalledTimes(3);
azureApi.coreApi.mockImplementation(
() =>
({
getTeams: jest.fn(() => [
{ id: 3, name: 'abc' },
{ id: 4, name: 'def' },
]),
getTeamMembersWithExtendedProperties: jest.fn(() => [
{ identity: { displayName: 'jyc', uniqueName: 'jyc', id: 123 } },
]),
} as any)
);
await azure.addAssignees(123, ['test@bonjour.fr', 'jyc', 'def']);
expect(azureApi.gitApi).toHaveBeenCalledTimes(4);
});
});

Expand All @@ -852,7 +865,7 @@ describe('platform/azure', () => {
} as any)
);
await azure.addReviewers(123, ['test@bonjour.fr', 'jyc', 'def']);
expect(azureApi.gitApi).toHaveBeenCalledTimes(3);
expect(azureApi.gitApi).toHaveBeenCalledTimes(4);
});
});

Expand Down
72 changes: 43 additions & 29 deletions lib/platform/azure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ interface Config {
repository: string;
}

interface User {
id: string;
name: string;
}

let config: Config = {} as any;

const defaults: any = {
Expand Down Expand Up @@ -643,33 +648,7 @@ export /* istanbul ignore next */ function getIssueList(): Promise<Issue[]> {
return Promise.resolve([]);
}

/**
*
* @param {number} issueNo
* @param {string[]} assignees
*/
export async function addAssignees(
issueNo: number,
assignees: string[]
): Promise<void> {
logger.trace(`addAssignees(${issueNo}, ${assignees})`);
await ensureComment({
number: issueNo,
topic: 'Add Assignees',
content: assignees.map(a => `@<${a}>`).join(', '),
});
}

/**
*
* @param {number} prNo
* @param {string[]} reviewers
*/
export async function addReviewers(
prNo: number,
reviewers: string[]
): Promise<void> {
logger.trace(`addReviewers(${prNo}, ${reviewers})`);
async function getUserIds(users: string[]): Promise<User[]> {
const azureApiGit = await azureApi.gitApi();
const azureApiCore = await azureApi.coreApi();
const repos = await azureApiGit.getRepositories();
Expand All @@ -689,7 +668,7 @@ export async function addReviewers(
const ids: { id: string; name: string }[] = [];
members.forEach(listMembers => {
listMembers.forEach(m => {
reviewers.forEach(r => {
users.forEach(r => {
if (
r.toLowerCase() === m.identity.displayName.toLowerCase() ||
r.toLowerCase() === m.identity.uniqueName.toLowerCase()
Expand All @@ -703,7 +682,7 @@ export async function addReviewers(
});

teams.forEach(t => {
reviewers.forEach(r => {
users.forEach(r => {
if (r.toLowerCase() === t.name.toLowerCase()) {
if (ids.filter(c => c.id === t.id).length === 0) {
ids.push({ id: t.id, name: r });
Expand All @@ -712,6 +691,41 @@ export async function addReviewers(
});
});

return ids;
}

/**
*
* @param {number} issueNo
* @param {string[]} assignees
*/
export async function addAssignees(
issueNo: number,
assignees: string[]
): Promise<void> {
logger.trace(`addAssignees(${issueNo}, ${assignees})`);
const ids = await getUserIds(assignees);
await ensureComment({
number: issueNo,
topic: 'Add Assignees',
content: ids.map(a => `@<${a.id}>`).join(', '),
});
}

/**
*
* @param {number} prNo
* @param {string[]} reviewers
*/
export async function addReviewers(
prNo: number,
reviewers: string[]
): Promise<void> {
logger.trace(`addReviewers(${prNo}, ${reviewers})`);
const azureApiGit = await azureApi.gitApi();

const ids = await getUserIds(reviewers);

await Promise.all(
ids.map(async obj => {
await azureApiGit.createPullRequestReviewer(
Expand Down