From 1bccc3dd51adfdba076e340029e419a60c6e6580 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Sat, 19 Oct 2024 00:33:29 +0530 Subject: [PATCH 01/11] trim labels --- lib/modules/platform/gitea/index.ts | 1 + lib/modules/platform/github/index.ts | 15 ++++--- lib/modules/platform/gitlab/index.ts | 1 + lib/modules/platform/types.ts | 1 + .../repository/update/pr/labels.spec.ts | 45 +++++++++++++++++++ lib/workers/repository/update/pr/labels.ts | 14 ++++++ 6 files changed, 72 insertions(+), 5 deletions(-) diff --git a/lib/modules/platform/gitea/index.ts b/lib/modules/platform/gitea/index.ts index d67cf0ceea290e..fdeab4f9408ecd 100644 --- a/lib/modules/platform/gitea/index.ts +++ b/lib/modules/platform/gitea/index.ts @@ -74,6 +74,7 @@ interface GiteaRepoConfig { } export const id = 'gitea'; +export const labelCharLimit = 50; const defaults = { hostType: 'gitea', diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index 23a8210574053a..36db9c0625b782 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -99,6 +99,7 @@ let config: LocalRepoConfig; let platformConfig: PlatformConfig; const GitHubMaxPrBodyLen = 60000; +export const labelCharLimit = 50; export function resetConfigs(): void { config = {} as never; @@ -1489,11 +1490,15 @@ async function addLabels( labels: string[] | null | undefined, ): Promise { logger.debug(`Adding labels '${labels?.join(', ')}' to #${issueNo}`); - const repository = config.parentRepo ?? config.repository; - if (is.array(labels) && labels.length) { - await githubApi.postJson(`repos/${repository}/issues/${issueNo}/labels`, { - body: labels, - }); + try { + const repository = config.parentRepo ?? config.repository; + if (is.array(labels) && labels.length) { + await githubApi.postJson(`repos/${repository}/issues/${issueNo}/labels`, { + body: labels, + }); + } + } catch (err) { + logger.debug({ err }, 'Error while adding labels, skipping'); } } diff --git a/lib/modules/platform/gitlab/index.ts b/lib/modules/platform/gitlab/index.ts index de544f48cce3e0..4fe8db09d8e327 100644 --- a/lib/modules/platform/gitlab/index.ts +++ b/lib/modules/platform/gitlab/index.ts @@ -92,6 +92,7 @@ const defaults = { }; export const id = 'gitlab'; +export const labelCharLimit = 255; const DRAFT_PREFIX = 'Draft: '; const DRAFT_PREFIX_DEPRECATED = 'WIP: '; diff --git a/lib/modules/platform/types.ts b/lib/modules/platform/types.ts index 752d98d779b274..971b3d3dafdbaa 100644 --- a/lib/modules/platform/types.ts +++ b/lib/modules/platform/types.ts @@ -280,6 +280,7 @@ export interface Platform { expandGroupMembers?(reviewersOrAssignees: string[]): Promise; maxBodyLength(): number; + labelCharLimit?: number; } export interface PlatformScm { diff --git a/lib/workers/repository/update/pr/labels.spec.ts b/lib/workers/repository/update/pr/labels.spec.ts index 7dca6fbc4517ee..709918abaaef56 100644 --- a/lib/workers/repository/update/pr/labels.spec.ts +++ b/lib/workers/repository/update/pr/labels.spec.ts @@ -1,3 +1,5 @@ +import { mocked } from '../../../../../test/util'; +import * as _platform from '../../../../modules/platform'; import { areLabelsModified, getChangedLabels, @@ -5,7 +7,15 @@ import { shouldUpdateLabels, } from './labels'; +jest.mock('../../../../modules/platform'); + +const platform = mocked(_platform); + describe('workers/repository/update/pr/labels', () => { + beforeEach(() => { + platform.platform.labelCharLimit = 50; + }); + describe('prepareLabels(config)', () => { it('returns empty array if no labels are configured', () => { const result = prepareLabels({}); @@ -82,6 +92,41 @@ describe('workers/repository/update/pr/labels', () => { expect(result).toBeArrayOfSize(0); expect(result).toEqual([]); }); + + describe('trim labels that exceed max char limit', () => { + const labels = [ + 'All', + 'The quick brown fox jumped over the lazy sleeping dog', // len: 51 + // len: 256 + 'Torem ipsum dolor sit amet, consectetur adipiscing elit. Sed fringilla erat eu lectus gravida varius. Maecenas suscipit risus nec erat mollis tempus. Vestibulum cursus urna et faucibus tempor. Nam eleifend libero in enim sodales, eu placerat enim dice rep!', + ]; + + it('github', () => { + expect(prepareLabels({ labels })).toEqual([ + 'All', + 'The quick brown fox jumped over the lazy sleeping', // len: 50 + 'Torem ipsum dolor sit amet, consectetur adipiscing', // len: 50 + ]); + }); + + it('gitlab', () => { + platform.platform.labelCharLimit = 255; + expect(prepareLabels({ labels })).toEqual([ + 'All', + 'The quick brown fox jumped over the lazy sleeping dog', // len: 51 + // len: 255 + 'Torem ipsum dolor sit amet, consectetur adipiscing elit. Sed fringilla erat eu lectus gravida varius. Maecenas suscipit risus nec erat mollis tempus. Vestibulum cursus urna et faucibus tempor. Nam eleifend libero in enim sodales, eu placerat enim dice rep', + ]); + }); + + it('gitea', () => { + expect(prepareLabels({ labels })).toEqual([ + 'All', + 'The quick brown fox jumped over the lazy sleeping', // len: 50 + 'Torem ipsum dolor sit amet, consectetur adipiscing', // len: 50 + ]); + }); + }); }); describe('getChangedLabels', () => { diff --git a/lib/workers/repository/update/pr/labels.ts b/lib/workers/repository/update/pr/labels.ts index 3c41e47b19740f..565e9ed6bad46e 100644 --- a/lib/workers/repository/update/pr/labels.ts +++ b/lib/workers/repository/update/pr/labels.ts @@ -2,15 +2,29 @@ import is from '@sindresorhus/is'; import { dequal } from 'dequal'; import type { RenovateConfig } from '../../../../config/types'; import { logger } from '../../../../logger'; +import { platform } from '../../../../modules/platform'; import * as template from '../../../../util/template'; +/** + * Filter labels that exceed maximum char limit based on platform limits + */ +function trimLabel(label: string, limit: number): string { + if (label.length > limit) { + return label.slice(0, limit).trim(); + } + + return label.trim(); +} + export function prepareLabels(config: RenovateConfig): string[] { + const labelCharLimit = platform.labelCharLimit ?? 50; const labels = config.labels ?? []; const addLabels = config.addLabels ?? []; return [...new Set([...labels, ...addLabels])] .filter(is.nonEmptyStringAndNotWhitespace) .map((label) => template.compile(label, config)) .filter(is.nonEmptyStringAndNotWhitespace) + .map((label) => trimLabel(label, labelCharLimit)) .sort(); } From a13ea038d26f84d7a829ccd39ae4fdcdfe4eb16b Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Sat, 19 Oct 2024 01:00:45 +0530 Subject: [PATCH 02/11] docs: add notes --- docs/usage/configuration-options.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index c3049b6f28fffa..52916f7886dd63 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -79,6 +79,11 @@ With the above config: - ESLint dependencies will have the label `linting` - All other dependencies will have the label `dependencies` + +!!! note + Make sure your labels do not exceed the maximum character limit specified by the platform you are on. + For example: Github limit the character length for labels at 50. + ## additionalBranchPrefix By default, the value for this config option is an empty string. @@ -2200,6 +2205,11 @@ Behavior details: The `labels` array is non-mergeable, meaning if multiple `packageRules` match then Renovate uses the last value for `labels`. If you want to add/combine labels, use the `addLabels` config option, which is mergeable. + +!!! note + Make sure your labels do not exceed the maximum character limit specified by the platform you are on. + For example: Github limit the character length for labels at 50. + ## lockFileMaintenance You can use `lockFileMaintenance` to refresh lock files to keep them up-to-date. From 0e9fc9cfce534a4ebde280e21c8d5c3896f37287 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Sat, 19 Oct 2024 01:03:31 +0530 Subject: [PATCH 03/11] refactor: remove extra changes --- lib/modules/platform/github/index.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index 36db9c0625b782..465e2c0c591387 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -1490,15 +1490,11 @@ async function addLabels( labels: string[] | null | undefined, ): Promise { logger.debug(`Adding labels '${labels?.join(', ')}' to #${issueNo}`); - try { - const repository = config.parentRepo ?? config.repository; - if (is.array(labels) && labels.length) { - await githubApi.postJson(`repos/${repository}/issues/${issueNo}/labels`, { - body: labels, - }); - } - } catch (err) { - logger.debug({ err }, 'Error while adding labels, skipping'); + const repository = config.parentRepo ?? config.repository; + if (is.array(labels) && labels.length) { + await githubApi.postJson(`repos/${repository}/issues/${issueNo}/labels`, { + body: labels, + }); } } From d28aeb53d00424a8428215fe1fe49bd2fe525ff6 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Sat, 19 Oct 2024 01:30:50 +0530 Subject: [PATCH 04/11] fix coverage --- lib/workers/repository/update/pr/labels.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/workers/repository/update/pr/labels.spec.ts b/lib/workers/repository/update/pr/labels.spec.ts index 709918abaaef56..d5038d13058f29 100644 --- a/lib/workers/repository/update/pr/labels.spec.ts +++ b/lib/workers/repository/update/pr/labels.spec.ts @@ -102,6 +102,7 @@ describe('workers/repository/update/pr/labels', () => { ]; it('github', () => { + platform.platform.labelCharLimit = undefined; // coverage expect(prepareLabels({ labels })).toEqual([ 'All', 'The quick brown fox jumped over the lazy sleeping', // len: 50 From 90ffe2f4abc3fae5ede5b41324b9ad27b2f7d7ba Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Sat, 19 Oct 2024 19:57:06 +0530 Subject: [PATCH 05/11] Update lib/workers/repository/update/pr/labels.ts Co-authored-by: Rhys Arkins --- lib/workers/repository/update/pr/labels.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/workers/repository/update/pr/labels.ts b/lib/workers/repository/update/pr/labels.ts index 565e9ed6bad46e..c2651116e97f5b 100644 --- a/lib/workers/repository/update/pr/labels.ts +++ b/lib/workers/repository/update/pr/labels.ts @@ -10,7 +10,7 @@ import * as template from '../../../../util/template'; */ function trimLabel(label: string, limit: number): string { if (label.length > limit) { - return label.slice(0, limit).trim(); + return label.trim().slice(0, limit).trim(); } return label.trim(); From 92d0254a013a09e7f99fd00dfb5ee34bf0f59b7f Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Sat, 19 Oct 2024 20:04:12 +0530 Subject: [PATCH 06/11] apply suggestions --- docs/usage/configuration-options.md | 4 ++-- lib/modules/platform/gitea/index.ts | 1 - lib/modules/platform/github/index.ts | 1 - lib/workers/repository/update/pr/labels.spec.ts | 13 ++++--------- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 52916f7886dd63..a0628db554be84 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -82,7 +82,7 @@ With the above config: !!! note Make sure your labels do not exceed the maximum character limit specified by the platform you are on. - For example: Github limit the character length for labels at 50. + We generally truncate labels at 50 characters except for Gitlab which have a 255 character limit ## additionalBranchPrefix @@ -2208,7 +2208,7 @@ If you want to add/combine labels, use the `addLabels` config option, which is m !!! note Make sure your labels do not exceed the maximum character limit specified by the platform you are on. - For example: Github limit the character length for labels at 50. + We generally truncate labels at 50 characters except for Gitlab which have a 255 character limit ## lockFileMaintenance diff --git a/lib/modules/platform/gitea/index.ts b/lib/modules/platform/gitea/index.ts index fdeab4f9408ecd..d67cf0ceea290e 100644 --- a/lib/modules/platform/gitea/index.ts +++ b/lib/modules/platform/gitea/index.ts @@ -74,7 +74,6 @@ interface GiteaRepoConfig { } export const id = 'gitea'; -export const labelCharLimit = 50; const defaults = { hostType: 'gitea', diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index 465e2c0c591387..23a8210574053a 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -99,7 +99,6 @@ let config: LocalRepoConfig; let platformConfig: PlatformConfig; const GitHubMaxPrBodyLen = 60000; -export const labelCharLimit = 50; export function resetConfigs(): void { config = {} as never; diff --git a/lib/workers/repository/update/pr/labels.spec.ts b/lib/workers/repository/update/pr/labels.spec.ts index d5038d13058f29..63702dacaca9c0 100644 --- a/lib/workers/repository/update/pr/labels.spec.ts +++ b/lib/workers/repository/update/pr/labels.spec.ts @@ -1,5 +1,4 @@ -import { mocked } from '../../../../../test/util'; -import * as _platform from '../../../../modules/platform'; +import { platform } from '../../../../../test/util'; import { areLabelsModified, getChangedLabels, @@ -7,13 +6,9 @@ import { shouldUpdateLabels, } from './labels'; -jest.mock('../../../../modules/platform'); - -const platform = mocked(_platform); - describe('workers/repository/update/pr/labels', () => { beforeEach(() => { - platform.platform.labelCharLimit = 50; + platform.labelCharLimit = 50; }); describe('prepareLabels(config)', () => { @@ -102,7 +97,7 @@ describe('workers/repository/update/pr/labels', () => { ]; it('github', () => { - platform.platform.labelCharLimit = undefined; // coverage + platform.labelCharLimit = undefined as never; // coverage expect(prepareLabels({ labels })).toEqual([ 'All', 'The quick brown fox jumped over the lazy sleeping', // len: 50 @@ -111,7 +106,7 @@ describe('workers/repository/update/pr/labels', () => { }); it('gitlab', () => { - platform.platform.labelCharLimit = 255; + platform.labelCharLimit = 255; expect(prepareLabels({ labels })).toEqual([ 'All', 'The quick brown fox jumped over the lazy sleeping dog', // len: 51 From bdb9e154dcd2f2bc197a0384ac729d1cf36c9986 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Mon, 21 Oct 2024 10:34:45 +0200 Subject: [PATCH 07/11] Update lib/workers/repository/update/pr/labels.ts Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> --- lib/workers/repository/update/pr/labels.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/workers/repository/update/pr/labels.ts b/lib/workers/repository/update/pr/labels.ts index c2651116e97f5b..268b257606a5b0 100644 --- a/lib/workers/repository/update/pr/labels.ts +++ b/lib/workers/repository/update/pr/labels.ts @@ -6,7 +6,7 @@ import { platform } from '../../../../modules/platform'; import * as template from '../../../../util/template'; /** - * Filter labels that exceed maximum char limit based on platform limits + * Filter labels that go over the maximum char limit, based on platform limits. */ function trimLabel(label: string, limit: number): string { if (label.length > limit) { From 141e33c0bb9bfe280bc44cc9882fe420896900cf Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Mon, 21 Oct 2024 10:35:29 +0200 Subject: [PATCH 08/11] Apply suggestions from code review Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> --- docs/usage/configuration-options.md | 8 ++++---- lib/workers/repository/update/pr/labels.spec.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index a0628db554be84..b5db090e3d6256 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -81,8 +81,8 @@ With the above config: !!! note - Make sure your labels do not exceed the maximum character limit specified by the platform you are on. - We generally truncate labels at 50 characters except for Gitlab which have a 255 character limit + Keep your labels within the maximum character limit for your Git hosting platform. + Renovate usually truncates labels to 50 characters, except for GitLab, which has a 255 character limit. ## additionalBranchPrefix @@ -2207,8 +2207,8 @@ If you want to add/combine labels, use the `addLabels` config option, which is m !!! note - Make sure your labels do not exceed the maximum character limit specified by the platform you are on. - We generally truncate labels at 50 characters except for Gitlab which have a 255 character limit + Keep your labels within the maximum character limit for your Git hosting platform. + Renovate usually truncates labels to 50 characters, except for GitLab, which has a 255 character limit. ## lockFileMaintenance diff --git a/lib/workers/repository/update/pr/labels.spec.ts b/lib/workers/repository/update/pr/labels.spec.ts index 63702dacaca9c0..eb84761229735f 100644 --- a/lib/workers/repository/update/pr/labels.spec.ts +++ b/lib/workers/repository/update/pr/labels.spec.ts @@ -88,7 +88,7 @@ describe('workers/repository/update/pr/labels', () => { expect(result).toEqual([]); }); - describe('trim labels that exceed max char limit', () => { + describe('trim labels that go over the max char limit', () => { const labels = [ 'All', 'The quick brown fox jumped over the lazy sleeping dog', // len: 51 From a2355c3ca686453ae5db66587cdba9a3fc75ea73 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Mon, 21 Oct 2024 17:23:03 +0530 Subject: [PATCH 09/11] Update lib/workers/repository/update/pr/labels.ts Co-authored-by: Sebastian Poxhofer --- lib/workers/repository/update/pr/labels.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/workers/repository/update/pr/labels.ts b/lib/workers/repository/update/pr/labels.ts index 268b257606a5b0..51b27831c4ffb0 100644 --- a/lib/workers/repository/update/pr/labels.ts +++ b/lib/workers/repository/update/pr/labels.ts @@ -9,11 +9,12 @@ import * as template from '../../../../util/template'; * Filter labels that go over the maximum char limit, based on platform limits. */ function trimLabel(label: string, limit: number): string { - if (label.length > limit) { - return label.trim().slice(0, limit).trim(); + const trimmed = label.trim(); + if (trimmed.length <= limit) { + return trimmed; } - return label.trim(); + return trimmed.slice(0, limit).trim(); } export function prepareLabels(config: RenovateConfig): string[] { From 2bdfc01fcff9873ec953c6cbe686ad67d84234f8 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Mon, 21 Oct 2024 18:03:18 +0530 Subject: [PATCH 10/11] fix tests --- lib/modules/platform/gitlab/index.ts | 5 ++++- lib/modules/platform/types.ts | 2 +- lib/workers/repository/update/pr/labels.spec.ts | 10 ++++------ lib/workers/repository/update/pr/labels.ts | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/modules/platform/gitlab/index.ts b/lib/modules/platform/gitlab/index.ts index 4fe8db09d8e327..ddf4ad3bfd1b64 100644 --- a/lib/modules/platform/gitlab/index.ts +++ b/lib/modules/platform/gitlab/index.ts @@ -92,7 +92,6 @@ const defaults = { }; export const id = 'gitlab'; -export const labelCharLimit = 255; const DRAFT_PREFIX = 'Draft: '; const DRAFT_PREFIX_DEPRECATED = 'WIP: '; @@ -924,6 +923,10 @@ export function maxBodyLength(): number { } } +export function labelCharLimit(): number { + return 255; +} + // Branch function matchesState(state: string, desiredState: string): boolean { diff --git a/lib/modules/platform/types.ts b/lib/modules/platform/types.ts index 971b3d3dafdbaa..91575d0981572c 100644 --- a/lib/modules/platform/types.ts +++ b/lib/modules/platform/types.ts @@ -280,7 +280,7 @@ export interface Platform { expandGroupMembers?(reviewersOrAssignees: string[]): Promise; maxBodyLength(): number; - labelCharLimit?: number; + labelCharLimit?(): number; } export interface PlatformScm { diff --git a/lib/workers/repository/update/pr/labels.spec.ts b/lib/workers/repository/update/pr/labels.spec.ts index eb84761229735f..480405997fbe90 100644 --- a/lib/workers/repository/update/pr/labels.spec.ts +++ b/lib/workers/repository/update/pr/labels.spec.ts @@ -7,10 +7,6 @@ import { } from './labels'; describe('workers/repository/update/pr/labels', () => { - beforeEach(() => { - platform.labelCharLimit = 50; - }); - describe('prepareLabels(config)', () => { it('returns empty array if no labels are configured', () => { const result = prepareLabels({}); @@ -97,7 +93,6 @@ describe('workers/repository/update/pr/labels', () => { ]; it('github', () => { - platform.labelCharLimit = undefined as never; // coverage expect(prepareLabels({ labels })).toEqual([ 'All', 'The quick brown fox jumped over the lazy sleeping', // len: 50 @@ -106,7 +101,10 @@ describe('workers/repository/update/pr/labels', () => { }); it('gitlab', () => { - platform.labelCharLimit = 255; + jest.spyOn(platform, 'labelCharLimit').mockImplementationOnce(() => { + return 255; + }); + // platform.labelCharLimit.mockReturnValueOnce(255); expect(prepareLabels({ labels })).toEqual([ 'All', 'The quick brown fox jumped over the lazy sleeping dog', // len: 51 diff --git a/lib/workers/repository/update/pr/labels.ts b/lib/workers/repository/update/pr/labels.ts index 51b27831c4ffb0..36b5d912cebab9 100644 --- a/lib/workers/repository/update/pr/labels.ts +++ b/lib/workers/repository/update/pr/labels.ts @@ -18,7 +18,7 @@ function trimLabel(label: string, limit: number): string { } export function prepareLabels(config: RenovateConfig): string[] { - const labelCharLimit = platform.labelCharLimit ?? 50; + const labelCharLimit = platform.labelCharLimit?.() ?? 50; const labels = config.labels ?? []; const addLabels = config.addLabels ?? []; return [...new Set([...labels, ...addLabels])] From 870713e42fed51fbff2e057ca4ce53ccb326582d Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Mon, 21 Oct 2024 18:11:58 +0530 Subject: [PATCH 11/11] fix coverage --- lib/modules/platform/gitlab/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/modules/platform/gitlab/index.ts b/lib/modules/platform/gitlab/index.ts index ddf4ad3bfd1b64..c8ba81c0dfc894 100644 --- a/lib/modules/platform/gitlab/index.ts +++ b/lib/modules/platform/gitlab/index.ts @@ -923,6 +923,7 @@ export function maxBodyLength(): number { } } +// istanbul ignore next: no need to test export function labelCharLimit(): number { return 255; }