1
Fork 0
mirror of https://github.com/RGBCube/GitHub2Forgejo synced 2025-07-27 05:07:45 +00:00

Handle error when repo is already mirrored

This commit is contained in:
RGBCube 2024-05-28 13:46:20 +03:00
parent 949ae479df
commit 2f2c670948
Signed by: RGBCube
SSH key fingerprint: SHA256:CzqbPcfwt+GxFYNnFVCqoN5Itn4YFrshg1TrnACpA5M
2 changed files with 28 additions and 25 deletions

View file

@ -6,8 +6,8 @@ Plain simple, just install [Nushell](https://nushell.sh) and run the script:
./migrate.nu --help ./migrate.nu --help
``` ```
You can either specify all the CLI flags for a uninteractive run, You can either specify all the environment variables
or run the script like so: for a uninteractive run, or run the script like so:
```nu ```nu
./migrate.nu ./migrate.nu

View file

@ -1,7 +1,7 @@
#!/usr/bin/env nu #!/usr/bin/env nu
def str-or [default: closure] { def or-default [default: closure] {
if $in == null or $in == "<NONE>" { if ($in | is-empty) {
do $default do $default
} else { } else {
$in $in
@ -9,22 +9,26 @@ def str-or [default: closure] {
} }
# Migrates a GitHub users repositories to a Gitea or Forgejo instance. # 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 [ def main [
--github-user: string = "<NONE>" # The user to migrate from. ...repo_urls: string # The GitHub repo URLs to migrate to Gitea or Forgejo. If not given, all will be fetched.
--github-token: string = "<NONE>" # An access token for fetching private repositories. Optional.
--gitea-url: string = "<NONE>" # The URL to the Gitea or Forgejo instance
--gitea-user: string = "<NONE>" # The user to migrate the repositories to.
--gitea-token: string = "<NONE>" # An access token for the user to actually insert repositories to.
--strategy: string = "<NONE>" # 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.
] { ] {
let github_user = $github_user | str-or { input $"(ansi red)GitHub username: (ansi reset)" } let github_user = $env | get -i GITHUB_USER | or-default { 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_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_user = $env | get -i GITEA_USER | or-default { 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_token = $env | get -i GITEA_TOKEN | or-default { input $"(ansi green)Gitea access token: (ansi reset)" }
let gitea_uid = ( let gitea_uid = (
http get $"($gitea_url)/api/v1/users/($gitea_user)" http get $"($gitea_url)/api/v1/users/($gitea_user)"
@ -36,7 +40,7 @@ def main [
exit 1 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 { let repo_urls = if ($repo_urls | length) != 0 {
$repo_urls $repo_urls
@ -56,7 +60,7 @@ def main [
$repo_urls | each {|url| $repo_urls | each {|url|
let repo_name = $url | split row "/" | last let repo_name = $url | split row "/" | last
let repo_is_private = if $github_token == "" { let repo_is_private = if ($github_token | is-empty) {
false false
} else { } else {
( (
@ -82,7 +86,7 @@ def main [
] ]
{ {
clone_addr: $url clone_addr: $url
mirror: ($strategy == "Mirrored") mirror: ($strategy != "cloned")
private: $repo_is_private private: $repo_is_private
uid: $gitea_uid uid: $gitea_uid
@ -91,11 +95,10 @@ def main [
} }
) )
echo $" (ansi green)Success!" if ($response | get -i message | is-not-empty) {
print $" (ansi yellow)Already mirrored!"
echo ($response | to json) } else {
print $" (ansi green)Success!"
# TODO: Handle ratelimits, 409's and access failures. Also print a }
# nice message and options on what to do next on error.
} }
} }