forked from PaulHatch/semantic-version
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
190 lines (155 loc) · 5.56 KB
/
Copy pathindex.js
File metadata and controls
190 lines (155 loc) · 5.56 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const core = require('@actions/core');
const exec = require("@actions/exec");
const eol = require('os').EOL;
const tagPrefix = core.getInput('tag_prefix') || '';
const cmd = async (command, ...args) => {
let output = '', errors = '';
const options = {
silent: true
};
options.listeners = {
stdout: (data) => { output += data.toString(); },
stderr: (data) => { errors += data.toString(); }
};
await exec.exec(command, args, options)
.catch(err => { core.info(`The command '${command} ${args.join(' ')}' failed: ${err}`); });
if (errors !== '') {
core.info(`stderr: ${errors}`);
}
return output;
};
const setOutput = (major, minor, patch, increment, changed, branch, namespace) => {
const format = core.getInput('format', { required: true });
var version = format
.replace('${major}', major)
.replace('${minor}', minor)
.replace('${patch}', patch)
.replace('${increment}', increment);
if (namespace !== '') {
version += `-${namespace}`
}
let tag;
if (major === 0 || patch !== 0) {
// Always tag pre-release/major version 0 as full version
tag = `${tagPrefix}${major}.${minor}.${patch}`;
} else if (minor !== 0) {
tag = `${tagPrefix}${major}.${minor}`;
} else {
tag = `${tagPrefix}${major}`;
}
const repository = process.env.GITHUB_REPOSITORY;
if (!changed) {
core.info('No changes detected for this commit');
}
core.info(`Version is ${major}.${minor}.${patch}+${increment}`);
if (repository !== undefined && !namespace) {
core.info(`To create a release for this version, go to https://github.com/${repository}/releases/new?tag=${tag}&target=${branch.split('/').reverse()[0]}`);
}
core.setOutput("version", version);
core.setOutput("major", major.toString());
core.setOutput("minor", minor.toString());
core.setOutput("patch", patch.toString());
core.setOutput("increment", increment.toString());
core.setOutput("changed", changed.toString());
core.setOutput("version_tag", tag);
};
async function run() {
try {
const remote = await cmd('git', 'remote');
const remoteExists = remote !== '';
const remotePrefix = remoteExists ? 'origin/' : '';
const branch = `${remotePrefix}${core.getInput('branch', { required: true })}`;
const majorPattern = core.getInput('major_pattern', { required: true });
const minorPattern = core.getInput('minor_pattern', { required: true });
const changePath = core.getInput('change_path') || '';
const namespace = core.getInput('namespace') || '';
const releasePattern = namespace === '' ? `${tagPrefix}*[0-9.]` : `${tagPrefix}*[0-9.]-${namespace}`;
let major = 0, minor = 0, patch = 0, increment = 0;
let changed = true;
let lastCommitAll = (await cmd('git', 'rev-list', '-n1', '--all')).trim();
if (lastCommitAll === '') {
// empty repo
setOutput('0', '0', '0', '0', changed, branch, namespace);
return;
}
//let commit = (await cmd('git', 'rev-parse', 'HEAD')).trim();
let tag = '';
try {
tag = (await cmd(
'git',
`describe`,
`--tags`,
`--abbrev=0`,
`--match=${releasePattern}`//,
//`${branch}`
)).trim();
}
catch (err) {
tag = '';
}
let root;
if (tag === '') {
if (remoteExists) {
core.warning('No tags are present for this repository. If this is unexpected, check to ensure that tags have been pulled from the remote.');
}
// no release tags yet, use the initial commit as the root
root = '';
} else {
// parse the version tag
let tagParts = tag.split('/');
let versionValues = tagParts[tagParts.length - 1]
.substr(tagPrefix.length)
.slice(0, namespace === '' ? 999 : -(namespace.length + 1))
.split('.');
major = parseInt(versionValues[0]);
minor = versionValues.length > 1 ? parseInt(versionValues[1]) : 0;
patch = versionValues.length > 2 ? parseInt(versionValues[2]) : 0;
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
core.setFailed(`Invalid tag ${tag} (${versionValues})`);
return;
}
root = await cmd('git', `merge-base`, tag, branch);
}
root = root.trim();
var logCommand = `git log --pretty="%s" --author-date-order ${(root === '' ? branch : `${root}..${branch}`)}`;
if (changePath !== '') {
logCommand += ` -- ${changePath}`;
}
const log = await cmd(logCommand);
if (changePath !== '') {
if (root === '') {
const changedFiles = await cmd(`git log --name-only --oneline ${branch} -- ${changePath}`);
changed = changedFiles.length > 0;
} else {
const changedFiles = await cmd(`git diff --name-only ${root}..${branch} -- ${changePath}`);
changed = changedFiles.length > 0;
}
}
let history = log
.trim()
.split(eol)
.reverse();
// Discover the change time from the history log by finding the oldest log
// that could set the version.
const majorIndex = history.findIndex(x => x.includes(majorPattern));
const minorIndex = history.findIndex(x => x.includes(minorPattern));
if (majorIndex !== -1) {
increment = history.length - (majorIndex + 1);
patch = 0;
minor = 0;
major++;
} else if (minorIndex !== -1) {
increment = history.length - (minorIndex + 1);
patch = 0;
minor++;
} else {
increment = history.length - 1;
patch++;
}
setOutput(major, minor, patch, increment, changed, branch, namespace);
} catch (error) {
core.error(error);
core.setFailed(error.message);
}
}
run();