From 722aae076de56defe7950044de45145896251e82 Mon Sep 17 00:00:00 2001 From: Bryan Wieger Date: Wed, 12 Feb 2025 21:12:39 -0800 Subject: [PATCH] first test for server pull requests --- .../bitbucket-server/pullRequests.test.ts | 196 ++++++++++++++++++ .../bitbucket-server/pullRequests.ts | 4 +- src/bitbucket/httpClient.ts | 12 ++ 3 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 src/bitbucket/bitbucket-server/pullRequests.test.ts diff --git a/src/bitbucket/bitbucket-server/pullRequests.test.ts b/src/bitbucket/bitbucket-server/pullRequests.test.ts new file mode 100644 index 00000000..3c62d7b9 --- /dev/null +++ b/src/bitbucket/bitbucket-server/pullRequests.test.ts @@ -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; + 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; + + 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: 'admin@admin.com', + 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: '

some words
\n
\nI can change the description
\n
\nChange me

\n', +}; diff --git a/src/bitbucket/bitbucket-server/pullRequests.ts b/src/bitbucket/bitbucket-server/pullRequests.ts index 344b812c..9fc94464 100644 --- a/src/bitbucket/bitbucket-server/pullRequests.ts +++ b/src/bitbucket/bitbucket-server/pullRequests.ts @@ -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, @@ -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 { const site = workspaceRepo.mainSiteRemote.site; diff --git a/src/bitbucket/httpClient.ts b/src/bitbucket/httpClient.ts index dfb9beff..1f2ee56f 100644 --- a/src/bitbucket/httpClient.ts +++ b/src/bitbucket/httpClient.ts @@ -11,6 +11,18 @@ export interface RequestRange { end: number; } +export interface IHTTPClient { + getUrl(url: string, cancelToken?: CancelToken): Promise; + get(urlSlug: string, queryParams?: any, cancelToken?: CancelToken): Promise; + getRaw(urlSlug: string, queryParams?: any): Promise; + getArrayBuffer(urlSlug: string, queryParams?: any): Promise; + getOctetStream(urlSlug: string, range?: RequestRange, queryParams?: any): Promise; + post(urlSlug: string, body: any, queryParams?: any): Promise; + put(urlSlug: string, body: any, queryParams?: any): Promise; + delete(urlSlug: string, body: any, queryParams?: any): Promise; + generateUrl(urlSlug: string, queryParams?: any): string; +} + export class HTTPClient { private transport: AxiosInstance;