Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for bitbucket #248

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ components:
enum:
- "gitlab"
- "github"
- "bitbucket"
example: "gitlab"
ClientId:
description: |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated by datamodel-codegen:
# filename: api.spec.yaml
# timestamp: 2024-06-03T07:31:23+00:00
# timestamp: 2024-06-10T08:17:29+00:00

from __future__ import annotations

Expand All @@ -14,6 +14,7 @@
class ProviderKind(Enum):
gitlab = "gitlab"
github = "github"
bitbucket = "bitbucket"


class ConnectionStatus(Enum):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,35 @@ def api_validate_account_response(self, response: Response) -> models.ConnectedA
return external_models.GitHubConnectedAccount.model_validate(response.json()).to_connected_account()


class BitBucketAdapter(ProviderAdapter):
"""Adapter for BitBucket OAuth2 clients."""

@property
def authorization_url(self) -> str:
"""The authorization URL for the OAuth2 protocol."""
return urljoin(self.client_url, "site/oauth2/authorize")

@property
def token_endpoint_url(self) -> str:
"""The token endpoint URL for the OAuth2 protocol."""
return urljoin(self.client_url, "site/oauth2/access_token")

@property
def api_url(self) -> str:
"""The URL used for API calls on the Resource Server."""
url = urlparse(self.client_url)
url = url._replace(netloc=f"api.{url.netloc}")
return urljoin(urlunparse(url), "2.0")

def api_validate_account_response(self, response: Response) -> models.ConnectedAccount:
"""Validates and returns the connected account response from the Resource Server."""
raise NotImplementedError()


_adapter_map: dict[ProviderKind, type[ProviderAdapter]] = {
ProviderKind.gitlab: GitLabAdapter,
ProviderKind.github: GitHubAdapter,
ProviderKind.bitbucket: BitBucketAdapter,
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""add bitbucket support

Revision ID: 33561ece81e5
Revises: c0631477aea4
Create Date: 2024-06-10 08:30:48.029894

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "33561ece81e5"
down_revision = "c0631477aea4"
branch_labels = None
depends_on = None

# Update the "ProviderKind" enum
old_kinds = ["gitlab", "github"]
new_kinds = list(old_kinds) + ["bitbucket"]

old_type = sa.Enum(*old_kinds, name="providerkind")
new_type = sa.Enum(*new_kinds, name="providerkind")
temp_type = sa.Enum(*new_kinds, name="providerkind_tmp")
temp_type_downgrade = sa.Enum(*old_kinds, name="providerkind_tmp")


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
temp_type.create(op.get_bind())
op.alter_column(
"oauth2_clients",
"kind",
type_=temp_type,
postgresql_using="kind::text::providerkind_tmp",
schema="connected_services",
)
op.execute("DROP TYPE providerkind")
new_type.create(op.get_bind())
op.alter_column(
"oauth2_clients",
"kind",
type_=new_type,
postgresql_using="kind::text::providerkind",
schema="connected_services",
)
op.execute("DROP TYPE providerkind_tmp")
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###

# Update rows where kind = "bitbucket"
table = sa.sql.table("oauth2_clients", sa.Column("kind", new_type), schema="connected_services")
op.execute(table.update().where(table.columns.kind == "bitbucket").values(kind="gitlab"))

temp_type_downgrade.create(op.get_bind())
op.alter_column(
"oauth2_clients",
"kind",
type_=temp_type_downgrade,
postgresql_using="kind::text::providerkind_tmp",
schema="connected_services",
)
op.execute("DROP TYPE providerkind")
old_type.create(op.get_bind())
op.alter_column(
"oauth2_clients",
"kind",
type_=old_type,
postgresql_using="kind::text::providerkind",
schema="connected_services",
)
op.execute("DROP TYPE providerkind_tmp")
pass
# ### end Alembic commands ###
Loading