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

fix: ignore empty labels during label merge and templating #14322

Merged
merged 8 commits into from
Feb 21, 2022
6 changes: 6 additions & 0 deletions lib/util/string.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import is from '@sindresorhus/is';
import { logger } from '../logger';

// Return true if the match string is found at index in content
Expand All @@ -23,3 +24,8 @@ export function replaceAt(
content.substr(index + oldString.length)
);
}

// Return true if the input is non-empty and not whitespace string
export function nonEmptyStringAndNotWhitespace(input: unknown): boolean {
return is.nonEmptyString(input) && !is.emptyStringOrWhitespace(input);
}
6 changes: 3 additions & 3 deletions lib/util/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ function getFilteredObject(input: CompileInput): FilteredObject {
for (const field of allAllowed) {
const value = obj[field];
if (is.array(value)) {
res[field] = value.map((element) =>
getFilteredObject(element as CompileInput)
);
res[field] = value
.filter(is.plainObject)
.map((element) => getFilteredObject(element as CompileInput));
} else if (is.plainObject(value)) {
res[field] = getFilteredObject(value);
} else if (!is.undefined(value)) {
Expand Down
29 changes: 29 additions & 0 deletions lib/workers/pr/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,26 @@ describe('workers/pr/index', () => {
expect(result).toEqual(['labelA', 'labelB', 'labelC']);
});

it('empty labels ignored', () => {
const result = prWorker.prepareLabels({
labels: ['labelA', ''],
addLabels: [' ', 'labelB'],
});
expect(result).toBeArrayOfSize(2);
expect(result).toEqual(['labelA', 'labelB']);
});

it('null labels ignored', () => {
const result = prWorker.prepareLabels({
labels: ['labelA', null],
// an empty space between two commas in an array is categorized as a null value
// eslint-disable-next-line no-sparse-arrays
addLabels: ['labelB', '', undefined, , ,],
});
expect(result).toBeArrayOfSize(2);
expect(result).toEqual(['labelA', 'labelB']);
});

it('template labels', () => {
const result = prWorker.prepareLabels({
labels: ['datasource-{{{datasource}}}'],
Expand All @@ -815,5 +835,14 @@ describe('workers/pr/index', () => {
expect(result).toBeArrayOfSize(1);
expect(result).toEqual(['datasource-npm']);
});

it('template labels with empty datasource', () => {
const result = prWorker.prepareLabels({
labels: ['{{{datasource}}}', ' {{{datasource}}} '],
datasource: null,
});
expect(result).toBeArrayOfSize(0);
expect(result).toEqual([]);
});
});
});
8 changes: 5 additions & 3 deletions lib/workers/pr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { sampleSize } from '../../util';
import { stripEmojis } from '../../util/emoji';
import { deleteBranch, getBranchLastCommitTime } from '../../util/git';
import { regEx } from '../../util/regex';
import { nonEmptyStringAndNotWhitespace } from '../../util/string';
import * as template from '../../util/template';
import { resolveBranchStatus } from '../branch/status-checks';
import { Limit, incLimitedValue, isLimitReached } from '../global/limits';
Expand Down Expand Up @@ -56,9 +57,10 @@ function prepareAssigneesReviewers(
export function prepareLabels(config: RenovateConfig): string[] {
const labels = config.labels ?? [];
const addLabels = config.addLabels ?? [];
return [...new Set([...labels, ...addLabels])].map((label) =>
template.compile(label, config)
);
return [...new Set([...labels, ...addLabels])]
.filter(nonEmptyStringAndNotWhitespace)
.map((label) => template.compile(label, config))
.filter(nonEmptyStringAndNotWhitespace);
}

export async function addAssigneesReviewers(
Expand Down