Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #84 from snyk-tech-services/develop
Browse files Browse the repository at this point in the history
release changes
  • Loading branch information
scott-es authored Mar 29, 2022
2 parents 41fe403 + e01a6f2 commit d4520ca
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 52 deletions.
11 changes: 11 additions & 0 deletions app/snyk_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ def __init__(
self.origin = origin
self.branch = branch
self.snyk_projects = snyk_projects

def __repr__(self):
return (
f"{self.full_name}" + "\n"
f"{self.org_id}" + "\n"
f"{self.org_name}" + "\n"
f"{self.integration_id}" + "\n"
f"{self.origin}" + "\n"
f"{self.branch}" + "\n"
f"{self.snyk_projects}")

def __getitem__(self, item):
return self.__dict__[item]

Expand Down
64 changes: 63 additions & 1 deletion app/tests/test_snyk_scm_refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
get_gh_repo_status,
passes_manifest_filter,
)
from app.utils.snyk_helper import get_snyk_projects_for_repo
from app.utils.snyk_helper import (
get_snyk_projects_for_repo,
get_snyk_repos_from_snyk_projects
)

class MockResponse:
""" mock response for github check """
Expand Down Expand Up @@ -119,6 +122,65 @@ def test_get_gh_repo_status_unauthorized(mocker):
with pytest.raises(RuntimeError):
get_gh_repo_status(snyk_repo)

def test_get_snyk_repos_from_snyk_projects():
""" test generating unique repos from project list """

snyk_gh_projects = [
{
"id": "12345",
"name": "scotte-snyk/test-project-1:package.json",
"repo_full_name": "scotte-snyk/test-project-1",
"repo_owner": "scotte-snyk",
"repo_name": "test-project-1",
"manifest": "package.json",
"org_id": "12345",
"org_name": "scotte-snyk",
"origin": "github",
"type": "npm",
"integration_id": "66d7ebef-9b36-464f-889c-b92c9ef5ce12",
"branch_from_name": "",
"branch": "master"
},
{
"id": "12345",
"name": "scotte-snyk/test-project-2:package.json",
"repo_full_name": "scotte-snyk/test-project-2",
"repo_owner": "scotte-snyk",
"repo_name": "test-project-2",
"manifest": "package.json",
"org_id": "12345",
"org_name": "scotte-snyk",
"origin": "github",
"type": "npm",
"integration_id": "66d7ebef-9b36-464f-889c-b92c9ef5ce12",
"branch_from_name": "",
"branch": "master"
},
]

snyk_repos_from_snyk_projects = [
SnykRepo(
'scotte-snyk/test-project-1',
"12345",
"scotte-snyk",
"66d7ebef-9b36-464f-889c-b92c9ef5ce12",
"github",
"master",
[snyk_gh_projects[0]]
),
SnykRepo(
'scotte-snyk/test-project-2',
"12345",
"scotte-snyk",
"66d7ebef-9b36-464f-889c-b92c9ef5ce12",
"github",
"master",
[snyk_gh_projects[1]]
)
]

assert str(get_snyk_repos_from_snyk_projects(snyk_gh_projects)) == str(snyk_repos_from_snyk_projects)

def test_get_snyk_project_for_repo():
""" test collecting projects for a repo """

Expand Down
82 changes: 31 additions & 51 deletions app/utils/snyk_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,63 +37,43 @@ def get_snyk_repos_from_snyk_orgs(snyk_orgs, ARGS):
snyk_repos = []
snyk_projects = build_snyk_project_list(snyk_orgs, ARGS)

repo_projects = []

# initialize to the first repo name
num_projects = len(snyk_projects)

if num_projects > 0:
curr_repo_name = snyk_projects[0]["repo_full_name"]
# print(f"curr repo name: {curr_repo_name}")

for (i, project) in enumerate(snyk_projects):
#if i == num_projects-1:
# print(f"encountered base case")
# snyk_repos.append(
# SnykRepo(snyk_projects[i]["repo_full_name"],
# snyk_projects[i]["org_id"],
# snyk_projects[i]["org_name"],
# snyk_projects[i]["integration_id"],
# snyk_projects[i]["origin"],
# snyk_projects[i]["branch"],
# repo_projects)
# )

# we encountered a new repo, or reached the end of the project list
if project["repo_full_name"] != curr_repo_name:
# print(f"encountered a new repo name: {project['repo_full_name']}")
# add repo to snyk_repos
snyk_repos.append(
SnykRepo(snyk_projects[i-1]["repo_full_name"],
snyk_projects[i-1]["org_id"],
snyk_projects[i-1]["org_name"],
snyk_projects[i-1]["integration_id"],
snyk_projects[i-1]["origin"],
snyk_projects[i-1]["branch"],
repo_projects)
)
repo_projects = [project]
# print(f"setting repo_projects to: {repo_projects}")

if i == num_projects-1:
# print("encountered last project")
snyk_repos.append(
SnykRepo(snyk_projects[i]["repo_full_name"],
snyk_projects[i]["org_id"],
snyk_projects[i]["org_name"],
snyk_projects[i]["integration_id"],
snyk_projects[i]["origin"],
snyk_projects[i]["branch"],
repo_projects)
)
snyk_repos = get_snyk_repos_from_snyk_projects(snyk_projects)

return snyk_repos

def get_snyk_repos_from_snyk_projects(snyk_projects):
""" Get list of unique repos built from an input of snyk projects"""
snyk_repos = []
# num_projects = len(snyk_projects)
curr_repo_name = ""

repo_projects = []
for (i, project) in enumerate(snyk_projects):

# we encountered a new repo
if project["repo_full_name"] != curr_repo_name:
# add repo to snyk_repos
snyk_repos.append(
SnykRepo(snyk_projects[i]["repo_full_name"],
snyk_projects[i]["org_id"],
snyk_projects[i]["org_name"],
snyk_projects[i]["integration_id"],
snyk_projects[i]["origin"],
snyk_projects[i]["branch"],
[x for x in snyk_projects \
if x["repo_full_name"] == project["repo_full_name"]])
)
repo_projects = []

else:
repo_projects.append(project)

else:
# add to project list for this repo
repo_projects.append(project)
# print(f"adding project: {project['manifest']}")
curr_repo_name = project["repo_full_name"]

curr_repo_name = project["repo_full_name"]
# print(f"curr repo name set to: {curr_repo_name}")
return snyk_repos

def build_snyk_project_list(snyk_orgs, ARGS):
Expand Down

0 comments on commit d4520ca

Please sign in to comment.