Skip to content

Commit

Permalink
Merge pull request #110 from OpenPecha/feat-add_release_asset
Browse files Browse the repository at this point in the history
added create release to add assets to the repo
  • Loading branch information
tenzin3 authored Jan 3, 2025
2 parents 673f7cf + 83dd7fc commit a9a271f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/openpecha/github_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import subprocess
import time
from uuid import uuid4
from pathlib import Path

from github import Github
Expand Down Expand Up @@ -130,3 +131,48 @@ def clone_repo(
return target_path
except subprocess.CalledProcessError as e:
raise GithubCloneError(f"Failed to clone {repo_name}. Error: {e}")


def get_bumped_tag(repo):
try:
latest_release_tag = repo.get_latest_release().tag_name
except Exception:
return "v0.1"

tag_number = float(latest_release_tag[1:])
bump_tag_number = round(tag_number + 0.1, 1)
return f"v{bump_tag_number}"


def upload_assets(release, tag_name=None, asset_paths=[]):
if not tag_name:
tag_name = release.tag_name
download_url = ""
for asset_path in asset_paths:
asset = release.upload_asset(str(asset_path))
download_url = asset.browser_download_url
print(f"[INFO] Uploaded asset {asset_path}")
return download_url

def create_release(
repo_name,
prerelease=False,
asset_paths=[],
org=None,
token=None,
):
repo = get_github_repo(repo_name, org, token)
if prerelease:
bumped_tag = uuid4().hex
message = "Pre-release for download"
else:
bumped_tag = get_bumped_tag(repo)
message = "Official Release"
new_release = repo.create_git_release(
bumped_tag, bumped_tag, message, prerelease=prerelease
)
print(f"[INFO] Created release {bumped_tag} for {repo_name}")
asset_download_url = upload_assets(
new_release, tag_name=bumped_tag, asset_paths=asset_paths
)
return asset_download_url
18 changes: 17 additions & 1 deletion src/openpecha/pecha/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import shutil
from collections import defaultdict
from pathlib import Path
from typing import Dict, Generator, List, Optional, Tuple, Union
Expand All @@ -9,7 +10,7 @@

from openpecha import utils
from openpecha.config import PECHAS_PATH
from openpecha.github_utils import clone_repo
from openpecha.github_utils import clone_repo, create_release
from openpecha.ids import get_annotation_id, get_base_id, get_initial_pecha_id, get_uuid
from openpecha.pecha.blupdate import update_layer
from openpecha.pecha.layer import LayerEnum, get_layer_collection, get_layer_group
Expand Down Expand Up @@ -401,6 +402,8 @@ def get_layer(self, basefile_name: str, annotation_type: LayerEnum):

def publish(
self,
asset_path: Optional[Path] = None,
asset_name: Optional[str] = "source_data",
branch: Optional[str] = "main",
is_private: bool = False,
):
Expand Down Expand Up @@ -433,3 +436,16 @@ def prepare_repo_description(title):
is_private=is_private,
branch=branch,
)
asset_paths = []
if asset_path:
repo_name = self.id
shutil.make_archive(asset_path.parent / asset_name, "zip", asset_path)
asset_paths.append(f"{asset_path.parent / asset_name}.zip")
create_release(
repo_name,
prerelease=False,
asset_paths=asset_paths,
org=self.storage.org_name,
token=self.storage.token,
)
(asset_path.parent / f"{asset_name}.zip").unlink()

0 comments on commit a9a271f

Please sign in to comment.