101 lines
3.8 KiB
Python
101 lines
3.8 KiB
Python
#
|
|
# Copyright (c) 2021 Airbyte, Inc., all rights reserved.
|
|
#
|
|
|
|
|
|
from typing import Any, List, Mapping, Tuple
|
|
|
|
from airbyte_cdk.models import SyncMode
|
|
from airbyte_cdk.sources import AbstractSource
|
|
from airbyte_cdk.sources.streams import Stream
|
|
from airbyte_cdk.sources.streams.http.auth import TokenAuthenticator
|
|
|
|
from .streams import (
|
|
Branches,
|
|
Commits,
|
|
EpicIssues,
|
|
Epics,
|
|
GitlabStream,
|
|
GroupLabels,
|
|
GroupMembers,
|
|
GroupMilestones,
|
|
GroupProjects,
|
|
Groups,
|
|
Issues,
|
|
Jobs,
|
|
MergeRequestCommits,
|
|
MergeRequests,
|
|
Pipelines,
|
|
PipelinesExtended,
|
|
ProjectLabels,
|
|
ProjectMembers,
|
|
ProjectMilestones,
|
|
Projects,
|
|
Releases,
|
|
Tags,
|
|
Users,
|
|
)
|
|
|
|
|
|
class SourceGitlab(AbstractSource):
|
|
def _generate_main_streams(self, config: Mapping[str, Any]) -> Tuple[GitlabStream, GitlabStream]:
|
|
gids = list(filter(None, config["groups"].split(" ")))
|
|
pids = list(filter(None, config["projects"].split(" ")))
|
|
|
|
if not pids and not gids:
|
|
raise Exception("Either groups or projects need to be provided for connect to Gitlab API")
|
|
|
|
auth = TokenAuthenticator(token=config["private_token"])
|
|
auth_params = dict(authenticator=auth, api_url=config["api_url"])
|
|
groups = Groups(group_ids=gids, **auth_params)
|
|
if gids:
|
|
projects = GroupProjects(project_ids=pids, parent_stream=groups, **auth_params)
|
|
else:
|
|
projects = Projects(project_ids=pids, **auth_params)
|
|
|
|
return groups, projects
|
|
|
|
def check_connection(self, logger, config) -> Tuple[bool, any]:
|
|
try:
|
|
groups, projects = self._generate_main_streams(config)
|
|
for stream in projects.stream_slices(sync_mode=SyncMode.full_refresh):
|
|
next(projects.read_records(sync_mode=SyncMode.full_refresh, stream_slice=stream))
|
|
return True, None
|
|
except Exception as error:
|
|
return False, f"Unable to connect to Gitlab API with the provided credentials - {repr(error)}"
|
|
|
|
def streams(self, config: Mapping[str, Any]) -> List[Stream]:
|
|
auth = TokenAuthenticator(token=config["private_token"])
|
|
auth_params = dict(authenticator=auth, api_url=config["api_url"])
|
|
|
|
groups, projects = self._generate_main_streams(config)
|
|
pipelines = Pipelines(parent_stream=projects, start_date=config["start_date"], **auth_params)
|
|
merge_requests = MergeRequests(parent_stream=projects, start_date=config["start_date"], **auth_params)
|
|
epics = Epics(parent_stream=groups, **auth_params)
|
|
|
|
streams = [
|
|
groups,
|
|
projects,
|
|
Branches(parent_stream=projects, repository_part=True, **auth_params),
|
|
Commits(parent_stream=projects, repository_part=True, start_date=config["start_date"], **auth_params),
|
|
epics,
|
|
EpicIssues(parent_stream=epics, **auth_params),
|
|
Issues(parent_stream=projects, start_date=config["start_date"], **auth_params),
|
|
Jobs(parent_stream=pipelines, **auth_params),
|
|
ProjectMilestones(parent_stream=projects, **auth_params),
|
|
GroupMilestones(parent_stream=groups, **auth_params),
|
|
ProjectMembers(parent_stream=projects, **auth_params),
|
|
GroupMembers(parent_stream=groups, **auth_params),
|
|
ProjectLabels(parent_stream=projects, **auth_params),
|
|
GroupLabels(parent_stream=groups, **auth_params),
|
|
merge_requests,
|
|
MergeRequestCommits(parent_stream=merge_requests, **auth_params),
|
|
Releases(parent_stream=projects, **auth_params),
|
|
Tags(parent_stream=projects, repository_part=True, **auth_params),
|
|
pipelines,
|
|
PipelinesExtended(parent_stream=pipelines, **auth_params),
|
|
Users(parent_stream=projects, **auth_params),
|
|
]
|
|
|
|
return streams
|