Skip to content

Commit

Permalink
[bitbucket-server] add to Git Integrations UI
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTugarev committed Apr 11, 2022
1 parent 81a79af commit 1448afc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
21 changes: 19 additions & 2 deletions components/dashboard/src/settings/Integrations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import AlertBox from "../components/AlertBox";
import CheckBox from "../components/CheckBox";
import ConfirmationModal from "../components/ConfirmationModal";
import { ContextMenuEntry } from "../components/ContextMenu";
import InfoBox from "../components/InfoBox";
import { Item, ItemField, ItemFieldContextMenu, ItemFieldIcon, ItemsList } from "../components/ItemsList";
import Modal from "../components/Modal";
import { PageWithSubMenu } from "../components/PageWithSubMenu";
Expand Down Expand Up @@ -443,7 +444,7 @@ function GitIntegrations() {
<div className="flex items-start sm:justify-between mb-2">
<div>
<h3>Git Integrations</h3>
<h2>Manage Git integrations for GitLab or GitHub self-hosted instances.</h2>
<h2>Manage Git integrations for self-managed instances of GitLab, GitHub, or Bitbucket.</h2>
</div>
{providers.length !== 0 ? (
<div className="mt-3 flex mt-0">
Expand Down Expand Up @@ -710,6 +711,8 @@ export function GitIntegrationModal(
return "github.example.com";
case "GitLab":
return "gitlab.example.com";
case "BitbucketServer":
return "bitbucket.example.com";
default:
return "";
}
Expand Down Expand Up @@ -737,7 +740,7 @@ export function GitIntegrationModal(
<div className="flex flex-col">
<span className="text-gray-500">
{props.headerText ||
"Configure a Git integration with a GitLab or GitHub self-hosted instance."}
"Configure an integration with a self-managed instance of GitLab, GitHub, or Bitbucket."}
</span>
</div>

Expand All @@ -756,9 +759,23 @@ export function GitIntegrationModal(
>
<option value="GitHub">GitHub</option>
<option value="GitLab">GitLab</option>
<option value="BitbucketServer">Bitbucket Server</option>
</select>
</div>
)}
{mode === "new" && type === "BitbucketServer" && (
<InfoBox className="my-4 mx-auto">
Support for OAuth2 in Bitbucket Server was{" "}
<a
target="_blank"
href="https://confluence.atlassian.com/bitbucketserver/bitbucket-data-center-and-server-7-20-release-notes-1101934428.html"
rel="noopener noreferrer"
className="gp-link"
>
added in version 7.20
</a>
</InfoBox>
)}
<div className="flex flex-col space-y-2">
<label htmlFor="hostName" className="font-medium">
Provider Host Name
Expand Down
14 changes: 13 additions & 1 deletion components/server/src/auth/auth-provider-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Config } from "../config";
import { v4 as uuidv4 } from "uuid";
import { oauthUrls as githubUrls } from "../github/github-urls";
import { oauthUrls as gitlabUrls } from "../gitlab/gitlab-urls";
import { oauthUrls as bbsUrls } from "../bitbucket-server/bitbucket-server-urls";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";

@injectable()
Expand Down Expand Up @@ -110,7 +111,18 @@ export class AuthProviderService {
}
protected initializeNewProvider(newEntry: AuthProviderEntry.NewEntry): AuthProviderEntry {
const { host, type, clientId, clientSecret } = newEntry;
const urls = type === "GitHub" ? githubUrls(host) : type === "GitLab" ? gitlabUrls(host) : undefined;
let urls;
switch (type) {
case "GitHub":
urls = githubUrls(host);
break;
case "GitLab":
urls = gitlabUrls(host);
break;
case "BitbucketServer":
urls = bbsUrls(host);
break;
}
if (!urls) {
throw new Error("Unexpected service type.");
}
Expand Down
5 changes: 4 additions & 1 deletion components/server/src/auth/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ export class Authenticator {

// prepare session
await AuthFlow.attach(req.session, { host, returnTo, overrideScopes: override });
let wantedScopes = scopes.split(",");
let wantedScopes = scopes
.split(",")
.map((s) => s.trim())
.filter((s) => s.length > 0);
if (wantedScopes.length === 0) {
if (authProvider.info.requirements) {
wantedScopes = authProvider.info.requirements.default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class BitbucketServerAuthProvider extends GenericAuthProvider {
...oauth,
authorizationUrl: oauth.authorizationUrl || `https://${this.params.host}/rest/oauth2/latest/authorize`,
tokenUrl: oauth.tokenUrl || `https://${this.params.host}/rest/oauth2/latest/token`,
settingsUrl: oauth.settingsUrl || `https://${this.params.host}/plugins/servlet/oauth/users/access-tokens/`,
settingsUrl: oauth.settingsUrl,
scope: BitbucketServerOAuthScopes.ALL.join(scopeSeparator),
scopeSeparator,
};
Expand Down Expand Up @@ -75,14 +75,4 @@ export class BitbucketServerAuthProvider extends GenericAuthProvider {
throw error;
}
};

protected normalizeScopes(scopes: string[]) {
const set = new Set(scopes);
for (const item of set.values()) {
if (!BitbucketServerOAuthScopes.Requirements.DEFAULT.includes(item)) {
set.delete(item);
}
}
return Array.from(set).sort();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export namespace BitbucketServerOAuthScopes {
/** View projects and repositories that are publicly accessible, including pulling code and cloning repositories. */
export const PUBLIC_REPOS = "PUBLIC_REPOS";
/** View projects and repositories the user account can view, including pulling code, cloning, and forking repositories. Create and comment on pull requests. */
export const REPOSITORY_READ = "REPO_READ";
export const REPO_READ = "REPO_READ";
/** Push over https, fork repo */
export const REPOSITORY_WRITE = "REPO_WRITE";
export const REPO_WRITE = "REPO_WRITE";

export const REPO_ADMIN = "REPO_ADMIN";
export const PROJECT_ADMIN = "PROJECT_ADMIN";

export const ALL = [PUBLIC_REPOS, REPOSITORY_READ, REPOSITORY_WRITE, REPO_ADMIN, PROJECT_ADMIN];
export const ALL = [PUBLIC_REPOS, REPO_READ, REPO_WRITE, REPO_ADMIN, PROJECT_ADMIN];

export const Requirements = {
/**
Expand Down
15 changes: 15 additions & 0 deletions components/server/src/bitbucket-server/bitbucket-server-urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

// cf. https://confluence.atlassian.com/bitbucketserver/bitbucket-oauth-2-0-provider-api-1108483661.html
//
export function oauthUrls(host: string) {
return {
authorizationUrl: `https://${host}/rest/oauth2/latest/authorize`,
tokenUrl: `https://${host}/rest/oauth2/latest/token`,
settingsUrl: ``,
};
}

0 comments on commit 1448afc

Please sign in to comment.