Skip to content

Instantly share code, notes, and snippets.

@gregdel
Last active April 26, 2022 01:01
Show Gist options
  • Save gregdel/870d58c80b80172445dc93294531d88c to your computer and use it in GitHub Desktop.
Save gregdel/870d58c80b80172445dc93294531d88c to your computer and use it in GitHub Desktop.
Gitlab to gogs migration
#!/bin/sh
set -e
_usage() {
echo "Usage: $0 [--user|org] USER_OR_ORG_NAME GOGS_REPO_NAME GITLAB_HTTPS_URL"
exit 0
}
MODE=$1
GOGS_USERNAME=$2
GOGS_REPO_NAME=$3
GITLAB_CLONE_URL=$4
case $MODE in
--user|--org);;
*) _usage ;;
esac
[ -z "$GITLAB_CLONE_URL" ] && _usage
[ -z "$GOGS_REPO_NAME" ] && _usage
[ -z "$GOGS_USERNAME" ] && _usage
echo "Migrating $GITLAB_CLONE_URL to $GOGS_USERNAME/$GOGS_REPO_NAME"
GOGS_URL=${GOGS_URL:-"https://yourgogs.url"}
GOGS_TOKEN=${GOGS_TOKEN:-"YOUR GOGS TOKEN"}
GITLAB_USERNAME=${GITLAB_USERNAME:-"your_gitlab_username"}
GITLAB_TOKEN=${GITLAB_TOKEN:-"your_gitlab_token"}
_api_call() {
# shellcheck disable=SC2086
curl -s -L -H "Authorization: token $GOGS_TOKEN" \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
"$GOGS_URL/api/v1/"$*
}
GOGS_UID=
[ "$MODE" = "--user" ] && GOGS_UID=$(_api_call "users/$GOGS_USERNAME" | jq .id)
[ "$MODE" = "--org" ] && GOGS_UID=$(_api_call "orgs/$GOGS_USERNAME" | jq .id)
echo "$GOGS_USERNAME has the user id $GOGS_UID"
migration_data=$(jq -n -c \
--arg name "$GOGS_REPO_NAME" \
--arg uid "$GOGS_UID" \
--arg clone_url "$GITLAB_CLONE_URL" \
--arg auth_username "$GITLAB_USERNAME" \
--arg auth_password "$GITLAB_TOKEN" \
'{
repo_name: $name,
clone_addr: $clone_url,
auth_username: $auth_username,
auth_password: $auth_password,
uid: $uid | tonumber,
private: true,
}'
)
_api_call repos/migrate --data "$migration_data" | jq .
@jorgedferreira
Copy link

Hi. Actually the token didn't work for me.

After some googling I saw some guys having a similar issue and they solved it by using gitlab's user password, I tested it and it worked.

Thanks for you great script! I have more than 80 repos to move, but now it's way easier!

@hero9
Copy link

hero9 commented Oct 25, 2018

Hello @gregdel !
I want to migrate my self hosted gogs to self-hosted gitlab.
Do YOU have any idea how can I do it with less pain?
Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment