This repository was archived by the owner on Feb 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
226 lines (204 loc) · 8.26 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
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
const core = require("@actions/core");
const github = require("@actions/github");
const cv = require("compare-versions");
const fs = require("fs");
const MultiRegExp2 = require("multi-regexp2").default;
const path = require("path");
try {
console.log("Examining PR branch files...");
const text = core.getInput("files", { required: true });
const dictionary = getDictionary(text);
const annotations = getAnnotations(dictionary);
const versions = cleanAndSortVersions(annotations.map(x => x.text));
addCount(annotations, versions);
if (versions.length == 0) {
console.log("No versions found on PR branch.");
return;
}
core.setOutput("annotations_path", "./annotations.json");
core.setOutput("failed_check_path", "./failed.txt");
if (versions.filter(x => cv(x, versions[0]) == 0).length != versions.length) {
console.log("The version numbers on the PR branch don't match!");
console.log(versions);
console.log(annotations);
fs.writeFileSync("./annotations.json", JSON.stringify(annotations));
fs.writeFileSync("./failed.txt", "1");
return;
} else {
console.log("The version numbers on the PR branch match.");
console.log(versions);
fs.writeFileSync("./annotations.json", "[]");
}
console.log("Examining master branch files...");
const masterPath = core.getInput("master_repo_path");
if (!masterPath) {
console.log("No master branch path set. Skipping master check.");
return;
}
const masterAnnotations = getAnnotations(dictionary, masterPath);
const masterVersions = masterAnnotations.map(x => x.text);
if (masterVersions.length == 0) {
console.log("No versions found on master branch. Skipping master check.");
return;
}
if (masterVersions.filter(x => cv(x, masterVersions[0]) == 0).length != masterVersions.length) {
console.log("The version numbers on the master branch don't match! Skipping master check.");
return;
}
if (cv(versions[0], masterVersions[0]) == 0) {
console.log("PR branch version is the same as master branch version!");
console.log(versions[0]);
setSameAsMaster(annotations, versions[0]);
fs.writeFileSync("./annotations.json", JSON.stringify(annotations));
fs.writeFileSync("./failed.txt", "1");
return;
} else if (cv(versions[0], masterVersions[0]) == -1) {
console.log("PR branch version is lower than master branch version!");
console.log(`PR: ${versions[0]}, MASTER: ${masterVersions[0]}`);
setLowerThanMaster(annotations, versions[0], masterVersions[0]);
fs.writeFileSync("./annotations.json", JSON.stringify(annotations));
fs.writeFileSync("./failed.txt", "1");
return;
} else {
console.log("PR branch version is not the same as the master branch version.");
console.log(`PR: ${versions[0]}, MASTER: ${masterVersions[0]}`);
fs.writeFileSync("./annotations.json", "[]");
}
} catch (error) {
core.setFailed("An error has occurred: " + error.message);
}
/**
* This function turns a custom string into a dictionary
* @example
* // Example of YAML syntax
* with:
* files: |
* QModManager/Properties/AssemblyInfo.cs | /\[assembly: AssemblyVersion\("v([0-9.]+)"\)\]/g
* Installer/QModsInstallerScript.iss | /version: "v([0-9.]+)"/g
* Data/latest-version.txt | /([0-9.]+)/g
* @param {string} text
* @returns {{[file: string]: string}}
*/
function getDictionary(text) {
const results = {};
const lines = text.split(/[\n\r]+/g);
for (const line of lines) {
const file = line.split("|")[0].trim();
const regex = line.split("|")[1].trim();
if (!results[file]) results[file] = [];
results[file].push(regex);
}
return results;
}
/**
* This function turns the dictionary returned by `getDictionary(string)` into an array of annotations which can be understood by the `Attest/annotations-action` action
* @param {{[file: string]: string}} dict
* @param {string} _path
* @returns {{message: string, path: string, column: {start: number, end: number}, line: {start: number, end: number}, level: "failure", text: string}[]}
*/
function getAnnotations(dict, _path = ".") {
const output = [];
for (const file in dict) { // For each file in the dictionary
const contents = fs.readFileSync(path.join(_path, file), "utf-8"); // Read file's contents
contents.split(/\n|(?:\r\n)/g).forEach((line, index) => { // For each line in the file
for (const regexText of dict[file]) { // For each regex in the dictionary
/** @type {RegExp} */
var regex;
eval("regex = " + regexText); // Directly obtain the regex from the string without worrying about escaping stuff
const regex2 = new MultiRegExp2(regex);
const matches = [...line.matchAll(regex)]; // Match the regex on the line
for (const match of matches) { // For each match
if (match[1]) { // If match is valid
/** @type {{match:string,start:number,end:number}} */
const indexMatch = regex2.execForGroup(match[0], 1); // Get the starting index of the first group, i.e. the version
output.push({ // Output match
"message": "Version numbers don't match.\n\nThere are:",
"path": file,
"column": {
"start": match.index + indexMatch.start, // The start of the first group, i.e. the version
"end": match.index + match[1].length + indexMatch.start, // The end of the first group, i.e. the version
},
"line": {
"start": index + 1, // The line of the match
"end": index + 1,
},
"level": "failure",
"text": match[1], // The first group of the match, i.e. the version
});
}
}
}
});
}
return output;
}
/**
* This function appends the number of occurrences of each version to the message of the annotations
* @param {{message: string, path: string, column: {start: number, end: number}, line: {start: number, end: number}, level: "failure", text: string}[]} annotations
* @param {string[]} versions
*/
function addCount(annotations, versions) {
const countObj = {};
for (const version of versions) {
if (!countObj[version]) countObj[version] = 0;
countObj[version]++;
}
const count = [];
for (const version in countObj) {
count.push({
version: cleanVersion(version),
count: countObj[version],
});
}
for (const annotation of annotations) {
for (const version of count) {
annotation.message += `\n- ${version.count} occurrence${version.count == 1 ? "" : "s"} of "${version.version}"`;
}
}
}
/**
* This function changes the message of the annotations to specify that the version is the same as the master version
* @param {{message: string, path: string, column: {start: number, end: number}, line: {start: number, end: number}, level: "failure", text: string}[]} annotations
* @param {string} version
*/
function setSameAsMaster(annotations, version) {
for (const annotation of annotations) {
annotation.message = `The version number is the same as the master branch! (${version})`;
}
}
/**
* This function changes the message of the annotations to specify that the version is lower than the master version
* @param {{message: string, path: string, column: {start: number, end: number}, line: {start: number, end: number}, level: "failure", text: string}[]} annotations
* @param {string} prVersion
* @param {string} masterVersion
*/
function setLowerThanMaster(annotations, prVersion, masterVersion) {
for (const annotation of annotations) {
annotation.message = `The version number (${prVersion}) is lower than the master branch! (${masterVersion})`;
}
}
/**
* Removes leading `v` and trailing `.0` from versions
* @param {string} version
* @returns {string}
*/
function cleanVersion(version) {
if (version.startsWith("v")) version = version.substr(1);
while (version.endsWith(".0")) version = version.substr(0, version.length - 2);
if (!version.includes(".")) version += ".0";
return version;
}
/**
* @example
* [ "1.0.1", "1.1", "1.0.1.0" ]
* // Will turn into
* [ "1.0.1", "1.0.1", "1.1" ]
* @param {string[]} versions
*/
function cleanAndSortVersions(versions) {
const result = [];
for (var version of versions) {
result.push(cleanVersion(version));
}
return result.sort(cv);
}