aboutsummaryrefslogtreecommitdiffstats
path: root/gh-clone.sh
blob: 1cefa328b683a332a09e4da22b28b101d02fe1fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
set -eux

USERNAME=$(git config github.user)
PASSWORD=$(pass show dev/github.com|awk '/^token:/ {print $2}')
ORGANIZATION="openSUSE-Python"
cd /home/pymirrors/repos/

ORIG_REPO="$1"

# Argument can be both full URL as well as just owner/reponame string
if [[ "$ORIG_REPO" =~ "http" ]]; then
    OR="${ORIG_REPO##*.com/}"
    ORIG_REPO="${OR%%.git}"
fi

GH_TMP_DATA=$(mktemp /tmp/gh_data.XXXXXX.json) || exit 1
trap 'rm -f "${GH_TMP_DATA}"' EXIT

DIRNAME="$(basename "${ORIG_REPO}")"
NEW_REPO="${ORGANIZATION}/${DIRNAME}"

# Repository has been already checked out
[ -d "$DIRNAME" ] && exit 0

git clone "https://github.com/${ORIG_REPO}.git"

cd "${DIRNAME}"

curl -s -u "${USERNAME}:${PASSWORD}" \
     --json "{ \"organization\": \"${ORGANIZATION}\" , \
               \"default_branch_only\": \"true\" }" \
    "https://api.github.com/repos/${ORIG_REPO}/forks" >"${GH_TMP_DATA}"
git remote add github \
    "$(awk -F '"' '/clone_url/ {print $4 ; exit}' "${GH_TMP_DATA}")"
git remote set-url --push github \
    "$(awk -F '"' '/ssh_url/ {print $4 ; exit}' "${GH_TMP_DATA}")"
git remote update
git remote -v