forked from Philmod/google-cloud-build-slack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (45 loc) · 1.5 KB
/
index.js
File metadata and controls
55 lines (45 loc) · 1.5 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
const github = require('octonode');
const util = require('util');
const config = require('./config.json');
const client = github.client(config.GITHUB_ACCESS_TOKEN);
// pending, success, error, or failure
const toGithubStatus = (gcbStatus) => {
if (gcbStatus === 'SUCCESS') {
return "success";
}
else if (gcbStatus === 'FAILURE') {
return "error";
}
else if (gcbStatus === 'INTERNAL_ERROR' || gcbStatus === 'TIMEOUT') {
return "failure";
}
return "pending";
}
// subscribe is the main function called by GCF.
const subscribe = (event, callback) => {
const build = eventToBuild(event.data.data);
console.log(`Build info: ${util.inspect(build)}`);
const { projectId, repoName, commitSha } = build.sourceProvenance.resolvedRepoSource;
const org = projectId;
let repo = repoName;
// GCB repoName is incorrect, try to fix by removing added prefix
const gcbPrefix = `github-${projectId}-`;
if (repoName.indexOf(gcbPrefix) === 0) {
// trim gcb generated prefix
repo = repo.slice(gcbPrefix.length);
}
const ghrepo = client.repo(`${org}/${repo}`);
// Update github commit status.
ghrepo.status(commitSha, {
state: toGithubStatus(build.status),
target_url: build.logUrl,
description: `Build ${build.status.toLowerCase().replace(/_/g, ' ')}.`
}, callback);
};
// eventToBuild transforms pubsub event message to a build object.
const eventToBuild = (data) => {
return JSON.parse(new Buffer(data, 'base64').toString());
}
module.exports = {
subscribe: subscribe
}