forked from game-ci/unity-test-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.ts
252 lines (216 loc) · 8.93 KB
/
input.ts
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
import UnityVersionParser from './unity-version-parser';
import fs from 'fs';
import { getInput } from '@actions/core';
import os from 'os';
import * as core from '@actions/core';
class Input {
static get testModes() {
return ['all', 'playmode', 'editmode', 'standalone'];
}
static isValidFolderName(folderName) {
const validFolderName = new RegExp(/^(\.|\.\/)?(\.?[\w~]+([ _-]?[\w~]+)*\/?)*$/);
return validFolderName.test(folderName);
}
static isValidGlobalFolderName(folderName) {
const validFolderName = new RegExp(/^(\.|\.\/|\/)?(\.?[\w~]+([ _-]?[\w~]+)*\/?)*$/);
return validFolderName.test(folderName);
}
/**
* When in package mode, we need to scrape the package's name from its package.json file
*/
static getPackageNameFromPackageJson(packagePath) {
const packageJsonPath = `${packagePath}/package.json`;
if (!fs.existsSync(packageJsonPath)) {
throw new Error(`Invalid projectPath - Cannot find package.json at ${packageJsonPath}`);
}
let packageJson;
try {
packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
} catch (error) {
if (error instanceof SyntaxError) {
throw new SyntaxError(`Unable to parse package.json contents as JSON - ${error.message}`);
}
throw new Error(`Unable to parse package.json contents as JSON - unknown error ocurred`);
}
const rawPackageName = packageJson.name;
if (typeof rawPackageName !== 'string') {
throw new TypeError(
`Unable to parse package name from package.json - package name should be string, but was ${typeof rawPackageName}`,
);
}
if (rawPackageName.length === 0) {
throw new Error(`Package name from package.json is a string, but is empty`);
}
return rawPackageName;
}
private static getSerialFromLicenseFile(license: string) {
const startKey = `<DeveloperData Value="`;
const endKey = `"/>`;
const startIndex = license.indexOf(startKey) + startKey.length;
if (startIndex < 0) {
throw new Error(`License File was corrupted, unable to locate serial`);
}
const endIndex = license.indexOf(endKey, startIndex);
// Slice off the first 4 characters as they are garbage values
return Buffer.from(license.slice(startIndex, endIndex), 'base64').toString('binary').slice(4);
}
/**
* When in package mode, we need to ensure that the Tests folder is present
*/
static verifyTestsFolderIsPresent(packagePath) {
if (!fs.existsSync(`${packagePath}/Tests`)) {
throw new Error(
`Invalid projectPath - Cannot find package tests folder at ${packagePath}/Tests`,
);
}
}
public static getFromUser() {
// Input variables specified in workflow using "with" prop.
const unityVersion = getInput('unityVersion') || 'auto';
const customImage = getInput('customImage') || '';
const rawProjectPath = getInput('projectPath') || '.';
const unityLicensingServer = getInput('unityLicensingServer') || '';
const unityLicensingProductIds = getInput('unityLicensingProductIds') || '';
const unityLicense = getInput('unityLicense') || (process.env['UNITY_LICENSE'] ?? '');
let unitySerial = process.env['UNITY_SERIAL'] ?? '';
const customParameters = getInput('customParameters') || '';
const testMode = (getInput('testMode') || 'all').toLowerCase();
const coverageOptions = getInput('coverageOptions') || '';
const rawArtifactsPath = getInput('artifactsPath') || 'artifacts';
const rawUseHostNetwork = getInput('useHostNetwork') || 'false';
const sshAgent = getInput('sshAgent') || '';
const rawSshPublicKeysDirectoryPath = getInput('sshPublicKeysDirectoryPath') || '';
const gitPrivateToken = getInput('gitPrivateToken') || '';
const githubToken = getInput('githubToken') || '';
const checkName = getInput('checkName') || 'Test Results';
const rawPackageMode = getInput('packageMode') || 'false';
let packageName = '';
const scopedRegistryUrl = getInput('scopedRegistryUrl') || '';
const rawScopes = getInput('registryScopes') || '';
let registryScopes: string[] = [];
const chownFilesTo = getInput('chownFilesTo') || '';
const dockerCpuLimit = getInput('dockerCpuLimit') || os.cpus().length.toString();
const bytesInMegabyte = 1024 * 1024;
let memoryMultiplier;
switch (os.platform()) {
case 'linux':
memoryMultiplier = 0.95;
break;
case 'win32':
memoryMultiplier = 0.8;
break;
default:
memoryMultiplier = 0.75;
break;
}
const dockerMemoryLimit =
getInput('dockerMemoryLimit') ||
`${Math.floor((os.totalmem() / bytesInMegabyte) * memoryMultiplier)}m`;
const dockerIsolationMode = getInput('dockerIsolationMode') || 'default';
const runAsHostUser = getInput('runAsHostUser') || 'false';
const containerRegistryRepository = getInput('containerRegistryRepository') || 'unityci/editor';
const containerRegistryImageVersion = getInput('containerRegistryImageVersion') || '3';
// Validate input
if (!this.testModes.includes(testMode)) {
throw new Error(`Invalid testMode ${testMode}`);
}
if (!this.isValidFolderName(rawProjectPath)) {
throw new Error(`Invalid projectPath "${rawProjectPath}"`);
}
if (!this.isValidFolderName(rawArtifactsPath)) {
throw new Error(`Invalid artifactsPath "${rawArtifactsPath}"`);
}
if (!this.isValidGlobalFolderName(rawSshPublicKeysDirectoryPath)) {
throw new Error(`Invalid sshPublicKeysDirectoryPath "${rawSshPublicKeysDirectoryPath}"`);
}
if (rawUseHostNetwork !== 'true' && rawUseHostNetwork !== 'false') {
throw new Error(`Invalid useHostNetwork "${rawUseHostNetwork}"`);
}
if (rawPackageMode !== 'true' && rawPackageMode !== 'false') {
throw new Error(`Invalid packageMode "${rawPackageMode}"`);
}
if (rawSshPublicKeysDirectoryPath !== '' && sshAgent === '') {
throw new Error(
'sshPublicKeysDirectoryPath is set, but sshAgent is not set. sshPublicKeysDirectoryPath is useful only when using sshAgent.',
);
}
// sanitize packageMode input and projectPath input since they are needed
// for input validation
const packageMode = rawPackageMode === 'true';
const projectPath = rawProjectPath.replace(/\/$/, '');
// if in package mode, attempt to get the package's name, and ensure tests are present
if (packageMode) {
if (unityVersion === 'auto') {
throw new Error(
'Package Mode is enabled, but unityVersion is set to "auto". unityVersion must manually be set in Package Mode.',
);
}
packageName = this.getPackageNameFromPackageJson(projectPath);
this.verifyTestsFolderIsPresent(projectPath);
if (scopedRegistryUrl !== '') {
if (rawScopes === '') {
throw new Error(
'Scoped registry is set, but registryScopes is not set. registryScopes is required when using scopedRegistryUrl.',
);
}
registryScopes = rawScopes.split(',').map(scope => scope.trim());
}
}
if (runAsHostUser !== 'true' && runAsHostUser !== 'false') {
throw new Error(`Invalid runAsHostUser "${runAsHostUser}"`);
}
if (unityLicensingServer === '' && !unitySerial) {
// No serial was present, so it is a personal license that we need to convert
if (!unityLicense) {
throw new Error(
`Missing Unity License File and no Serial was found. If this
is a personal license, make sure to follow the activation
steps and set the UNITY_LICENSE GitHub secret or enter a Unity
serial number inside the UNITY_SERIAL GitHub secret.`,
);
}
unitySerial = this.getSerialFromLicenseFile(unityLicense);
}
if (unitySerial !== undefined && unitySerial.length === 27) {
core.setSecret(unitySerial);
core.setSecret(`${unitySerial.slice(0, -4)}XXXX`);
}
// Sanitise other input
const artifactsPath = rawArtifactsPath.replace(/\/$/, '');
const sshPublicKeysDirectoryPath = rawSshPublicKeysDirectoryPath.replace(/\/$/, '');
const useHostNetwork = rawUseHostNetwork === 'true';
const editorVersion =
unityVersion === 'auto' ? UnityVersionParser.read(projectPath) : unityVersion;
// Return sanitised input
return {
editorVersion,
customImage,
projectPath,
customParameters,
testMode,
coverageOptions,
artifactsPath,
useHostNetwork,
sshAgent,
sshPublicKeysDirectoryPath,
gitPrivateToken,
githubToken,
checkName,
packageMode,
packageName,
scopedRegistryUrl,
registryScopes,
chownFilesTo,
dockerCpuLimit,
dockerMemoryLimit,
dockerIsolationMode,
unityLicensingServer,
unityLicensingProductIds,
runAsHostUser,
containerRegistryRepository,
containerRegistryImageVersion,
unitySerial,
};
}
}
export default Input;