From 2f2c6709489d5be44bbb540e7cf75a0bd0f0f8e8 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Tue, 28 May 2024 13:46:20 +0300 Subject: [PATCH] Handle error when repo is already mirrored --- README.md | 4 ++-- migrate.nu | 49 ++++++++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 257605e..fe16e17 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ Plain simple, just install [Nushell](https://nushell.sh) and run the script: ./migrate.nu --help ``` -You can either specify all the CLI flags for a uninteractive run, -or run the script like so: +You can either specify all the environment variables +for a uninteractive run, or run the script like so: ```nu ./migrate.nu diff --git a/migrate.nu b/migrate.nu index f5cb993..1d26bc0 100755 --- a/migrate.nu +++ b/migrate.nu @@ -1,7 +1,7 @@ #!/usr/bin/env nu -def str-or [default: closure] { - if $in == null or $in == "" { +def or-default [default: closure] { + if ($in | is-empty) { do $default } else { $in @@ -9,22 +9,26 @@ def str-or [default: closure] { } # Migrates a GitHub users repositories to a Gitea or Forgejo instance. +# +# Here is the accepted environment variables, if one isn't set, it will +# be prompted for: +# +# GITHUB_USER: The user to migrate from. +# GITHUB_TOKEN: An access token for fetching private repositories. Optional. +# GITEA_URL: The URL to the Gitea or Forgejo instance +# GITEA_USER: The user to migrate the repositories to. +# GITEA_TOKEN: An access token for the user to actually insert repositories to. +# STRATEGY: The strategy. Valid options are "mirrored" or "cloned" (case insensitive). def main [ - --github-user: string = "" # The user to migrate from. - --github-token: string = "" # An access token for fetching private repositories. Optional. - --gitea-url: string = "" # The URL to the Gitea or Forgejo instance - --gitea-user: string = "" # The user to migrate the repositories to. - --gitea-token: string = "" # An access token for the user to actually insert repositories to. - --strategy: string = "" # The strategy. Valid options are "Mirrored" or "Cloned" (case sensitive). - ...repo_urls: string # The GitHub repo URLs to migrate to Gitea or Forgejo. If not given, all will be fetched. + ...repo_urls: string # The GitHub repo URLs to migrate to Gitea or Forgejo. If not given, all will be fetched. ] { - let github_user = $github_user | str-or { input $"(ansi red)GitHub username: (ansi reset)" } - let github_token = $github_token | str-or { input $"(ansi red)GitHub access token (ansi yellow)\((ansi blue)optional, only used for private repositories(ansi yellow))(ansi red): (ansi reset)" } + let github_user = $env | get -i GITHUB_USER | or-default { input $"(ansi red)GitHub username: (ansi reset)" } + let github_token = $env | get -i GITHUB_TOKEN | or-default { input $"(ansi red)GitHub access token (ansi yellow)\((ansi blue)optional, only used for private repositories(ansi yellow))(ansi red): (ansi reset)" } - let gitea_url = $gitea_url | str-or { input $"(ansi green)Gitea instance URL: (ansi reset)" } | str trim --right --char "/" + let gitea_url = $env | get -i GITEA_URL | or-default { input $"(ansi green)Gitea instance URL \(with https://): (ansi reset)" } | str trim --right --char "/" - let gitea_user = $gitea_user | str-or { input $"(ansi green)Gitea username or organization to migrate to: (ansi reset)" } - let gitea_token = $gitea_token | str-or { input $"(ansi green)Gitea access token: (ansi reset)" } + let gitea_user = $env | get -i GITEA_USER | or-default { input $"(ansi green)Gitea username or organization to migrate to: (ansi reset)" } + let gitea_token = $env | get -i GITEA_TOKEN | or-default { input $"(ansi green)Gitea access token: (ansi reset)" } let gitea_uid = ( http get $"($gitea_url)/api/v1/users/($gitea_user)" @@ -36,7 +40,7 @@ def main [ exit 1 } - let strategy = $strategy | str-or { [ Mirrored Cloned ] | input list $"(ansi cyan)Should the repos be mirrored, or just cloned once? (ansi reset)" } + let strategy = $env | get -i STRATEGY | or-default { [ Mirrored Cloned ] | input list $"(ansi cyan)Should the repos be mirrored, or just cloned once? (ansi reset)" } | str downcase let repo_urls = if ($repo_urls | length) != 0 { $repo_urls @@ -56,7 +60,7 @@ def main [ $repo_urls | each {|url| let repo_name = $url | split row "/" | last - let repo_is_private = if $github_token == "" { + let repo_is_private = if ($github_token | is-empty) { false } else { ( @@ -82,7 +86,7 @@ def main [ ] { clone_addr: $url - mirror: ($strategy == "Mirrored") + mirror: ($strategy != "cloned") private: $repo_is_private uid: $gitea_uid @@ -91,11 +95,10 @@ def main [ } ) - echo $" (ansi green)Success!" - - echo ($response | to json) - - # TODO: Handle ratelimits, 409's and access failures. Also print a - # nice message and options on what to do next on error. + if ($response | get -i message | is-not-empty) { + print $" (ansi yellow)Already mirrored!" + } else { + print $" (ansi green)Success!" + } } }