added support for starred

This commit is contained in:
Askill 2024-04-06 16:36:38 +02:00
parent 37686e894e
commit 403056e17b
4 changed files with 20 additions and 8 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"nuxt.isNuxtApp": false
}

8
Dockerfile Normal file
View File

@ -0,0 +1,8 @@
FROM bash:4.3.48-alpine3.19
COPY github-git-mirror.sh /
ENV LOCAL_GIT_ACCESS_TOKEN=""
ENV LOCAL_GIT_URL=""
ENV GIT_USER=""
ENV TARGET_ORG=""
ENV MODDE=""
CMD ["bash", "/github-git-mirror.sh", "${GIT_USER}", "${TARGET_ORG}", "${MODE}"]

View File

@ -12,7 +12,7 @@ Using [[https://github.com/][GitHub]] user =juergenhoetzel= and Gitea organizati
#+BEGIN_SRC bash
export ACCESS_TOKEN=3a765a661619136db92aa267d90c457573x98812
export GITEA_URL=http://gitea.fritz.box:3000
export LOCAL_GIT_URL=http://gitea.fritz.box:3000
github2gitea-mirror juergenhoetzel githubmirror
#+END_SRC

15
github2gitea-mirror → github-git-mirror.sh Executable file → Normal file
View File

@ -5,37 +5,39 @@ set -euo pipefail
# Mirror starred Github repos to a Gitea Organization
CURL="curl -f -S -s"
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
if [[ -z "${LOCAL_GIT_ACCESS_TOKEN}" || -z "${LOCAL_GIT_URL}" ]];then
echo -e "Please set gitea LOCAL_GIT_ACCESS token and url in environment:\nexport LOCAL_GIT_ACCESS_TOKEN=abc\nexport LOCAL_GIT_URL=http://gitea:3000\n\nexport MODE=repos\n" >&2
echo -e "Don't use trailing slash in URL!"
exit 1
fi
github_user="${1:-}"
gitea_organization="${2:-}"
# starred or repos2
mode="${3:-}"
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")
header_options=(-H "Authorization: Bearer ${LOCAL_GIT_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 "${header_options[@]}" $GITEA_URL/api/v1/orgs/${gitea_organization} |jq .id)
uid=$($CURL "${header_options[@]}" $LOCAL_GIT_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 \
while $CURL "https://api.github.com/users/${github_user}/${mode}?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
if ! $CURL -w "%{http_code}\n" "${header_options[@]}" -d @- -X POST $LOCAL_GIT_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
@ -56,4 +58,3 @@ repos_to_migration() {
fetch_starred_repos
repos_to_migration