Hey there, fellow JavaScript devs! Ready to supercharge your BitBucket workflow with webhooks? Let's dive right in and get those real-time updates flowing!
Webhooks are like your repo's personal messenger, giving you a heads-up whenever something interesting happens. With BitBucket's API, setting them up is a breeze. We'll focus on creating webhooks for user-facing integrations, so you can keep your team in the loop without breaking a sweat.
Before we start, make sure you've got:
First things first, let's get that API client up and running:
npm install bitbucket
Now, let's initialize it with your credentials:
const { Bitbucket } = require('bitbucket'); const bitbucket = new Bitbucket({ auth: { username: 'your-username', password: 'your-app-password' } });
Pro tip: Use app passwords instead of your actual password. Safety first!
Time to create that webhook! Here's a quick example:
async function createWebhook() { try { const { data } = await bitbucket.webhooks.create({ workspace: 'your-workspace', repo_slug: 'your-repo', _body: { description: 'My awesome webhook', url: 'https://your-webhook-url.com', active: true, events: ['repo:push', 'issue:created'] } }); console.log('Webhook created:', data); } catch (err) { console.error('Error creating webhook:', err); } }
Want to tweak those settings? No problem:
async function updateWebhook(webhookId) { try { const { data } = await bitbucket.webhooks.update({ workspace: 'your-workspace', repo_slug: 'your-repo', webhook_uid: webhookId, _body: { events: ['repo:push', 'issue:created', 'pr:opened'], url: 'https://your-new-webhook-url.com' } }); console.log('Webhook updated:', data); } catch (err) { console.error('Error updating webhook:', err); } }
Now, let's set up a simple Express server to catch those incoming webhooks:
const express = require('express'); const app = express(); app.use(express.json()); app.post('/webhook', (req, res) => { const payload = req.body; console.log('Received webhook:', payload); // Do something awesome with the payload res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));
Security is key! Let's add a secret and verify those signatures:
const crypto = require('crypto'); app.post('/webhook', (req, res) => { const signature = req.headers['x-hub-signature']; const payload = JSON.stringify(req.body); const secret = 'your-webhook-secret'; const hmac = crypto.createHmac('sha256', secret); const digest = 'sha256=' + hmac.update(payload).digest('hex'); if (signature === digest) { console.log('Webhook verified'); // Process the webhook res.sendStatus(200); } else { console.log('Webhook verification failed'); res.sendStatus(403); } });
BitBucket's got your back with a handy test feature. Head to your webhook settings and hit that "Test connection" button. You can also check out the delivery history to see what's been happening.
If things aren't working as expected, double-check your URL, events, and don't forget to look at those server logs!
And there you have it! You're now a BitBucket webhook wizard. Remember, webhooks are powerful tools that can streamline your workflow and keep everyone in sync. So go forth and integrate!
Want to dive deeper? Check out the BitBucket API documentation for more advanced usage and features.
Happy coding, and may your webhooks always deliver!