fix(platform): smart truncate pr body (#4527)

This commit is contained in:
Michael Kriese 2019-09-25 12:42:11 +02:00 committed by Rhys Arkins
parent bb56c364f6
commit 77a0291d4d
9 changed files with 237 additions and 36 deletions

View file

@ -6,6 +6,7 @@ import GitStorage from '../git/storage';
import { logger } from '../../logger';
import { PlatformConfig, RepoParams, RepoConfig } from '../common';
import { sanitize } from '../../util/sanitize';
import { smartTruncate } from '../utils/pr-body';
interface Config {
storage: GitStorage;
@ -487,7 +488,7 @@ export async function mergePr(pr: number) {
export function getPrBody(input: string) {
// Remove any HTML we use
return input
return smartTruncate(input, 4000)
.replace(new RegExp(`\n---\n\n.*?<!-- ${appSlug}-rebase -->.*?\n`), '')
.replace('<summary>', '**')
.replace('</summary>', '**')

View file

@ -8,6 +8,7 @@ import GitStorage from '../git/storage';
import { logger } from '../../logger';
import { PlatformConfig, RepoParams, RepoConfig } from '../common';
import { sanitize } from '../../util/sanitize';
import { smartTruncate } from '../utils/pr-body';
/*
* Version: 5.3 (EOL Date: 15 Aug 2019)
@ -952,12 +953,11 @@ export async function mergePr(prNo: number, branchName: string) {
export function getPrBody(input: string) {
logger.debug(`getPrBody(${input.split('\n')[0]})`);
// Remove any HTML we use
return input
return smartTruncate(input, 30000)
.replace(/<\/?summary>/g, '**')
.replace(/<\/?details>/g, '')
.replace(new RegExp(`\n---\n\n.*?<!-- .*?-rebase -->.*?(\n|$)`), '')
.replace(new RegExp('<!--.*?-->', 'g'), '')
.substring(0, 30000);
.replace(new RegExp('<!--.*?-->', 'g'), '');
}
export function getCommitMessages() {

View file

@ -10,6 +10,7 @@ import { appSlug } from '../../config/app-strings';
import * as comments from './comments';
import { PlatformConfig, RepoParams, RepoConfig } from '../common';
import { sanitize } from '../../util/sanitize';
import { smartTruncate } from '../utils/pr-body';
let config: utils.Config = {} as any;
@ -694,12 +695,11 @@ export async function mergePr(prNo: number, branchName: string) {
export function getPrBody(input: string) {
// Remove any HTML we use
return input
return smartTruncate(input, 50000)
.replace(/<\/?summary>/g, '**')
.replace(/<\/?details>/g, '')
.replace(new RegExp(`\n---\n\n.*?<!-- ${appSlug}-rebase -->.*?\n`), '')
.replace(/\]\(\.\.\/pull\//g, '](../../pull-requests/')
.substring(0, 50000);
.replace(/\]\(\.\.\/pull\//g, '](../../pull-requests/');
}
// Return the commit SHA for a branch

View file

@ -16,6 +16,7 @@ import {
urls,
} from '../../config/app-strings';
import { sanitize } from '../../util/sanitize';
import { smartTruncate } from '../utils/pr-body';
const defaultConfigFile = configFileNames[0];
@ -1711,39 +1712,16 @@ export async function mergePr(prNo: number, branchName: string) {
return true;
}
// istanbul ignore next
function smartTruncate(input: string) {
if (input.length < 60000) {
return input;
}
const releaseNotesMatch = input.match(
new RegExp(`### Release Notes.*### ${appName} configuration`, 'ms')
);
// istanbul ignore if
if (releaseNotesMatch) {
const divider = `</details>\n\n---\n\n### ${appName} configuration`;
const [releaseNotes] = releaseNotesMatch;
const nonReleaseNotesLength =
input.length - releaseNotes.length - divider.length;
const availableLength = 60000 - nonReleaseNotesLength;
return input.replace(
releaseNotes,
releaseNotes.slice(0, availableLength) + divider
);
}
return input.substring(0, 60000);
}
export function getPrBody(input: string) {
if (config.isGhe) {
return smartTruncate(input);
return smartTruncate(input, 60000);
}
const massagedInput = input
// to be safe, replace all github.com links with renovatebot redirector
.replace(/href="https?:\/\/github.com\//g, 'href="https://togithub.com/')
.replace(/]\(https:\/\/github\.com\//g, '](https://togithub.com/')
.replace(/]: https:\/\/github\.com\//g, ']: https://togithub.com/');
return smartTruncate(massagedInput);
return smartTruncate(massagedInput, 60000);
}
export async function getVulnerabilityAlerts() {

View file

@ -8,6 +8,7 @@ import { PlatformConfig, RepoParams, RepoConfig } from '../common';
import { configFileNames } from '../../config/app-strings';
import { logger } from '../../logger';
import { sanitize } from '../../util/sanitize';
import { smartTruncate } from '../utils/pr-body';
const defaultConfigFile = configFileNames[0];
let config: {
@ -830,10 +831,13 @@ export async function mergePr(iid: number) {
}
export function getPrBody(input: string) {
return input
return smartTruncate(
input
.replace(/Pull Request/g, 'Merge Request')
.replace(/PR/g, 'MR')
.replace(/\]\(\.\.\/pull\//g, '](../merge_requests/');
.replace(/\]\(\.\.\/pull\//g, '](../merge_requests/'),
1000000
);
}
export function getCommitMessages() {

View file

@ -0,0 +1,23 @@
import { appName } from '../../config/app-strings';
export function smartTruncate(input: string, len: number): string {
if (input.length < len) {
return input;
}
const releaseNotesMatch = input.match(
new RegExp(`### Release Notes.*### ${appName} configuration`, 'ms')
);
if (releaseNotesMatch) {
const divider = `</details>\n\n---\n\n### ${appName} configuration`;
const [releaseNotes] = releaseNotesMatch;
const nonReleaseNotesLength =
input.length - releaseNotes.length - divider.length;
const availableLength = len - nonReleaseNotesLength;
if (availableLength <= 0) return input.substring(0, len);
return input.replace(
releaseNotes,
releaseNotes.slice(0, availableLength) + divider
);
}
return input.substring(0, len);
}

View file

@ -0,0 +1,60 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`platform/utils/pr-body .smartTruncate truncates to 300 not smart 1`] = `
"This PR contains the following updates:
| Package | Type | Update | Change |
|---|---|---|---|
| [renovate/renovate](https://togithub.com/renovatebot/renovate) | final | minor | \`19.46.0-slim\` -> \`19.47.0-slim\` |
---
### Release Notes
<details>
<summary>renovatebot/renovate</summary>
### [\`v19."
`;
exports[`platform/utils/pr-body .smartTruncate truncates to 1000 1`] = `
"This PR contains the following updates:
| Package | Type | Update | Change |
|---|---|---|---|
| [renovate/renovate](https://togithub.com/renovatebot/renovate) | final | minor | \`19.46.0-slim\` -> \`19.47.0-slim\` |
---
### Release Notes
<details>
<summary>renovatebot/renovate</summary>
### [\`v19.47.0\`](https://togithub.com/renovatebot/renovate/releases/19.47.0)
##### Features
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togi</details>
---
### Renovate configuration
:date: **Schedule**: At any time (no schedule defined).
:vertical_traffic_light: **Automerge**: Enabled.
:recycle: **Rebasing**: Whenever PR becomes conflicted, or if you modify the PR title to begin with \\"\`rebase!\`\\".
:no_bell: **Ignore**: Close this PR and you won't be reminded about this update again.
---
- [ ] <!-- renovate-rebase -->If you want to rebase/retry this PR, check this box
---
This PR has been generated by [Renovate Bot](https://togithub.com/marketplace/renovate). View repository job log [here](https://renovatebot.com/dashboard#VisualOn/docker-images).
"
`;

View file

@ -0,0 +1,101 @@
This PR contains the following updates:
| Package | Type | Update | Change |
|---|---|---|---|
| [renovate/renovate](https://togithub.com/renovatebot/renovate) | final | minor | `19.46.0-slim` -> `19.47.0-slim` |
---
### Release Notes
<details>
<summary>renovatebot/renovate</summary>
### [`v19.47.0`](https://togithub.com/renovatebot/renovate/releases/19.47.0)
##### Features
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
- **docker:** AWS ECR authentication support ([#&#8203;4497](https://togithub.com/renovatebot/renovate/issues/4497)) ([acb114a](https://togithub.com/renovatebot/renovate/commit/acb114a))
</details>
---
### Renovate configuration
:date: **Schedule**: At any time (no schedule defined).
:vertical_traffic_light: **Automerge**: Enabled.
:recycle: **Rebasing**: Whenever PR becomes conflicted, or if you modify the PR title to begin with "`rebase!`".
:no_bell: **Ignore**: Close this PR and you won't be reminded about this update again.
---
- [ ] <!-- renovate-rebase -->If you want to rebase/retry this PR, check this box
---
This PR has been generated by [Renovate Bot](https://togithub.com/marketplace/renovate). View repository job log [here](https://renovatebot.com/dashboard#VisualOn/docker-images).

View file

@ -0,0 +1,34 @@
import fs from 'fs-extra';
import { smartTruncate } from '../../../lib/platform/utils/pr-body';
describe('platform/utils/pr-body', () => {
let prBody: string;
beforeAll(async () => {
prBody = await fs.readFile(
'test/platform/utils/_fixtures/pr-body.txt',
'utf8'
);
});
describe('.smartTruncate', () => {
it('truncates to 1000', () => {
const body = smartTruncate(prBody, 1000);
expect(body).toMatchSnapshot();
expect(body.length < prBody.length).toEqual(true);
});
it('truncates to 300 not smart', () => {
const body = smartTruncate(prBody, 300);
expect(body).toMatchSnapshot();
expect(body.length).toEqual(300);
});
it('truncates to 10', () => {
const body = smartTruncate('Lorem ipsum dolor sit amet', 10);
expect(body).toEqual('Lorem ipsu');
});
it('does not truncate', () => {
expect(smartTruncate(prBody, 60000)).toEqual(prBody);
});
});
});