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: add default reviewers to manual backports #274

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions spec/fixtures/backport_pull_request.closed.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"head": {
"ref": "123456789iuytdxcvbnjhfdriuyfedfgy54escghjnbg"
},
"base": {
"ref": "main",
"repo": {
"default_branch": "main"
}
},
"body": "Backport of #14\nSee that PR for details.\nNotes: <!-- One-line Change Summary Here-->",
"created_at": "2018-11-01T17:29:51Z",
"merged_at": "2018-11-01T17:30:11Z",
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/backport_pull_request.merged.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"head": {
"ref": "123456789iuytdxcvbnjhfdriuyfedfgy54escghjnbg"
},
"base": {
"ref": "main",
"repo": {
"default_branch": "main"
}
},
"body": "Backport of #14\nSee that PR for details.\nNotes: <!-- One-line Change Summary Here-->",
"created_at": "2018-11-01T17:29:51Z",
"merged_at": "2018-11-01T17:30:11Z",
Expand Down
12 changes: 12 additions & 0 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,12 @@ Notes: <!-- One-line Change Summary Here-->`,
head: {
ref: '123456789iuytdxcvbnjhfdriuyfedfgy54escghjnbg',
},
base: {
ref: 'main',
repo: {
default_branch: 'main',
},
},
labels: [
{
color: 'ededed',
Expand Down Expand Up @@ -429,6 +435,12 @@ Notes: <!-- One-line Change Summary Here-->`,
head: {
ref: '123456789iuytdxcvbnjhfdriuyfedfgy54escghjnbg',
},
base: {
ref: 'main',
repo: {
default_branch: 'main',
},
},
labels: [
{
color: 'ededed',
Expand Down
61 changes: 61 additions & 0 deletions spec/operations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import * as path from 'path';
import simpleGit from 'simple-git';
import { initRepo } from '../src/operations/init-repo';
import { setupRemotes } from '../src/operations/setup-remotes';
import { tagBackportReviewers } from '../src/utils';
import { PRChange } from '../src/enums';
import { updateManualBackport } from '../src/operations/update-manual-backport';

let dirObject: { dir?: string } | null = null;

Expand All @@ -13,6 +16,22 @@ const saveDir = (o: { dir: string }) => {
return o.dir;
};

const backportPRMergedEvent = require('./fixtures/backport_pull_request.merged.json');
const backportPRClosedEvent = require('./fixtures/backport_pull_request.closed.json');
const backportPROpenedEvent = require('./fixtures/backport_pull_request.opened.json');

jest.mock('../src/utils', () => ({
tagBackportReviewers: jest.fn().mockReturnValue(Promise.resolve()),
isSemverMinorPR: jest.fn().mockReturnValue(false),
}));

jest.mock('../src/utils/label-utils', () => ({
labelExistsOnPR: jest.fn().mockResolvedValue(true),
getSemverLabel: jest.fn().mockResolvedValue(false),
addLabels: jest.fn(),
removeLabel: jest.fn(),
}));

describe('runner', () => {
jest.setTimeout(30000);
console.error = jest.fn();
Expand Down Expand Up @@ -101,4 +120,46 @@ describe('runner', () => {
}
});
});

describe('updateManualBackport()', () => {
let context: any;
let octokit = {
pulls: {
get: jest.fn().mockReturnValue(Promise.resolve({})),
},
issues: {
createComment: jest.fn().mockReturnValue(Promise.resolve({})),
listComments: jest.fn().mockReturnValue(Promise.resolve({ data: [] })),
},
};

it('tags reviewers on manual backport creation', async () => {
context = {
...backportPROpenedEvent,
octokit,
repo: jest.fn(),
};
await updateManualBackport(context, PRChange.OPEN, 1234);
expect(tagBackportReviewers).toHaveBeenCalled();
});

it('does not tag reviewers on merged PRs', async () => {
context = {
...backportPRMergedEvent,
octokit,
repo: jest.fn(),
};
await updateManualBackport(context, PRChange.MERGE, 1234);
expect(tagBackportReviewers).not.toHaveBeenCalled();
});
it('does not tag reviewers on closed PRs', async () => {
context = {
...backportPRClosedEvent,
octokit,
repo: jest.fn(),
};
await updateManualBackport(context, PRChange.CLOSE, 1234);
expect(tagBackportReviewers).not.toHaveBeenCalled();
});
});
});
9 changes: 8 additions & 1 deletion src/operations/update-manual-backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
BACKPORT_REQUESTED_LABEL,
SKIP_CHECK_LABEL,
} from '../constants';
import { isSemverMinorPR } from '../utils';
import { tagBackportReviewers, isSemverMinorPR } from '../utils';
import { WebHookPRContext } from '../types';

/**
Expand Down Expand Up @@ -135,6 +135,13 @@ please check out #${pr.number}`;
}),
);
}

// Tag default reviewers to manual backport
await tagBackportReviewers({
context,
user: pr.user.login,
targetPrNumber: pr.number,
});
} else if (type === PRChange.MERGE) {
log(
'updateManualBackport',
Expand Down
51 changes: 33 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,34 @@ const createBackportComment = async (
return body;
};

export const tagBackportReviewers = async ({
context,
user,
targetPrNumber,
}: {
context: SimpleWebHookRepoContext;
user: string;
targetPrNumber: number;
}) => {
const reviewers = [];
const hasWrite = await checkUserHasWriteAccess(context, user);
// Optionally request a default review team for backports.
// If the PR author has write access, also request their review.
if (hasWrite) reviewers.push(user);
if (DEFAULT_BACKPORT_REVIEW_TEAM) {
reviewers.push(DEFAULT_BACKPORT_REVIEW_TEAM);
}

if (reviewers.length > 0) {
await context.octokit.pulls.requestReviewers(
context.repo({
pull_number: targetPrNumber,
reviewers,
}),
);
}
};

export const backportImpl = async (
robot: Probot,
context: SimpleWebHookRepoContext,
Expand Down Expand Up @@ -561,24 +589,11 @@ export const backportImpl = async (
}),
);

const reviewers = [];
const hasWrite = await checkUserHasWriteAccess(context, pr.user.login);

// Optionally request a default review team for backports.
// If the PR author has write access, also request their review.
if (hasWrite) reviewers.push(pr.user.login);
if (DEFAULT_BACKPORT_REVIEW_TEAM) {
reviewers.push(DEFAULT_BACKPORT_REVIEW_TEAM);
}

if (reviewers.length > 0) {
await context.octokit.pulls.requestReviewers(
context.repo({
pull_number: newPr.number,
reviewers,
}),
);
}
await tagBackportReviewers({
context,
user: pr.user.login,
targetPrNumber: newPr.number,
});

log(
'backportImpl',
Expand Down
Loading