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: add validation check for manual bp release notes #285

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion spec/fixtures/backport_pull_request.merged.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"default_branch": "main"
}
},
"body": "Backport of #14\nSee that PR for details.\nNotes: <!-- One-line Change Summary Here-->",
"body": "Backport of #14\nSee that PR for details.\nNotes: new cool stuff added",
"created_at": "2018-11-01T17:29:51Z",
"merged_at": "2018-11-01T17:30:11Z",
"labels": [
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/pull_request.opened.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"user": {
"login": "codebytere"
},
"body": "New cool stuff",
"body": "New cool stuff \nNotes: new cool stuff added",
"labels": [],
"head": {
"sha": "ABC"
Expand Down
2 changes: 1 addition & 1 deletion spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ Notes: <!-- One-line Change Summary Here-->`,
const pr = {
body: `Backport of #14
See that PR for details.
Notes: <!-- One-line Change Summary Here-->`,
Notes: new cool stuff added`,
created_at: '2018-11-01T17:29:51Z',
head: {
ref: '123456789iuytdxcvbnjhfdriuyfedfgy54escghjnbg',
Expand Down
58 changes: 56 additions & 2 deletions spec/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import * as logUtils from '../src/utils/log-util';
import { LogLevel } from '../src/enums';
import { tagBackportReviewers } from '../src/utils';
import {
tagBackportReviewers,
isValidManualBackportReleaseNotes,
} from '../src/utils';
import * as utils from '../src/utils';
import * as logUtils from '../src/utils/log-util';

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

jest.mock('../src/constants', () => ({
...jest.requireActual('../src/constants'),
Expand Down Expand Up @@ -74,4 +81,51 @@ describe('utils', () => {
);
});
});

describe('isValidManualBackportReleaseNotes', () => {
const backportPRMissingReleaseNotes = backportPROpenedEvent;
const backportPRWithReleaseNotes = backportPRMergedEvent;
const originalPRWithReleaseNotes = PROpenedEvent.payload.pull_request;
const originalPRMissingReleaseNotes = PRClosedEvent.payload.pull_request;
const originalPRWithReleaseNotes2 =
backportPRMergedEvent.payload.pull_request;

it('should return valid if release notes match original PR for single PR', async () => {
const context = { ...backportPRWithReleaseNotes };
expect(
await isValidManualBackportReleaseNotes(context, [
originalPRWithReleaseNotes,
]),
).toBe(true);
});

it('should return valid if release notes match original PR for multiple PR', async () => {
const context = { ...backportPRWithReleaseNotes };
expect(
await isValidManualBackportReleaseNotes(context, [
originalPRWithReleaseNotes,
originalPRMissingReleaseNotes,
]),
).toBe(true);
});

it('should return not valid if release notes do not match original PR for single PR', async () => {
const context = { ...backportPRMissingReleaseNotes };
expect(
await isValidManualBackportReleaseNotes(context, [
originalPRWithReleaseNotes,
]),
).toBe(false);
});

it('should return not valid if release notes do not match original PR for multiple PR', async () => {
const context = { ...backportPRMissingReleaseNotes };
expect(
await isValidManualBackportReleaseNotes(context, [
originalPRWithReleaseNotes,
originalPRWithReleaseNotes2,
]),
).toBe(false);
});
});
});
68 changes: 45 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
backportImpl,
getPRNumbersFromPRBody,
isAuthorizedUser,
isValidManualBackportReleaseNotes,
labelClosedPR,
} from './utils';
import {
Expand Down Expand Up @@ -249,8 +250,6 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => {
];
const FASTTRACK_LABELS: string[] = ['fast-track 🚅'];

const failureMap = new Map();

// There are several types of PRs which might not target main yet which are
// inherently valid; e.g roller-bot PRs. Check for and allow those here.
if (oldPRNumbers.length === 0) {
Expand Down Expand Up @@ -286,7 +285,9 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => {
robot.log(
`#${pr.number} has backport numbers - checking their validity now`,
);
const failureMap = new Map();
const supported = await getSupportedBranches(context);
const oldPRs = [];

for (const oldPRNumber of oldPRNumbers) {
robot.log(`Checking validity of #${oldPRNumber}`);
Expand All @@ -296,6 +297,8 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => {
}),
);

oldPRs.push(oldPR);
alicelovescake marked this conversation as resolved.
Show resolved Hide resolved

// The current PR is only valid if the PR it is backporting
// was merged to main or to a supported release branch.
if (
Expand All @@ -312,29 +315,48 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => {
failureMap.set(oldPRNumber, cause);
}
}
}

for (const oldPRNumber of oldPRNumbers) {
if (failureMap.has(oldPRNumber)) {
robot.log(
`#${pr.number} is targeting a branch that is not ${
pr.base.repo.default_branch
} - ${failureMap.get(oldPRNumber)}`,
for (const oldPRNumber of oldPRNumbers) {
if (failureMap.has(oldPRNumber)) {
robot.log(
`#${pr.number} is targeting a branch that is not ${
pr.base.repo.default_branch
} - ${failureMap.get(oldPRNumber)}`,
);
await updateBackportValidityCheck(context, checkRun, {
title: 'Invalid Backport',
summary: `This PR is targeting a branch that is not ${
pr.base.repo.default_branch
} but ${failureMap.get(oldPRNumber)}`,
conclusion: CheckRunStatus.FAILURE,
});
} else {
robot.log(`#${pr.number} is a valid backport of #${oldPRNumber}`);
await updateBackportValidityCheck(context, checkRun, {
title: 'Valid Backport',
summary: `This PR is declared as backporting "#${oldPRNumber}" which is a valid PR that has been merged into ${pr.base.repo.default_branch}`,
conclusion: CheckRunStatus.SUCCESS,
});
}
}

if (['opened', 'edited'].includes(action)) {
robot.log(`Checking validity of release notes`);
// Check to make sure backport PR has the same release notes as at least one of the old prs
const isValidReleaseNotes = await isValidManualBackportReleaseNotes(
context,
oldPRs as WebHookPR[],
);
await updateBackportValidityCheck(context, checkRun, {
title: 'Invalid Backport',
summary: `This PR is targeting a branch that is not ${
pr.base.repo.default_branch
} but ${failureMap.get(oldPRNumber)}`,
conclusion: CheckRunStatus.FAILURE,
});
} else {
robot.log(`#${pr.number} is a valid backport of #${oldPRNumber}`);
await updateBackportValidityCheck(context, checkRun, {
title: 'Valid Backport',
summary: `This PR is declared as backporting "#${oldPRNumber}" which is a valid PR that has been merged into ${pr.base.repo.default_branch}`,
conclusion: CheckRunStatus.SUCCESS,
});

if (!isValidReleaseNotes) {
await updateBackportValidityCheck(context, checkRun, {
title: 'Invalid Backport',
summary: `The release notes of the backport PR do not match those of ${oldPRNumbers
.map((pr) => `#${pr}`)
.join(', ')}.`,
conclusion: CheckRunStatus.FAILURE,
});
Comment on lines +352 to +358
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is going to get clobbered by the update a bit further down in this file:

trop/src/index.ts

Lines 358 to 362 in 47f78d4

await updateBackportValidityCheck(context, checkRun, {
title: 'Valid Backport',
summary: `This PR is declared as backporting "#${oldPRNumber}" which is a valid PR that has been merged into ${pr.base.repo.default_branch}`,
conclusion: CheckRunStatus.SUCCESS,
});

Might take some more refactoring to ensure the clobbering doesn't happen. However, I'm a bit worried about a regression since we don't have good test coverage here. Could you look at expanding test coverage in spec/index.spec.ts to cover updateBackportValidityCheck? A separate PR first expanding test coverage might be best since it will keep the changes in this one more concise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointers!

  • Created a PR to expand test coverage of index.spec.ts test: add test for updateBackportValidityCheck #289
  • Updated the logic in this PR to move the release notes check after all the failure map check so it doesn't get clobbered
  • After the test PR gets merged, I will update this PR with additional updateBackportValidityCheck so we can be confident it works

}
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/operations/update-manual-backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
SKIP_CHECK_LABEL,
} from '../constants';
import { PRChange, PRStatus, LogLevel } from '../enums';
import { WebHookPRContext } from '../types';
import { WebHookPR, WebHookPRContext } from '../types';
import { isSemverMinorPR, tagBackportReviewers } from '../utils';
import * as labelUtils from '../utils/label-utils';
import { log } from '../utils/log-util';
Expand Down
48 changes: 30 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import {
SimpleWebHookRepoContext,
WebHookIssueContext,
WebHookPR,
WebHookPRContext,
WebHookRepoContext,
} from './types';
import { Context, Probot } from 'probot';
import { Probot } from 'probot';

const { parse: parseDiff } = require('what-the-diff');

Expand Down Expand Up @@ -298,7 +299,7 @@ export const getPRNumbersFromPRBody = (pr: WebHookPR, checkNotBot = false) => {
* @param context Context
* @param pr Pull Request
*/
const getOriginalBackportNumber = async (
export const getOriginalBackportNumber = async (
context: SimpleWebHookRepoContext,
pr: WebHookPR,
) => {
Expand Down Expand Up @@ -365,6 +366,23 @@ const checkUserHasWriteAccess = async (
return ['write', 'admin'].includes(userInfo.permission);
};

const getReleaseNotes = (pr: Pick<WebHookPR, 'body'>) => {
const onelineMatch = pr.body?.match(
/(?:(?:\r?\n)|^)notes: (.+?)(?:(?:\r?\n)|$)/gi,
);
const multilineMatch = pr.body?.match(
/(?:(?:\r?\n)Notes:(?:\r?\n)((?:\*.+(?:(?:\r?\n)|$))+))/gi,
);

if (onelineMatch && onelineMatch[0]) {
return `\n\n${onelineMatch[0]}`;
} else if (multilineMatch && multilineMatch[0]) {
return `\n\n${multilineMatch[0]}`;
} else {
return '\n\nNotes: no-notes';
}
};

const createBackportComment = async (
context: SimpleWebHookRepoContext,
pr: WebHookPR,
Expand All @@ -377,23 +395,9 @@ const createBackportComment = async (
`Creating backport comment for #${prNumber}`,
);

let body = `Backport of #${prNumber}\n\nSee that PR for details.`;

const onelineMatch = pr.body?.match(
/(?:(?:\r?\n)|^)notes: (.+?)(?:(?:\r?\n)|$)/gi,
);
const multilineMatch = pr.body?.match(
/(?:(?:\r?\n)Notes:(?:\r?\n)((?:\*.+(?:(?:\r?\n)|$))+))/gi,
);

const releaseNotes = getReleaseNotes(pr);
// attach release notes to backport PR body
if (onelineMatch && onelineMatch[0]) {
body += `\n\n${onelineMatch[0]}`;
} else if (multilineMatch && multilineMatch[0]) {
body += `\n\n${multilineMatch[0]}`;
} else {
body += '\n\nNotes: no-notes';
}
const body = `Backport of #${prNumber}\n\nSee that PR for details.${releaseNotes}`;

return body;
};
Expand Down Expand Up @@ -798,3 +802,11 @@ export const backportImpl = async (
},
);
};

export const isValidManualBackportReleaseNotes = async (
context: WebHookPRContext,
oldPRs: WebHookPR[],
) => {
const backportPRReleaseNotes = getReleaseNotes(context.payload.pull_request);
return oldPRs.some((pr) => getReleaseNotes(pr) === backportPRReleaseNotes);
};