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/update for rest #182

Open
wants to merge 7 commits into
base: feat/migrate-REST-projects
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
updated to handle retrieving and deleting group level tags
Vincent Faires committed Aug 30, 2023
commit 7deb1b85546df9c4dca084af63738632b39100ff
12 changes: 7 additions & 5 deletions snyk/client.py
Original file line number Diff line number Diff line change
@@ -19,8 +19,8 @@
class SnykClient(object):
WEB_URL = "https://app.snyk.io"
API_URL = "https://api.snyk.io/rest"
VERSION = "2023-07-28~experimental"
#API_URL = "https://api.snyk.io/v1"
VERSION = "2023-08-04~experimental"
API_URL_V1 = "https://api.snyk.io/v1"
USER_AGENT = "pysnyk/%s" % __version__

def __init__(
@@ -200,7 +200,7 @@ def get(
fkwargs = {"headers": self.api_headers}

logger.debug(f"GET: {debug_url}")

resp = retry_call(
self.request,
fargs=[requests.get, url],
@@ -246,7 +246,7 @@ def get_rest_pages(self, path: str, params: dict = {}) -> List:
"""
Helper function to collect paginated responses from the rest API into a single
list.

ff
This collects the "data" list from the first reponse and then appends the
any further "data" lists if a next link is found in the links field.
"""
@@ -260,7 +260,9 @@ def get_rest_pages(self, path: str, params: dict = {}) -> List:
if 'limit' in params.keys():
limit = params["limit"]
else:
limit = self.client.limit
limit = self.limit
params["limit"] = self.limit
print("limit: ", limit)

data = list()

90 changes: 71 additions & 19 deletions snyk/managers.py
Original file line number Diff line number Diff line change
@@ -51,24 +51,25 @@ def factory(klass, client, instance=None):
else:
key = klass.__name__
manager = {
"Project": ProjectManager,
"Organization": OrganizationManager,
"Member": MemberManager,
"License": LicenseManager,
"Dependency": DependencyManager,
"Entitlement": EntitlementManager,
"Setting": SettingManager,
"Ignore": IgnoreManager,
"JiraIssue": JiraIssueManager,
"DependencyGraph": DependencyGraphManager,
"IssueSet": IssueSetManager,
"IssueSetAggregated": IssueSetAggregatedManager2,
"Integration": IntegrationManager,
"IntegrationSetting": IntegrationSettingManager,
"Tag": TagManager,
"IssuePaths": IssuePathsManager,
"OrganizationGroup": OrganizationGroupManager,
"User": UserManager,
"Project": ProjectManager,
"Organization": OrganizationManager,
"Member": MemberManager,
"License": LicenseManager,
"Dependency": DependencyManager,
"Entitlement": EntitlementManager,
"Setting": SettingManager,
"Ignore": IgnoreManager,
"JiraIssue": JiraIssueManager,
"DependencyGraph": DependencyGraphManager,
"IssueSet": IssueSetManager,
"IssueSetAggregated": IssueSetAggregatedManager2,
"Integration": IntegrationManager,
"IntegrationSetting": IntegrationSettingManager,
"Tag": TagManager,
"IssuePaths": IssuePathsManager,
"OrganizationGroup": OrganizationGroupManager,
"OrganizationGroupTags": OrganizationGroupTagManager,
"User": UserManager,
}[key]

return manager(klass, client, instance)
@@ -166,6 +167,36 @@ def get(self, id: str):

return self.klass.from_dict(org_template)

class OrganizationGroupTagManager(Manager):
def all(self):
# Version 1 endpoint
self.client.api_url = self.client.API_URL_V1

resp = self.client.get(f'/group/{self.instance.id}/tags?perPage=50000&page=1')

# Reset to REST endpoint
self.client.api_url = self.client.API_URL

return resp.json()['tags']

def delete(self,key: str, value: str, force: bool=False) -> bool:
# Version 1 endpoint
self.client.api_url = self.client.API_URL_V1

body = {'key': key,
'value': value,
'force': force}

resp = self.client.post(f'/group/{self.instance.id}/tags/delete', body)

# Reset to REST endpoint
self.client.api_url = self.client.API_URL

if resp.status_code == 200:
return True
else:
return False

class OrganizationGroupManager(Manager):
def all(self):
params = {'limit': self.client.limit}
@@ -196,7 +227,7 @@ def get(self, id: str):
except Exception as e:
raise e

return self.klass(resp['attributes']['name'],resp['id'])
return self.klass(resp['attributes']['name'],resp['id'],self.client)


def filter(self, **kwargs: Any):
@@ -593,24 +624,41 @@ def update(self, **kwargs: bool) -> bool:

class IgnoreManager(DictManager):
def all(self) -> Dict[str, List[object]]:
# Version 1 endpoint
self.client.api_url = self.client.API_URL_V1

path = "org/%s/project/%s/ignores" % (
self.instance.organization.id,
self.instance.id,
)
resp = self.client.get(path)

# Reset to REST endpoint
self.client.api_url = self.client.API_URL
return resp.json()



class JiraIssueManager(DictManager):
def all(self) -> Dict[str, List[object]]:
# Version 1 endpoint
self.client.api_url = self.client.API_URL_V1

path = "org/%s/project/%s/jira-issues" % (
self.instance.organization.id,
self.instance.id,
)
resp = self.client.get(path)

# Reset to REST endpoint
self.client.api_url = self.client.API_URL

return resp.json()

def create(self, issue_id: str, fields: Any) -> Dict[str, str]:
# Version 1 endpoint
self.client.api_url = self.client.API_URL_V1

path = "org/%s/project/%s/issue/%s/jira-issue" % (
self.instance.organization.id,
self.instance.id,
@@ -619,6 +667,10 @@ def create(self, issue_id: str, fields: Any) -> Dict[str, str]:
post_body = {"fields": fields}
resp = self.client.post(path, post_body)
response_data = resp.json()

# Reset to REST endpoint
self.client.api_url = self.client.API_URL

# The response we get is not following the schema as specified by the api
# https://snyk.docs.apiary.io/#reference/projects/project-jira-issues-/create-jira-issue
if (
6 changes: 6 additions & 0 deletions snyk/models.py
Original file line number Diff line number Diff line change
@@ -134,6 +134,12 @@ class OrganizationGroup(DataClassJSONMixin):
name: str
id: str

client: Optional[Any] = None

@property
def tags(self) -> Manager:
return Manager.factory("OrganizationGroupTags", self.client, self)


@dataclass
class Package(DataClassJSONMixin):