Skip to content

Commit

Permalink
[Integration][Snyk] Make Snyk Integration accept a list of organizati…
Browse files Browse the repository at this point in the history
…ons (#980)
  • Loading branch information
oiadebayo authored Sep 5, 2024
1 parent bbc7bdf commit bfd4767
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion integrations/snyk/.port/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ configurations:
- name: organizationId
type: string
required: false
description: The unique identifier for the specific organization. When provided, the integration fetches data for a single organization. To find the organization id, see the <a href="https://docs.snyk.io/snyk-admin/groups-and-organizations/organizations/organization-general-settings" target="_blank">Snyk documentation</a>
description: A comma-separated list of the unique identifier for specific organization(s). When provided, the integration fetches data for the specific organization(s). To find the organization id, see the <a href="https://docs.snyk.io/snyk-admin/groups-and-organizations/organizations/organization-general-settings" target="_blank">Snyk documentation</a>
- name: groups
type: string
required: false
Expand Down
8 changes: 8 additions & 0 deletions integrations/snyk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- towncrier release notes start -->

## 0.1.85 (2024-09-04)


### Features

- Allowed users to provide multiple organization IDs to the integration using the existing `organizationId` param in the spec.yaml file


## 0.1.84 (2024-09-04)


Expand Down
9 changes: 6 additions & 3 deletions integrations/snyk/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import hashlib
import hmac
from typing import Any, cast
from typing import Any, cast, Optional
from enum import StrEnum
from fastapi import Request
from loguru import logger
Expand Down Expand Up @@ -36,12 +36,15 @@ def generate_signature(payload: bytes, secret: str) -> str:


def init_client() -> SnykClient:
def parse_list(value: str) -> Optional[list[str]]:
return [item.strip() for item in value.split(",")] if value else None

return SnykClient(
ocean.integration_config["token"],
ocean.integration_config["api_url"],
ocean.integration_config.get("app_host"),
ocean.integration_config.get("organization_id"),
ocean.integration_config.get("groups"),
parse_list(ocean.integration_config.get("organization_id", "")),
parse_list(ocean.integration_config.get("groups", "")),
ocean.integration_config.get("webhook_secret"),
)

Expand Down
2 changes: 1 addition & 1 deletion integrations/snyk/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "snyk"
version = "0.1.84"
version = "0.1.85"
description = "Snyk integration powered by Ocean"
authors = ["Isaac Coffie <[email protected]>"]

Expand Down
27 changes: 13 additions & 14 deletions integrations/snyk/snyk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def __init__(
token: str,
api_url: str,
app_host: str | None,
organization_id: str | None,
group_ids: str | None,
organization_ids: list[str] | None,
group_ids: list[str] | None,
webhook_secret: str | None,
):
self.token = token
self.api_url = f"{api_url}/v1"
self.app_host = app_host
self.organization_id = organization_id
self.organization_ids = organization_ids
self.group_ids = group_ids
self.rest_api_url = f"{api_url}/rest"
self.webhook_secret = webhook_secret
Expand Down Expand Up @@ -407,32 +407,31 @@ async def get_organizations_in_groups(self) -> list[Any]:

all_organizations = await self.get_all_organizations()

if self.organization_id:
logger.info(f"Specified organization ID: {self.organization_id}")
if self.organization_ids:
logger.info(f"Specified organization ID(s): {self.organization_ids}")
matching_organization = [
org for org in all_organizations if org["id"] == self.organization_id
org for org in all_organizations if org["id"] in self.organization_ids
]

logger.info(
f"Fetched {len(matching_organization)} organizations for the given organization ID(s)."
)
if matching_organization:
event.attributes[CacheKeys.GROUP] = matching_organization
return matching_organization
else:
logger.warning(
f"Specified organization ID '{self.organization_id}' not found in the fetched organizations."
f"Specified organization ID(s) '{self.organization_ids}' not found in the fetched organizations."
)
return []

elif self.group_ids:
groups = self.group_ids.split(",")

logger.info(
f"Found {len(groups)} groups to filter. Group IDs: {str(groups)}. Getting all organizations associated with these groups"
f"Found {len(self.group_ids)} groups to filter. Group IDs: {str(self.group_ids)}. Getting all organizations associated with these groups"
)

matching_organizations_in_groups = [
org
for org in all_organizations
if org.get("attributes") and org["attributes"].get("group_id") in groups
if org.get("attributes")
and org["attributes"].get("group_id") in self.group_ids
]

logger.info(
Expand Down

0 comments on commit bfd4767

Please sign in to comment.