-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
80 lines (74 loc) · 2.57 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
const core = require('@actions/core');
const shell = require(`shelljs`);
const jwt = require('jsonwebtoken');
const axios = require('axios');
function getSettingsFor(targetName, settingId) {
const buildSettings = shell.exec(`xcodebuild -target ${targetName} -showBuildSettings`, { silent: true });
const bundleIds = buildSettings.split(`\n`).filter(function(item) {
return item.includes(settingId)
});
if (bundleIds.length == 1) {
const bundleId = bundleIds[0].replace(`${settingId} = `,"");
return bundleId.replace(/\s/g, "");;
} else {
throw `Could not find the Bundle ID of the given target ${targetName}`;
}
}
function getToken(issuerID, minute, privateKey, keyId) {
const payload = {
exp: Math.floor(Date.now() / 1000) + (minute * 60),
aud: "appstoreconnect-v1",
iss: issuerID
};
const options = {
algorithm: "ES256",
header: {
kid: keyId
}
}
return jwt.sign(payload, privateKey, options);
}
async function get(url, params, token, method = "GET") {
const options = {
url: url,
method: method,
headers: {
'Authorization': `Bearer ${token}`
},
params: params
}
const response = await axios.request(options);
return response.data;
}
async function bumpVersion() {
try {
const appStoreConnectPrivateKey = core.getInput("appStoreConnectPrivateKey");
const keyID = core.getInput("keyID");
const issuerID = core.getInput("issuerID");
const targetName = core.getInput("targetName");
const bundleIdIdentifier = "PRODUCT_BUNDLE_IDENTIFIER";
const bundleId = getSettingsFor(targetName, bundleIdIdentifier);
const token = getToken(issuerID, 2, Buffer.from(appStoreConnectPrivateKey, "utf8"), keyID);
const appResponse = await get("https://api.appstoreconnect.apple.com/v1/apps", { "filter[bundleId]" : bundleId }, token);
const appId = appResponse.data[0].id;
if (appId) {
const builds = await get("https://api.appstoreconnect.apple.com/v1/builds", { "filter[app]": appId, limit: 1, sort: "-version" }, token);
if (builds.data.length == 0) {
shell.exec(`xcrun agvtool new-version -all 1`);
return;
}
const currentBuildNumber = builds.data[0].attributes.version;
if (currentBuildNumber) {
shell.exec(`xcrun agvtool new-version -all ${currentBuildNumber}`);
shell.exec(`xcrun agvtool bump -all`);
} else {
throw `Could not find the Version Number for ${appId}`;
}
} else {
throw `Could not find the App ID for ${bundleId}`;
}
} catch (error) {
core.setFailed(error);
}
}
bumpVersion();