# Path: valohai_cli/api.py
# def request(self, method, url, **kwargs) -> Response:  # type: ignore
#     api_error_class = kwargs.pop('api_error_class', APIError)
#     handle_errors = bool(kwargs.pop('handle_errors', True))
#     try:
#         resp = super().request(method, url, **kwargs)
#     except requests.ConnectionError as ce:
#         host = urlparse(ce.request.url).netloc
#         if 'Connection refused' in str(ce):
#             raise CLIException(
#                 f'Unable to connect to {host} (connection refused); try again soon.'
#             ) from ce
#         raise
# 
#     if handle_errors and resp.status_code >= 400:
#         cls = (APINotFoundError if resp.status_code == 404 else api_error_class)
#         raise cls(resp)
#     return resp
# 
# Path: valohai_cli/ctx.py
# @overload
# def get_project(dir: Optional[str] = None, require: Literal[True] = True) -> ProjectOrRemoteProject:
#     ...
# 
# Path: valohai_cli/messages.py
# def success(message: str, err: bool = True) -> None:
#     click.echo(_format_message(message, SUCCESS_EMOJI, 'Success!', 'green'), err=err)


from logging import info, warning
from valohai_cli.api import request
from valohai_cli.ctx import get_project
from valohai_cli.messages import success
import click




@click.command()
def fetch() -> None:
    """
    Fetch new commits for the linked project.
    """
    project = get_project(require=True)
    resp = request('post', f'/api/v0/projects/{project.id}/fetch/')
    data = resp.json()
    commits = data.get('commits', ())
    if commits:
        for commit in commits:
            success(f"Fetched: {commit['ref']} ({commit['identifier']})")

# Path: valohai_cli/api.py
# def request(self, method, url, **kwargs) -> Response:  # type: ignore
#     api_error_class = kwargs.pop('api_error_class', APIError)
#     handle_errors = bool(kwargs.pop('handle_errors', True))
#     try:
#         resp = super().request(method, url, **kwargs)
#     except requests.ConnectionError as ce:
#         host = urlparse(ce.request.url).netloc
#         if 'Connection refused' in str(ce):
#             raise CLIException(
#                 f'Unable to connect to {host} (connection refused); try again soon.'
#             ) from ce
#         raise
# 
#     if handle_errors and resp.status_code >= 400:
#         cls = (APINotFoundError if resp.status_code == 404 else api_error_class)
#         raise cls(resp)
#     return resp
# 
# Path: valohai_cli/ctx.py
# @overload
# def get_project(dir: Optional[str] = None, require: Literal[True] = True) -> ProjectOrRemoteProject:
#     ...
# 
# Path: valohai_cli/messages.py
# def success(message: str, err: bool = True) -> None:
#     click.echo(_format_message(message, SUCCESS_EMOJI, 'Success!', 'green'), err=err)


# Path: valohai_cli/commands/project/fetch.py
from logging import info, warning
from valohai_cli.api import request
from valohai_cli.ctx import get_project
from valohai_cli.messages import success
import click





@click.command()
def fetch() -> None:
    """
    Fetch new commits for the linked project.
    """
    project = get_project(require=True)
    resp = request('post', f'/api/v0/projects/{project.id}/fetch/')
    data = resp.json()
    commits = data.get('commits', ())
    if commits:
        for commit in commits:
            success(f"Fetched: {commit['ref']} ({commit['identifier']})")

        success(f'{len(commits)} new commits were fetched!')