Skip to content

Commit e51133d

Browse files
committed
Added upload manifests endpoint for reachability
1 parent 4d3d213 commit e51133d

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "socketdev"
7-
version = "3.0.14"
7+
version = "3.0.15"
88
requires-python = ">= 3.9"
99
dependencies = [
1010
'requests',

socketdev/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from socketdev.analytics import Analytics
2626
from socketdev.alerttypes import AlertTypes
2727
from socketdev.basics import Basics
28+
from socketdev.uploadmanifests import UploadManifests
2829
from socketdev.log import log
2930

3031
__author__ = "socket.dev"
@@ -74,6 +75,7 @@ def __init__(self, token: str, timeout: int = 1200):
7475
self.analytics = Analytics(self.api)
7576
self.alerttypes = AlertTypes(self.api)
7677
self.basics = Basics(self.api)
78+
self.uploadmanifests = UploadManifests(self.api)
7779

7880
@staticmethod
7981
def set_timeout(timeout: int):
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import logging
3+
from typing import List, Optional, Union
4+
from ..utils import Utils
5+
6+
log = logging.getLogger("socketdev")
7+
8+
9+
class UploadManifests:
10+
def __init__(self, api):
11+
self.api = api
12+
13+
def upload_manifest_files(self, org_slug: str, file_paths: List[str], workspace: Optional[str] = None, base_path: Optional[str] = None, base_paths: Optional[List[str]] = None, use_lazy_loading: bool = True) -> str:
14+
"""
15+
Upload manifest files to Socket API and return tarHash.
16+
17+
Args:
18+
org_slug: Organization slug
19+
file_paths: List of manifest file paths to upload
20+
workspace: Base directory path to make paths relative to
21+
base_path: Optional base path to strip from key names for cleaner file organization
22+
base_paths: Optional list of base paths to strip from key names (takes precedence over base_path)
23+
use_lazy_loading: Whether to use lazy file loading (default: True)
24+
25+
Returns:
26+
str: The tarHash from the upload response
27+
28+
Raises:
29+
Exception: If upload fails
30+
"""
31+
# Filter to only existing files
32+
valid_files = [f for f in file_paths if os.path.exists(f) and os.path.isfile(f)]
33+
34+
if not valid_files:
35+
raise Exception("No valid manifest files found to upload")
36+
37+
# Prepare files for upload using the utility function
38+
if use_lazy_loading:
39+
loaded_files = Utils.load_files_for_sending_lazy(
40+
valid_files,
41+
workspace=workspace,
42+
base_path=base_path,
43+
base_paths=base_paths
44+
)
45+
else:
46+
# Fallback to basic file loading if needed
47+
loaded_files = []
48+
for file_path in valid_files:
49+
key = os.path.basename(file_path)
50+
with open(file_path, 'rb') as f:
51+
loaded_files.append((key, (key, f.read())))
52+
53+
# Make the upload request
54+
path = f"orgs/{org_slug}/upload-manifest-files"
55+
response = self.api.do_request(path=path, files=loaded_files, method="POST")
56+
57+
if response.status_code != 200:
58+
raise Exception(f"Upload failed with status {response.status_code}: {response.text}")
59+
60+
result = response.json()
61+
tar_hash = result.get('tarHash')
62+
63+
if not tar_hash:
64+
raise Exception("Server did not return a tarHash")
65+
66+
log.info(f"Successfully uploaded {len(valid_files)} manifest files, tarHash: {tar_hash}")
67+
return tar_hash

socketdev/utils/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, List, Tuple
1+
from typing import Literal, List, Tuple, Optional
22
import logging
33
import os
44
import weakref
@@ -233,7 +233,7 @@ def validate_integration_type(integration_type: str) -> IntegrationType:
233233
return integration_type # type: ignore
234234

235235
@staticmethod
236-
def load_files_for_sending_lazy(files: List[str], workspace: str = None, max_open_files: int = 100, base_path: str = None, base_paths: List[str] = None) -> List[Tuple[str, Tuple[str, LazyFileLoader]]]:
236+
def load_files_for_sending_lazy(files: List[str], workspace: Optional[str] = None, max_open_files: int = 100, base_path: Optional[str] = None, base_paths: Optional[List[str]] = None) -> List[Tuple[str, Tuple[str, LazyFileLoader]]]:
237237
"""
238238
Prepares files for sending to the Socket API using lazy loading.
239239
@@ -342,4 +342,6 @@ def load_files_for_sending_lazy(files: List[str], workspace: str = None, max_ope
342342
lazy_file = LazyFileLoader(file_path, key)
343343
payload = (key, (key, lazy_file))
344344
send_files.append(payload)
345-
return send_files
345+
346+
log.debug(f"Prepared {len(send_files)} files for lazy loading")
347+
return send_files

socketdev/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.0.14"
1+
__version__ = "3.0.15"

0 commit comments

Comments
 (0)