Redmine-Integration/rhodecode_chiliproject.sh

120 lines
6.9 KiB
Bash
Raw Normal View History

2012-04-09 16:51:08 +04:00
#!/bin/bash
2012-04-16 16:01:02 +04:00
# load config files
2012-04-16 17:39:30 +04:00
if [ -f /etc/rhodecode_chiliproject ]; then
source /etc/rhodecode_chiliproject
elif [ -f ~/etc/rhodecode_chiliproject ]; then
source ~/etc/rhodecode_chiliproject
else
echo "Config file not found ;-("
exit -1
fi
2012-04-09 16:51:08 +04:00
2012-04-16 16:03:51 +04:00
# === REMOVE ALL BROKEN REPOSITORY LINKS IN CHILIPROJECT MYSQL DATABASE ===
ALL_MYSQL_REPOS=`mysql -h$CHILI_MYSQL_HOSTNAME -u $CHILI_MYSQL_USER -e "SELECT url,root_url,id
FROM $CHILI_MYSQL_DBNAME.repositories
WHERE type='Mercurial'
2012-05-07 21:35:45 +04:00
OR type='Repository::Mercurial'" \
| grep -v tables_col|xargs|sed "s/ /\n/g"|tail -n+4`
2012-04-09 16:51:08 +04:00
repos_to_remove=
current_url=
current_root_url=
let n=0
for v in $ALL_MYSQL_REPOS; do
let idx=n/3
case $((n%3)) in
0) current_url=$v;;
1) current_root_url=$v;;
2) [ ! -d $current_url/.hg -a ! -d $current_root_url/.hg ] && repos_to_remove="$repos_to_remove,$v";;
2012-04-09 16:51:08 +04:00
esac
let n++
done;
repos_to_remove=${repos_to_remove#,}
[ "$repos_to_remove" != "" ] && mysql -h$CHILI_MYSQL_HOSTNAME -u $CHILI_MYSQL_USER -e "DELETE
FROM $CHILI_MYSQL_DBNAME.repositories
WHERE id IN ($repos_to_remove)"
2012-04-09 16:51:08 +04:00
# === GET DATA FROM RHODECODE SQLITE BASE ===
rh_repos_path=`sqlite3 $RHODECODE_SQLITE_PATH "SELECT ui_value
FROM rhodecode_ui
WHERE ui_section='paths'"`
2012-04-09 16:51:08 +04:00
SQLITE_RESULTS=`sqlite3 $RHODECODE_SQLITE_PATH "SELECT repo_name,repo_type,users.email
FROM repositories,users
WHERE repositories.user_id=users.user_id;"`
2012-04-09 16:51:08 +04:00
# initializing repos arrays and count them
repos_names=
repos_paths=
repos_types=
repos_mails=
2012-04-09 16:51:08 +04:00
let nrepos=0
for r in $SQLITE_RESULTS; do
repos_paths[$nrepos]=$rh_repos_path/${r%|*|*}
2012-04-09 16:51:08 +04:00
tmp=${repos_paths[$nrepos]%/}; repos_names[$nrepos]=${tmp##*/}
tmp=${r%|*}; repos_types[$nrepos]=${tmp#*|}
2012-04-09 16:51:08 +04:00
case ${repos_types[$nrepos]} in
hg) repos_types[$nrepos]='Mercurial';;
git) repos_types[$nrepos]='Git';;
2012-04-09 16:51:08 +04:00
esac
2012-05-07 10:38:45 +04:00
repos_mails[$nrepos]=${r#*|*|}
2012-04-09 16:51:08 +04:00
let nrepos++
done
# === FOR ALL REPOS FROM RHODECODE DATABASE===
for i in `seq 0 $((nrepos-1))`; do
2012-04-16 16:03:51 +04:00
# === GET DATA FROM CHILIPROJECT MYSQL BASE ===
2012-04-09 16:51:08 +04:00
USERID=`mysql -h$CHILI_MYSQL_HOSTNAME -u $CHILI_MYSQL_USER -e "SELECT id
FROM $CHILI_MYSQL_DBNAME.users
WHERE users.status='1'
AND users.mail='${repos_mails[$i]}'
AND users.type='User'" \
2012-04-17 18:06:34 +04:00
| grep -v tables_col|xargs|sed "s/ /\n/g"|tail -n+2`
2012-04-09 16:51:08 +04:00
[ "$USERID" == "" ] && continue
PROJECTID=`mysql -h$CHILI_MYSQL_HOSTNAME -u $CHILI_MYSQL_USER -e "SELECT id FROM $CHILI_MYSQL_DBNAME.projects
2012-04-17 18:06:34 +04:00
WHERE (name='${repos_names[$i]}'
OR identifier='${repos_names[$i]}')
AND status='1'" \
2012-04-17 18:06:34 +04:00
| grep -v tables_col|xargs|sed "s/ /\n/g"|tail -n+2`
2012-04-09 16:51:08 +04:00
[ "$PROJECTID" == "" ] && continue
ALREADY_EXIST=`mysql -h$CHILI_MYSQL_HOSTNAME -u $CHILI_MYSQL_USER -e "SELECT id
FROM $CHILI_MYSQL_DBNAME.repositories
WHERE project_id=$PROJECTID
OR url='${repos_paths[$i]}'
OR root_url='${repos_paths[$i]}'" \
| grep -v tables_col|xargs|sed "s/ /\n/g"|tail -n+2`
[ "$ALREADY_EXIST" != "" ] && continue
2012-05-22 18:37:36 +04:00
roles_mysql_string=`echo $CHILI_REQUIRED_ROLES | sed "s~\>~'~g ; s~\<~,'~g ; s~^,~~ ; s~ ~~g"`
2012-04-10 20:34:41 +04:00
ROLES=`mysql --default-character-set=utf8 -h$CHILI_MYSQL_HOSTNAME -u $CHILI_MYSQL_USER -e "SELECT roles.name
2012-04-17 18:06:34 +04:00
FROM $CHILI_MYSQL_DBNAME.roles,
$CHILI_MYSQL_DBNAME.member_roles,
$CHILI_MYSQL_DBNAME.members
2012-04-17 18:06:34 +04:00
WHERE roles.id=member_roles.role_id
AND member_roles.member_id=members.id
AND members.user_id='$USERID'
AND members.project_id='$PROJECTID'
2012-05-22 18:37:36 +04:00
AND roles.name IN ($roles_mysql_string)" \
2012-04-17 18:06:34 +04:00
| grep -v tables_col|xargs|sed "s/ /\n/g"|tail -n+2`
2012-04-09 16:51:08 +04:00
[ "$ROLES" == "" ] && continue
2012-04-16 16:03:51 +04:00
# === ATTACH RHODECODE REPOSITORY TO CHILIPROJECT ===
2012-04-09 16:51:08 +04:00
# DEBUG
echo "insert $PROJECTID,${repos_paths[$i]},${repos_types[$i]}"
mysql -h$CHILI_MYSQL_HOSTNAME -u $CHILI_MYSQL_USER -e "INSERT INTO $CHILI_MYSQL_DBNAME.repositories(project_id,
2012-04-17 18:06:34 +04:00
url,
root_url,
type,
path_encoding)
2012-04-17 18:06:34 +04:00
VALUES('$PROJECTID',
'${repos_paths[$i]}',
'${repos_paths[$i]}',
'${repos_types[$i]}',
'')"
2012-04-09 16:51:08 +04:00
done