# Bitbucket pull request notifications on your phone

> Get a push notification when a Bitbucket pull request is opened, approved, gets a change request or a comment, or is merged.

- Company: Bitbucket (https://www.justpush.io/recipes/bitbucket)
- Event: `pullrequest:created` (Pull request: Created)
- Tags: Code hosting, Pull requests
- Install: https://studio.justpush.io/recipes/bitbucket/pull-request
- Web page: https://www.justpush.io/recipes/bitbucket/pull-request

## Setup

1. Click Install in Studio and sign in. The recipe is added to your account and you get a personal webhook URL.
2. In your Bitbucket repository, open Repository settings > Webhooks and click Add webhook.
3. Enter a title and paste your webhook URL (shown in Studio after install) as the URL.
4. Under Triggers, pick Choose from a full list of triggers, tick Pull request: Created and save.
5. Under **Triggers**, pick **Choose from a full list of triggers** and tick the **Pull Request** events you want pushes for:
6. **Created**: a pull request is opened (drafts are skipped)
7. **Approved**
8. **Changes Request created**
9. **Merged**
10. **Comment created** (optional; Low priority)
11. Updates, declines and other comment events are ignored, so ticking extra boxes won't cause noise.
12. Bitbucket signs deliveries when you set a **Secret** (`X-Hub-Signature: sha256=…`), but JustPush doesn't check that signature yet, so keep your endpoint URL private.

## Code

Studio calls `handleRequest(request)` with the incoming webhook (`{ method, headers, body, query, raw }`) and sends the message object it returns. Returning `null` sends nothing.

```js
// Bitbucket → pullrequest:* (pull request)
// Fires when a pull request is opened, approved, gets a change request or a comment, or is merged.
// Docs: https://support.atlassian.com/bitbucket-cloud/docs/event-payloads/#Pull-request-events

// The pull request events worth a push.
const ACTIONS = {
    "pullrequest:created": { emoji: "🔀", verb: "opened", priority: 0 },
    "pullrequest:approved": { emoji: "👍", verb: "approved", priority: 0 },
    "pullrequest:changes_request_created": { emoji: "✋", verb: "changes requested", priority: 0 },
    "pullrequest:comment_created": { emoji: "💬", verb: "comment", priority: -1 },
    "pullrequest:fulfilled": { emoji: "🟣", verb: "merged", priority: -1 },
}

// Header names can arrive in any case.
function header(request, name) {
    const headers = request.headers || {}
    const key = Object.keys(headers).find((k) => k.toLowerCase() === name)
    const value = key ? headers[key] : null
    return Array.isArray(value) ? value[0] : value
}

// Look a key up in a table without tripping over inherited names like "toString".
function pick(table, key, fallback) {
    return Object.prototype.hasOwnProperty.call(table, key) ? table[key] : fallback
}

// Keep long text readable in a notification.
function clip(value, max = 120) {
    const text = typeof value === "string" ? value.replace(/\s+/g, " ").trim() : ""
    return text.length > max ? text.slice(0, max - 1) + "…" : text || null
}

// Only link to real http(s) URLs.
function link(url, cta) {
    return typeof url === "string" && /^https?:\/\//.test(url) ? [{ cta, url }] : []
}

// Without the X-Event-Key header (Studio's Test runner), guess the event from the payload's shape.
function guess(body) {
    const pr = body.pullrequest
    if (!pr || typeof pr !== "object") return null
    if (body.comment) return "pullrequest:comment_created"
    if (body.approval) return "pullrequest:approved"
    if (body.changes_request) return "pullrequest:changes_request_created"
    if (pr.state === "MERGED") return "pullrequest:fulfilled"
    return pr.state === "OPEN" ? "pullrequest:created" : null
}

function handleRequest(request) {
    const body = request.body && typeof request.body === "object" ? request.body : {}
    const event = header(request, "x-event-key") ?? guess(body)
    const what = pick(ACTIONS, event, null)
    const pr = body.pullrequest

    // Guard: only the pull request events above. Updates, declines and other comment events are skipped.
    if (!what || !pr || typeof pr !== "object") {
        console.log(`Ignoring Bitbucket "${event ?? "unknown"}" event`)
        return null
    }

    // Drafts aren't ready for eyes yet.
    if (event === "pullrequest:created" && pr.draft) {
        console.log("Skipping draft pull request")
        return null
    }

    const repo = body.repository?.full_name ?? pr.destination?.repository?.full_name ?? "a repository"
    const who = body.actor?.display_name ?? body.actor?.nickname
    const number = pr.id ? `PR #${pr.id}` : "PR"
    const url = pr.links?.html?.href

    if (event === "pullrequest:comment_created") {
        return {
            title: `💬 ${who ?? "Someone"} on ${number}: ${pr.title ?? "a pull request"}`,
            message: [clip(body.comment?.content?.raw) ?? "New comment", repo].join(" — "),
            topic: "Bitbucket",
            priority: what.priority, // Low — read it when you're back in Bitbucket
            buttons: link(body.comment?.links?.html?.href ?? url, "View comment"),
        }
    }

    const from = pr.source?.branch?.name
    const to = pr.destination?.branch?.name

    return {
        title: `${what.emoji} ${number} ${what.verb}: ${pr.title ?? "Untitled pull request"}`,
        message: [repo, from && to ? `${from} → ${to}` : null, who ? `by ${who}` : null].filter(Boolean).join(" · "),
        topic: "Bitbucket",
        priority: what.priority, // Normal while it needs eyes, Low once merged
        buttons: link(url, "View pull request"),
    }
}
```

## Bitbucket pullrequest:created webhook payload (sample)

```json
{
  "actor": {
    "type": "user",
    "uuid": "{a1b2c3d4-0000-4000-8000-000000000001}",
    "nickname": "sam-lee",
    "account_id": "557058:00000000-0000-0000-0000-000000000001",
    "display_name": "Sam Lee"
  },
  "repository": {
    "name": "web",
    "type": "repository",
    "uuid": "{673a6070-3421-46c9-9d48-90745f7bfe8e}",
    "links": {
      "html": {
        "href": "https://bitbucket.org/acme/web"
      }
    },
    "full_name": "acme/web",
    "is_private": true
  },
  "pullrequest": {
    "id": 42,
    "draft": false,
    "links": {
      "html": {
        "href": "https://bitbucket.org/acme/web/pull-requests/42"
      }
    },
    "state": "OPEN",
    "title": "Add Apple Pay to checkout",
    "author": {
      "type": "user",
      "nickname": "sam-lee",
      "display_name": "Sam Lee"
    },
    "source": {
      "branch": {
        "name": "feature/apple-pay"
      },
      "commit": {
        "hash": "d3022fc0ca3d"
      }
    },
    "reviewers": [],
    "created_on": "2026-09-28T09:20:11.179678+00:00",
    "updated_on": "2026-09-28T09:20:11.205705+00:00",
    "description": "Adds Apple Pay as a payment method.",
    "destination": {
      "branch": {
        "name": "main"
      },
      "commit": {
        "hash": "ce5965ddd289"
      }
    },
    "participants": [],
    "close_source_branch": true
  }
}
```

## FAQ

### Does this work on iPhone and Android?

Yes. Install the JustPush app from the App Store or Google Play and sign in. Every phone signed in to your account gets the notification.

### Do I need to write code?

No. Install the recipe in Studio, paste your webhook URL into Bitbucket and you are done. The code is there if you want to change the text, the sound or the buttons.

### Can I change what the notification says?

Yes. After install the recipe's code is yours. Edit it in Studio and test it against the sample payload before you save.

### What does it cost?

JustPush is free for 30 days. After that it's $19.99 a year, or $39.99 once. There is no extra charge for recipes.
