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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
1 change: 1 addition & 0 deletions spec/operations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const backportPROpenedEvent = require('./fixtures/backport_pull_request.opened.j
jest.mock('../src/utils', () => ({
tagBackportReviewers: jest.fn().mockReturnValue(Promise.resolve()),
isSemverMinorPR: jest.fn().mockReturnValue(false),
updateManualBackportReleaseNotes: jest.fn(),
}));

jest.mock('../src/utils/label-utils', () => ({
Expand Down
59 changes: 57 additions & 2 deletions spec/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import * as logUtils from '../src/utils/log-util';
import { LogLevel } from '../src/enums';
import { tagBackportReviewers } from '../src/utils';
import {
tagBackportReviewers,
updateManualBackportReleaseNotes,
} 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');

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

describe('updateManualBackportReleaseNotes', () => {
const octokit = {
pulls: {
update: jest.fn(),
get: jest.fn(),
},
};

const context = {
octokit,
repo: jest.fn((obj) => obj),
...backportPROpenedEvent,
};

const backportPRMissingReleaseNotes =
backportPROpenedEvent.payload.pull_request;
const backportPRWithReleaseNotes =
backportPRMergedEvent.payload.pull_request;
const originalPRWithReleaseNotes = PROpenedEvent.payload.pull_request;

beforeEach(() => {
jest.clearAllMocks();
});

it('should not update backport PR if release notes match original PR', async () => {
await updateManualBackportReleaseNotes(
context,
backportPRWithReleaseNotes,
originalPRWithReleaseNotes,
);

expect(context.octokit.pulls.update).not.toHaveBeenCalled();
});

it('should update backport PR if release notes do not match original PR', async () => {
jest.spyOn(utils, 'getOriginalBackportNumber').mockResolvedValue(1234);
await updateManualBackportReleaseNotes(
context,
backportPRMissingReleaseNotes,
originalPRWithReleaseNotes,
);

expect(context.octokit.pulls.update).toHaveBeenCalledWith({
pull_number: 7,
body: 'Backport of #1234\n\nSee that PR for details.\n\n\nNotes: new cool stuff added',
});
});
});
});
14 changes: 12 additions & 2 deletions src/operations/update-manual-backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import {
SKIP_CHECK_LABEL,
} from '../constants';
import { PRChange, PRStatus, LogLevel } from '../enums';
import { WebHookPRContext } from '../types';
import { isSemverMinorPR, tagBackportReviewers } from '../utils';
import { WebHookPR, WebHookPRContext } from '../types';
import {
isSemverMinorPR,
tagBackportReviewers,
updateManualBackportReleaseNotes,
} from '../utils';
import * as labelUtils from '../utils/label-utils';
import { log } from '../utils/log-util';

Expand Down Expand Up @@ -69,6 +73,12 @@ export const updateManualBackport = async (
context.repo({ pull_number: oldPRNumber }),
);

// Update backport PR release notes if it does not match original PR
await updateManualBackportReleaseNotes(
context,
pr,
originalPR as WebHookPR,
);
// Propagate semver label from the original PR if the maintainer didn't add it.
const originalPRSemverLabel = labelUtils.getSemverLabel(originalPR);
if (originalPRSemverLabel) {
Expand Down
66 changes: 48 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
WebHookPR,
WebHookRepoContext,
} from './types';
import { Context, Probot } from 'probot';
import { Probot } from 'probot';

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

Expand Down Expand Up @@ -297,7 +297,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 @@ -364,6 +364,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 @@ -376,23 +393,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 @@ -787,3 +790,30 @@ export const backportImpl = async (
},
);
};

export const updateManualBackportReleaseNotes = async (
context: SimpleWebHookRepoContext,
backportPr: WebHookPR,
originalPr: WebHookPR,
) => {
const backportPRReleaseNotes = getReleaseNotes(backportPr);
const originalPRReleaseNotes = getReleaseNotes(originalPr);
// If release notes match, do nothing
if (backportPRReleaseNotes === originalPRReleaseNotes) {
return;
}

log(
'updateManualBackport',
LogLevel.WARN,
`Manual backport does not match the release notes of the original PR.`,
);

// Update backport PR with new description that includes matching release notes to original PR
await context.octokit.pulls.update(
context.repo({
pull_number: backportPr.number,
body: await createBackportComment(context, originalPr),
}),
);
alicelovescake marked this conversation as resolved.
Show resolved Hide resolved
};