From 93aa2a5150f5e512d72e36c5614687c33fef0014 Mon Sep 17 00:00:00 2001 From: jboursier Date: Tue, 31 Jan 2023 19:14:13 +0100 Subject: [PATCH] Implement unarchiving one or many repositories Signed-off-by: jboursier --- src/cli.py | 64 +++++++++++++++++++++++++++++- src/ghas_cli/utils/repositories.py | 6 ++- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/cli.py b/src/cli.py index e20a8e7..95f495f 100644 --- a/src/cli.py +++ b/src/cli.py @@ -477,7 +477,33 @@ def repositories_archive( token: str, ) -> None: """Archive a repository""" - click.echo(repositories.archive(organization, token, repository)) + click.echo(repositories.archive(organization, token, repository, archive=True)) + + +@repositories_cli.command("unarchive") +@click.option( + "-r", + "--repository", + prompt="Repository name", +) +@click.option( + "-t", + "--token", + prompt=False, + type=str, + default=None, + hide_input=True, + confirmation_prompt=False, + show_envvar=True, +) +@click.option("-o", "--organization", prompt="Organization name", type=str) +def repositories_unarchive( + repository: str, + organization: str, + token: str, +) -> None: + """Unarchive a repository""" + click.echo(repositories.archive(organization, token, repository, archive=False)) ######### @@ -1177,13 +1203,47 @@ def mass_archive( click.echo(f"{repo}...", nl=False) if repositories.archive( - organization=organization, token=token, repository=repo + organization=organization, token=token, repository=repo, archive=True ): click.echo(" Archived.") else: click.echo(" Not Archived.", err=True) +@mass_cli.command("unarchive") +@click.argument("input_repos_list", type=click.File("r")) +@click.option( + "-t", + "--token", + prompt=False, + type=str, + default=None, + hide_input=True, + confirmation_prompt=False, + show_envvar=True, +) +@click.option("-o", "--organization", prompt="Organization name", type=str) +def mass_unarchive( + input_repos_list: Any, + organization: str, + token: str, +) -> None: + repos_list = input_repos_list.readlines() + + for repo in repos_list: + + repo = repo.rstrip("\n") + + click.echo(f"{repo}...", nl=False) + + if repositories.archive( + organization=organization, token=token, repository=repo, archive=False + ): + click.echo(" Unarchived.") + else: + click.echo(" Not Unarchived.", err=True) + + @mass_cli.command("issue_upcoming_archive") @click.argument("input_repos_list", type=click.File("r")) @click.option( diff --git a/src/ghas_cli/utils/repositories.py b/src/ghas_cli/utils/repositories.py index 691b5a0..4166d3a 100644 --- a/src/ghas_cli/utils/repositories.py +++ b/src/ghas_cli/utils/repositories.py @@ -240,10 +240,12 @@ def get_default_branch_last_updated( ) -def archive(organization: str, token: str, repository: str) -> bool: +def archive( + organization: str, token: str, repository: str, archive: bool = True +) -> bool: headers = network.get_github_headers(token) - payload = {"archived": True} + payload = {"archived": archive} status = requests.patch( url=f"https://api.github.com/repos/{organization}/{repository}",