/
branch-name.ts
88 lines (76 loc) · 2.93 KB
/
branch-name.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { clean as cleanGitRef } from 'clean-git-ref';
import hasha from 'hasha';
import slugify from 'slugify';
import type { RenovateConfig } from '../../../config/types';
import { logger } from '../../../logger';
import * as template from '../../../util/template';
const MIN_HASH_LENGTH = 6;
/**
* Clean git branch name
*
* Remove what clean-git-ref fails to:
* - leading dot/leading dot after slash
* - trailing dot
* - whitespace
*/
function cleanBranchName(branchName: string): string {
return cleanGitRef(branchName)
.replace(/^\.|\.$/, '') // leading or trailing dot
.replace(/\/\./g, '/') // leading dot after slash
.replace(/\s/g, ''); // whitespace
}
/* eslint-disable no-param-reassign */
export function generateBranchName(update: RenovateConfig): void {
// Check whether to use a group name
if (update.groupName) {
logger.debug('Using group branchName template');
logger.debug(
`Dependency ${update.depName} is part of group ${update.groupName}`
);
update.groupSlug = slugify(update.groupSlug || update.groupName, {
lower: true,
});
if (update.updateType === 'major' && update.separateMajorMinor) {
if (update.separateMultipleMajor) {
const newMajor = String(update.newMajor);
update.groupSlug = `major-${newMajor}-${update.groupSlug}`;
} else {
update.groupSlug = `major-${update.groupSlug}`;
}
}
if (update.updateType === 'patch' && update.separateMinorPatch) {
update.groupSlug = `patch-${update.groupSlug}`;
}
update.branchTopic = update.group.branchTopic || update.branchTopic;
update.branchName = update.group.branchName || update.branchName;
}
if (update.hashedBranchLength) {
let hashLength = update.hashedBranchLength - update.branchPrefix.length;
if (hashLength <= MIN_HASH_LENGTH) {
logger.warn(
`\`hashedBranchLength\` must allow for at least ${MIN_HASH_LENGTH} characters hashing in addition to \`branchPrefix\`. Using ${MIN_HASH_LENGTH} character hash instead.`
);
hashLength = MIN_HASH_LENGTH;
}
const additionalBranchPrefix = template.compile(
String(update.additionalBranchPrefix || ''),
update
);
const branchTopic = template.compile(
String(update.branchTopic || ''),
update
);
let hashInput = additionalBranchPrefix + branchTopic;
// Compile extra times in case of nested templates
hashInput = template.compile(hashInput, update);
hashInput = template.compile(hashInput, update);
const hash = hasha(hashInput);
update.branchName = update.branchPrefix + hash.slice(0, hashLength);
} else {
update.branchName = template.compile(update.branchName, update);
// Compile extra times in case of nested templates
update.branchName = template.compile(update.branchName, update);
update.branchName = template.compile(update.branchName, update);
}
update.branchName = cleanBranchName(update.branchName);
}