Created
October 7, 2018 19:04
-
-
Save GuyMograbi/b1e30c49df1578ddbd3422ebd52cfafe to your computer and use it in GitHub Desktop.
trigger bitbucket pipeline from lambda. overcome 5 scheduled builds limit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
function doCall (opts = {}, callback) { | |
console.log(`doCall with opts ${JSON.stringify(opts, {}, 2)}`); | |
const data = { | |
'target': { | |
'selector': { | |
'type': 'branches', | |
'pattern': opts.pipeline_name | |
}, | |
'ref_type': 'branch', | |
'type': 'pipeline_ref_target', | |
'ref_name': opts.target_branch | |
} | |
}; | |
const username = opts.username; | |
const token = opts.token; | |
const req = https.request({ | |
method: 'POST', | |
port: 443, | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': JSON.stringify(data).length, | |
'Authorization': 'Basic ' + new Buffer(username + ':' + token).toString('base64') | |
}, | |
hostname: 'api.bitbucket.org', | |
path: `/2.0/repositories/${opts.repo_slug}/` | |
}, (res) => { | |
let result = ''; | |
res.on('data', function(data) { | |
result += data.toString(); | |
}) | |
res.on('end', function() { | |
console.log(result); | |
callback(null, JSON.parse(result)); | |
}); | |
res.on('error', function(e) { | |
callback(e); | |
}) | |
}); | |
req.write(JSON.stringify(data)); | |
req.end(); | |
} | |
exports.handler = async (event) => { | |
return new Promise((resolve, reject) => { | |
doCall(event, (error, data) => { | |
if (error) { | |
reject(error); | |
} | |
else { | |
resolve(data); | |
} | |
}) | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment