forked from wpilibsuite/xcode-notarize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
282 lines (226 loc) · 8.75 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// MIT License - Copyright (c) 2020 Stefan Arentz <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
const fs = require('fs');
const core = require('@actions/core');
const execa = require('execa');
const plist = require('plist');
const sleep = (ms) => {
return new Promise(res => setTimeout(res, ms));
};
const parseConfiguration = () => {
const configuration = {
productPath: core.getInput("product-path", {required: true}),
username: core.getInput("appstore-connect-username", {required: true}),
password: core.getInput("appstore-connect-password", {required: true}),
primaryBundleId: core.getInput("primary-bundle-id"),
ascProvider: core.getInput("asc-provider"),
verbose: core.getInput("verbose") === "true",
};
if (!fs.existsSync(configuration.productPath)) {
throw Error(`Product path ${configuration.productPath} does not exist.`);
}
return configuration
};
const archive = async ({productPath}) => {
const archivePath = "/tmp/archive.zip"; // TODO Temporary file
const args = [
"-c", // Create an archive at the destination path
"-k", // Create a PKZip archive
"--keepParent", // Embed the parent directory name src in dst_archive.
productPath, // Source
archivePath, // Destination
];
try {
await execa("ditto", args);
} catch (error) {
core.error(error);
return null;
}
return archivePath;
};
const submit = async ({productPath, archivePath, primaryBundleId, username, password, ascProvider, verbose}) => {
//
// Make sure the product exists.
//
if (!fs.existsSync(productPath)) {
throw Error(`No product could be found at ${productPath}`);
}
//
// The notarization process requires us to submit a 'primary
// bundle id' - this is just a unique identifier for notarizing
// this specific product. If it is not provided then we simply
// use the actual bundle identifier from the Info.plist
//
if (primaryBundleId === "") {
const path = productPath + "/Contents/Info.plist";
if (fs.existsSync(path)) {
const info = plist.parse(fs.readFileSync(path, "utf8"));
primaryBundleId = info.CFBundleIdentifier;
}
}
if (primaryBundleId === null) {
throw Error("No primary-bundle-id set and could not determine bundle identifier from product.");
}
//
// Run altool to notarize this application. This only submits the
// application to the queue on Apple's server side. It does not
// actually tell us if the notarization was succesdful or not, for
// that we need to poll using the request UUID that is returned.
//
const args = [
"altool",
"--output-format", "json",
"--notarize-app",
"-f", archivePath,
"--primary-bundle-id", primaryBundleId,
"-u", username,
"-p", password
];
if (ascProvider !== null && ascProvider !== "") {
args.push("--asc-provider")
args.push(ascProvider);
}
if (verbose === true) {
args.push("--verbose");
}
let xcrun = execa("xcrun", args, {reject: false});
if (verbose == true) {
xcrun.stdout.pipe(process.stdout);
xcrun.stderr.pipe(process.stderr);
}
const {exitCode, stdout, stderr} = await xcrun;
if (exitCode === undefined) {
// TODO Command did not run at all
throw Error("Unknown failure - altool did not run at all?");
}
if (exitCode !== 0) {
// TODO Maybe print stderr - see where that ends up in the output? console.log("STDERR", stderr);
const response = JSON.parse(stdout);
if (verbose === true) {
console.log(response);
}
for (const productError of response["product-errors"]) {
core.error(`${productError.code} - ${productError.message}`);
}
return null;
}
const response = JSON.parse(stdout);
if (verbose === true) {
console.log(response);
}
return response["notarization-upload"]["RequestUUID"];
};
const wait = async ({uuid, username, password, verbose}) => {
const args = [
"altool",
"--output-format", "json",
"--notarization-info",
uuid,
"-u", username,
"-p", password
];
if (verbose === true) {
args.push("--verbose");
}
for (let i = 0; i < 20; i++) {
let xcrun = execa("xcrun", args, {reject: false});
if (verbose == true) {
xcrun.stdout.pipe(process.stdout);
xcrun.stderr.pipe(process.stderr);
}
const {exitCode, stdout, stderr} = await xcrun;
if (exitCode === undefined) {
// TODO Command did not run at all
throw Error("Unknown failure - altool did not run at all?");
}
if (exitCode !== 0) {
// TODO Maye print stderr - see where that ends up in the output? console.log("STDERR", stderr);
const response = JSON.parse(stdout);
if (verbose === true) {
console.log(response);
}
for (const productError of response["product-errors"]) {
core.error(`${productError.code} - ${productError.message}`);
}
return false;
}
const response = JSON.parse(stdout);
if (verbose === true) {
console.log(response);
}
const notarizationInfo = response["notarization-info"];
switch (notarizationInfo["Status"]) {
case "in progress":
core.info(`Notarization status <in progress>`);
break;
case "invalid":
core.error(`Notarization status <invalid> - ${notarizationInfo["Status Message"]}`);
return false;
case "success":
core.info(`Notarization status <success>`);
return true;
default:
core.error(`Notarization status <${notarizationInfo["Status"]}> - TODO`);
return false;
}
await sleep(30000);
}
core.error("Failed to get final notarization status on time.");
return false;
};
const main = async () => {
try {
const configuration = parseConfiguration();
const archivePath = await core.group('Archiving Application', async () => {
const archivePath = await archive(configuration)
if (archivePath !== null) {
core.info(`Created application archive at ${archivePath}`);
}
return archivePath;
});
if (archivePath == null) {
core.setFailed("Notarization failed");
return;
}
const uuid = await core.group('Submitting for Notarizing', async () => {
let uuid = await submit({archivePath: archivePath, ...configuration});
if (uuid !== null) {
core.info(`Submitted package for notarization. Request UUID is ${uuid}`);
}
return uuid;
});
if (uuid == null) {
core.setFailed("Notarization failed");
return;
}
await sleep(15000); // TODO On a busy day, it can take a while before the build can be checked?
const success = await core.group('Waiting for Notarization Status', async () => {
return await wait({uuid: uuid, archivePath: archivePath, ...configuration})
});
if (success == false) {
core.setFailed("Notarization failed");
return;
}
core.setOutput('product-path', configuration.productPath);
} catch (error) {
core.setFailed(`Notarization failed with an unexpected error: ${error.message}`);
}
};
main();