Skip to content

Commit

Permalink
[Integration][Gitlab] Support both private and oauth token authentica…
Browse files Browse the repository at this point in the history
…tions (#1199)

# Description

What:
 - Added support for OAuth2.0 token
 - Added OAuth installation specification for Port

## Type of change

Please leave one option from the following and delete the rest:

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] New Integration (non-breaking change which adds a new integration)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Non-breaking change (fix of existing functionality that will not
change current behavior)
- [ ] Documentation (added/updated documentation)

<h4> All tests should be run against the port production
environment(using a testing org). </h4>

### Core testing checklist

- [ ] Integration able to create all default resources from scratch
- [ ] Resync finishes successfully
- [ ] Resync able to create entities
- [ ] Resync able to update entities
- [ ] Resync able to detect and delete entities
- [ ] Scheduled resync able to abort existing resync and start a new one
- [ ] Tested with at least 2 integrations from scratch
- [ ] Tested with Kafka and Polling event listeners
- [ ] Tested deletion of entities that don't pass the selector


### Integration testing checklist

- [x] Integration able to create all default resources from scratch
- [x] Resync able to create entities
- [x] Resync able to update entities
- [x] Resync able to detect and delete entities
- [x] Resync finishes successfully
- [ ] If new resource kind is added or updated in the integration, add
example raw data, mapping and expected result to the `examples` folder
in the integration directory.
- [ ] If resource kind is updated, run the integration with the example
data and check if the expected result is achieved
- [ ] If new resource kind is added or updated, validate that
live-events for that resource are working as expected
- [ ] Docs PR link [here](#)

### Preflight checklist

- [ ] Handled rate limiting
- [ ] Handled pagination
- [ ] Implemented the code in async
- [ ] Support Multi account

## Screenshots

Include screenshots from your environment showing how the resources of
the integration will look.

## API Documentation

Provide links to the API documentation used for this integration.

---------

Co-authored-by: Tom Tankilevitch <[email protected]>
  • Loading branch information
omby8888 and Tankilevitch authored Dec 11, 2024
1 parent f2ea850 commit f5a3928
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
12 changes: 12 additions & 0 deletions integrations/gitlab/.port/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,17 @@ configurations:
\"subgroup_events\",
\"confidential_issues_events\"]"
sensitive: true
saas:
enabled: true
oauthConfiguration:
requiredSecrets:
- name: tokenMapping
value: '{(.oauthData.accessToken): ["**"]} | @json'
description: '"Token mapping for Gitlab OAuth2 integration"'
valuesOverride:
integrationSpec:
gitlabHost: '"https://gitlab.com"'
appSpec:
minimumScheduledResyncInterval: '2h'
deploymentMethodOverride:
- type: helm
9 changes: 9 additions & 0 deletions integrations/gitlab/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

<!-- towncrier release notes start -->

0.2.0 (2024-12-11)
====================

### Improvements

- Added support for OAuth2.0 token
- Added OAuth installation specification for Port


0.1.147 (2024-12-10)
====================

Expand Down
7 changes: 3 additions & 4 deletions integrations/gitlab/gitlab_integration/events/setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import Type, List

from gitlab import Gitlab

from loguru import logger

from gitlab_integration.events.event_handler import EventHandler, SystemEventHandler
Expand All @@ -26,6 +24,7 @@
GitlabEventListenerConflict,
GitlabIllegalEventName,
)
from gitlab_integration.utils import generate_gitlab_client

event_handler = EventHandler()
system_event_handler = SystemEventHandler()
Expand Down Expand Up @@ -161,7 +160,7 @@ async def create_webhooks_by_client(
groups_hooks_events_override: dict[str, WebhookGroupConfig] | None,
group_mapping: list[str],
) -> tuple[GitlabService, list[str]]:
gitlab_client = Gitlab(gitlab_host, token)
gitlab_client = generate_gitlab_client(gitlab_host, token)
gitlab_service = GitlabService(gitlab_client, app_host, group_mapping)

groups_for_webhooks = await gitlab_service.get_filtered_groups_for_webhooks(
Expand Down Expand Up @@ -203,7 +202,7 @@ async def setup_application(
logger.info("Using system hook")
validate_use_system_hook(token_mapping)
token, group_mapping = list(token_mapping.items())[0]
gitlab_client = Gitlab(gitlab_host, token)
gitlab_client = generate_gitlab_client(gitlab_host, token)
gitlab_service = GitlabService(gitlab_client, app_host, group_mapping)
setup_system_listeners([gitlab_service])

Expand Down
24 changes: 19 additions & 5 deletions integrations/gitlab/gitlab_integration/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List

import gitlab
from gitlab import Gitlab
from gitlab_integration.gitlab_service import GitlabService
from loguru import logger
Expand All @@ -10,6 +11,23 @@
RETRY_TRANSIENT_ERRORS = True


def generate_gitlab_client(host: str, token: str) -> Gitlab:
try:
gitlab_client = Gitlab(
host, token, retry_transient_errors=RETRY_TRANSIENT_ERRORS
)
gitlab_client.auth()
logger.info("Successfully authenticated using the provided private token")
except gitlab.exceptions.GitlabAuthenticationError:
gitlab_client = Gitlab(
host, oauth_token=token, retry_transient_errors=RETRY_TRANSIENT_ERRORS
)
gitlab_client.auth()
logger.info("Successfully authenticated using the provided OAuth2.0 token")

return gitlab_client


def get_all_services() -> List[GitlabService]:
logic_settings = ocean.integration_config
all_tokens_services = []
Expand All @@ -18,11 +36,7 @@ def get_all_services() -> List[GitlabService]:
f"Creating gitlab clients for {len(logic_settings['token_mapping'])} tokens"
)
for token, group_mapping in logic_settings["token_mapping"].items():
gitlab_client = Gitlab(
logic_settings["gitlab_host"],
token,
retry_transient_errors=RETRY_TRANSIENT_ERRORS,
)
gitlab_client = generate_gitlab_client(logic_settings["gitlab_host"], token)
gitlab_service = GitlabService(
gitlab_client, logic_settings["app_host"], group_mapping
)
Expand Down
2 changes: 1 addition & 1 deletion integrations/gitlab/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "gitlab"
version = "0.1.147"
version = "0.2.0"
description = "Gitlab integration for Port using Port-Ocean Framework"
authors = ["Yair Siman-Tov <[email protected]>"]

Expand Down

0 comments on commit f5a3928

Please sign in to comment.