target:

By the end of this tutorial, Danger JS runs on every pull request in Bitbucket Pipelines, logged in with a repository access token, and comments on the pull request when a rule fails.

stack:

Bitbucket Cloud, Bitbucket Pipelines
Danger JS 14
Node.js 24 (node:24-slim image)

ref:

intro:

Danger's docs say you can log in with a repository access token. They don't say that it also needs DANGER_BITBUCKETCLOUD_UUID. Without it, danger ci fails with a 403 on /2.0/user, and the docs don't tell you how to find that UUID. This post covers the whole setup, including the part I couldn't find anywhere.

steps:

Step 1. Install Danger

npm install --save-dev danger 

Add a script to package.json:

"scripts": { "danger:ci": "danger ci --dangerfile dangerfile.js --failOnErrors" } 

--failOnErrors makes the pipeline step fail when a rule calls fail().

Step 2. Create the dangerfile
dangerfile.js in the repo root. One rule, to start with: the pull request needs a description.

const { danger, fail } = require('danger'); // danger local (a preview without a pull request) has no description const pr = danger.bitbucket_cloud?.pr; if (pr && !(pr.description ?? '').trim()) { fail('Add a description: what this pull request changes, why, and how to check it.'); } 

Note: Bitbucket fills the description with your commit messages when you open a pull request. Delete that text to see the rule fail.

Step 3. Add the pull request pipeline
In bitbucket-pipelines.yml:

image: node:24-slim pipelines: pull-requests: '**': - step: name: Danger script: - npm ci - npm run danger:ci 

'**' runs it for pull requests from any branch. Danger reads the changed files through Bitbucket's API, so the image doesn't need git.

Step 4. Create a repository access token

Repository settings > Security > Access tokens > Create access token.

  • name: danger. This is also the name of the bot user that writes the comments.
  • scope: Pull requests: Write (Danger posts, updates and deletes its comment)

access token form

Note: ticking Pull requests: Write also ticks Repositories: Read and Write. Bitbucket doesn't let you untick them.

Copy the token. Bitbucket shows it once.

access token

Step 5. Add the token as a secured variable
Repository settings > Pipelines > Repository variables:

  • name: DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN
  • value: the token
  • tick Secured

Step 6. Open a pull request, and get a 403
pen a pull request. The pipeline runs, and npm run danger:ci fails:

Request failed [403]: https://api.bitbucket.org/2.0/user Response: { "type": "error", "error": { "message": "This API is not accessible by this authentication mechanism" } } Error: Error: 403 - Forbidden (Have you allowed permission 'account' for this credential?) 

Here's why. Danger needs to know which user it comments as, so on the next run it can find its own comment and update it, instead of adding a new one. If you don't give it a UUID, it asks Bitbucket's /2.0/user for one. A repository access token isn't allowed to call /2.0/user, and there's no "account" scope to tick on that form.

You can see it in Danger's code, node_modules/danger/distribution/platforms/bitbucket_cloud/BitBucketCloudAPI.js:

if (this.uuid == null) { this.d("UUID not found, trying to get from " + this.baseURL + "/user"); // ... message += " (Have you allowed permission 'account' for this credential?)"; } 

The fix is the variable DANGER_BITBUCKETCLOUD_UUID: the UUID of the token's bot user. Then Danger skips /2.0/user.

Step 7. Find the bot's UUID
The token's bot user is the author of any comment the token writes. So post one comment with the token and read user.uuid from the answer.

You don't have the token anymore (step 4), but the pipeline does. So let the pipeline post the comment.

Create a test branch, and edit bitbucket-pipelines.yml on it: add the node -e line as the first script line of the step, before npm ci. The node:24-slim image has no curl, so it uses Node's fetch:

image: node:24-slim pipelines: pull-requests: '**': - step: name: Danger script: # TEMPORARY: prints the token's bot user UUID, then remove this line  - >- node -e "fetch('https://api.bitbucket.org/2.0/repositories/' + process.env.BITBUCKET_REPO_FULL_NAME + '/pullrequests/' + process.env.BITBUCKET_PR_ID + '/comments', { method: 'POST', headers: { Authorization: 'Bearer ' + process.env.DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN, 'Content-Type': 'application/json' }, body: JSON.stringify({ content: { raw: 'Danger token check. Delete this comment.' } }) }).then((r) => r.json()).then((j) => console.log('Danger bot UUID:', j.user ? j.user.uuid : JSON.stringify(j)))" - npm ci - npm run danger:ci 

>- is YAML's folded style: it keeps the long command as one line, without YAML tripping over its quotes and colons.

The same JavaScript, spread out:

fetch( // the comments of this pull request; Bitbucket Pipelines sets both variables 'https://api.bitbucket.org/2.0/repositories/' + process.env.BITBUCKET_REPO_FULL_NAME // workspace/repo + '/pullrequests/' + process.env.BITBUCKET_PR_ID // only set in pull request pipelines + '/comments', { method: 'POST', // create a comment headers: { // as the token's bot user Authorization: 'Bearer ' + process.env.DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN, 'Content-Type': 'application/json', }, body: JSON.stringify({ content: { raw: 'Danger token check. Delete this comment.' } }), }, ) .then((r) => r.json()) // the answer is the new comment, with its author: the bot user .then((j) => console.log('Danger bot UUID:', j.user ? j.user.uuid : JSON.stringify(j))); 

Commit, push the branch, and open a pull request from it (BITBUCKET_PR_ID only exists in a pull request pipeline). The pipeline log shows:

Danger bot UUID: {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} 

uuid in pipeline log

npm run danger:ci still fails with the 403 this time; that's fine.
Then clean up: delete the "Danger token check" comment on the pull request, and remove the node -e line from bitbucket-pipelines.yml again. Otherwise every run posts another comment.

Note: the UUID isn't a secret. It's an ID, like a username. It can't log in by itself.

Step 8. Add the UUID variable
Repository settings > Pipelines > Repository variables:

  • name: DANGER_BITBUCKETCLOUD_UUID
  • value: the UUID with the braces, exactly as the log printed it

Note: Danger throws "DANGER_BITBUCKETCLOUD_UUID must be wraped with brackets" without them. And it compares the UUID as text with comment.user.uuid from Bitbucket's API, which always has braces.

Rerun the pipeline. Now Danger comments on the pull request:

danger comment

Write a description and rerun again: the step goes green, and Danger deletes its comment.

Note: every repository access token is its own bot user, so every repo has its own UUID. A new token (for example after it expires) means a new UUID too. With a wrong UUID, Danger doesn't fail: it just can't find its own comment, so it adds a new one on every run.