Skip to content

Commit

Permalink
feat(code/onboarding): infrastructure for onboarding PR rebase/retry …
Browse files Browse the repository at this point in the history
…checkbox (#17673)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
Gabriel-Ladzaretti and viceice committed Sep 20, 2022
1 parent 9596bfa commit 23e2508
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 17 deletions.
9 changes: 5 additions & 4 deletions lib/modules/platform/azure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,10 +1087,11 @@ 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(
`"plus also [a link](https://github.com/foo/bar/issues/5)"`
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)).toBe(
'plus also [a link](https://github.com/foo/bar/issues/5)'
);
});
});
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 @@ -746,7 +746,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 @@ -996,11 +996,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 @@ -488,7 +488,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 -->/);

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]';
}

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

0 comments on commit 23e2508

Please sign in to comment.