Skip to content

Commit dd0a440

Browse files
authored
Example10 (snyk-labs#31)
* Fixing example10
1 parent dee3a14 commit dd0a440

10 files changed

+109
-83
lines changed

.gitignore

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
.idea
2-
__pycache__
3-
output
41
.coverage
2+
.idea
53
.mypy_cache
4+
5+
__pycache__
6+
67
dist
8+
output
9+
pysnyk.egg-info

examples/ProjectDependenciesReport.py

+30-38
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,23 @@ def list_of_dictionaries_to_map(input_list, key_field, data_fields_list=None):
2424

2525
def get_project_tree(snyk_token, org_id, project_id):
2626
client = SnykClient(snyk_token)
27-
json_res_dep_graph = client.snyk_projects_get_product_dependency_graph(
28-
org_id, project_id
29-
)
30-
print_json(json_res_dep_graph)
27+
res_dep_graph = client.organizations.get(org_id).projects.get(project_id).dependency_graph
28+
print(res_dep_graph)
3129

3230
print("\nPackages (Flat List):")
33-
for pkg in json_res_dep_graph["depGraph"]["pkgs"]:
34-
print("%s | %s" % (pkg["id"], pkg["info"]))
31+
for pkg in res_dep_graph.pkgs:
32+
print("%s | %s" % (pkg.id, pkg.info))
3533

36-
all_packages = json_res_dep_graph["depGraph"]["pkgs"]
34+
all_packages = res_dep_graph.pkgs
3735

3836
print("\nGraph data:")
39-
graph = json_res_dep_graph["depGraph"]["graph"]
40-
root_node_id = graph["rootNodeId"]
41-
nodes = graph["nodes"]
37+
graph = res_dep_graph.graph
38+
root_node_id = graph.rootNodeId
39+
nodes = graph.nodes
4240

4341
for node in nodes:
44-
print("%s | %s" % (node["nodeId"], node["pkgId"]))
45-
child_nodes = node["deps"]
42+
print("%s | %s" % (node.nodeId, node.pkgId))
43+
child_nodes = node.deps
4644
if len(child_nodes) > 0:
4745
for child_node in child_nodes:
4846
print(" - Child: %s" % child_node)
@@ -52,62 +50,58 @@ def get_project_tree(snyk_token, org_id, project_id):
5250
packages_lookup_map = {}
5351
for pkg in all_packages:
5452
print(pkg)
55-
package_id = pkg["id"]
53+
package_id = pkg.id
5654
packages_lookup_map[package_id] = {
57-
"package_name": pkg["info"]["name"],
58-
"package_version": pkg["info"]["version"],
55+
"package_name": pkg.info.name,
56+
"package_version": pkg.info.version,
5957
}
6058

6159
# Get licenses for all dependencies in the project
62-
lst_res_license = client.snyk_dependencies_list_all_dependencies_by_project(
63-
org_id, project_id
64-
)
60+
lst_res_license = client.organizations.get(org_id).projects.get(project_id).dependencies.all()
6561

6662
# make into a lookup table by package_id
6763
package_id_to_license_info_map = {} # package_id -> { license info }
6864
for r in lst_res_license:
69-
package_id = r["id"]
70-
licenses = r["licenses"]
65+
package_id = r.id
66+
licenses = r.licenses
7167
package_id_to_license_info_map[package_id] = licenses
7268

7369
print("\n\npackage_id_to_license_info_map:")
74-
print_json(package_id_to_license_info_map)
70+
print(package_id_to_license_info_map)
7571

7672
# Get the license issues and then enhance package_id_to_license_info_map with the license classification or none
77-
get_project_issues_response = client.snyk_projects_project_issues(
78-
org_id, project_id
79-
)
80-
license_issues_list = get_project_issues_response["issues"]["licenses"]
73+
issues = client.organizations.get(org_id).projects.get(project_id).issueset.all().issues
74+
license_issues_list = issues.licenses
8175

8276
# map to lookup table
83-
license_issues_lookup_map = list_of_dictionaries_to_map(license_issues_list, "id")
77+
license_issues_lookup_map = {license_issue.id: license_issue.severity for license_issue in license_issues_list}
8478

8579
for pkgId, licensesList in package_id_to_license_info_map.items():
8680
for l in licensesList:
87-
license_id = l["id"]
81+
license_id = l.id
8882
print(license_id)
8983

9084
if license_id in license_issues_lookup_map:
9185
print("append additional info")
92-
severity = license_issues_lookup_map[license_id]["severity"]
93-
l["severity"] = severity
86+
severity = license_issues_lookup_map[license_id]
87+
l.severity = severity
9488
else:
95-
l["severity"] = "none"
89+
l.severity = "none"
9690

9791
# lookup the license id in license_issues_lookup_map and see if there's an issue
9892
# add a 'classification' to the licenseInfo
9993

10094
# Convert nodes to a dictionary by nodeId
10195
node_lookup_map = {}
10296
for node in nodes:
103-
node_id = node["nodeId"]
104-
package_id = node["pkgId"]
97+
node_id = node.nodeId
98+
package_id = node.pkgId
10599
node_lookup_map[node_id] = {
106-
"pkgId": node["pkgId"],
100+
"pkgId": node.pkgId,
107101
# TODO: Pull in the packages_name and package_version from packages_lookup_map
108102
"package_name": packages_lookup_map[package_id]["package_name"],
109103
"package_version": packages_lookup_map[package_id]["package_version"],
110-
"deps": node["deps"],
104+
"deps": node.deps,
111105
}
112106

113107
print(node_lookup_map)
@@ -126,10 +120,9 @@ def get_project_tree(snyk_token, org_id, project_id):
126120

127121
# Now create a new structure based on node_lookup_map which is a deeply nested structure of the same data
128122
project_structured_tree = {}
123+
129124

130-
def get_node_to_append(
131-
node_id, base_path
132-
): # might make sense to rename get_dependencies
125+
def get_node_to_append(node_id, base_path): # might make sense to rename get_dependencies
133126
obj = node_lookup_map[node_id]
134127
pkgId = obj["pkgId"]
135128
print("node_id: %s" % pkgId)
@@ -160,7 +153,6 @@ def get_node_to_append(
160153
# print(root_node_package_id)
161154
project_dependencies_structure = get_node_to_append(root_node_id, "")
162155
project_structured_tree = {"project": project_dependencies_structure}
163-
164156
return project_structured_tree
165157

166158

examples/README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
The following examples require you to create a file at `~/.ssh/tokens/snyk-api-token` which contains a valid Snyk API token - either your [personal token](https://app.snyk.io/account) or a [service account](https://snyk.io/docs/service-accounts/) token. This file should contain the token alone with no formatting.
55

66
## Running a Script
7-
The sample scripts are all a bit different, so you should try them each out or look at the code.
8-
9-
In general the form is:
10-
```
11-
python examples/<script-name.py> --orgId=<your-org_id> ...
12-
```
7+
The sample scripts are all a bit different, so you should try them each out or look at the code.
8+
9+
1. The dependencies are managed by [poetry](https://poetry.eustace.io/). Installation is described on the [poetry docs](https://poetry.eustace.io/docs/#introduction)
10+
1. Install the dependencies using `poetry install`
11+
1. You can execute an example in general using:
12+
```
13+
poetry run python examples/<script-name.py> --orgId=<your-org_id> ...
14+
```
1315
1416
## List Project Issues
1517
```

examples/api-demo-10-project-deps-licenses-report.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ def get_flat_dependencies(dep_list):
6565
licences = d["licenses"]
6666
path = d["path"]
6767

68-
simplified_liceses_list = [l["title"] for l in licences]
68+
simplified_liceses_list = [l.title for l in licences]
6969
licenses_str = ", ".join(simplified_liceses_list)
7070

71-
license_issues_list = [l["severity"] for l in licences]
71+
license_issues_list = [l.severity for l in licences]
7272
license_issues_str = ", ".join(license_issues_list)
7373

7474
flat_dep_list.append(

examples/api-demo-6-find-issues-without-jira-tickets.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@ def parse_command_line_args():
2121
project_id = args.projectId
2222

2323
client = SnykClient(snyk_token)
24+
org = client.organizations.get(org_id)
2425
project = client.organizations.get(org_id).projects.get(project_id)
25-
issues = project.issues.issues
26+
issues = project.issueset.all().issues
2627
jira_issues = project.jira_issues.all()
2728

28-
all_issue_ids = []
29-
all_issue_ids.extend([i.id for i in issues.vulnerabilities])
30-
all_issue_ids.extend([i.id for i in issues.licenses])
31-
3229
snyk_issue_with_jira_issues = list(jira_issues.keys())
3330

3431
for issue in issues.vulnerabilities + issues.licenses:
3532
if issue.id not in list(jira_issues.keys()):
3633
print("Found issue without Jira issue: %s" % issue.id)
3734
print(
3835
" https://app.snyk.io/org/%s/project/%s#%s"
39-
% (org_id, project_id, issue.id)
36+
% (org.name, project_id, issue.id)
4037
)

poetry.lock

+23-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pytest-black = "^0.3.7"
1616
pytest-cov = "^2.7"
1717
pytest-mypy = "^0.3.2"
1818
requests-mock = "^1.6"
19+
xlsxwriter = "^1.1.8"
1920

2021
[build-system]
2122
requires = ["poetry>=0.12"]

snyk/managers.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,13 @@ def all(self, page: int = 1):
178178
"total"
179179
] # contains the total number of results (for pagination use)
180180

181-
results = dependency_data["results"]
181+
results = [self.klass.from_dict(item) for item in dependency_data["results"]]
182182

183183
if total > (page * results_per_page):
184184
next_results = self.all(page + 1)
185185
results.extend(next_results)
186-
return results
187186

188-
dependencies = []
189-
for dependency_data in results:
190-
dependencies.append(self.klass.from_dict(dependency_data))
191-
return dependencies
187+
return results
192188

193189

194190
class EntitlementManager(DictManager):
@@ -292,7 +288,9 @@ def all(self) -> Any:
292288
)
293289
resp = self.client.get(path)
294290
dependency_data = resp.json()
295-
return self.klass.from_dict(dependency_data)
291+
if "depGraph" in dependency_data:
292+
return self.klass.from_dict(dependency_data["depGraph"])
293+
raise SnykError
296294

297295

298296
class IssueSetManager(SingletonManager):

0 commit comments

Comments
 (0)