Skip to content

Commit

Permalink
Merge pull request #10 from zkarpinski/features/uploadProjectCodebase
Browse files Browse the repository at this point in the history
added upload codebase api support
  • Loading branch information
zkarpinski committed Jan 10, 2024
2 parents fec6f3a + 693aaf7 commit 6417dda
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
12 changes: 8 additions & 4 deletions codeinsight_sdk/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import requests
import logging

from .handlers import ProjectHandler, Handler, ReportHandler
from .models import Project, ProjectInventory, Report
from .handlers import ProjectHandler, ReportHandler
from .exceptions import CodeInsightError

logger = logging.getLogger(__name__)
Expand All @@ -25,7 +24,7 @@ def __init__(self,
self.__timeout = timeout
self.__verify_ssl = verify_ssl

def request(self, method, url_part: str, params: dict = None, body: any = None ):
def request(self, method, url_part: str, params: dict = None, body: any = None, data: any = None, content_type: str = None):
url = f"{self.api_url}/{url_part}"

# Iterate over params and remove any that are None (Empty)
Expand All @@ -34,8 +33,13 @@ def request(self, method, url_part: str, params: dict = None, body: any = None )
if v is None:
del params[k]

# Update headers if content_type is specified
headers = self.__api_headers
if content_type:
headers['Content-Type'] = content_type

response = requests.request(method, url,
headers=self.__api_headers, params=params, json=body,
headers=self.__api_headers, params=params, json=body, data=data,
timeout=self.__timeout, verify=self.__verify_ssl)

if not response.ok:
Expand Down
27 changes: 22 additions & 5 deletions codeinsight_sdk/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Handler(abc.ABC):
def __init__(self, client):
self.client = client
self.cls = None

@staticmethod
def create(client, cls):
k = cls.__name__
Expand All @@ -19,7 +19,7 @@ def create(client, cls):
if handler is None:
raise ValueError(f"Handler not found for class '{k}'")
return handler(client)

@abc.abstractmethod
def get(self):
pass
Expand Down Expand Up @@ -79,7 +79,7 @@ def all(self) -> List[Project]:
for project_data in resp.json()['data']:
projects.append(self.cls.from_dict(project_data))
return projects

def get(self, id:int) -> Project:
"""
Retrieves a project by its ID.
Expand All @@ -94,7 +94,7 @@ def get(self, id:int) -> Project:
resp = self.client.request("GET", url_part=path)
project_data = resp.json()['data']
return self.cls.from_dict(project_data)

def get_id(self, project_name:str) -> int:
"""
Retrieves the ID of a project based on its name.
Expand Down Expand Up @@ -190,7 +190,24 @@ def get_inventory(self,project_id:int,
project.inventoryItems.extend(chunk.inventoryItems)

return project


def upload_codebase(self, project_id:int,
codebase_path:str,
deleteExistingFileOnServer: bool = False,
expansionLevel: int = 1,
deleteArchiveAfterExpand: bool = False,
archiveDirSuffix: str = None,
) -> int:
path = "project/uploadProjectCodebase"
params = {"projectId": project_id,
"deleteExistingFileOnServer": deleteExistingFileOnServer,
"expansionLevel": expansionLevel,
"deleteArchiveAfterExpand": deleteArchiveAfterExpand,
"archiveDirSuffix": archiveDirSuffix}
code_file = {"file": open(codebase_path, 'rb')}
content_type = "application/octet-stream"
resp = self.client.request("POST", url_part=path, params=params, data=code_file,content_type=content_type)
return resp.status_code

class ReportHandler(Handler):
"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "codeinsight_sdk"
version = "0.0.6"
version = "0.0.7"
description = "A Python client for the Revenera Code Insight"
authors = ["Zachary Karpinski <[email protected]>"]
readme = "README.md"
Expand Down
Binary file added tests/resources/test_codebase.zip
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ def test_get_project_inventory_multipage(self,client):
assert project_inventory.projectId == project_id
assert len(project_inventory.inventoryItems) == total_records
assert project_inventory.inventoryItems[0].vulnerabilities[0].vulnerabilityName == "CVE-2020-1234"

def test_upload_codebase(self,client):
project_id = 1
codebase_path = "tests/resources/test_codebase.zip"
with requests_mock.Mocker() as m:
m.post(f"{TEST_URL}/codeinsight/api/project/uploadProjectCodebase", text='{"data": {"id":1}}')
resp = client.projects.upload_codebase(project_id, codebase_path)
assert resp == 200

#### FIX THIS! ####
def test_get_project_inventory_summary(self,client):
Expand Down

0 comments on commit 6417dda

Please sign in to comment.