Skip to content

Commit

Permalink
[Bitbucket]: Pull Requests: Support Bitbucket Server (#2598)
Browse files Browse the repository at this point in the history
* New Service: Bitbucket Server: Pull Request Count

* [Bitbucket]: Pull Requests: Add Support for bitbucket-server

* Update examples to use namedParams instead of exampleUrl

* Simplify cloud vs server check

* [Bitbucket]: Add support for bitbucket cloud private repos

* Add additional tests for bitbucket server

* [Bitbucket]: Add tests for basic auth

* [Bitbucket] Format secrets according to style guides

* [Bitbucket] Add link to server REST documentation

* Punt adding VSCode debug task to separate PR

* [Bitbucket] Remove extra truthy check on serverSecrets

* [Bitbucket] Fix credentials after rename

* [Bitbucket] Use query parameters for Bitbucket Server support

* Fix bitbucket creds in secret template

* [Bitbucket] staticExample -> staticPreview

* Remove VSCode specific gitignore entries

* [Bitbucket] Normalize pluralization of PullReqeust(s) to match file name
  • Loading branch information
nlowe authored and calebcartwright committed Jan 12, 2019
1 parent fb8c7e9 commit fc4ed93
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 14 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
shields.env
.git/
.gitignore
.vscode/
88 changes: 74 additions & 14 deletions services/bitbucket/bitbucket-pull-request.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,76 @@

const Joi = require('joi')
const BaseJsonService = require('../base-json')
const serverSecrets = require('../../lib/server-secrets')
const { metric } = require('../../lib/text-formatters')
const { nonNegativeInteger } = require('../validators')

const bitbucketPullRequestsSchema = Joi.object({
const bitbucketPullRequestSchema = Joi.object({
size: nonNegativeInteger,
}).required()

function pullRequestClassGenerator(raw) {
const routePrefix = raw ? 'pr-raw' : 'pr'
const badgeSuffix = raw ? '' : ' open'

return class BitbucketPullRequests extends BaseJsonService {
async fetch({ user, repo }) {
const url = `https://bitbucket.org/api/2.0/repositories/${user}/${repo}/pullrequests/`
return this._requestJson({
url,
schema: bitbucketPullRequestsSchema,
options: { qs: { state: 'OPEN', limit: 0 } },
errorMessages: { 403: 'private repo' },
})
return class BitbucketPullRequest extends BaseJsonService {
async fetchCloud({ args, user, repo }) {
args.url = `https://bitbucket.org/api/2.0/repositories/${user}/${repo}/pullrequests/`
args.options = { qs: { state: 'OPEN', limit: 0 } }

if (
serverSecrets.bitbucket_username &&
serverSecrets.bitbucket_password
) {
args.options.auth = {
user: serverSecrets.bitbucket_username,
pass: serverSecrets.bitbucket_password,
}
}

return this._requestJson(args)
}

// https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm46229602363312
async fetchServer({ args, server, user, repo }) {
args.url = `${server}/rest/api/1.0/projects/${user}/repos/${repo}/pull-requests`
args.options = {
qs: {
state: 'OPEN',
limit: 100,
withProperties: false,
withAttributes: false,
},
}

if (
serverSecrets.bitbucket_server_username &&
serverSecrets.bitbucket_server_password
) {
args.options.auth = {
user: serverSecrets.bitbucket_server_username,
pass: serverSecrets.bitbucket_server_password,
}
}

return this._requestJson(args)
}

async fetch({ server, user, repo }) {
const args = {
schema: bitbucketPullRequestSchema,
errorMessages: {
401: 'invalid credentials',
403: 'private repo',
404: 'not found',
},
}

if (server !== undefined) {
return this.fetchServer({ args, server, user, repo })
} else {
return this.fetchCloud({ args, user, repo })
}
}

static render({ prs }) {
Expand All @@ -31,8 +81,8 @@ function pullRequestClassGenerator(raw) {
}
}

async handle({ user, repo }) {
const data = await this.fetch({ user, repo })
async handle({ user, repo }, { server }) {
const data = await this.fetch({ server, user, repo })
return this.constructor.render({ prs: data.size })
}

Expand All @@ -47,8 +97,8 @@ function pullRequestClassGenerator(raw) {
static get route() {
return {
base: `bitbucket/${routePrefix}`,
format: '([^/]+)/([^/]+)',
capture: ['user', 'repo'],
pattern: `:user/:repo`,
queryParams: ['server'],
}
}

Expand All @@ -63,6 +113,16 @@ function pullRequestClassGenerator(raw) {
},
staticPreview: this.render({ prs: 22 }),
},
{
title: 'Bitbucket Server open pull requests',
namedParams: {
user: 'foo',
repo: 'bar',
},
queryParams: { server: 'https://bitbucket.mydomain.net' },
pattern: ':user/:repo',
staticPreview: this.render({ prs: 42 }),
},
]
}
}
Expand Down
33 changes: 33 additions & 0 deletions services/bitbucket/bitbucket-test-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const sinon = require('sinon')
const serverSecrets = require('../../lib/server-secrets')

const user = 'admin'
const pass = 'password'

function mockBitbucketCreds() {
serverSecrets['bitbucket_username'] = undefined
serverSecrets['bitbucket_password'] = undefined
sinon.stub(serverSecrets, 'bitbucket_username').value(user)
sinon.stub(serverSecrets, 'bitbucket_password').value(pass)
}

function mockBitbucketServerCreds() {
serverSecrets['bitbucket_server_username'] = undefined
serverSecrets['bitbucket_server_password'] = undefined
sinon.stub(serverSecrets, 'bitbucket_server_username').value(user)
sinon.stub(serverSecrets, 'bitbucket_server_password').value(pass)
}

function restore() {
sinon.restore()
}

module.exports = {
user,
pass,
mockBitbucketCreds,
mockBitbucketServerCreds,
restore,
}
120 changes: 120 additions & 0 deletions services/bitbucket/bitbucket.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

const Joi = require('joi')
const ServiceTester = require('../service-tester')
const {
mockBitbucketCreds,
mockBitbucketServerCreds,
restore,
user,
pass,
} = require('./bitbucket-test-helpers')
const {
isBuildStatus,
isMetric,
Expand Down Expand Up @@ -85,6 +92,119 @@ t.create('pr (private repo)')
.get('/pr/chris48s/example-private-repo.json')
.expectJSON({ name: 'pull requests', value: 'private repo' })

t.create('pr (server)')
.get('/pr/project/repo.json?server=https://bitbucket.mydomain.net')
.intercept(nock =>
nock('https://bitbucket.mydomain.net/rest/api/1.0/projects')
.get('/project/repos/repo/pull-requests')
.query({
state: 'OPEN',
limit: 100,
withProperties: false,
withAttributes: false,
})
.reply(200, { size: 42 })
)
.expectJSONTypes(
Joi.object().keys({
name: 'pull requests',
value: isMetricOpenIssues,
})
)

t.create('pr (server, invalid credentials)')
.get('/pr/project/repo.json?server=https://bitbucket.mydomain.net')
.intercept(nock =>
nock('https://bitbucket.mydomain.net/rest/api/1.0/projects')
.get('/project/repos/repo/pull-requests')
.query({
state: 'OPEN',
limit: 100,
withProperties: false,
withAttributes: false,
})
.reply(401)
)
.expectJSONTypes(
Joi.object().keys({
name: 'pull requests',
value: 'invalid credentials',
})
)

t.create('pr (server, private repo)')
.get('/pr/project/repo.json?server=https://bitbucket.mydomain.net')
.intercept(nock =>
nock('https://bitbucket.mydomain.net/rest/api/1.0/projects')
.get('/project/repos/repo/pull-requests')
.query({
state: 'OPEN',
limit: 100,
withProperties: false,
withAttributes: false,
})
.reply(403)
)
.expectJSONTypes(
Joi.object().keys({
name: 'pull requests',
value: 'private repo',
})
)

t.create('pr (server, not found)')
.get('/pr/project/repo.json?server=https://bitbucket.mydomain.net')
.intercept(nock =>
nock('https://bitbucket.mydomain.net/rest/api/1.0/projects')
.get('/project/repos/repo/pull-requests')
.query({
state: 'OPEN',
limit: 100,
withProperties: false,
withAttributes: false,
})
.reply(404)
)
.expectJSONTypes(
Joi.object().keys({
name: 'pull requests',
value: 'not found',
})
)

t.create('pr (auth)')
.before(mockBitbucketCreds)
.get('/pr/atlassian/python-bitbucket.json')
.intercept(nock =>
nock('https://bitbucket.org/api/2.0/repositories/')
.get(/.*/)
.basicAuth({ user, pass })
.reply(200, { size: 42 })
)
.finally(restore)
.expectJSONTypes(
Joi.object().keys({
name: 'pull requests',
value: isMetricOpenIssues,
})
)

t.create('pr (server, auth)')
.before(mockBitbucketServerCreds)
.get('/pr/project/repo.json?server=https://bitbucket.mydomain.net')
.intercept(nock =>
nock('https://bitbucket.mydomain.net/rest/api/1.0/projects')
.get(/.*/)
.basicAuth({ user, pass })
.reply(200, { size: 42 })
)
.finally(restore)
.expectJSONTypes(
Joi.object().keys({
name: 'pull requests',
value: isMetricOpenIssues,
})
)
// tests for Bitbucket Pipelines

function bitbucketApiResponse(status) {
Expand Down

0 comments on commit fc4ed93

Please sign in to comment.