Skip to content

Instantly share code, notes, and snippets.

/wh
Created Aug 19, 2015

Embed
What would you like to do?
#!/bin/bash
PRJ=$1
PKG=$2
REPO=$3
BRANCH=${4:-master}
ME=$(basename $0)
WHUSER=cibot
WHURL=https://webhook.merproject.org/webhook/
OBS=https://build.merproject.org
usage() {
cat <<EOF
usage: $ME <project> <package> <git repo> [<branch>]
eg: $ME home:lbt:hack not-a-ux https://github.com/lbt/mer-not-a-ux.git pkg-mer
branch defaults to 'master'
EOF
}
fatal() {
echo $@
exit 1
}
project_exists() {
osc meta prj $PRJ >/dev/null 2>&1
}
package_exists() {
osc meta pkg $PRJ $PKG >/dev/null 2>&1
}
create_project() {
echo "Creating project $PRJ"
cat <<EOF | osc meta prj $PRJ -F -
<project name="$PRJ">
<title>$prj</title>
<description/>
</project>
EOF
}
create_package() {
echo "Creating package $PRJ/$PKG"
cat <<EOF | osc meta pkg $PRJ $PKG -F -
<package name="$PKG" project="$PRJ">
<title>$PRJ</title>
<description/>
</package>
EOF
}
project_has_cibot() {
PRJ_META=$(osc meta prj $PRJ 2>&1)
echo "$PRJ_META" | grep "<person" | grep '"$WHUSER"' | grep 'role="maintainer"' > /dev/null
}
project_add_cibot() {
echo "Adding $WHUSER to $PRJ"
# in the line range 0->line-matching-"<person", if line matches <person, append a WHUSER line
echo "$PRJ_META" | sed "0,/<person/ {/<person/a\
<person userid='$WHUSER' role='maintainer'\/>
}"| osc meta prj $PRJ -F -
}
add_service() {
echo "Creating webhook"
cat <<EOF | osc api -T /dev/stdin -X PUT source/$PRJ/$PKG/_service > /dev/null
<services>
<service name="webhook">
<param name="repourl">$REPO</param>
<param name="branch">$BRANCH</param>
</service>
</services>
EOF
}
task() {
echo "$@"
}
if [[ ! $PRJ ]]; then
usage; fatal "<project> not given"
elif [[ ! $PKG ]]; then
usage; fatal "<package> not given"
elif [[ ! $REPO ]]; then
usage; fatal "<repo> not given"
elif (( $# > 4 )); then
usage; fatal "Too many arguments"
fi
if ! project_exists; then
if create_project; then
task "Update description and set some repositories at $OBS/project/show/$prj"
else
fatal "Could not create missing project: $PRJ"
fi
fi
# We create the project with no 'person' so OBS adds the current user.
# Then add cibot if it is not there
if ! project_has_cibot; then
project_add_cibot
fi
if ! package_exists; then
if create_package; then
task "Update description at $OBS/package/show/$prj/$pkg"
else
fatal "Could not create missing package: $PRJ/$PKG"
fi
fi
if add_service; then
task "Webhook added - make sure the git service for the $REPO webhook trigger points to $WHURL"
else
fatal "Could not add a service to $PRJ $PKG"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.