-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
172 lines (134 loc) · 4.93 KB
/
index.js
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
// Heavily based on https://github.com/mydea/action-tag-date-version with some changes to the date and added prefix
const { setFailed, getInput, setOutput } = require('@actions/core');
const { context } = require('@actions/github');
const { exec } = require('@actions/exec');
const semver = require('semver');
async function run() {
try {
const prerelease = getInput('prerelease', { required: false });
const prefix = getInput('prefix');
const outputOnly = getInput('output-only', { required: false }) === 'true';
const currentVersionTag = await getCurrentTag();
if (currentVersionTag) {
console.log(`Already at version ${currentVersionTag}, skipping...`);
setOutput('version', currentVersionTag);
return;
}
const nextVersion = await getNextVersionTag(prefix, prerelease);
console.log(`Next version: ${nextVersion}`);
if (!outputOnly) {
await exec(`git tag ${nextVersion}`);
try {
await execGetOutput(`git push origin ${nextVersion}`);
} catch (error) {
const errorMessage = `${error}`;
if (
!errorMessage.includes('reference already exists') &&
!errorMessage.includes(
'Updates were rejected because the tag already exists in the remote.'
) &&
!errorMessage.includes('shallow update not allowed')
) {
throw error;
}
console.log(
`It seems the version ${nextVersion} was already created on origin in the meanwhile, skipping...`
);
}
} else {
console.log(`Only outputting version because output-only is set to ${outputOnly}`);
}
setOutput('version', nextVersion);
} catch (error) {
setFailed(error.message);
}
}
run();
async function getCurrentTag() {
await exec('git fetch --tags -f');
// First Check if there is already a release tag at the head...
const currentTags = await execGetOutput(
`git tag --points-at ${context.sha}`
);
return currentTags.map(processVersion).filter(Boolean)[0];
}
async function getNextVersionTag(prefix, prerelease) {
const allTags = await execGetOutput('git tag');
const previousVersionTags = allTags
.map(processVersion)
.filter(Boolean)
.sort((a, b) => a.localeCompare(b));
const nextTag = prerelease
? getPrereleaseVersion(previousVersionTags, prerelease)
: getNextDateVersion(previousVersionTags);
return `${prefix}${nextTag}`;
}
function getNextDateVersion(previousVersionTags) {
const { year, month } = getDateParts();
const newVersionParts = [`${year}`, `${month}`, 0];
while (_tagExists(newVersionParts, previousVersionTags)) {
newVersionParts[2]++;
}
return newVersionParts.join('.');
}
function getPrereleaseVersion(previousVersionTags, prerelease) {
const nextVersion = getNextDateVersion(previousVersionTags);
const nextVersionParts = nextVersion.split('.');
let prereleaseVersion = 0;
while (
_tagExists(nextVersionParts, previousVersionTags, [
prerelease,
prereleaseVersion,
])
) {
prereleaseVersion++;
}
return `${nextVersion}-${prerelease}.${prereleaseVersion}`;
}
function _tagExists(tagParts, previousVersionTags, prereleaseParts) {
let newTag = tagParts.join('.');
if (prereleaseParts) {
const [prerelease, prereleaseVersion] = prereleaseParts;
newTag = `${newTag}-${prerelease}.${prereleaseVersion}`;
}
return previousVersionTags.find((tag) => tag === newTag);
}
function processVersion(version) {
if (!semver.valid(version)) {
return false;
}
const { major, minor, version: parsedVersion } = semver.parse(version);
const { year: currentYear, month: currentMonth } = getDateParts();
if (major !== currentYear || minor !== currentMonth) {
return false;
}
return parsedVersion;
}
function getDateParts() {
const date = new Date();
const year = date.getUTCFullYear();
const month = date.getUTCMonth() + 1;
return { year, month };
}
async function execGetOutput(command) {
let collectedOutput = [];
let collectedErrorOutput = [];
const options = {
listeners: {
stdout: (data) => {
const output = data.toString().split('\n');
collectedOutput = collectedOutput.concat(output);
},
stderr: (data) => {
const output = data.toString().split('\n');
collectedErrorOutput = collectedErrorOutput.concat(output);
},
},
};
try {
await exec(command, [], options);
} catch (error) {
throw new Error(collectedErrorOutput);
}
return collectedOutput;
}