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
0 commit comments