Skip to content

Commit

Permalink
first test for server pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
bwieger-atlassian-com committed Feb 13, 2025
1 parent 7ecc8da commit 722aae0
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 2 deletions.
196 changes: 196 additions & 0 deletions src/bitbucket/bitbucket-server/pullRequests.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import { ServerPullRequestApi } from './pullRequests';
import { BitbucketSite } from '../model';
import { IHTTPClient } from '../httpClient';

describe('ServerPullRequestApi', () => {
let api: ServerPullRequestApi;
let mockClient: jest.Mocked<IHTTPClient>;
let site: BitbucketSite;

beforeEach(() => {
mockClient = {
get: jest.fn(),
post: jest.fn(),
put: jest.fn(),
delete: jest.fn(),
getRaw: jest.fn(),
} as unknown as jest.Mocked<IHTTPClient>;

api = new ServerPullRequestApi(mockClient);
site = { ownerSlug: 'owner', repoSlug: 'repo', details: { userId: 'user' } } as BitbucketSite;
});

it('should get with a v8 if no 404s', async () => {
mockClient.get.mockImplementation((url) => {
if (
url.includes('/rest/api/1.0/projects/owner/repos/repo/pull-requests/PR-1/blocker-comments?count=true')
) {
return Promise.resolve(getTaskCountDataV8);
}
if (url.includes('/rest/api/1.0/projects/owner/repos/repo/pull-requests/PR-1')) {
return Promise.resolve({ data: getPullRequestData });
}

return Promise.reject(new Error('Not Found'));
});

await api.get(site, 'PR-1');

expect(mockClient.get).lastCalledWith(
'/rest/api/1.0/projects/owner/repos/repo/pull-requests/PR-1/blocker-comments?count=true',
);
});
});

const getTaskCountDataV8 = {
data: {
OPEN: 5,
},
headers: {},
};

const getPullRequestData = {
id: 1,
version: 4,
title: 'a change',
description: 'some words\\\n\\\nI can change the description\\\n\\\nChange me',
state: 'OPEN',
open: true,
closed: false,
draft: false,
createdDate: 1739222096918,
updatedDate: 1739329991386,
fromRef: {
id: 'refs/heads/testing-3',
displayId: 'testing-3',
latestCommit: 'b70a6199c92a978bf9a4862e1b96c9301400cbfa',
type: 'BRANCH',
repository: {
slug: 'testing-axon',
id: 1,
name: 'testing-axon',
hierarchyId: '451f980d695b670538b0',
scmId: 'git',
state: 'AVAILABLE',
statusMessage: 'Available',
forkable: true,
project: {
key: 'AX',
id: 1,
name: 'axon-test',
public: false,
type: 'NORMAL',
links: {
self: [
{
href: 'https://instenv-452647-24mv.instenv.internal.atlassian.com/projects/AX',
},
],
},
avatarUrl: '/projects/AX/avatar.png?s=64&v=1735870958971',
},
public: false,
archived: false,
links: {
clone: [
{
href: 'https://instenv-452647-24mv.instenv.internal.atlassian.com/scm/ax/testing-axon.git',
name: 'http',
},
{
href: 'ssh://git@instenv-452647-24mv-alt-3a61ca65736a0f8b.elb.us-east-1.amazonaws.com:7999/ax/testing-axon.git',
name: 'ssh',
},
],
self: [
{
href: 'https://instenv-452647-24mv.instenv.internal.atlassian.com/projects/AX/repos/testing-axon/browse',
},
],
},
},
},
toRef: {
id: 'refs/heads/main',
displayId: 'main',
latestCommit: 'de320695ae3092a489bcb31da5a8c8bb75d833a1',
type: 'BRANCH',
repository: {
slug: 'testing-axon',
id: 1,
name: 'testing-axon',
hierarchyId: '451f980d695b670538b0',
scmId: 'git',
state: 'AVAILABLE',
statusMessage: 'Available',
forkable: true,
project: {
key: 'AX',
id: 1,
name: 'axon-test',
public: false,
type: 'NORMAL',
links: {
self: [
{
href: 'https://instenv-452647-24mv.instenv.internal.atlassian.com/projects/AX',
},
],
},
avatarUrl: '/projects/AX/avatar.png?s=64&v=1735870958971',
},
public: false,
archived: false,
links: {
clone: [
{
href: 'https://instenv-452647-24mv.instenv.internal.atlassian.com/scm/ax/testing-axon.git',
name: 'http',
},
{
href: 'ssh://git@instenv-452647-24mv-alt-3a61ca65736a0f8b.elb.us-east-1.amazonaws.com:7999/ax/testing-axon.git',
name: 'ssh',
},
],
self: [
{
href: 'https://instenv-452647-24mv.instenv.internal.atlassian.com/projects/AX/repos/testing-axon/browse',
},
],
},
},
},
locked: false,
author: {
user: {
name: 'admin',
emailAddress: '[email protected]',
active: true,
displayName: 'Ansible Admin',
id: 2,
slug: 'admin',
type: 'NORMAL',
links: {
self: [
{
href: 'https://instenv-452647-24mv.instenv.internal.atlassian.com/users/admin',
},
],
},
avatarUrl: 'https://secure.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028.jpg?s=64&d=mm',
},
role: 'AUTHOR',
approved: false,
status: 'UNAPPROVED',
},
reviewers: [],
participants: [],
links: {
self: [
{
href: 'https://instenv-452647-24mv.instenv.internal.atlassian.com/projects/AX/repos/testing-axon/pull-requests/1',
},
],
},
descriptionAsHtml: '<p>some words<br />\n<br />\nI can change the description<br />\n<br />\nChange me</p>\n',
};
4 changes: 2 additions & 2 deletions src/bitbucket/bitbucket-server/pullRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CacheMap } from '../../util/cachemap';
import { Time } from '../../util/time';
import { getFileNameFromPaths } from '../../views/pullrequest/diffViewHelper';
import { clientForSite } from '../bbUtils';
import { HTTPClient } from '../httpClient';
import { IHTTPClient } from '../httpClient';
import {
ApprovalStatus,
BitbucketSite,
Expand All @@ -32,7 +32,7 @@ export class ServerPullRequestApi implements PullRequestApi {
private defaultReviewersCache: CacheMap = new CacheMap();
private fileContentCache: CacheMap = new CacheMap();

constructor(private client: HTTPClient) {}
constructor(private client: IHTTPClient) {}

async getList(workspaceRepo: WorkspaceRepo, queryParams?: any): Promise<PaginatedPullRequests> {
const site = workspaceRepo.mainSiteRemote.site;
Expand Down
12 changes: 12 additions & 0 deletions src/bitbucket/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export interface RequestRange {
end: number;
}

export interface IHTTPClient {
getUrl(url: string, cancelToken?: CancelToken): Promise<any>;
get(urlSlug: string, queryParams?: any, cancelToken?: CancelToken): Promise<any>;
getRaw(urlSlug: string, queryParams?: any): Promise<any>;
getArrayBuffer(urlSlug: string, queryParams?: any): Promise<any>;
getOctetStream(urlSlug: string, range?: RequestRange, queryParams?: any): Promise<any>;
post(urlSlug: string, body: any, queryParams?: any): Promise<any>;
put(urlSlug: string, body: any, queryParams?: any): Promise<any>;
delete(urlSlug: string, body: any, queryParams?: any): Promise<any>;
generateUrl(urlSlug: string, queryParams?: any): string;
}

export class HTTPClient {
private transport: AxiosInstance;

Expand Down

0 comments on commit 722aae0

Please sign in to comment.