-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (69 loc) · 2.7 KB
/
index.js
File metadata and controls
80 lines (69 loc) · 2.7 KB
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 axios = require('axios');
const {getFileSize} = require('./src/fileHelpers');
const sendFile = require('./src/sendFile');
const sendMultipartFile = require('./src/sendMultipartFile');
core.info(`> Starting to upload the app to Oversecured...`)
const API_KEY = core.getInput('access_token');
const INTEGRATION_ID = core.getInput('integration_id');
const BRANCH_NAME = core.getInput('branch_name') || 'main';
const APP_PATH = core.getInput('app_path');
const DELETE_RUNNING = core.getInput('delete_running');
const BASE_URL = 'https://api.oversecured.com/v1';
const ADD_VERSION = `${BASE_URL}/integrations/${INTEGRATION_ID}/branches/${BRANCH_NAME}/versions/add`;
async function run() {
try {
core.info(`App path: ${APP_PATH}`)
core.info(`Integration ID: ${INTEGRATION_ID}`)
core.info(`Branch name: ${BRANCH_NAME}`)
core.info(`Delete running: ${DELETE_RUNNING}`)
const apiSession = axios.create({
baseURL: BASE_URL,
headers: {'Authorization': API_KEY}
});
const fileName = APP_PATH.split('/').pop();
const platform = getPlatform(fileName);
let bucket_key
let fileSize = getFileSize(APP_PATH);
if (fileSize > 500 * 1024 * 1024) { // 500 MB
bucket_key = await sendMultipartFile(apiSession, fileName, platform, APP_PATH)
} else {
bucket_key = await sendFile(apiSession, fileName, platform, APP_PATH)
}
if (bucket_key) {
core.info(`Creating a new version...`)
let addVersionReq = {
'file_name': fileName,
'bucket_key': bucket_key
};
if (DELETE_RUNNING === 'true') {
addVersionReq = {
...addVersionReq,
'delete_running': true
}
}
let addVersionResponse = await apiSession.post(ADD_VERSION, addVersionReq);
if (addVersionResponse.status !== 200) {
core.error(`Wrong response code: ${addVersionResponse.status}`);
core.error('> App upload failed!')
} else {
core.info('> App uploaded successfully!')
}
} else {
core.error('> App upload failed!')
}
} catch (error) {
core.setFailed(error.message);
}
}
function getPlatform(fileName) {
fileName = fileName.toLowerCase();
if (fileName.endsWith('.apk') || fileName.endsWith('.aab')) {
return 'android';
}
if (fileName.endsWith('.zip')) {
return 'ios';
}
throw new Error(`App file ${fileName} has invalid extension. Only .apk, .aab, and .zip are allowed.`);
}
run();