commit 3f74e793ad447d28eb2a54ed4b910d87e4df8fdb Author: Juergen Hoetzel Date: Sat Jun 20 15:01:58 2020 +0200 Initial import diff --git a/README.org b/README.org new file mode 100644 index 0000000..0bdb4b1 --- /dev/null +++ b/README.org @@ -0,0 +1,26 @@ +#+title: Mirror your starred Github repos to a Gitea organization + +* Inspiration for this script + +[[https://github.com/go-gitea/gitea/issues/8424][Proposal: Mirror an entire github organization into gitea organization]] + +I wanted to mirror my starred repos instead. Mirroring an organization is on the [[*TODOs][TODOs]] list. + +* Usage + +Using [[https://github.com/][GitHub]] user =juergenhoetzel= and Gitea organization =githubmirror= + +#+BEGIN_SRC bash + export ACCESS_TOKEN=3a765a661619136db92aa267d90c457573x98812 + export GITEA_URL=http://gitea.fritz.box:3000 + github2gitea-mirror juergenhoetzel githubmirror +#+END_SRC + + +* TODOs + - [ ] Mirror github organization + - [ ] Mirror private repos + + + + diff --git a/github2gitea-mirror b/github2gitea-mirror new file mode 100755 index 0000000..28c1007 --- /dev/null +++ b/github2gitea-mirror @@ -0,0 +1,56 @@ +#!/bin/bash -e +# Mirror starred Github repos to a Gitea Organization +CURL="curl -f" + +if [[ -z "${ACCESS_TOKEN}" || -z "${GITEA_URL}" ]];then + echo -e "Please set gitea access token and url in environment:\nexport ACCESS_TOKEN=abc\nexport GITEA_URL=http://gitea:3000\n" >&2 + echo -e "Don't use trailing slash in URL!" + exit 1 +fi + +github_user="${1}" +gitea_organization="${2}" + +if [[ -z "${github_user}" || -z "${gitea_organization}" ]]; then + echo "Usage: $0 github_user gitea_organization" >&2 + exit 1 +fi + +header_options=(-H "Authorization: Bearer ${ACCESS_TOKEN}" -H "accept: application/json" -H "Content-Type: application/json") +jsonoutput=$(mktemp -d -t github-repos-XXXXXXXX) +trap "rm -rvf ${jsonoutput}" EXIT + +uid=$($CURL -s "${header_options[@]}" $GITEA_URL/api/v1/orgs/${gitea_organization} |jq .id) + +fetch_starred_repos(){ + i=1 + # Github API just returns empty arrays instead of 404 + while $CURL "https://api.github.com/users/${github_user}/starred?page=${i}&per_page=100" >${jsonoutput}/${i}.json \ + && (( $(jq <${jsonoutput}/${i}.json '.|length') > 0 )) ; do + (( i++ )) + done +} + +create_migration_repo() { + if ! $CURL -w "%{http_code}\n" "${header_options[@]}" -d @- -X POST $GITEA_URL/api/v1/repos/migrate > ${jsonoutput}/result.txt 2>${jsonoutput}/stderr.txt;then + local code=$(<${jsonoutput}/result.txt) + if (( code != 409 ));then # 409 == repo already exits + cat ${jsonoutput}/stderr.txt >&2 + fi + fi +} + +repos_to_migration() { + for f in ${jsonoutput}/*.json; do + n=$(jq '.|length'<$f) + (( n-- )) # last element + for i in $(seq 0 $n); do + jq ".[$i]|.uid=${uid}|.mirror=true|.clone_addr=.clone_url|.repo_name=.name|{uid,repo_name,clone_addr,description}" <$f \ + | create_migration_repo + done + done +} + +fetch_starred_repos +repos_to_migration +