fix(platform): trim labels (#32015)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com>
Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
This commit is contained in:
RahulGautamSingh 2024-10-21 20:33:27 +05:30 committed by GitHub
parent 34a04c0b97
commit a94403589e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 70 additions and 0 deletions

View file

@ -79,6 +79,11 @@ With the above config:
- ESLint dependencies will have the label `linting` - ESLint dependencies will have the label `linting`
- All other dependencies will have the label `dependencies` - All other dependencies will have the label `dependencies`
<!-- prettier-ignore -->
!!! note
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 ## additionalBranchPrefix
By default, the value for this config option is an empty string. By default, the value for this config option is an empty string.
@ -2196,6 +2201,11 @@ Behavior details:
The `labels` array is non-mergeable, meaning if multiple `packageRules` match then Renovate uses the last value for `labels`. 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. If you want to add/combine labels, use the `addLabels` config option, which is mergeable.
<!-- prettier-ignore -->
!!! note
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 ## lockFileMaintenance
You can use `lockFileMaintenance` to refresh lock files to keep them up-to-date. You can use `lockFileMaintenance` to refresh lock files to keep them up-to-date.

View file

@ -923,6 +923,11 @@ export function maxBodyLength(): number {
} }
} }
// istanbul ignore next: no need to test
export function labelCharLimit(): number {
return 255;
}
// Branch // Branch
function matchesState(state: string, desiredState: string): boolean { function matchesState(state: string, desiredState: string): boolean {

View file

@ -280,6 +280,7 @@ export interface Platform {
expandGroupMembers?(reviewersOrAssignees: string[]): Promise<string[]>; expandGroupMembers?(reviewersOrAssignees: string[]): Promise<string[]>;
maxBodyLength(): number; maxBodyLength(): number;
labelCharLimit?(): number;
} }
export interface PlatformScm { export interface PlatformScm {

View file

@ -1,3 +1,4 @@
import { platform } from '../../../../../test/util';
import { import {
areLabelsModified, areLabelsModified,
getChangedLabels, getChangedLabels,
@ -82,6 +83,44 @@ describe('workers/repository/update/pr/labels', () => {
expect(result).toBeArrayOfSize(0); expect(result).toBeArrayOfSize(0);
expect(result).toEqual([]); expect(result).toEqual([]);
}); });
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
// 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', () => {
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
// 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', () => { describe('getChangedLabels', () => {

View file

@ -2,15 +2,30 @@ import is from '@sindresorhus/is';
import { dequal } from 'dequal'; import { dequal } from 'dequal';
import type { RenovateConfig } from '../../../../config/types'; import type { RenovateConfig } from '../../../../config/types';
import { logger } from '../../../../logger'; import { logger } from '../../../../logger';
import { platform } from '../../../../modules/platform';
import * as template from '../../../../util/template'; 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 {
const trimmed = label.trim();
if (trimmed.length <= limit) {
return trimmed;
}
return trimmed.slice(0, limit).trim();
}
export function prepareLabels(config: RenovateConfig): string[] { export function prepareLabels(config: RenovateConfig): string[] {
const labelCharLimit = platform.labelCharLimit?.() ?? 50;
const labels = config.labels ?? []; const labels = config.labels ?? [];
const addLabels = config.addLabels ?? []; const addLabels = config.addLabels ?? [];
return [...new Set([...labels, ...addLabels])] return [...new Set([...labels, ...addLabels])]
.filter(is.nonEmptyStringAndNotWhitespace) .filter(is.nonEmptyStringAndNotWhitespace)
.map((label) => template.compile(label, config)) .map((label) => template.compile(label, config))
.filter(is.nonEmptyStringAndNotWhitespace) .filter(is.nonEmptyStringAndNotWhitespace)
.map((label) => trimLabel(label, labelCharLimit))
.sort(); .sort();
} }