Skip to content

Instantly share code, notes, and snippets.

@nacin
Last active March 21, 2016 09:37
Show Gist options
  • Save nacin/4758127 to your computer and use it in GitHub Desktop.
Save nacin/4758127 to your computer and use it in GitHub Desktop.
A script that leverages Trac XML-RPC (I know, I know) to upload patches. Usage: `trac-attach.sh 12345`
#!/bin/sh
# A script that leverages Trac XML-RPC (I know, I know) to upload patches.
#
# This script is written specifically for the Mac, in that it reads from
# your keychain to derive your SVN password. You can change the SVN_PASS
# line below if you wanted to pull from ~/.svn/ or what not.
#
# Basic usage: `trac-attach.sh 12345` uploads a patch to ticket #12345,
# using the name 12345.diff. If there exists a 12345.diff, the patch is
# automatically incremented by Trac, as in 12345.2.diff, 12345.3.diff, etc.
#
# More advanced usage: `trac-attach.sh 12345.diff` uploads a patch to
# #12345, using the explicit name 12345.diff. This will *replace* an existing
# 12345.diff file. (The only time you should ever replace a patch is if
# you are correcting a file upload you only made moments ago.)
#
# Descriptions: The second argument allows for a description, as in:
# `trac-attach.sh 12345 "Does something cool."`. If a description is omitted
# and you are explicitly replacing an existing file, Trac automatically
# keeps the current description. (This is how the web interface works, too.)
#
# By Andrew Nacin, based on a script by Michael Adams. Mike gets credit for
# the pipe juggling. I get the blame for the rest.
SVN_USER='nacin'
SVN_PASS=$(security find-generic-password -g -l "<https://core.svn.wordpress.org:443> Use your WordPress.org login" 2>&1 | grep password: | sed -e 's/password: "//' -e 's/"$//')
if [[ -p /dev/stdin || ! -t 0 ]]
then # We're in a pipe or a redirect, store stdin
TICKET_ATTACHMENT=`mktemp -t trac.att.XXXXXXXXXX`
if [[ ! $TICKET_ATTACHMENT ]]
then
echo "Could not create temp file" >&2
exit 1
fi
TICKET_ATTACHMENT=$(cat | base64)
else # no attachment
TICKET_ATTACHMENT=$(svn diff | base64)
fi
TICKET_ID=${1?Usage: $0 TICKET_ID [TRAC_SUBDOMAIN]}
if [[ $TICKET_ID == *".diff" ]]
then
ATTACHMENT_NAME=$TICKET_ID
TICKET_ID=$( echo "$TICKET_ID" | cut -d'.' -f 1 )
OVERRIDE=1
else
ATTACHMENT_NAME="$TICKET_ID.diff"
OVERRIDE=0
fi
TRAC_SUBDOMAIN=${3:-core}
if [ "$TRAC_SUBDOMAIN" != "core" ]; then
ATTACHMENT_NAME="$TRAC_SUBDOMAIN.$ATTACHMENT_NAME"
fi
DESCRIPTION=${2:-}
TMP_FILE=$(mktemp -t trac.att.XXXXXXXXXX)
echo "<?xml version=\"1.0\"?>
<methodCall>
<methodName>ticket.putAttachment</methodName>
<params>
<param><int>${TICKET_ID}</int></param>
<param><string>${ATTACHMENT_NAME}</string></param>
<param><string>${DESCRIPTION}</string></param>
<param><base64>${TICKET_ATTACHMENT}</base64></param>
<param><boolean>${OVERRIDE}</boolean></param>
</params>
</methodCall>" > "$TMP_FILE"
RESULT=$(curl -u "${SVN_USER}:${SVN_PASS}" -s -H "Content-Type: application/xml" --data "@${TMP_FILE}" "https://${TRAC_SUBDOMAIN}.trac.wordpress.org/login/rpc")
ATTACHED=$(echo "$RESULT" | grep "${TICKET_ID}" | sed -e 's/<value><string>//' -e 's/<\/string><\/value>//')
if [[ $ATTACHED ]]
then
echo "http://${TRAC_SUBDOMAIN}.trac.wordpress.org/attachment/ticket/${TICKET_ID}/${ATTACHED}"
else
echo $RESULT >&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment