Browse Source

init repo

jenkins-x-bot 2 months ago
commit
9d99ddb66f
2 changed files with 82 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 81 0
      run.sh

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+workspace/

+ 81 - 0
run.sh

@@ -0,0 +1,81 @@
+#!/usr/bin/env bash
+
+GOGS_HOST=""
+GOGS_PROTO=""
+GOGS_PORT=""
+GOGS_USER=""
+GOGS_GIT_USER=""
+GOGS_ACCESS_TOKEN=""
+
+GITEA_HOST=""
+GITEA_PROTO=""
+GITEA_PORT=""
+GITEA_USER=""
+GITEA_GIT_USER=""
+GITEA_ACCESS_TOKEN=""
+
+get_gogs_repos() {
+#  -H "Authorization: token ${GOGS_ACCESS_TOKEN}" \
+  curl -s \
+    --request GET \
+    "${GOGS_PROTO}://${GOGS_HOST}:${GOGS_PORT}/api/v1/repos/search?q=u&uid=1" \
+    | jq -r '.data[].full_name'
+}
+
+create_gitea_repo() {
+  local repo="$1"
+
+  echo "${repo}: creating"
+  curl \
+    --request POST \
+    -H "accept: application/json" \
+    -H "Content-Type: application/json" \
+    -H "Authorization: token ${GITEA_ACCESS_TOKEN}" \
+    "${GITEA_PROTO}://${GITEA_HOST}:${GITEA_PORT}/api/v1/user/repos" \
+    -d '{ "auto_init": true, "name": "'"${repo}"'", "private": false }'
+  echo
+}
+
+delete_gitea_repo() {
+  local repo="$1"
+
+  echo "${repo}: deleting"
+  curl \
+    --request DELETE \
+    -H "accept: application/json" \
+    -H "Content-Type: application/json" \
+    -H "Authorization: token ${GITEA_ACCESS_TOKEN}" \
+    "${GITEA_PROTO}://${GITEA_HOST}:${GITEA_PORT}/api/v1/repos/${GITEA_USER}/${repo}"
+  echo
+}
+
+while read -r line; do
+  repo=$(basename "${line}")
+  url="${GOGS_GIT_USER}@${GOGS_HOST}:${GOGS_USER}/${repo}.git"
+
+  echo "${repo}: cloning"
+  echo "${repo}: from ${url}"
+
+  # https://git.wiki.kernel.org/index.php/Git_FAQ#How_do_I_clone_a_repository_with_all_remotely_tracked_branches.3F
+  git clone --mirror "${url}" "workspace/${repo}/.git" 2> /dev/null
+  cd "workspace/${repo}" || exit 1
+  git config --bool core.bare false 2> /dev/null
+  cd ../.. || exit 1
+done < <(get_gogs_repos)
+
+while read -r line; do
+  repo=$(basename "${line}")
+  url="${GITEA_GIT_USER}@${GITEA_HOST}:${GITEA_USER}/${repo}.git"
+
+  cd "${line}" || exit 1
+  create_gitea_repo "${repo}"
+  #delete_gitea_repo "${repo}"
+
+  git remote rename origin gogs
+  git remote add gitea "${url}"
+  git remote -v
+  git push --mirror gitea
+  cd ../.. || exit 1
+
+
+done < <(find . -mindepth 2 -maxdepth 2 -type d -not -name workspace;)