-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
executable file
·72 lines (68 loc) · 2.37 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
const axios = require('axios');
/**
* Triggered from a message on a Cloud Pub/Sub topic.
*
* @param {!Object} event Event payload.
* @param {!Object} context Metadata for the event.
*/
exports.processPubSubMessage = (event, context) => {
const pubsubMessage = event.data;
const dataString = Buffer.from(pubsubMessage, 'base64').toString();
const message = JSON.parse(dataString);
const commitSha = message.sourceProvenance.resolvedRepoSource.commitSha;
const repoName = message.sourceProvenance.resolvedRepoSource.repoName;
const [bitbucket, username, repo_slug] = repoName.split('_');
// Build Bitbucket payload data.
const payload = {
type: 'string',
created_on: message.createTime,
description: `Status: ${message.status}`,
key: 'string',
name: 'Google Cloud Build',
refname: `buildTriggerId: ${message.buildTriggerId}`,
state: getBitbucketState(message.status),
updated_on: message.finishTime,
url: message.logUrl,
uuid: message.id,
}
// Send request to Bitbucket.
const token = process.env.BITBUCKET_TOKEN;
const url = getBuildUrl(username, repo_slug, commitSha);
axios.post(url, payload, {
headers: { Authorization: `Basic ${token}` }
})
.then(function(response){
console.log(response);
})
.catch(function(error){
console.log(error);
});
/**
* See: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/commit/%7Bnode%7D/statuses/build
*
* @param {string} username
* @param {string} repo_slug
* @param {string} commitSha
*/
function getBuildUrl(username, repo_slug, commitSha) {
const baseUrl = 'https://api.bitbucket.org/2.0/repositories';
return `${baseUrl}/${username}/${repo_slug}/commit/${commitSha}/statuses/build`;;
}
/**
* Translates states from Google Cloud Build Message to Bitbucket.
* See: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/commit/%7Bnode%7D/statuses/build
*
* @param {string} status
*/
function getBitbucketState(status) {
switch(status.toLowerCase()) {
case 'success':
return 'SUCCESSFUL';
case 'queued':
case 'working':
return 'INPROGRESS';
default:
return 'FAILED';
}
}
};