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(code/onboarding): infrastructure for onboarding PR rebase/retry checkbox #17673

Merged
7 changes: 4 additions & 3 deletions lib/modules/platform/azure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,10 @@ describe('modules/platform/azure/index', () => {

describe('massageMarkdown(input)', () => {
it('returns updated pr body', () => {
const input =
'\n---\n\n - [ ] <!-- rebase-check --> rebase\nplus also [a link](https://github.com/foo/bar/issues/5)';
expect(azure.massageMarkdown(input)).toMatchInlineSnapshot(
const prBody =
'\n---\n\n - [ ] <!-- rebase-check --> rebase\n<!--renovate-config-hash:-->' +
'plus also [a link](https://github.com/foo/bar/issues/5)';
expect(azure.massageMarkdown(prBody)).toMatchInlineSnapshot(
`"plus also [a link](https://github.com/foo/bar/issues/5)"`
);
Gabriel-Ladzaretti marked this conversation as resolved.
Show resolved Hide resolved
});
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/platform/azure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ export function massageMarkdown(input: string): string {
'rename PR to start with "rebase!"'
)
.replace(regEx(`\n---\n\n.*?<!-- rebase-check -->.*?\n`), '')
.replace(regEx(/<!--renovate-debug:.*?-->/), '');
.replace(regEx(/<!--renovate-(?:debug|config-hash):.*?-->/g), '');
}

/* istanbul ignore next */
Expand Down
9 changes: 4 additions & 5 deletions lib/modules/platform/bitbucket/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,11 +964,10 @@ describe('modules/platform/bitbucket/index', () => {

describe('massageMarkdown()', () => {
it('returns diff files', () => {
expect(
bitbucket.massageMarkdown(
'<details><summary>foo</summary>bar</details>text<details>'
)
).toMatchSnapshot();
const prBody =
'<details><summary>foo</summary>bar</details>text<details>' +
'\n---\n\n - [ ] <!-- rebase-check --> rebase\n<!--renovate-config-hash:-->';
expect(bitbucket.massageMarkdown(prBody)).toMatchSnapshot();
});
});

Expand Down
2 changes: 1 addition & 1 deletion lib/modules/platform/bitbucket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ export function massageMarkdown(input: string): string {
.replace(regEx(/<\/?details>/g), '')
.replace(regEx(`\n---\n\n.*?<!-- rebase-check -->.*?\n`), '')
.replace(regEx(/\]\(\.\.\/pull\//g), '](../../pull-requests/')
.replace(regEx(/<!--renovate-debug:.*?-->/), '');
.replace(regEx(/<!--renovate-(?:debug|config-hash):.*?-->/g), '');
}

export async function ensureIssue({
Expand Down
44 changes: 41 additions & 3 deletions lib/modules/platform/pr-body.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hasha from 'hasha';
import { getPrBodyStruct, hashBody } from './pr-body';

describe('modules/platform/pr-body', () => {
Expand Down Expand Up @@ -47,13 +48,50 @@ describe('modules/platform/pr-body', () => {
);
});

it('returns rebaseRequested flag', () => {
expect(getPrBodyStruct('- [x] <!-- rebase-check -->')).toEqual({
hash: '023952693e1e00a52a71b65d9b4804bca6ca9f215c20f6e029dbf420f322d541',
it('hashes an undefined body', () => {
// nullish operator branch coverage
const hash =
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
expect(hashBody(undefined)).toBe(hash);
});

it('returns rebaseRequested=true flag', () => {
const input = '- [x] <!-- rebase-check -->';
const hash = hashBody(input);
expect(getPrBodyStruct(input)).toEqual({
hash,
rebaseRequested: true,
});
});

it('returns rebaseRequested=false flag', () => {
const input = '- [ ] <!-- rebase-check -->';
const hash = hashBody(input);
expect(getPrBodyStruct(input)).toEqual({
hash,
rebaseRequested: false,
});
});

it('returns rebaseRequested=undefined flag', () => {
const input = '- <!-- rebase-check -->';
const hash = hashBody(input);
expect(getPrBodyStruct(input)).toEqual({
hash,
});
});

it('returns raw config hash', () => {
const config = '{}';
const rawConfigHash = hasha(config, { algorithm: 'sha256' });
const input = `<!--renovate-config-hash:${rawConfigHash}-->`;
const hash = hashBody(input);
expect(getPrBodyStruct(input)).toEqual({
hash,
rawConfigHash,
});
});

it('strips reviewable section', () => {
expect(getPrBodyStruct('foo<!-- Reviewable:start -->bar')).toEqual({
hash: hashBody('foo'),
Expand Down
28 changes: 25 additions & 3 deletions lib/modules/platform/pr-body.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import is from '@sindresorhus/is';
import hasha from 'hasha';
import { logger } from '../../logger';
import { stripEmojis } from '../../util/emoji';
Expand All @@ -9,6 +10,12 @@ export const prDebugDataRe = regEx(
/\n?<!--renovate-debug:(?<payload>.*?)-->\n?/
);

const renovateConfigHashRe = regEx(
/\n?<!--renovate-config-hash:(?<payload>.*?)-->\n?/
);

const prCheckboxRe = regEx(/- (?<checkbox>\[[\sx]]) <!-- rebase-check -->/);

viceice marked this conversation as resolved.
Show resolved Hide resolved
function noWhitespaceOrHeadings(input: string): string {
return input.replace(regEx(/\r?\n|\r|\s|#/g), '');
}
Expand All @@ -28,15 +35,24 @@ export function hashBody(body: string | undefined): string {
return result;
}

function isRebaseRequested(body: string | undefined): boolean {
return !!body?.includes(`- [x] <!-- rebase-check -->`);
function isRebaseRequested(body: string): boolean | undefined {
const match = prCheckboxRe.exec(body);
if (!match) {
return undefined;
}
return match.groups?.checkbox === '[x]';
viceice marked this conversation as resolved.
Show resolved Hide resolved
}

export function getRenovateDebugPayload(body: string): string | undefined {
const match = prDebugDataRe.exec(body);
return match?.groups?.payload;
}

export function getRenovateConfigHashPayload(body: string): string | undefined {
const match = renovateConfigHashRe.exec(body);
return match?.groups?.payload;
}

export function getPrBodyStruct(
input: string | undefined | null
): PrBodyStruct {
Expand All @@ -45,10 +61,16 @@ export function getPrBodyStruct(
const result: PrBodyStruct = { hash };

const rebaseRequested = isRebaseRequested(body);
if (rebaseRequested) {

if (!is.undefined(rebaseRequested)) {
result.rebaseRequested = rebaseRequested;
}

const rawConfigHash = getRenovateConfigHashPayload(body);
if (rawConfigHash) {
result.rawConfigHash = rawConfigHash;
}

const debugPayload = getRenovateDebugPayload(body);
if (debugPayload) {
try {
Expand Down
1 change: 1 addition & 0 deletions lib/modules/platform/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface PrDebugData {

export interface PrBodyStruct {
hash: string;
rawConfigHash?: string;
rebaseRequested?: boolean;
debugData?: PrDebugData;
}
Expand Down