-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
commit.ts
58 lines (56 loc) · 1.93 KB
/
commit.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
import is from '@sindresorhus/is';
import minimatch from 'minimatch';
import { GlobalConfig } from '../../config/global';
import { CONFIG_SECRETS_EXPOSED } from '../../constants/error-messages';
import { logger } from '../../logger';
import { commitAndPush } from '../../platform/commit';
import { sanitize } from '../../util/sanitize';
import type { BranchConfig } from '../types';
export function commitFilesToBranch(
config: BranchConfig
): Promise<string | null> {
let updatedFiles = config.updatedPackageFiles.concat(config.updatedArtifacts);
// istanbul ignore if
if (is.nonEmptyArray(config.excludeCommitPaths)) {
updatedFiles = updatedFiles.filter(({ path: filePath }) => {
const matchesExcludePaths = config.excludeCommitPaths.some(
(excludedPath) => minimatch(filePath, excludedPath, { dot: true })
);
if (matchesExcludePaths) {
logger.debug(`Excluding ${filePath} from commit`);
return false;
}
return true;
});
}
if (!is.nonEmptyArray(updatedFiles)) {
logger.debug(`No files to commit`);
return null;
}
const fileLength = [...new Set(updatedFiles.map((file) => file.path))].length;
logger.debug(`${fileLength} file(s) to commit`);
// istanbul ignore if
if (GlobalConfig.get('dryRun')) {
logger.info('DRY-RUN: Would commit files to branch ' + config.branchName);
return null;
}
// istanbul ignore if
if (
config.branchName !== sanitize(config.branchName) ||
config.commitMessage !== sanitize(config.commitMessage)
) {
logger.debug(
{ branchName: config.branchName },
'Secrets exposed in branchName or commitMessage'
);
throw new Error(CONFIG_SECRETS_EXPOSED);
}
// API will know whether to create new branch or not
return commitAndPush({
branchName: config.branchName,
files: updatedFiles,
message: config.commitMessage,
force: !!config.forceCommit,
platformCommit: !!config.platformCommit,
});
}