UNPKG

2.3 kBJavaScriptView Raw
1const {isString, isRegExp} = require('lodash');
2const AggregateError = require('aggregate-error');
3const pEachSeries = require('p-each-series');
4const DEFINITIONS = require('../definitions/branches');
5const getError = require('../get-error');
6const {fetch, fetchNotes, verifyBranchName} = require('../git');
7const expand = require('./expand');
8const getTags = require('./get-tags');
9const normalize = require('./normalize');
10
11module.exports = async (repositoryUrl, ciBranch, context) => {
12 const {cwd, env} = context;
13
14 const remoteBranches = await expand(
15 repositoryUrl,
16 context,
17 context.options.branches.map((branch) => (isString(branch) || isRegExp(branch) ? {name: branch} : branch))
18 );
19
20 await pEachSeries(remoteBranches, async ({name}) => {
21 await fetch(repositoryUrl, name, ciBranch, {cwd, env});
22 });
23
24 await fetchNotes(repositoryUrl, {cwd, env});
25
26 const branches = await getTags(context, remoteBranches);
27
28 const errors = [];
29 const branchesByType = Object.entries(DEFINITIONS).reduce(
30 (branchesByType, [type, {filter}]) => ({[type]: branches.filter(filter), ...branchesByType}),
31 {}
32 );
33
34 const result = Object.entries(DEFINITIONS).reduce((result, [type, {branchesValidator, branchValidator}]) => {
35 branchesByType[type].forEach((branch) => {
36 if (branchValidator && !branchValidator(branch)) {
37 errors.push(getError(`E${type.toUpperCase()}BRANCH`, {branch}));
38 }
39 });
40
41 const branchesOfType = normalize[type](branchesByType);
42
43 if (!branchesValidator(branchesOfType)) {
44 errors.push(getError(`E${type.toUpperCase()}BRANCHES`, {branches: branchesOfType}));
45 }
46
47 return {...result, [type]: branchesOfType};
48 }, {});
49
50 const duplicates = [...branches]
51 .map((branch) => branch.name)
52 .sort()
53 .filter((_, idx, array) => array[idx] === array[idx + 1] && array[idx] !== array[idx - 1]);
54
55 if (duplicates.length > 0) {
56 errors.push(getError('EDUPLICATEBRANCHES', {duplicates}));
57 }
58
59 await pEachSeries(branches, async (branch) => {
60 if (!(await verifyBranchName(branch.name))) {
61 errors.push(getError('EINVALIDBRANCHNAME', branch));
62 }
63 });
64
65 if (errors.length > 0) {
66 throw new AggregateError(errors);
67 }
68
69 return [...result.maintenance, ...result.release, ...result.prerelease];
70};