Skip to content

Commit 98355ae

Browse files
committed
Validate auth token on init with dataverse
1 parent d1a5db3 commit 98355ae

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

src/hermes/commands/init/base.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def __init__(self, parser: argparse.ArgumentParser):
196196
self.tokens: dict = {}
197197
self.setup_method: str = ""
198198
self.deposit_platform: DepositPlatform = DepositPlatform()
199+
self.git_branch: str = ""
199200
self.git_remote: str = ""
200201
self.git_remote_url = ""
201202
self.git_hoster: GitHoster = GitHoster.Empty
@@ -344,7 +345,8 @@ def test_initialization(self) -> None:
344345
self.no_git_setup()
345346
sys.exit()
346347

347-
# Look at git remotes
348+
# Look at git branch & remotes
349+
self.git_branch = git_info.get_current_branch()
348350
remotes = git_info.get_remotes()
349351
if remotes:
350352
self.git_remote = remotes[0]
@@ -544,7 +546,7 @@ def create_zenodo_token(self) -> None:
544546
else:
545547
sc.echo(f"The token could not be validated by {self.deposit_platform.name}. "
546548
"Make sure to enter the complete token.\n"
547-
"(If this error persists, you should try switching to the manual setup mode.)",
549+
"(If this error persists, you should restart and switch to the manual setup mode.)",
548550
formatting=sc.Formats.WARNING)
549551

550552
def create_dataverse_token(self):
@@ -556,8 +558,20 @@ def create_dataverse_token(self):
556558
if self.setup_method == "m":
557559
sc.press_enter_to_continue()
558560
else:
559-
# TODO try to validate the token
560-
self.deposit_platform.token = sc.answer("Enter the token here: ")
561+
while True:
562+
token = sc.answer("Enter the token here: ")
563+
token_valid_url = f"{self.deposit_platform.url}/api/users/token"
564+
token_valid_response = requests.get(token_valid_url, headers={"X-Dataverse-key": token})
565+
if token_valid_response.ok:
566+
sc.echo(f"The token was validated by {self.deposit_platform.name}.",
567+
formatting=sc.Formats.OKGREEN)
568+
self.deposit_platform.token = token
569+
break
570+
else:
571+
sc.echo(f"The token could not be validated by {self.deposit_platform.name}. "
572+
"Make sure to enter the complete token.\n"
573+
"(If this error persists, you should restart and switch to the manual setup mode.)",
574+
formatting=sc.Formats.WARNING)
561575

562576
def create_rodare_token(self):
563577
token_url = urljoin(self.deposit_platform.url, "account/settings/applications/tokens/new/")
@@ -807,7 +821,8 @@ def choose_push_branch(self) -> None:
807821
push_choice = sc.choose(
808822
"When should the automated HERMES process start?",
809823
[
810-
"When I push a branch",
824+
"When I push on custom branch",
825+
f"When I push on current branch ({self.git_branch})",
811826
"When I push a specific tag (not implemented)",
812827
]
813828
)
@@ -818,6 +833,12 @@ def choose_push_branch(self) -> None:
818833
formatting=sc.Formats.OKGREEN)
819834
sc.echo()
820835
elif push_choice == 1:
836+
self.ci_parameters["push_branch"] = self.git_branch
837+
bold_branch = sc.Formats.BOLD.wrap_around(self.git_branch)
838+
sc.echo(f"The HERMES pipeline will be activated when you push on {bold_branch}",
839+
formatting=sc.Formats.OKGREEN)
840+
sc.echo()
841+
elif push_choice == 2:
821842
sc.echo("Setting up triggering by tags is currently not implemented.", formatting=sc.Formats.WARNING)
822843
sc.echo(f"You can visit {TUTORIAL_URL} to set it up manually later-on.", formatting=sc.Formats.WARNING)
823844

src/hermes/commands/init/util/git_info.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_valid_cwd(cwd="") -> str:
3030
return str(path)
3131

3232

33-
def run_git_command(command: str, cwd="") -> str:
33+
def run_git_command(command: str, cwd="", throw_exception=False) -> str:
3434
"""
3535
Runs any git command using subprocess. Raises Exception when returncode != 0.
3636
:param command: The command as string with or without the 'git' main command.
@@ -44,7 +44,7 @@ def run_git_command(command: str, cwd="") -> str:
4444
if command_list[0] != "git":
4545
command_list.insert(0, "git")
4646
# Run subprocess
47-
result = subprocess.run(command_list, cwd=cwd, capture_output=True, text=True)
47+
result = subprocess.run(command_list, cwd=cwd, capture_output=True, text=True, check=throw_exception)
4848
# Return output or error
4949
if result.returncode != 0:
5050
raise Exception(result.stderr)
@@ -87,11 +87,11 @@ def get_current_branch() -> str:
8787
"""
8888
Returns the name of the current branch.
8989
"""
90-
branch_info = run_git_command("branch")
91-
for line in branch_info.splitlines():
92-
if line.startswith("*"):
93-
return line.split()[1].strip()
94-
raise Exception("Current branch not found.")
90+
try:
91+
branch_name = run_git_command("rev-parse --abbrev-ref HEAD", throw_exception=True)
92+
return branch_name.strip()
93+
except subprocess.CalledProcessError:
94+
return ""
9595

9696

9797
def is_git_installed() -> bool:

0 commit comments

Comments
 (0)