scripts / flightgear / jenkins-status /
f8aa39b 2 years ago
1 contributor
71 lines | 1.945kb
#!/bin/bash

source_path=${1:-"$HOME/scripts/flightgear/source"}
job=fgfs

function xmlgetnext () {
    local IFS='>'
    read -d '<' TAG VALUE
    # by design, the first TAG/VALUE pair is empty
    # to avoid infinite loops at end of file parsing we return an error
    # the next time we find an empty TAG
    if test -z "$TAG"; then
        test ${xmlgetnext_firstentry:-1} -eq 1 && xmlgetnext_firstentry=0 || return 1;
    fi
    # process $TAG only if necessary
    local _TAG=$(printf '%q' $TAG)
    if test ${_TAG:0:1} = '$'; then
        TAG=$(tr '\n' ' ' <<< $TAG | sed 's/  */ /g; s/ *$//')
    fi
}

exit_trap () {
    rm $apixml
}

set -e
trap exit_trap EXIT

apixml=$(mktemp --dry-run /dev/shm/XXXXXXXXXX)

wget -qO- --header 'Accept:application/xml' http://build.flightgear.org:8080/job/$job/api/xml > $apixml

declare -A code

while xmlgetnext; do
    case "${TAG:0:1}" in
        ''|'?'|'!')
            continue;;
        /)
            property=${property%/*};;
        *)
            if test "${TAG: -1}" != '/'; then
                property+=/${TAG%% *}
            fi;;
    esac

    case "$property" in
        /workflowJob/healthReport/score)
            score=$VALUE;;

        /workflowJob/lastSuccessfulBuild/url)
            wget -qO- --header 'Accept:application/xml' ${VALUE}/api/xml >> $apixml;;

        /workflowRun/action/lastBuiltRevision/branch/SHA1)
            sha1=$VALUE;;     

        /workflowRun/action/remoteUrl)
            [[ ${VALUE##*/} = @(flight|sim)gear ]] && code[${VALUE##*/}]=$sha1;;

        /workflowRun/result)
            result=$VALUE
            for path in ${!code[@]}; do
                if test $(git -C "$source_path/$path" rev-parse HEAD) != ${code[$path]}; then
                    echo "mismatch revision from jenkins info"
                    exit
                fi
            done
            echo "result: $result, score $score"
            exit;;
    esac
done < $apixml