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

Last change on this file since 117792 was 117792, checked in by cam.lafit, 6 weeks ago

Distinguer le dépot git du depot svn

  • Sur la zone l'aborescence n'est pas toujours de la forme ORGANISATION/DEPOT
  • On prend le cas simple où le depot git est le dernier noeud du chemin svn
  • Property svn:executable set to *
File size: 3.8 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    -m|--model)
23    MODEL="$2"
24    shift # past argument
25    shift # past value
26    ;;
27    -d|--delete)
28    DELETE=1
29    shift # past argument
30    ;;
31    -h|--help)
32    shift # past argument
33    printf "ImportOneRepository, a command line interface to import a svn repository to gitea"
34    printf "  -r <repository> or --repository <reposoitoy> \\t\\t specify the svn repository name to import"
35        printf "  -o <organization> or --orgnanization <organization> \\t gitea organization where to import repository."
36        printf "  -m <model> or --model <model> \\t\\t specify the model svn structure to respesct (dist or zone)"
37        printf "  -d or --delete \\t\\t Remove gitea repository first"
38        exit 0
39    ;;
40    --*)    # unknown option
41    cli_output "Invalid option: ${1}. Type --help to show help" red notime
42    shift
43    exit 1
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    FILES+=("$1") # save it in an array for later
52    shift # past argument
53    ;;
54esac
55done
56set -- "${FILES[@]}" # restore positional parameters
57
58#Control requirement configuration
59if [ ! -f ImportToGitea.conf ]; then
60    echo "Gitea configuration not set, can't continue"
61    exit 1
62fi
63
64. ImportToGitea.conf
65
66#Control model
67if [ ! -f "models/${MODEL}.conf" ]; then
68    echo "Model is not set, can't continue"
69    exit 1
70fi
71
72if [ -z "${ORGANIZATION}" ]; then
73    echo "Organization missing, can't continue"
74    exit 1
75fi
76
77if [ -z "${REPOSITORY}" ]; then
78    echo "repository missing, can't continue"
79    exit 0
80fi
81
82echo "$REPOSITORY will be import from svn following $MODEL to $ORGANIZATION"
83
84#Disting svn and git repository
85repository_svn="$REPOSITORY"
86repository_git=$(basename "$REPOSITORY")
87
88#Import svn to git
89repo_source=$TMP_SUBGIT_DIR/$repository_svn
90#Restore subgit configuration
91repo_target=$GITEA_REPO/$ORGANIZATION/$repository_git.git
92#LowerCase
93repo_target=${repo_target,,}
94
95mkdir -p "$repo_source"
96subgit configure --svn-url svn://svn.tld "$repo_source"
97cp "models/${MODEL}.conf" "$repo_source/subgit/config"
98sed -i "s/__PLUGIN__/$repository_svn/" "$repo_source/subgit/config"
99subgit install "$repo_source"
100subgit uninstall "$repo_source"
101
102if [ ! -z "$DELETE" ]; then
103    subgit uninstall "$repo_target"
104    curl -H "Authorization: token $GITEA_TOKEN" \
105            -X "DELETE" \
106            "$GITEA_HOST:$GITEA_PORT/api/v1/repos/$ORGANIZATION/$repository_git"
107fi
108
109#Create Gitea repository
110ORGANIZATION_ID=$(curl -sH "Authorization: token $GITEA_TOKEN" \
111        "$GITEA_HOST:$GITEA_PORT/api/v1/orgs/$ORGANIZATION" | jq ".id")
112TEAM_ID=$(curl -sH "Authorization: token $GITEA_TOKEN" \
113        "$GITEA_HOST:$GITEA_PORT/api/v1/orgs/$ORGANIZATION/teams" |jq '.[] | select(.name == "contrib") | .id ')
114
115
116curl -H "Authorization: token $GITEA_TOKEN" \
117        --data "clone_addr=$repo_source&uid=$ORGANIZATION_ID&repo_name=$repository_git" \
118        "$GITEA_HOST:$GITEA_PORT/api/v1/repos/migrate"
119
120curl -X PUT \
121        -H "Authorization: token $GITEA_TOKEN" \
122        "https://git.spip.net/api/v1/teams/$TEAM_ID/repos/$ORGANIZATION/$repository_git"
123
124#Restore subgit configuration
125cp -bar "$repo_source/"{subgit,svn,db,logs} "$repo_target/"
126cd "$repo_target/" || exit 1
127rename 's/(hooks\/)(.*receive).d$/$1user-$2.d/' hooks/*
128subgit install "$repo_target"
129subgit register --key /var/git/subgit/subgit.key "$repo_target"
130
131#Clean Directory
132rm -rf "$repo_source"
133
134chown "$GIT_UID:$GIT_GID" -R "$repo_target"
Note: See TracBrowser for help on using the repository browser.