Skip to content

Commit 4bc6d7d

Browse files
committed
feat: Add Bitbucket Data Center support
1 parent 2ef88f2 commit 4bc6d7d

File tree

11 files changed

+879
-310
lines changed

11 files changed

+879
-310
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
- main
1010

1111
env:
12-
IMAGE_NAME: ghcr.io/renovatebot/renovate-approve-bot-bitbucket-cloud
12+
IMAGE_NAME: ghcr.io/renovatebot/renovate-approve-bot-bitbucket
1313

1414
concurrency:
1515
group: ${{ github.workflow }}-${{ github.event.number || github.ref }}

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
FROM docker.io/library/node:14.19.1-alpine@sha256:8845b4f88f64f8c56a39236648ba22946e806a6153c10911f77b70e5a2edb4ca
22

33
LABEL \
4-
org.opencontainers.image.source="https://github.com/renovatebot/renovate-approve-bot-bitbucket-cloud" \
5-
org.opencontainers.image.url="https://github.com/renovatebot/renovate-approve-bot-bitbucket-cloud" \
4+
org.opencontainers.image.source="https://github.com/renovatebot/renovate-approve-bot-bitbucket" \
5+
org.opencontainers.image.url="https://github.com/renovatebot/renovate-approve-bot-bitbucket" \
66
org.opencontainers.image.licenses="ISC"
77

88
WORKDIR /opt/app

README.md

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# renovate-approve-bot - Bitbucket Cloud Edition
1+
# renovate-approve-bot - Bitbucket Edition
22

3-
A job to approve Pull Requests from [Renovate Bot](https://github.com/renovatebot/renovate) on Bitbucket Cloud. This enables you to require Pull Request approvals on your repository while also utilising Renovate's "automerge" feature.
3+
A job to approve Pull Requests from [Renovate Bot](https://github.com/renovatebot/renovate) on Bitbucket Cloud and Data Center (and Server). This enables you to require Pull Request approvals on your repository while also utilising Renovate's "automerge" feature.
44

55
For Github, see [renovatebot/renovate-approve-bot](https://github.com/renovatebot/renovate-approve-bot).
66

7-
[![build](https://github.com/renovatebot/renovate-approve-bot-bitbucket-cloud/actions/workflows/build.yml/badge.svg)](https://github.com/renovatebot/renovate-approve-bot-bitbucket-cloud/actions/workflows/build.yml)
7+
[![build](https://github.com/renovatebot/renovate-approve-bot-bitbucket/actions/workflows/build.yml/badge.svg)](https://github.com/renovatebot/renovate-approve-bot-bitbucket/actions/workflows/build.yml)
88

99
## How it works
1010

@@ -14,7 +14,7 @@ On each run, the bot will:
1414
2. Filter out PRs where "automerge" is disabled
1515
3. Approve the "automerge" PRs
1616

17-
## Usage
17+
## Bitbucket Cloud usage
1818

1919
1. Create a Bitbucket Cloud account for the renovate-approve-bot and add it to your team (Recommended)
2020
2. [Create an App password](https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/) with `pullrequest:write` scope
@@ -33,7 +33,7 @@ On each run, the bot will:
3333
--env BITBUCKET_USERNAME \
3434
--env BITBUCKET_PASSWORD \
3535
--env RENOVATE_BOT_USER \
36-
ghcr.io/renovatebot/renovate-approve-bot-bitbucket-cloud:latest
36+
ghcr.io/renovatebot/renovate-approve-bot-bitbucket:latest
3737
```
3838

3939
- From source:
@@ -43,7 +43,7 @@ On each run, the bot will:
4343
node ./index.js
4444
```
4545

46-
## Bitbucket Pipelines example
46+
### Bitbucket Pipelines example
4747

4848
Example to run renovate-approve-bot in a custom Bitbucket Pipeline on a schedule:
4949

@@ -56,15 +56,46 @@ Example to run renovate-approve-bot in a custom Bitbucket Pipeline on a schedule
5656
renovate-approve-bot:
5757
- step:
5858
name: Renovate Approve Bot
59-
image: ghcr.io/renovatebot/renovate-approve-bot-bitbucket-cloud:latest
59+
image: ghcr.io/renovatebot/renovate-approve-bot-bitbucket:latest
6060
script:
6161
- export RENOVATE_BOT_USER=your-renovate-bot-user
6262
- node /opt/app/index.js
6363
```
6464

6565
3. Create a [schedule](https://support.atlassian.com/bitbucket-cloud/docs/pipeline-triggers/#On-schedule) for the custom pipeline (e.g. Hourly)
6666

67+
## Bitbucket Data Center and Server usage
68+
69+
1. Create an account for the renovate-approve-bot in your Bitbucket Data Center or Server instance
70+
2. Create a [HTTP access token](https://confluence.atlassian.com/bitbucketserver/http-access-tokens-939515499.html) (formerly known as personal access token) for the renovate-approve-bot account
71+
3. Grant read access on your repositories to the renovate-approve-bot account
72+
4. Optionally, add the renovate-approve-bot account to the default reviewers if you require approval from default reviewers
73+
5. Set the environment variables:
74+
- `BITBUCKET_URL`: URL of your Bitbucket Data Center or Server instance
75+
- `BITBUCKET_USERNAME`: Bitbucket username associated with the account used for renovate-approve-bot
76+
- `BITBUCKET_PASSWORD`: HTTP access token of the renovate-approve-bot account
77+
- `RENOVATE_BOT_USER`: Bitbucket username of your Renovate Bot
78+
6. Run the bot (on a schedule similarly to Renovate Bot, e.g. as a [Cron](https://en.wikipedia.org/wiki/Cron) job):
79+
80+
- With Docker:
81+
82+
```shell
83+
docker run --rm \
84+
--env BITBUCKET_URL \
85+
--env BITBUCKET_USERNAME \
86+
--env BITBUCKET_PASSWORD \
87+
--env RENOVATE_BOT_USER \
88+
ghcr.io/renovatebot/renovate-approve-bot-bitbucket:latest
89+
```
90+
91+
- From source:
92+
93+
```shell
94+
npm install --production
95+
node ./index.js
96+
```
97+
6798
## Security / Disclosure
6899

69-
If you discover any important bug with `renovate-approve-bot-bitbucket-cloud` that may pose a security problem, please disclose it confidentially to renovate-disclosure@whitesourcesoftware.com first, so that it can be assessed and hopefully fixed prior to being exploited.
100+
If you discover any important bug with `renovate-approve-bot-bitbucket` that may pose a security problem, please disclose it confidentially to renovate-disclosure@whitesourcesoftware.com first, so that it can be assessed and hopefully fixed prior to being exploited.
70101
Please do not raise GitHub issues for security-related doubts or problems.

adapter/bitbucket-cloud.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const got = require('got');
2+
3+
const MANUAL_MERGE_MESSAGE = 'merge this manually';
4+
5+
class BitbucketCloudAdapter {
6+
constructor(options) {
7+
this.options = options;
8+
this.log = options.log;
9+
this.defaultRequestOptions = {
10+
prefixUrl: 'https://api.bitbucket.org',
11+
// If we use username/password options, they will be URL encoded
12+
// https://github.com/sindresorhus/got/issues/1169
13+
// https://github.com/nodejs/node/issues/31439
14+
headers: {
15+
Authorization: `Basic ${Buffer.from(
16+
`${options.BITBUCKET_USERNAME}:${options.BITBUCKET_PASSWORD}`
17+
).toString('base64')}`,
18+
},
19+
responseType: 'json',
20+
};
21+
}
22+
23+
isAutomerging(pr) {
24+
try {
25+
return !pr.description.includes(MANUAL_MERGE_MESSAGE);
26+
} catch (error) {
27+
this.log.error(error);
28+
return false;
29+
}
30+
}
31+
32+
getPullRequests() {
33+
const prEndpoint = `/2.0/pullrequests/${this.options.RENOVATE_BOT_USER}`;
34+
this.log.info(
35+
'Requesting %s%s...',
36+
this.defaultRequestOptions.prefixUrl,
37+
prEndpoint
38+
);
39+
40+
return got.paginate('', {
41+
...this.defaultRequestOptions,
42+
pathname: prEndpoint,
43+
pagination: {
44+
transform: (response) =>
45+
response.body.values
46+
.filter((pr) => this.isAutomerging(pr))
47+
.map((pr) => pr.links.self.href),
48+
paginate: (response) => {
49+
if ('next' in response.body && response.body.next !== '') {
50+
this.options.log.info('Requesting %s...', response.body.next);
51+
return {
52+
url: new URL(response.body.next),
53+
};
54+
}
55+
this.options.log.info('All pull-requests gathered.');
56+
return false;
57+
},
58+
},
59+
});
60+
}
61+
62+
approvePullRequest(prHref) {
63+
return got('', {
64+
...this.defaultRequestOptions,
65+
prefixUrl: `${prHref}/approve`,
66+
method: 'POST',
67+
throwHttpErrors: false,
68+
});
69+
}
70+
}
71+
72+
module.exports = BitbucketCloudAdapter;

0 commit comments

Comments
 (0)