Complete and Secure Slack-Bitbucket integration in 20 minutes. No OAuth, no DB.

updated on 01 January 2025

It’s nice to have slack notifications from Bitbucket, right? That’s why so many teams are using bots, like official Bitbucket bot or ReviewNudgeBot to have an pull requests updates in Slack.

Or we can develop our own bot. In such case, we can adjust its functionality to what our team needs or add some feature other bots are missing.

Wait, Isn't That Complicated? 🤔

I know what you're thinking: "Oh great, now I need to wrestle with Slack apps, figure out authentication, deal with proper scopes... and then do it all over again for Bitbucket?"

Don't worry - your CTO won't need to lose sleep over some hacky webhook exposing your company's code. We're going to do this the smart way!

In this guide, we'll create a fully functional Bitbucket-Slack integration that's:

  • Safe by design (no sketchy security!)
  • Super configurable
  • No OAuth headaches
  • No database needed
  • No fancy landing page (it's a Slack bot, not a web app!)

High-level design

Here's our sneaky-simple approach:

  1. Use Bitbucket's repository webhooks to catch all the action
  2. Have our server grab those read-only events
  3. Turn that boring JSON into something humans actually want to read
  4. Shoot it over to Slack through a workspace-specific webhook

That's it! Simple but secure. Let's get building!

Step 1: Create Your Slack Channel

Quick and easy - create a new channel like slack-bitbucket-pr-events. Nothing fancy needed here!

Step 2: set up Slack channel webhook

Install Incoming Webhook Slack app

Configure webhook for your workspace.
Open app in Slack workspace and click “Configuration”

Then click Edit configuration.
In new window - specify channel name slack-bitbucket-pr-events

Save changes.
Want to make sure it works? Check out the "Setup Instructions" → "Example" section for a test command. Just remember to change the channel name in there!

Step 3: Server Time!

It’s nice to have Slack notifications about Bitbucket pull requests, right? That’s why so many teams are using bots, like official Bitbucket bot or ReviewNudgeBot to have an pull requests updates in Slack. Or we can develop our own bot. In such case, we can adjust its functionality to what our team needs or add some feature other bots are missing.

Let's get a simple Express server going:

npm init
npm install express

Add post handler to the index.js file

const express = require("express");
//const {handleBitbucketWebhookEvent} = require("./bitbucketController");

const app = express();
const port = 3000;

app.use(express.json())

app.post('/', async (req, res) => {
    const bitbucketEvent = req.headers['x-event-key'];

    console.log('[AppBitbucket] Received Bitbucket webhook event', req.headers);
    const payload = req.body;

    console.log('[AppBitbucket] Received Bitbucket webhook event', bitbucketEvent);
    console.log('[AppBitbucket] Received webhook event keys', Object.keys(payload));


    //await handleBitbucketWebhookEvent(bitbucketEvent, payload);

    res.status(200).send({ message: 'success' });
});

app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});

Start server:

node index.js

We’ll return to server implementation to map Bitbucket events and send Slack messages.

Step 4: Make It Public

Time for some NGrok magic:

ngrok http 3000

Grab that shiny HTTPS URL - we'll need it in a sec!

Step 5: Bitbucket, Meet Webhook

Open Bitbucket repo → Go to “Repository Settings” → Webhooks → Click “Add webhook”

Paste URL from NGrok output as URL, keep secret empty and select all pull request events and some repository events (for build events) 

Pro tip: Turn on webhook history in "View requests" - super handy for debugging!

Step 6: The Fun Part - Making It Work!

Let’s add business logic to our express server to parse and compose Slack message.

Bitbucket webhook events are sent as JSON objects. You can check body of requests in the “View requests” section of the webhook.

  1. Create bitbucketController.js to handle all those events:
        Grab the code from our repo
  2.  Add slackSender.js to shoot messages to Slack:
         Snag it from here
  3.  Fire it up again:
    node index.js

Step 7: Take It For a Spin!

Go to your Bitbucket repo and create new PR - add title and description and hit Create.

Go to Slack channel and verify that new message is sent to your integration channel.

Congratulations, you made it!

You can experiment with more events - add a comment, reply to comment, resolve comment, close Pull request. All events are covered in the server you are running. You can also verify in Webhook “View requests” section in Bitbucket repo settings that events are sent to your webhook, events body and response statuses.

- New comment

- Approve PR

- Merge PR

You Did It! 🎉

Look at that - you've got your very own Bitbucket-Slack integration up and running! No complicated bot setup, no security nightmares, just clean, simple notifications right where you want them.

Want to make it even better? Go wild! Add more events, jazz up those messages, make it your own. The sky's the limit!

Remember: Sometimes the simplest solution is the best one. Who needs all that OAuth complexity when you can build something this clean and effective in no time? 😎

Read more

Built on Unicorn Platform