Skip to content

Commit

Permalink
feat(onboarding): merge labels and addLabels and template onboard…
Browse files Browse the repository at this point in the history
…ing labels (#13433)


Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
secustor and viceice committed Jan 10, 2022
1 parent 0bd8a6e commit 1194cc7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
48 changes: 48 additions & 0 deletions lib/workers/pr/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,4 +701,52 @@ describe('workers/pr/index', () => {
});
});
});

describe('prepareLabels(config)', () => {
it('returns empty array if no labels are configured', () => {
const result = prWorker.prepareLabels({});
expect(result).toBeArrayOfSize(0);
});

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

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

it('merge labels and addLabels', () => {
const result = prWorker.prepareLabels({
labels: ['labelA', 'labelB'],
addLabels: ['labelC'],
});
expect(result).toBeArrayOfSize(3);
expect(result).toEqual(['labelA', 'labelB', 'labelC']);
});

it('deduplicate merged labels and addLabels', () => {
const result = prWorker.prepareLabels({
labels: ['labelA', 'labelB'],
addLabels: ['labelB', 'labelC'],
});
expect(result).toBeArrayOfSize(3);
expect(result).toEqual(['labelA', 'labelB', 'labelC']);
});

it('template labels', () => {
const result = prWorker.prepareLabels({
labels: ['datasource-{{{datasource}}}'],
datasource: 'npm',
});
expect(result).toBeArrayOfSize(1);
expect(result).toEqual(['datasource-npm']);
});
});
});
12 changes: 9 additions & 3 deletions lib/workers/pr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ function prepareAssigneesReviewers(
return filterUnavailableUsers(config, normalizedUsernames);
}

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)
);
}

export async function addAssigneesReviewers(
config: RenovateConfig,
pr: Pr
Expand Down Expand Up @@ -430,9 +438,7 @@ export async function ensurePr(
targetBranch: config.baseBranch,
prTitle,
prBody,
labels: [...new Set([...config.labels, ...config.addLabels])].map(
(label) => template.compile(label, config)
),
labels: prepareLabels(config),
platformOptions: getPlatformPrOptions(config),
draftPR: config.draftPR,
});
Expand Down
18 changes: 18 additions & 0 deletions lib/workers/repository/onboarding/pr/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ describe('workers/repository/onboarding/pr/index', () => {
expect(platform.createPr).toHaveBeenCalledTimes(1);
createPrBody = platform.createPr.mock.calls[0][0].prBody;
});

it('creates PR with labels', async () => {
await ensureOnboardingPr(
{
...config,
labels: ['label'],
addLabels: ['label', 'additional-label'],
},
packageFiles,
branches
);
expect(platform.createPr).toHaveBeenCalledTimes(1);
expect(platform.createPr.mock.calls[0][0].labels).toEqual([
'label',
'additional-label',
]);
});

it('returns if PR does not need updating', async () => {
platform.getBranchPr.mockResolvedValue(
partial<Pr>({
Expand Down
8 changes: 6 additions & 2 deletions lib/workers/repository/onboarding/pr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import type { PackageFile } from '../../../../manager/types';
import { platform } from '../../../../platform';
import { emojify } from '../../../../util/emoji';
import { deleteBranch, isBranchModified } from '../../../../util/git';
import { addAssigneesReviewers, getPlatformPrOptions } from '../../../pr';
import {
addAssigneesReviewers,
getPlatformPrOptions,
prepareLabels,
} from '../../../pr';
import type { BranchConfig } from '../../../types';
import { getBaseBranchDesc } from './base-branch';
import { getConfigDesc } from './config-description';
Expand Down Expand Up @@ -127,7 +131,7 @@ If you need any further assistance then you can also [request help here](${confi
return;
}
logger.debug('Creating onboarding PR');
const labels: string[] = config.addLabels ?? [];
const labels: string[] = prepareLabels(config);
try {
if (GlobalConfig.get('dryRun')) {
logger.info('DRY-RUN: Would create onboarding PR');
Expand Down

0 comments on commit 1194cc7

Please sign in to comment.