-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: feat(data): add function to push a commit to the platform
- Loading branch information
Stainless Bot
committed
Nov 4, 2024
1 parent
5b3bd38
commit 7b5a29e
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""Pushes a commit to the Openlayer platform.""" | ||
|
||
import os | ||
import tarfile | ||
import tempfile | ||
from typing import Optional | ||
|
||
|
||
from ... import Openlayer | ||
from . import StorageType, _upload | ||
|
||
|
||
def push( | ||
client: Openlayer, | ||
directory: str, | ||
project_id: str, | ||
message: str = "New commit", | ||
storage_type: Optional[StorageType] = None, | ||
) -> None: | ||
"""Push a new commit to the Openlayer platform. | ||
This is equivalent to running `openlayer push` from the Openlayer CLI.""" | ||
if not os.path.exists(directory): | ||
raise ValueError(f"Directory {directory} does not exist.") | ||
|
||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
tar_file_path = os.path.join(tmp_dir, "bundle.tar") | ||
with tarfile.open(tar_file_path, mode="w") as tar: | ||
tar.add(directory, arcname=os.path.basename(directory)) | ||
|
||
# Upload tar storage | ||
uploader = _upload.Uploader(client, storage_type) | ||
object_name = "bundle.tar" | ||
presigned_url_response = client.storage.presigned_url.create( | ||
object_name=object_name, | ||
) | ||
uploader.upload( | ||
file_path=tar_file_path, | ||
object_name=object_name, | ||
presigned_url_response=presigned_url_response, | ||
) | ||
|
||
# Create the project version (commit) | ||
client.projects.commits.create( | ||
project_id=project_id, | ||
commit={"message": message, "source": "cli"}, | ||
storage_uri=presigned_url_response.storage_uri, | ||
) |