Skip to content

Commit

Permalink
Launchpad: add Bitbucket Server support (#44)
Browse files Browse the repository at this point in the history
* Add support for Bitbucket Server to Launchpad

* Update provider-apis package to use PR converter util function
  • Loading branch information
jdgarcia authored Jun 5, 2024
1 parent a847d42 commit 70a8733
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"watch:firefox": "yarn run set-manifest:firefox:force && yarn watch"
},
"dependencies": {
"@gitkraken/provider-apis": "0.22.7",
"@gitkraken/provider-apis": "0.22.8",
"@tanstack/query-async-storage-persister": "5.32.0",
"@tanstack/react-query": "5.32.0",
"@tanstack/react-query-persist-client": "5.32.0",
Expand Down
1 change: 1 addition & 0 deletions src/popup/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const useFocusViewConnectedProviders = (userId: string) => {
connection.provider === 'gitlab' ||
connection.provider === 'gitlabSelfHosted' ||
connection.provider === 'bitbucket' ||
connection.provider === 'bitbucketServer' ||
connection.provider === 'azure',
)
.map(connection => connection.provider as FocusViewSupportedProvider);
Expand Down
35 changes: 35 additions & 0 deletions src/providers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { Account } from '@gitkraken/provider-apis';
import {
AzureDevOps,
Bitbucket,
BitbucketServerUtils,
EntityIdentifierProviderType,
EntityIdentifierUtils,
EntityType,
GitHub,
GitLab,
} from '@gitkraken/provider-apis';
import { fetchProviderToken } from './gkApi';
import { GKDotDevUrl } from './shared';
import type { FocusViewData, FocusViewSupportedProvider, Provider, ProviderToken } from './types';

export const ProviderMeta: Record<FocusViewSupportedProvider, { name: string; iconSrc: string }> = {
Expand All @@ -16,6 +19,7 @@ export const ProviderMeta: Record<FocusViewSupportedProvider, { name: string; ic
gitlab: { name: 'GitLab', iconSrc: 'img/gitlab-color.svg' },
gitlabSelfHosted: { name: 'GitLab Self-Managed', iconSrc: 'img/gitlab-color.svg' },
bitbucket: { name: 'Bitbucket', iconSrc: 'img/bitbucket-color.svg' },
bitbucketServer: { name: 'Bitbucket Server', iconSrc: 'img/bitbucket-color.svg' },
azure: { name: 'Azure DevOps', iconSrc: 'img/azuredevops-color.svg' },
};

Expand Down Expand Up @@ -57,6 +61,7 @@ const fetchGitLabFocusViewData = async (token: ProviderToken) => {

const { data: pullRequests } = await gitlab.getPullRequestsAssociatedWithUser({
username: providerUser.username,
includeFromArchivedRepos: false,
});

return { providerUser: providerUser, pullRequests: pullRequests.map(pr => ({ ...pr, uuid: '' })) };
Expand All @@ -74,6 +79,34 @@ const fetchBitbucketFocusViewData = async (token: ProviderToken) => {
return { providerUser: providerUser, pullRequests: pullRequests.map(pr => ({ ...pr, uuid: '' })) };
};

const fetchBitbucketServerFocusViewData = async (token: ProviderToken) => {
// Bitbucket Server does not have the CORS header set to be able to make requests from the browser,
// so we proxy the request through the API.
const res = await fetch(`${GKDotDevUrl}/api/provider/bitbucket-server/proxy`, {
headers: {
Authorization: `Bearer ${token.accessToken}`,
XDestination: `${token.domain}/rest/api/latest/dashboard/pull-requests`,
},
});

if (!res.ok) {
throw new Error('Failed to fetch Bitbucket Server pull requests');
}

const data = await res.json();

return {
providerUser: data.user as Account,
pullRequests: (data.body.values as any[]).map(pullRequest => ({
...BitbucketServerUtils.restApiPullRequestToCommonPullRequest(pullRequest),
// Bitbucket Server PR ids are just the number, they are not unique across repos, so instead
// we use the PR url as the id.
id: pullRequest.links.self[0].href,
uuid: '',
})),
};
};

const fetchAzureFocusViewData = async (token: ProviderToken) => {
const azureDevOps = new AzureDevOps({ token: token.accessToken });

Expand Down Expand Up @@ -109,6 +142,8 @@ export const fetchFocusViewData = async (provider: FocusViewSupportedProvider):
return fetchGitLabFocusViewData(providerToken);
case 'bitbucket':
return fetchBitbucketFocusViewData(providerToken);
case 'bitbucketServer':
return fetchBitbucketServerFocusViewData(providerToken);
case 'azure':
return fetchAzureFocusViewData(providerToken);
default:
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ export type Provider =
| 'jira'
| 'trello'
| 'githubEnterprise'
| 'gitlabSelfHosted';
| 'gitlabSelfHosted'
| 'bitbucketServer';

export type FocusViewSupportedProvider =
| 'github'
| 'githubEnterprise'
| 'gitlab'
| 'gitlabSelfHosted'
| 'bitbucket'
| 'bitbucketServer'
| 'azure';

export type PullRequestDraftCounts = Record<string, { count: number } | undefined>;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.44.0.tgz#961a5903c74139390478bdc808bcde3fc45ab7af"
integrity sha512-Ag+9YM4ocKQx9AarydN0KY2j0ErMHNIocPDrVo8zAE44xLTjEtz81OdR68/cydGtk6m6jDb5Za3r2useMzYmSw==

"@gitkraken/provider-apis@0.22.7":
version "0.22.7"
resolved "https://registry.npmjs.org/@gitkraken/provider-apis/-/provider-apis-0.22.7.tgz#f217e1d275b578c33a38ce03564a10ee6b6b3e2c"
integrity sha512-twLjzIahStD4FkI10UxxikLLocE0r9IkX2qPjZiBfhyZbGKVdnFEdYdbZaeKOxTt87T6LyTPhdGnRLMrZfCWpA==
"@gitkraken/provider-apis@0.22.8":
version "0.22.8"
resolved "https://registry.npmjs.org/@gitkraken/provider-apis/-/provider-apis-0.22.8.tgz#5e70e00b94a386275b141fb42b055c7124adce15"
integrity sha512-i2/+gC1j940zjS8rn124HYPLXa7O8t3i5PbLNNB6Xp+wFCTmLeILkcuIFAV1xH1291KoqaWY0HJMZlsmpdo7ww==
dependencies:
js-base64 "3.7.5"
node-fetch "2.7.0"
Expand Down

0 comments on commit 70a8733

Please sign in to comment.