Skip to content

Commit

Permalink
Merge pull request #35 from snyk-ps/feat/target-branch
Browse files Browse the repository at this point in the history
feat: support for additional api filters fromfile
  • Loading branch information
EricFernandezSnyk authored Aug 28, 2024
2 parents 57a32e0 + 6f9481a commit effed94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
11 changes: 9 additions & 2 deletions snyk_tags/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import typer
from rich import print
from snyk import SnykClient
from typing import Dict

from snyk_tags import __app_name__, __version__, attribute, github

Expand Down Expand Up @@ -69,7 +70,13 @@ def apply_tag_to_project(

# Tagging loop
def apply_tags_to_projects(
token: str, org_ids: list, name: str, tag: str, key: str, tenant: str
token: str,
org_ids: list,
name: str,
tag: str,
key: str,
tenant: str,
filters: Dict[str, any],
) -> None:
with create_client(token=token, tenant=tenant) as client:
for org_id in org_ids:
Expand All @@ -81,7 +88,7 @@ def apply_tags_to_projects(
client_v3 = SnykClient(
token=token, url=base_url, version="2023-08-31~experimental"
)
params = {"limit": 100}
params = {"limit": 100, "names_start_with": name, **filters}
projects = client_v3.get_rest_pages(
f"/orgs/{org_id}/projects", params=params
)
Expand Down
17 changes: 14 additions & 3 deletions snyk_tags/files.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /usr/bin/env python3
import typer
from pathlib import Path
from typing import List
from typing import List, Optional
import csv
from snyk_tags import collection, attribute, remove
from rich import print
Expand Down Expand Up @@ -37,6 +37,7 @@ def target_tag(
),
):
for path in file:
filters = {}
if path.is_file():
openfile = open(path)
if ".csv" in openfile.name:
Expand All @@ -46,12 +47,17 @@ def target_tag(
target = row.get("target")
key = row.get("key")
value = row.get("value")
filters = {
attr: val
for attr, val in row.items()
if attr in ["target_reference", "origins", "types"]
}
typer.secho(
f"\nAdding the tag key {key} and tag value {value} to projects within {target} for easy filtering via the UI",
bold=True,
)
collection.apply_tags_to_projects(
snyktkn, [org_id], target, value, key, tenant
snyktkn, [org_id], target, value, key, tenant, filters
)
openfile.close()
elif ".json" in openfile.name:
Expand All @@ -61,12 +67,17 @@ def target_tag(
target = row.get("target")
key = row.get("key")
value = row.get("value")
filters = {
attr: val
for attr, val in row.items()
if attr in ["target_reference", "origins", "types"]
}
typer.secho(
f"\nAdding the tag key {key} and tag value {value} to projects within {target} for easy filtering via the UI",
bold=True,
)
collection.apply_tags_to_projects(
snyktkn, [org_id], target, value, key, tenant
snyktkn, [org_id], target, value, key, tenant, filters
)
openfile.close()
else:
Expand Down

0 comments on commit effed94

Please sign in to comment.