-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (24 loc) · 1.07 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
const functions = require('@google-cloud/functions-framework');
const { Storage } = require('@google-cloud/storage');
const { BigQuery } = require('@google-cloud/bigquery');
const storage = new Storage();
const bigqueryClient = new BigQuery();
functions.cloudEvent('upload', cloudEvent => {
const file = cloudEvent.data;
if (file.name.match(/^.+\/mutation\.json$/)) {
async function downloadIntoMemory() {
const contents = await storage.bucket(file.bucket).file(file.name).download();
const content = JSON.parse(contents.toString());
const mapped = { files: Object.keys(content.files).map(fileName => ({ file_name: fileName, ...content.files[fileName] })) };
console.log(mapped);
return mapped;
}
async function insertRowsAsStream(mappedReport) {
await bigqueryClient
.dataset("mutation_tests")
.table("stryker")
.insert(mappedReport);
}
downloadIntoMemory().then(insertRowsAsStream).catch(console.error);
}
});