Create a commit to bitbucket from AWS Lambda
const createClient = require('bitbucketjs');
const path = require('path');
const get = require('lodash.get');
const apiRoot = 'api.bitbucket.org/';
const repoRoot = '/2.0/repositories';
const refFilename = '.contentful';
const {
TASK_REPO: repo,
TASK_BB_USERNAME: username,
TASK_BB_PASSWORD: password,
} = process.env;
const client = createClient({ username, password });
function pathFor(uri) {
return `https://${path.join(apiRoot, repoRoot, uri)}`;
}
exports.handler = (event, context, callback) => {
if (!event.headers['X-Contentful-Topic']) {
callback({ message: 'Bad Request' });
}
const id = get(event.body, 'sys.id');
const getFile = client.request.get(pathFor(`${repo}/src/${refFilename}`));
getFile.then((file) => {
console.log(file);
});
return;
const createCommit = client.request
.post(pathFor(`${repo}/src`))
.send(`${refFilename}=${encodeURIComponent(JSON.stringify(event.body))}`)
.send(`message=AUTO COMMIT: Contentful webhook update ${id}`);
createCommit
.then(() => {
callback(null, JSON.stringify({
message: 'Committed contentful change.',
id,
}));
})
.catch(callback);
};