Updating Mantis Issues from Jenkins via SOAP

The following script can be used to update mantis issues by adding a note from the bash command line. The script is meant to be executed from jenkins because it pulls the last SVN log of the committed build and includes the message in the log. Nevertheless the same concepts should apply for adding a note programmatically via cURL.

###########################################################################
#                       Update Mantis Issues via SOAP                     #
###########################################################################
 
SVN_USER="username"
SVN_PASSWORD="password"
MANTIS_USER="usernmae"
MANTIS_PASSWORD="password"
MANTIS_WEBSITE="https://mantis.website.tld"
LAST_COMMIT_MESSAGE=`svn log -l 1 --xml --username "$SVN_USER" --password "$SVN_PASSWORD" | xmllint --xpath 'string(//logentry/msg)' -`
LAST_COMMIT_MESSAGE_ESCAPED="${LAST_COMMIT_MESSAGE//$'\n'/<br />}"
MESSAGE="The latest build contains some changes that may be relevant to this issue:<br/><br/>$LAST_COMMIT_MESSAGE_ESCAPED<br/><br/>"
MESSAGE_ESCAPED=`echo $MESSAGE | recode ascii..html`
 
# Print out every command.
set -x
 
# Update all tickets mentioned in the commit via SOAP.
for ISSUE_ID in `echo $LAST_COMMIT_MESSAGE | grep -Po '#[0-9]+' | sed 's/#//g'`; do
    # Build the WSDL payload.
    SOAP_PAYLOAD='<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <q1:mc_issue_note_add xmlns:q1="'$MANTIS_WEBSITE'/api/soap/mantisconnect.php">
            <username xsi:type="xsd:string">'$MANTIS_USER'</username>
            <password xsi:type="xsd:string">'$MANTIS_PASSWORD'</password>
            <issue_id xsi:type="xsd:integer">'$ISSUE_ID'</issue_id>
            <note href="#id1"/>
        </q1:mc_issue_note_add>
        <q2:IssueNoteData id="id1" xsi:type="q2:IssueNoteData" xmlns:q2="'$MANTIS_WEBSITE'/api/soap/mantisconnect.php">
            <text xsi:type="xsd:string">'$MESSAGE_ESCAPED'</text>
        </q2:IssueNoteData>
    </s:Body>
</s:Envelope>'
    # Sent the SOAP request.
    curl --user "$MANTIS_USER":"$MANTIS_PASSWORD" --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction:mc_issue_note_add" --data "$SOAP_PAYLOAD" "$MANTIS_WEBSITE/api/soap/mantisconnect.php"
done
 
# Restore shell settings.
set +x