source: spip-zone/_outils_/importToGitea/trunk/ImportOneRepository.sh @ 119890

Last change on this file since 119890 was 119890, checked in by cedric@…, 2 years ago

Ajout d'une option -t/--target pour definir le nom du repository cible (par defaut calcule d'apres la source)

  • Property svn:executable set to *
File size: 4.0 KB
Line 
1#!/usr/bin/env bash
2
3hash curl 2>/dev/null || { echo >&2 "curl is required. Stopped."; exit 1; }
4hash jq 2>/dev/null || { echo >&2 "jq is required. Stopped."; exit 1; }
5
6# Manage arguments
7while [[ $# -gt 0 ]]
8do
9key="$1"
10
11case $key in
12    -r|--repository)
13    REPOSITORY="$2"
14    shift # past argument
15    shift # past value
16    ;;
17    -o|--organization)
18    ORGANIZATION="$2"
19    shift # past argument
20    shift # past value
21    ;;
22    -t|--target)
23    TARGET="$2"
24    shift # past argument
25    shift # past value
26    ;;
27    -m|--model)
28    MODEL="$2"
29    shift # past argument
30    shift # past value
31    ;;
32    -d|--delete)
33    DELETE=1
34    shift # past argument
35    ;;
36    -h|--help)
37    shift # past argument
38    printf "ImportOneRepository, a command line interface to import a svn repository to gitea"
39    printf "  -r <repository> or --repository <reposoitoy> \\t\\t specify the svn repository name to import"
40        printf "  -o <organization> or --orgnanization <organization> \\t gitea organization where to import repository."
41        printf "  -m <model> or --model <model> \\t\\t specify the model svn structure to respesct (dist or zone)"
42        printf "  -d or --delete \\t\\t Remove gitea repository first"
43        exit 0
44    ;;
45    --*)    # unknown option
46    cli_output "Invalid option: ${1}. Type --help to show help" red notime
47    shift
48    exit 1
49    ;;
50    -*)    # unknown option
51    cli_output "Invalid option: ${1}. Type --help to show help" red notime
52    shift
53    exit 1
54    ;;
55    *)    # unknown option
56    FILES+=("$1") # save it in an array for later
57    shift # past argument
58    ;;
59esac
60done
61set -- "${FILES[@]}" # restore positional parameters
62
63#Control requirement configuration
64if [ ! -f ImportToGitea.conf ]; then
65    echo "Gitea configuration not set, can't continue"
66    exit 1
67fi
68
69. ImportToGitea.conf
70
71#Control model
72if [ ! -f "models/${MODEL}.conf" ]; then
73    echo "Model is not set, can't continue"
74    exit 1
75fi
76
77if [ -z "${ORGANIZATION}" ]; then
78    echo "Organization missing, can't continue"
79    exit 1
80fi
81
82if [ -z "${REPOSITORY}" ]; then
83    echo "repository missing, can't continue"
84    exit 0
85fi
86
87if [ -z "${TARGET}" ]; then
88    TARGET=$(basename "$REPOSITORY")
89fi
90
91echo "$REPOSITORY will be import from svn following $MODEL to $ORGANIZATION"
92
93#Disting svn and git repository
94repository_svn="$REPOSITORY"
95repository_git="$TARGET"
96
97#Import svn to git
98repo_source=$TMP_SUBGIT_DIR/$repository_svn
99#Restore subgit configuration
100repo_target=$GITEA_REPO/$ORGANIZATION/$repository_git.git
101#LowerCase
102repo_target=${repo_target,,}
103
104mkdir -p "$repo_source"
105subgit configure --svn-url svn://svn.tld "$repo_source"
106cp "models/${MODEL}.conf" "$repo_source/subgit/config"
107sed -i "s#__PLUGIN__#$repository_svn#" "$repo_source/subgit/config"
108subgit install "$repo_source"
109subgit uninstall "$repo_source"
110
111if [ ! -z "$DELETE" ]; then
112    subgit uninstall "$repo_target"
113    curl -H "Authorization: token $GITEA_TOKEN" \
114            -X "DELETE" \
115            "$GITEA_HOST:$GITEA_PORT/api/v1/repos/$ORGANIZATION/$repository_git"
116fi
117
118#Create Gitea repository
119ORGANIZATION_ID=$(curl -sH "Authorization: token $GITEA_TOKEN" \
120        "$GITEA_HOST:$GITEA_PORT/api/v1/orgs/$ORGANIZATION" | jq ".id")
121TEAM_ID=$(curl -sH "Authorization: token $GITEA_TOKEN" \
122        "$GITEA_HOST:$GITEA_PORT/api/v1/orgs/$ORGANIZATION/teams" |jq '.[] | select(.name == "contrib") | .id ')
123
124
125curl -H "Authorization: token $GITEA_TOKEN" \
126        --data "clone_addr=$repo_source&uid=$ORGANIZATION_ID&repo_name=$repository_git" \
127        "$GITEA_HOST:$GITEA_PORT/api/v1/repos/migrate"
128
129curl -X PUT \
130        -H "Authorization: token $GITEA_TOKEN" \
131        "https://git.spip.net/api/v1/teams/$TEAM_ID/repos/$ORGANIZATION/$repository_git"
132
133#Restore subgit configuration
134cp -bar "$repo_source/"{subgit,svn,db,logs} "$repo_target/"
135cd "$repo_target/" || exit 1
136rename 's/(hooks\/)(.*receive).d$/$1user-$2.d/' hooks/*
137subgit install "$repo_target"
138sudo subgit register --key /var/git/subgit/subgit.key "$repo_target"
139
140#Clean Directory
141rm -rf "$repo_source"
142
143sudo chown "$GIT_UID:$GIT_GID" -R "$repo_target"
Note: See TracBrowser for help on using the repository browser.