-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
46 lines (41 loc) · 1.47 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
/**
* gcloud functions deploy <FUNC_NAME> --runtime nodejs6 --trigger-resource cloud-builds --trigger-event google.pubsub.topic.publish
* @param {object} event The Cloud Functions event.
* @param {function} callback The callback function.
*/
const { Storage } = require('@google-cloud/storage');
exports.${badge} = (event, callback) => {
const pubsubMessage = event.data;
if (pubsubMessage.data) {
buildResource = JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString())
if (buildResource.source) {
if (buildResource.source.repoSource.repoName && buildResource.source.repoSource.branchName) {
repo = buildResource.source.repoSource.repoName === "${repo}";
branch = buildResource.source.repoSource.branchName === "${branch}";
}
} else {
callback();
}
if (buildResource.status) {
status = buildResource.status;
} else {
callback();
}
const storage = new Storage();
if (repo && branch && status == "SUCCESS") {
storage.bucket("${bucket}")
.file("build/success.svg")
.copy(storage.bucket("${bucket}")
.file("build/${badge}.svg"));
console.log("switched badge to build success")
}
if (repo && branch && status == "FAILURE") {
storage.bucket("${bucket}")
.file("build/failure.svg")
.copy(storage.bucket("${bucket}")
.file("build/${badge}.svg"));
console.log("switched badge to build failure")
}
}
callback();
};