Skip to content

Commit

Permalink
feat(cli): add the draft description in the gas commit pop-up editor
Browse files Browse the repository at this point in the history
PR Closed: #815
  • Loading branch information
yeyong.yu authored and yuyouyu32 committed Jul 14, 2021
1 parent 1ca8515 commit 58fcf1d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
11 changes: 5 additions & 6 deletions tensorbay/cli/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import click

from .tbrn import TBRN, TBRNType
from .utility import edit_input, error, get_dataset_client, get_gas
from .utility import edit_message, error, format_hint, get_dataset_client, get_gas

_COMMIT_HINT = """{}
_COMMIT_HINT = """
# Please enter the commit message for your changes.
# The Default commit message is set as the draft title.
# Lines starting with '#' will be ignored.
Expand All @@ -32,11 +32,10 @@ def _implement_commit(obj: Dict[str, str], tbrn: str, message: Tuple[str, ...])
error(f'To commit, "{info}" must be in draft status, like "{info}#1"')

dataset_client.checkout(draft_number=info.draft_number)
if message:
title, description = message[0], "\n".join(message[1:])
else:
title, description = edit_input(_COMMIT_HINT.format(dataset_client.get_draft().title))

draft = dataset_client.get_draft()
hint_message = format_hint(draft.title, draft.description, _COMMIT_HINT)
title, description = edit_message(message, hint_message)
if not title:
error("Aborting commit due to empty commit message")

Expand Down
30 changes: 9 additions & 21 deletions tensorbay/cli/draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
from ..client.struct import ROOT_COMMIT_ID
from ..exception import ResourceNotExistError
from .tbrn import TBRN, TBRNType
from .utility import edit_input, error, get_dataset_client, get_gas
from .utility import edit_message, error, format_hint, get_dataset_client, get_gas

_DRAFT_HINT = """
# Please enter the title for your draft.
# Please enter the message for your draft.
# Lines starting with '#' will be ignored.
# And an empty draft title aborts the creation.
# And an empty draft message aborts the creation.
"""


Expand Down Expand Up @@ -46,13 +46,9 @@ def _create_draft(dataset_client: DatasetClientType, info: TBRN, message: Tuple[
if info.is_draft:
error(f'Create a draft in draft status "{info}" is not permitted')

if message:
title, description = message[0], "\n".join(message[1:])
else:
title, description = edit_input(_DRAFT_HINT)

title, description = edit_message(message, _DRAFT_HINT)
if not title:
error("Aborting creating draft due to empty draft title")
error("Aborting creating draft due to empty draft message")

dataset_client.create_draft(title=title, description=description)
status = dataset_client.status
Expand Down Expand Up @@ -105,19 +101,11 @@ def _edit_draft(dataset_client: DatasetClientType, info: TBRN, message: Tuple[st
if not info.is_draft:
error("Draft number is required when editing draft")

if message:
title, description = message[0], "\n".join(message[1:])
else:
draft = dataset_client.get_draft()
hint = [draft.title]
original_description = draft.description
if original_description:
hint.extend(["", original_description])
hint.append(_DRAFT_HINT)
title, description = edit_input("\n".join(hint))

draft = dataset_client.get_draft()
hint_message = format_hint(draft.title, draft.description, _DRAFT_HINT)
title, description = edit_message(message, hint_message)
if not title:
error("Aborting updating draft due to empty draft title")
error("Aborting updating draft due to empty draft message")

dataset_client.update_draft(title=title, description=description)
click.echo(f"{info.get_tbrn()} is updated successfully!")
Expand Down
40 changes: 39 additions & 1 deletion tensorbay/cli/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def get_dataset_client(gas: GAS, info: TBRN, is_fusion: Optional[bool] = None) -
return dataset_client


def edit_input(hint: str) -> Tuple[str, str]:
def _edit_input(hint: str) -> Tuple[str, str]:
"""Edit information input from the editor.
Arguments:
Expand Down Expand Up @@ -307,3 +307,41 @@ def shorten(origin: str) -> str:
"""
return origin[:7]


def format_hint(title: str, description: str, original_hint: str) -> str:
"""Generate complete hint message.
Arguments:
title: The title of the draft to edit or commit.
description: The description of the draft to edit or commit.
original_hint: The original hint message.
Returns:
The complete hint message.
"""
hint: Tuple[str, ...] = (title,)
if description:
hint += ("", description)
hint += (original_hint,)
return "\n".join(hint)


def edit_message(message: Tuple[str, ...], hint_message: str) -> Tuple[str, str]:
"""Edit draft information.
Arguments:
message: The message given in the CLI.
hint_message: The hint message to show on the pop-up editor.
Returns:
The extracted title and the description.
"""
if message:
title, description = message[0], "\n".join(message[1:])
else:
title, description = _edit_input(hint_message)

return title, description

0 comments on commit 58fcf1d

Please sign in to comment.