# Path: scar/exceptions.py
# class GitHubTagNotFoundError(ScarError):
#     """
#     The specified tag was not found in the GitHub repository
# 
#     :ivar version: Tag used for the search
#     """
# 
#     fmt = "The tag '{tag}' was not found in the GitHub repository."
# 
# class YamlFileNotFoundError(ScarError):
#     """
#     The yaml configuration file does not exist
# 
#     :ivar file_path: Path of the file
#     """
#     fmt = "Unable to find the yaml file '{file_path}'"


import base64
import json
import os
import re
import shutil
import subprocess
import tarfile
import tempfile
import uuid
import sys
import yaml
import scar.logger as logger
import scar.http.request as request
from copy import deepcopy
from zipfile import ZipFile
from io import BytesIO
from typing import Optional, Dict, List, Generator, Union, Any, Tuple
from distutils import dir_util
from packaging import version
from scar.exceptions import GitHubTagNotFoundError, YamlFileNotFoundError

    @staticmethod
    def get_asset_url(user: str, project: str, asset_name: str,
                      tag_name: str='latest') -> Optional[str]:
        """Get the download asset url from the specified github tagged project."""
        if tag_name == 'latest':
            url = f'https://api.github.com/repos/{user}/{project}/releases/latest'
        else:
            if GitHubUtils.exists_release_in_repo(user, project, tag_name):
                url = f'https://api.github.com/repos/{user}/{project}/releases/tags/{tag_name}'
            else:
                raise GitHubTagNotFoundError(tag=tag_name)
        response = json.loads(request.get_file(url))
        if isinstance(response, dict) and 'assets' in response:
            for asset in response['assets']:
                if asset['name'] == asset_name:
                    return asset['browser_download_url']
        return None

    @staticmethod
    def get_source_code_url(user: str, project: str, tag_name: str='latest') -> str:
        """Get the source code's url from the specified github tagged project."""
        source_url = ""
        repo_url = ""
        if tag_name == 'latest':
            repo_url = f'https://api.github.com/repos/{user}/{project}/releases/latest'
        else:
            if GitHubUtils.exists_release_in_repo(user, project, tag_name):
                repo_url = f'https://api.github.com/repos/{user}/{project}/releases/tags/{tag_name}'
            else:

# Path: scar/exceptions.py
# class GitHubTagNotFoundError(ScarError):
#     """
#     The specified tag was not found in the GitHub repository
# 
#     :ivar version: Tag used for the search
#     """
# 
#     fmt = "The tag '{tag}' was not found in the GitHub repository."
# 
# class YamlFileNotFoundError(ScarError):
#     """
#     The yaml configuration file does not exist
# 
#     :ivar file_path: Path of the file
#     """
#     fmt = "Unable to find the yaml file '{file_path}'"


# Path: scar/utils.py
import base64
import json
import os
import re
import shutil
import subprocess
import tarfile
import tempfile
import uuid
import sys
import yaml
import scar.logger as logger
import scar.http.request as request
from copy import deepcopy
from zipfile import ZipFile
from io import BytesIO
from typing import Optional, Dict, List, Generator, Union, Any, Tuple
from distutils import dir_util
from packaging import version
from scar.exceptions import GitHubTagNotFoundError, YamlFileNotFoundError


    @staticmethod
    def get_asset_url(user: str, project: str, asset_name: str,
                      tag_name: str='latest') -> Optional[str]:
        """Get the download asset url from the specified github tagged project."""
        if tag_name == 'latest':
            url = f'https://api.github.com/repos/{user}/{project}/releases/latest'
        else:
            if GitHubUtils.exists_release_in_repo(user, project, tag_name):
                url = f'https://api.github.com/repos/{user}/{project}/releases/tags/{tag_name}'
            else:
                raise GitHubTagNotFoundError(tag=tag_name)
        response = json.loads(request.get_file(url))
        if isinstance(response, dict) and 'assets' in response:
            for asset in response['assets']:
                if asset['name'] == asset_name:
                    return asset['browser_download_url']
        return None

    @staticmethod
    def get_source_code_url(user: str, project: str, tag_name: str='latest') -> str:
        """Get the source code's url from the specified github tagged project."""
        source_url = ""
        repo_url = ""
        if tag_name == 'latest':
            repo_url = f'https://api.github.com/repos/{user}/{project}/releases/latest'
        else:
            if GitHubUtils.exists_release_in_repo(user, project, tag_name):
                repo_url = f'https://api.github.com/repos/{user}/{project}/releases/tags/{tag_name}'
            else:

                raise GitHubTagNotFoundError(tag=tag_name)