-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.js
255 lines (219 loc) · 9.12 KB
/
action.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const core = require('@actions/core');
const xpath = require('xpath');
const DOMParser = require('xmldom').DOMParser;
const fs = require('fs');
const urlExist = require('url-exist');
const {HttpClient} = require('@actions/http-client');
const equal = require('deep-equal');
const { promisify } = require('util');
const exec = promisify(require('child_process').exec)
const client = new HttpClient('nextcloud-version-matrix')
function versionHashBranch(version) {
return urlExist(`https://github.com/nextcloud/server/tree/stable${version}`);
}
async function getBranch(version) {
if (await versionHashBranch(version)) {
return `stable${version}`;
} else {
return "master";
}
}
function range(from, to) {
return [...Array(to - from + 1).keys()].map(i => i + from);
}
async function getSupportedVersions(branch) {
// yes, this is hacky, but it saves having to keep a list updated
let res = await client.get(`https://raw.githubusercontent.com/nextcloud/server/${branch}/lib/versioncheck.php`);
let versionCheckCode = await res.readBody();
let min, max;
if (versionCheckCode.match(/PHP_VERSION_ID < (\d+)/)) {
min = parseVersionId(versionCheckCode.match(/PHP_VERSION_ID < (\d+)/)[1]);
max = parseVersionId(versionCheckCode.match(/PHP_VERSION_ID >= (\d+)/)[1]);
} else {
min = parseFloat(versionCheckCode.match(/PHP_VERSION, '([\d.]+)'\) === -1/)[1]);
max = parseFloat(versionCheckCode.match(/PHP_VERSION, '([\d.]+)'\) !== -1/)[1]);
}
max = parseFloat((max - 0.1).toFixed(1));
return {min: min, max: max};
}
function parseVersionId(raw) {
let matches = raw.match(/^(\d\d)(\d)/)
return parseInt(matches[1], 10) / 10 + parseInt(matches[2], 10) / 10
}
async function isPhpVersionReleased(version) {
return await urlExist(`https://github.com/php/php-src/releases/tag/php-${version.toFixed(1)}.0`);
}
async function distroSupportsPhpVersion(version) {
try {
const output = await exec(`apt-cache policy php${version}`);
return output.stdout.includes("Candidate")
} catch (_e) {
return false;
}
}
function onlyUnique(value, index, array) {
return array.findIndex(item => equal(value, item)) === index;
}
function cartesianProduct(input) {
const inputArray = [];
for (let key of Object.keys(input)) {
const item = {};
item[key] = input[key];
inputArray.push(item);
}
return cartesianProductInner(inputArray);
}
// https://stackoverflow.com/a/18959668
function cartesianProductInner(input, current) {
if (!input || !input.length) { return []; }
let head = input[0];
let tail = input.slice(1);
let output = [];
for (let key in head) {
for (let i = 0; i < head[key].length; i++) {
let newCurrent = copy(current);
newCurrent[key] = head[key][i];
if (tail.length) {
let productOfTail =
cartesianProductInner(tail, newCurrent);
output = output.concat(productOfTail);
} else output.push(newCurrent);
}
}
return output;
}
function copy(obj) {
let res = {};
for (let p in obj) res[p] = obj[p];
return res;
}
(async () => {
try {
const filename = core.getInput('filename') || 'appinfo/info.xml';
const matrixInput = JSON.parse(core.getInput('matrix') || '{}');
const withPhpInput = core.getInput('with_php') || '[]';
console.log(withPhpInput);
let withPhp = [];
if (withPhpInput.startsWith('[') && withPhpInput.endsWith(']')) {
withPhp = JSON.parse(withPhpInput);
} else {
withPhp = [withPhpInput];
}
const content = fs.readFileSync(filename, 'utf8');
const document = new DOMParser().parseFromString(content);
const minVersion = parseInt(xpath.select1("//info//dependencies//nextcloud/@min-version", document).value, 10);
const maxVersion = parseInt(xpath.select1("//info//dependencies//nextcloud/@max-version", document).value, 10);
console.log(`App supports from ${minVersion} till ${maxVersion}`);
const versions = range(minVersion, maxVersion);
// we reverse the order to put highest version first for the "branched off check"
const versionData= (await Promise.all(versions.reverse().map(async (version) => {
const branch = await getBranch(version);
const php = await getSupportedVersions(branch);
return {
"phpMin": php.min,
"phpMax": php.max,
"branch": branch,
}
}))).reverse();
// matrix with a single php version per server version
const serverMatrix = cartesianProduct({
"server-versions": versionData.map(data => data.branch),
...matrixInput
});
serverMatrix.forEach(row => {
const phpMax = versionData.find(data => data.branch === row["server-versions"]).phpMax;
row["php-versions"] = phpMax.toFixed(1);
});
for (let extraPhpVersion of withPhp) {
serverMatrix.push({
"php-versions": extraPhpVersion,
"server-versions": "master",
});
}
core.setOutput("versions", JSON.stringify(versions));
const branches = versionData.map(data => data.branch).filter(onlyUnique);
const phpMin = Math.min(...versionData.map(data => data.phpMin));
const phpMax = Math.max(...versionData.map(data => data.phpMax));
const php = withPhp.concat([]); // clone, not alias
for(let version = phpMin; version <= phpMax; version += 0.1) {
// floats are a pain
version = parseFloat(version.toFixed(1));
if (await isPhpVersionReleased(version)) {
php.push(version.toFixed(1));
} else {
// no more minors for this major
version = Math.ceil(version) - 0.1;
}
}
php.sort();
const distroPhp = [];
let availablePhp = phpMax;
for(let version of php) {
if (await distroSupportsPhpVersion(version)) {
distroPhp.push(version)
availablePhp = version;
}
}
// matrix with a single server version per php version
const phpMatrix = cartesianProduct({
"php-versions": php,
...matrixInput
});
phpMatrix.forEach(row => {
const php = row['php-versions'];
const candidateVersion = versionData.findLast(data => data.phpMin <= php && data.phpMax >= php);
row["server-versions"] = candidateVersion?.branch ?? "master";
});
// matrix with every php and server combination
const fullMatrix = cartesianProduct({
"server-versions": versionData.map(data => data.branch),
"php-versions": php,
...matrixInput
}).filter(row => {
const php = row['php-versions'];
const version = versionData.find(version => version.branch === row['server-versions']);
return version.phpMin <= php && version.phpMax >= php;
});
for (let extraPhpVersion of withPhp) {
fullMatrix.push({
"php-versions": extraPhpVersion,
"server-versions": "master",
});
}
// matrix with at least one item for every server and php version
let testMatrix = phpMatrix.concat(serverMatrix).filter(onlyUnique);
console.log(`App supports from php ${phpMin.toFixed(1)} till php ${phpMax.toFixed(1)}`);
core.setOutput("branches", JSON.stringify(branches));
core.setOutput("ocp-branches", JSON.stringify(branches.map(branch => `dev-${branch}`)));
core.setOutput("php-versions", JSON.stringify(php));
core.setOutput("php-min-list", JSON.stringify([php[0]]));
core.setOutput("php-max-list", JSON.stringify([php[php.length - 1]]));
core.setOutput("php-available-list", JSON.stringify([availablePhp]));
core.setOutput("branches-min-list", JSON.stringify([branches[0]]));
core.setOutput("branches-max-list", JSON.stringify([branches[branches.length - 1]]));
core.setOutput("php-min", php[0]);
core.setOutput("php-max", php[php.length - 1]);
core.setOutput("php-available", availablePhp);
core.setOutput("branches-min", branches[0]);
core.setOutput("branches-max", branches[branches.length - 1]);
core.setOutput("matrix", JSON.stringify({
include: serverMatrix
}));
core.setOutput("sparse-matrix", JSON.stringify({
include: testMatrix
}));
core.setOutput("full-matrix", JSON.stringify({
include: fullMatrix
}));
core.setOutput("ocp-matrix", JSON.stringify({
include: serverMatrix.map(row => {
const ocpVersion = `dev-${row["server-versions"]}`;
delete row["server-versions"];
row["ocp-version"] = ocpVersion;
return row;
})
}));
} catch (error) {
core.setFailed(error.message);
}
})()