run.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env bash
  2. GOGS_HOST=""
  3. GOGS_PROTO=""
  4. GOGS_PORT=""
  5. GOGS_USER=""
  6. GOGS_GIT_USER=""
  7. GOGS_ACCESS_TOKEN=""
  8. GITEA_HOST=""
  9. GITEA_PROTO=""
  10. GITEA_PORT=""
  11. GITEA_USER=""
  12. GITEA_GIT_USER=""
  13. GITEA_ACCESS_TOKEN=""
  14. get_gogs_repos() {
  15. # -H "Authorization: token ${GOGS_ACCESS_TOKEN}" \
  16. curl -s \
  17. --request GET \
  18. "${GOGS_PROTO}://${GOGS_HOST}:${GOGS_PORT}/api/v1/repos/search?q=u&uid=1" \
  19. | jq -r '.data[].full_name'
  20. }
  21. create_gitea_repo() {
  22. local repo="$1"
  23. echo "${repo}: creating"
  24. curl -s \
  25. --request POST \
  26. -H "accept: application/json" \
  27. -H "Content-Type: application/json" \
  28. -H "Authorization: token ${GITEA_ACCESS_TOKEN}" \
  29. "${GITEA_PROTO}://${GITEA_HOST}:${GITEA_PORT}/api/v1/user/repos" \
  30. -d '{ "auto_init": true, "name": "'"${repo}"'", "private": false }'
  31. }
  32. delete_gitea_repo() {
  33. local repo="$1"
  34. echo "${repo}: deleting"
  35. curl -s \
  36. --request DELETE \
  37. -H "accept: application/json" \
  38. -H "Content-Type: application/json" \
  39. -H "Authorization: token ${GITEA_ACCESS_TOKEN}" \
  40. "${GITEA_PROTO}://${GITEA_HOST}:${GITEA_PORT}/api/v1/repos/${GITEA_USER}/${repo}"
  41. }
  42. rm -rf workspace
  43. while read -r line; do
  44. repo=$(basename "${line}")
  45. url="${GOGS_GIT_USER}@${GOGS_HOST}:${GOGS_USER}/${repo}.git"
  46. echo "${repo}: cloning"
  47. echo "${repo}: from ${url}"
  48. # https://git.wiki.kernel.org/index.php/Git_FAQ#How_do_I_clone_a_repository_with_all_remotely_tracked_branches.3F
  49. git clone --mirror "${url}" "workspace/${repo}/.git" 2> /dev/null
  50. cd "workspace/${repo}" || exit 1
  51. git config --bool core.bare false 2> /dev/null
  52. cd ../.. || exit 1
  53. done < <(get_gogs_repos)
  54. while read -r line; do
  55. repo=$(basename "${line}")
  56. url="${GITEA_GIT_USER}@${GITEA_HOST}:${GITEA_USER}/${repo}.git"
  57. cd "${line}" || exit 1
  58. if [ "$1" = "cleanup" ]; then
  59. delete_gitea_repo "${repo}"
  60. else
  61. create_gitea_repo "${repo}"
  62. git remote rename origin gogs
  63. git remote add gitea "${url}"
  64. git push --mirror gitea
  65. fi
  66. cd ../.. || exit 1
  67. done < <(find workspace -mindepth 1 -maxdepth 1 -type d;)