-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (72 loc) · 2.82 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
const assert = require("assert");
const { inspect } = require("util");
const { command } = require("execa");
const core = require("@actions/core");
const { request } = require("@octokit/request");
const path = require("path");
const fs = require("fs");
const admin = require("firebase-admin");
import {context, GitHub} from '@actions/github'
main();
async function main() {
try {
const inputs = {
serviceJson: core.getInput('service-json'),
githubToken: core.getInput('github-token'),
bucketName: core.getInput("bucketName"),
bucketFolder: core.getInput("bucketFolder"),
directoryPath: core.getInput("directoryPath"),
testNamePrefix: core.getInput("testNamePrefix")
};
core.debug(`Bucket name: ${inspect(inputs.bucketName)}`);
core.debug(`Bucket folder: ${inspect(inputs.bucketFolder)}`);
core.debug(`Directory path: ${inspect(inputs.directoryPath)}`);
core.debug(`Test name prefix: ${inspect(inputs.testNamePrefix)}`);
// Initialization
const client = new GitHub(inputs.githubToken, { })
const serviceAccount = JSON.parse(inputs.serviceJson);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: inputs.bucketName
});
var destinationLabel = `time-${Date.now().toString()}`
if (isPullRequest(context)) {
destinationLabel = `pr-numer-${context.issue.number}-${destinationLabel}`
}
const destinationFolder = `${inputs.bucketFolder}/${destinationLabel}`
const bucket = admin.storage().bucket();
const archiveFilePath = `${path.dirname(inputs.directoryPath)}/tests.tgz`
await runShellCommand(`tar -C ${path.dirname(inputs.directoryPath)} -czf ${archiveFilePath} ${path.basename(inputs.directoryPath)}`)
const uploadedFile = await bucket.upload(archiveFilePath, {
destination: `${destinationFolder}/test.tgz`,
predefinedAcl: "publicRead",
resumable: false
})
const id = uploadedFile[0]["id"]
const url = `https://firebasestorage.googleapis.com/v0/b/${inputs.bucketName}/o/${id}?alt=media`
const body = `❌ ${inputs.testNamePrefix} tests run has FAILED. \n Results - ${url}`
if (isPullRequest(context)) {
await client.issues.createComment({...context.issue, body: body})
} else {
core.info(body)
}
} catch (error) {
core.debug(inspect(error));
core.setFailed(error.message);
}
}
function isPullRequest(context) {
return context.issue != null && context.issue.number != null
}
async function runShellCommand(commandString) {
core.debug(`$ ${commandString}`);
try {
const { stdout, stderr } = await command(commandString, { shell: true });
const output = [stdout, stderr].filter(Boolean).join("\n");
core.debug(output);
return output;
} catch (error) {
core.debug(inspect(error));
throw error;
}
}