Skip to content

Commit

Permalink
refactor(bitbucket): Switch to new http wrapper (#6392)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
zharinov and rarkins committed Jun 1, 2020
1 parent b4b6618 commit 01be649
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 91 deletions.
35 changes: 0 additions & 35 deletions lib/platform/bitbucket/bb-got-wrapper.ts

This file was deleted.

4 changes: 2 additions & 2 deletions lib/platform/bitbucket/comments.spec.ts
@@ -1,5 +1,5 @@
import * as httpMock from '../../../test/httpMock';
import { api } from './bb-got-wrapper';
import { setBaseUrl } from '../../util/http/bitbucket';
import * as comments from './comments';

const baseUrl = 'https://api.bitbucket.org';
Expand All @@ -13,7 +13,7 @@ describe('platform/comments', () => {
httpMock.reset();
httpMock.setup();

api.setBaseUrl(baseUrl);
setBaseUrl(baseUrl);
});

describe('ensureComment()', () => {
Expand Down
10 changes: 6 additions & 4 deletions lib/platform/bitbucket/comments.ts
@@ -1,8 +1,10 @@
import { logger } from '../../logger';
import { BitbucketHttp } from '../../util/http/bitbucket';
import { EnsureCommentConfig } from '../common';
import { api } from './bb-got-wrapper';
import { Config, accumulateValues } from './utils';

const bitbucketHttp = new BitbucketHttp();

interface Comment {
content: { raw: string };
id: number;
Expand Down Expand Up @@ -31,7 +33,7 @@ async function addComment(
prNo: number,
raw: string
): Promise<void> {
await api.post(
await bitbucketHttp.postJson(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}/comments`,
{
body: { content: { raw } },
Expand All @@ -45,7 +47,7 @@ async function editComment(
commentId: number,
raw: string
): Promise<void> {
await api.put(
await bitbucketHttp.putJson(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}/comments/${commentId}`,
{
body: { content: { raw } },
Expand All @@ -58,7 +60,7 @@ async function deleteComment(
prNo: number,
commentId: number
): Promise<void> {
await api.delete(
await bitbucketHttp.deleteJson(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}/comments/${commentId}`
);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/platform/bitbucket/index.spec.ts
Expand Up @@ -3,6 +3,7 @@ import * as httpMock from '../../../test/httpMock';
import { REPOSITORY_DISABLED } from '../../constants/error-messages';
import { logger as _logger } from '../../logger';
import { BranchStatus } from '../../types';
import { setBaseUrl } from '../../util/http/bitbucket';
import { Platform, RepoParams } from '../common';

const baseUrl = 'https://api.bitbucket.org';
Expand Down Expand Up @@ -83,6 +84,8 @@ describe('platform/bitbucket', () => {
username: 'abc',
password: '123',
});

setBaseUrl(baseUrl);
});

afterEach(async () => {
Expand Down
104 changes: 73 additions & 31 deletions lib/platform/bitbucket/index.ts
Expand Up @@ -11,6 +11,7 @@ import { PR_STATE_ALL, PR_STATE_OPEN } from '../../constants/pull-requests';
import { logger } from '../../logger';
import { BranchStatus } from '../../types';
import * as hostRules from '../../util/host-rules';
import { BitbucketHttp, setBaseUrl } from '../../util/http/bitbucket';
import { sanitize } from '../../util/sanitize';
import {
BranchStatusConfig,
Expand All @@ -31,10 +32,11 @@ import {
import GitStorage, { StatusResult } from '../git/storage';
import { smartTruncate } from '../utils/pr-body';
import { readOnlyIssueBody } from '../utils/read-only-issue-body';
import { api } from './bb-got-wrapper';
import * as comments from './comments';
import * as utils from './utils';

const bitbucketHttp = new BitbucketHttp();

const BITBUCKET_PROD_ENDPOINT = 'https://api.bitbucket.org/';

let config: utils.Config = {} as any;
Expand Down Expand Up @@ -88,7 +90,7 @@ export async function initRepo({
hostType: PLATFORM_TYPE_BITBUCKET,
url: endpoint,
});
api.setBaseUrl(endpoint);
setBaseUrl(endpoint);
config = {
repository,
username: opts.username,
Expand All @@ -97,7 +99,7 @@ export async function initRepo({
let info: utils.RepoInfo;
try {
info = utils.repoInfoTransformer(
(await api.get(`/2.0/repositories/${repository}`)).body
(await bitbucketHttp.getJson(`/2.0/repositories/${repository}`)).body
);

if (optimizeForDisabled) {
Expand All @@ -108,7 +110,7 @@ export async function initRepo({
let renovateConfig: RenovateConfig;
try {
renovateConfig = (
await api.get<RenovateConfig>(
await bitbucketHttp.getJson<RenovateConfig>(
`/2.0/repositories/${repository}/src/${info.mainbranch}/renovate.json`
)
).body;
Expand Down Expand Up @@ -272,7 +274,7 @@ export async function deleteBranch(
if (closePr) {
const pr = await findPr({ branchName, state: PR_STATE_OPEN });
if (pr) {
await api.post(
await bitbucketHttp.postJson(
`/2.0/repositories/${config.repository}/pullrequests/${pr.number}/decline`
);
}
Expand Down Expand Up @@ -311,7 +313,7 @@ export function getCommitMessages(): Promise<string[]> {

async function isPrConflicted(prNo: number): Promise<boolean> {
const diff = (
await api.get(
await bitbucketHttp.get(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}/diff`,
{ json: false } as any
)
Expand All @@ -320,10 +322,27 @@ async function isPrConflicted(prNo: number): Promise<boolean> {
return utils.isConflicted(parseDiff(diff));
}

interface PrResponse {
id: string;
state: string;
links: {
commits: {
href: string;
};
};
source: {
branch: {
name: string;
};
};
}

// Gets details for a PR
export async function getPr(prNo: number): Promise<Pr | null> {
const pr = (
await api.get(`/2.0/repositories/${config.repository}/pullrequests/${prNo}`)
await bitbucketHttp.getJson<PrResponse>(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}`
)
).body;

// istanbul ignore if
Expand All @@ -345,7 +364,9 @@ export async function getPr(prNo: number): Promise<Pr | null> {

// we only want the first two commits, because size tells us the overall number
const url = pr.links.commits.href + '?pagelen=2';
const { body } = await api.get<utils.PagedResult<Commit>>(url);
const { body } = await bitbucketHttp.getJson<utils.PagedResult<Commit>>(
url
);
const size = body.size || body.values.length;

// istanbul ignore if
Expand Down Expand Up @@ -379,11 +400,17 @@ export async function getPr(prNo: number): Promise<Pr | null> {
const escapeHash = (input: string): string =>
input ? input.replace(/#/g, '%23') : input;

interface BranchResponse {
target: {
hash: string;
};
}

// Return the commit SHA for a branch
async function getBranchCommit(branchName: string): Promise<string | null> {
try {
const branch = (
await api.get(
await bitbucketHttp.getJson<BranchResponse>(
`/2.0/repositories/${config.repository}/refs/branches/${escapeHash(
branchName
)}`
Expand Down Expand Up @@ -491,7 +518,7 @@ export async function setBranchStatus({
url,
};

await api.post(
await bitbucketHttp.postJson(
`/2.0/repositories/${config.repository}/commit/${sha}/statuses/build`,
{ body }
);
Expand All @@ -512,7 +539,7 @@ async function findOpenIssues(title: string): Promise<BbIssue[]> {
);
return (
(
await api.get(
await bitbucketHttp.getJson<{ values: BbIssue[] }>(
`/2.0/repositories/${config.repository}/issues?q=${filter}`
)
).body.values || /* istanbul ignore next */ []
Expand Down Expand Up @@ -543,7 +570,7 @@ export async function findIssue(title: string): Promise<Issue> {
}

async function closeIssue(issueNumber: number): Promise<void> {
await api.put(
await bitbucketHttp.putJson(
`/2.0/repositories/${config.repository}/issues/${issueNumber}`,
{
body: { state: 'closed' },
Expand Down Expand Up @@ -587,7 +614,7 @@ export async function ensureIssue({
const [issue] = issues;
if (String(issue.content.raw).trim() !== description.trim()) {
logger.debug('Issue updated');
await api.put(
await bitbucketHttp.putJson(
`/2.0/repositories/${config.repository}/issues/${issue.id}`,
{
body: {
Expand All @@ -602,12 +629,18 @@ export async function ensureIssue({
}
} else {
logger.info('Issue created');
await api.post(`/2.0/repositories/${config.repository}/issues`, {
body: {
title,
content: { raw: readOnlyIssueBody(description), markup: 'markdown' },
},
});
await bitbucketHttp.postJson(
`/2.0/repositories/${config.repository}/issues`,
{
body: {
title,
content: {
raw: readOnlyIssueBody(description),
markup: 'markdown',
},
},
}
);
return 'created';
}
} catch (err) /* istanbul ignore next */ {
Expand Down Expand Up @@ -641,7 +674,7 @@ export /* istanbul ignore next */ async function getIssueList(): Promise<
);
return (
(
await api.get(
await bitbucketHttp.getJson<{ values: Issue[] }>(
`/2.0/repositories/${config.repository}/issues?q=${filter}`
)
).body.values || /* istanbul ignore next */ []
Expand Down Expand Up @@ -687,9 +720,12 @@ export async function addReviewers(
reviewers: reviewers.map((username: string) => ({ username })),
};

await api.put(`/2.0/repositories/${config.repository}/pullrequests/${prId}`, {
body,
});
await bitbucketHttp.putJson(
`/2.0/repositories/${config.repository}/pullrequests/${prId}`,
{
body,
}
);
}

export /* istanbul ignore next */ function deleteLabel(): never {
Expand Down Expand Up @@ -737,7 +773,7 @@ export async function createPr({

if (config.bbUseDefaultReviewers) {
const reviewersResponse = (
await api.get<utils.PagedResult<Reviewer>>(
await bitbucketHttp.getJson<utils.PagedResult<Reviewer>>(
`/2.0/repositories/${config.repository}/default-reviewers`
)
).body;
Expand Down Expand Up @@ -765,9 +801,12 @@ export async function createPr({

try {
const prInfo = (
await api.post(`/2.0/repositories/${config.repository}/pullrequests`, {
body,
})
await bitbucketHttp.postJson<PrResponse>(
`/2.0/repositories/${config.repository}/pullrequests`,
{
body,
}
)
).body;
// TODO: fix types
const pr: Pr = {
Expand Down Expand Up @@ -800,9 +839,12 @@ export async function updatePr(
description: string
): Promise<void> {
logger.debug(`updatePr(${prNo}, ${title}, body)`);
await api.put(`/2.0/repositories/${config.repository}/pullrequests/${prNo}`, {
body: { title, description: sanitize(description) },
});
await bitbucketHttp.putJson(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}`,
{
body: { title, description: sanitize(description) },
}
);
}

export async function mergePr(
Expand All @@ -812,7 +854,7 @@ export async function mergePr(
logger.debug(`mergePr(${prNo}, ${branchName})`);

try {
await api.post(
await bitbucketHttp.postJson(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}/merge`,
{
body: {
Expand Down
6 changes: 5 additions & 1 deletion lib/platform/bitbucket/utils.spec.ts
@@ -1,15 +1,19 @@
import * as httpMock from '../../../test/httpMock';
import { setBaseUrl } from '../../util/http/bitbucket';
import * as utils from './utils';

const range = (count: number) => [...Array(count).keys()];

const baseUrl = 'https://api.bitbucket.org';

describe('accumulateValues()', () => {
it('paginates', async () => {
httpMock.reset();
httpMock.setup();
setBaseUrl(baseUrl);

httpMock
.scope('https://api.bitbucket.org')
.scope(baseUrl)
.get('/some-url?pagelen=10')
.reply(200, {
values: range(10),
Expand Down

0 comments on commit 01be649

Please sign in to comment.