Bitbucket Integration Guide | PagerDuty

Integration Guide for Bitbucket

Bitbucket is the Git solution for professional teams. Bitbucket core features include pull requests, branch permissions and inline comments. With the Bitbucket integration for PagerDuty, you can notify on-call engineers about failures within your Bitbucket pipelines.

Note: this integration is intended for Bitbucket cloud.

PagerDuty Configuration

Within your PagerDuty account, a Custom Event Transformer needs to be created. This allows PagerDuty to process webhooks from your Bitbucket account. Each repository in Bitbucket can send failure information to different PagerDuty Services. Determine which Bitbucket repository and PagerDuty service you would like to integrate.

  1. Go to Configuration Services.
  2. Select the PagerDuty service you wish to integrate with, then go to the Integration tab
  3. Press New Integration. You will be taken to the Add an Integration screen.
  4. Provide a title for the integration, such as "Bitbucket". From the Integration Type drop down, select Custom Event Transformer, then press Add Integration.
  5. You will be taken back to the Integrations tab showing the newly added integration. Click the gear icon for the newly added integration and select Edit.
  1. The Edit Integration screen allows you to provide Javascript to process the webhook that will come from Bitbucket.

Paste the provided Event Transformer JavaScript snippet.

var webhook = PD.inputRequest;
var body = webhook.body || JSON.parse(webhook.rawBody);
var emit_event = false;
var event_type;

if(body.commit_status.state == "INPROGRESS") {
  event_type = PD.Trigger;
  emit_event = false;
}
if(body.commit_status.state == "FAILED") {
  event_type = PD.Trigger;
  emit_event = true;
}
if(body.commit_status.state == "SUCCESSFUL") {
  event_type = PD.Resolve;
  emit_event = true;
}

delete body.commit_status.repository_links;
delete body.commit_status.links;
delete body.commit_status.commit.author.user;

var normalized_event = {
  event_type: event_type,
  incident_key: body.commit_status.commit.hash,
  description: "Failed " + body.commit_status.type + ": " + body.commit_status.name + " | " + body.commit_status.repository.full_name,
  details: body.commit_status,
  client: "View in Bitbucket",
  client_url: body.commit_status.url
};

if (emit_event) {
	PD.emitGenericEvents([normalized_event]);
}
  1. Save the configuration by pressing the Save Changes button. You will then be taken to a screen showing you the configuration for your Bitbucket integration.
  1. From this screen, highlight and copy the Integration URL value. This will be needed when you configure webhooks in your Bitbucket account.

Bitbucket Configuration

The remaining configuration resides on the Bitbucket side, through the Bitbucket user interface. You will be adding webhooks that will send failure information to PagerDuty.

  1. Open the repository you want to integrate with PagerDuty. From the left side navigation, open Settings.
  2. Click Webhooks from the Workflow sub-heading, followed by Add Webhook.
  3. Provide a title, then paste the PagerDuty URL you copied from the above steps.
  4. Change the Triggers radio option to Choose from a full list of triggers.
  5. Under Repository, select Build status updated
  1. Save the configuration.

You have successfully completed setting up the integration. When there is a failure in your pipelines, a PagerDuty incident will notify on-call responders. When the failure recovers, a SUCCESSFUL message is sent to PagerDuty and the PagerDuty incident will be automatically resolved.

Bitbucket Integration Guide | PagerDuty

Integration Guide for Bitbucket