Skip to content

Commit 6417dda

Browse files
authored
Merge pull request #10 from zkarpinski/features/uploadProjectCodebase
added upload codebase api support
2 parents fec6f3a + 693aaf7 commit 6417dda

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

codeinsight_sdk/client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import requests
22
import logging
33

4-
from .handlers import ProjectHandler, Handler, ReportHandler
5-
from .models import Project, ProjectInventory, Report
4+
from .handlers import ProjectHandler, ReportHandler
65
from .exceptions import CodeInsightError
76

87
logger = logging.getLogger(__name__)
@@ -25,7 +24,7 @@ def __init__(self,
2524
self.__timeout = timeout
2625
self.__verify_ssl = verify_ssl
2726

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

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

36+
# Update headers if content_type is specified
37+
headers = self.__api_headers
38+
if content_type:
39+
headers['Content-Type'] = content_type
40+
3741
response = requests.request(method, url,
38-
headers=self.__api_headers, params=params, json=body,
42+
headers=self.__api_headers, params=params, json=body, data=data,
3943
timeout=self.__timeout, verify=self.__verify_ssl)
4044

4145
if not response.ok:

codeinsight_sdk/handlers.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Handler(abc.ABC):
88
def __init__(self, client):
99
self.client = client
1010
self.cls = None
11-
11+
1212
@staticmethod
1313
def create(client, cls):
1414
k = cls.__name__
@@ -19,7 +19,7 @@ def create(client, cls):
1919
if handler is None:
2020
raise ValueError(f"Handler not found for class '{k}'")
2121
return handler(client)
22-
22+
2323
@abc.abstractmethod
2424
def get(self):
2525
pass
@@ -79,7 +79,7 @@ def all(self) -> List[Project]:
7979
for project_data in resp.json()['data']:
8080
projects.append(self.cls.from_dict(project_data))
8181
return projects
82-
82+
8383
def get(self, id:int) -> Project:
8484
"""
8585
Retrieves a project by its ID.
@@ -94,7 +94,7 @@ def get(self, id:int) -> Project:
9494
resp = self.client.request("GET", url_part=path)
9595
project_data = resp.json()['data']
9696
return self.cls.from_dict(project_data)
97-
97+
9898
def get_id(self, project_name:str) -> int:
9999
"""
100100
Retrieves the ID of a project based on its name.
@@ -190,7 +190,24 @@ def get_inventory(self,project_id:int,
190190
project.inventoryItems.extend(chunk.inventoryItems)
191191

192192
return project
193-
193+
194+
def upload_codebase(self, project_id:int,
195+
codebase_path:str,
196+
deleteExistingFileOnServer: bool = False,
197+
expansionLevel: int = 1,
198+
deleteArchiveAfterExpand: bool = False,
199+
archiveDirSuffix: str = None,
200+
) -> int:
201+
path = "project/uploadProjectCodebase"
202+
params = {"projectId": project_id,
203+
"deleteExistingFileOnServer": deleteExistingFileOnServer,
204+
"expansionLevel": expansionLevel,
205+
"deleteArchiveAfterExpand": deleteArchiveAfterExpand,
206+
"archiveDirSuffix": archiveDirSuffix}
207+
code_file = {"file": open(codebase_path, 'rb')}
208+
content_type = "application/octet-stream"
209+
resp = self.client.request("POST", url_part=path, params=params, data=code_file,content_type=content_type)
210+
return resp.status_code
194211

195212
class ReportHandler(Handler):
196213
"""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "codeinsight_sdk"
3-
version = "0.0.6"
3+
version = "0.0.7"
44
description = "A Python client for the Revenera Code Insight"
55
authors = ["Zachary Karpinski <1206496+zkarpinski@users.noreply.github.com>"]
66
readme = "README.md"

tests/resources/test_codebase.zip

279 Bytes
Binary file not shown.

tests/test_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ def test_get_project_inventory_multipage(self,client):
124124
assert project_inventory.projectId == project_id
125125
assert len(project_inventory.inventoryItems) == total_records
126126
assert project_inventory.inventoryItems[0].vulnerabilities[0].vulnerabilityName == "CVE-2020-1234"
127+
128+
def test_upload_codebase(self,client):
129+
project_id = 1
130+
codebase_path = "tests/resources/test_codebase.zip"
131+
with requests_mock.Mocker() as m:
132+
m.post(f"{TEST_URL}/codeinsight/api/project/uploadProjectCodebase", text='{"data": {"id":1}}')
133+
resp = client.projects.upload_codebase(project_id, codebase_path)
134+
assert resp == 200
127135

128136
#### FIX THIS! ####
129137
def test_get_project_inventory_summary(self,client):

0 commit comments

Comments
 (0)