# HG changeset patch # User Santeri Piippo # Date 1402259200 -10800 # Node ID b7f23f86cb2c4902098a76888d2e94fab113d799 # Parent 9d63b4f82112c79e7744be4455a773d1490ed152# Parent 9aabb075337c1e0cec250ca8dc8168ebf134efd7 - merge diff -r 9aabb075337c -r b7f23f86cb2c .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Sun Jun 08 23:26:40 2014 +0300 @@ -0,0 +1,1 @@ +settings.cfg diff -r 9aabb075337c -r b7f23f86cb2c LICENSE --- a/LICENSE Sun Jun 08 14:05:36 2014 +0000 +++ b/LICENSE Sun Jun 08 23:26:40 2014 +0300 @@ -1,5 +1,5 @@ - Copyright 2012-2014 Santeri Piippo + Copyright 2014 Santeri Piippo All rights reserved. Redistribution and use in source and binary forms, with or without diff -r 9aabb075337c -r b7f23f86cb2c pull-request.sh --- a/pull-request.sh Sun Jun 08 14:05:36 2014 +0000 +++ b/pull-request.sh Sun Jun 08 23:26:40 2014 +0300 @@ -7,6 +7,13 @@ exit 1 fi +# Get path to this script +pushd $(dirname $0) >/dev/null + selfpath=$(dirname $(readlink $0)) +popd >/dev/null + +# What's the upstream? +# TODO: can this be dynamically retrieved from bitbucket? upstream=$(hg paths upstream |rev |cut -d '/' -f 1,2 |rev) if [ -z "$upstream" ] @@ -17,6 +24,8 @@ echo "upstream is $upstream" +# Get all heads and filter merges out since we don't generally want to post +# pull requests for those. allheads=$(hg heads --template '{node}@{parents}\n' |sed 's@ @#@g') i=0 @@ -28,8 +37,16 @@ fi done +if [ -z "$heads" ] +then + echo "No non-merge heads. Nothing to do." + exit 0 +fi + numheads=$(echo $heads |wc -w) +# Ask the user which head to request pull of. We do this even if there is just +# one head, to give the user a chance to review exactly what is being submitted. echo "There are $numheads non-merge heads:" for head in $heads @@ -57,9 +74,14 @@ read head_index done +# Data for the pull request head=$(echo $heads |cut -d ' ' -f $head_index) -title=$(hg log -r ${head} -r tip --template '{desc}')yy +title=$(hg log -r ${head} -r tip --template '{desc}') source_repo=$(hg paths default |rev |cut -d '/' -f 1,2 |rev) +bturl=$(grep bturl $selfpath/settings.cfg |cut -d ' ' -f 2) +btuser=$(grep btuser $selfpath/settings.cfg |cut -d ' ' -f 2) +btpass=$(grep btpass $selfpath/settings.cfg |cut -d ' ' -f 2) +ticket=$(hg log -r $head --template '{bookmarks}' |cut -f 2 -d '|') while [ "$(echo $title |wc -c)" -gt "255" ] do @@ -70,14 +92,13 @@ echo "Requesting pull of $head" -json=$(cat ~/.bin/pullreq_template.json \ +# Get the JSON template and fill in the values +json=$(cat $selfpath/pullrequest_template.json \ |sed "s@{{TITLE}}@${title}@" \ |sed "s@{{SOURCE_REPO}}@${source_repo}@" \ |sed "s@{{HEAD}}@${head}@") -jsonfile=$(tempfile) -echo $json >$jsonfile - +# Get bitbucket credentials echo -n "Username: " read username echo -n "Password: " @@ -86,8 +107,12 @@ response=$(tempfile) -curl -X POST -H "Content-Type: application/json" -u "${username}:${password}" \ - "https://api.bitbucket.org/2.0/repositories/${upstream}/pullrequests" -d "$json" -o "$response" >/dev/stdout 2>&1 - -echo "repsonse: $response" -rm $jsonfile +# Post the pull request. +curl \ + -X POST \ + -H "Content-Type: application/json" \ + -u "${username}:${password}" \ + -d "$json" \ + -o "$response" \ + "https://api.bitbucket.org/2.0/repositories/${upstream}/pullrequests" \ + >/dev/stdout 2>&1 \ No newline at end of file diff -r 9aabb075337c -r b7f23f86cb2c pullreq_to_mantisbt.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pullreq_to_mantisbt.py Sun Jun 08 23:26:40 2014 +0300 @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +import json +import urllib +import urllib2 +import sys +import re +from suds.xsd.doctor import Import +from suds.xsd.doctor import ImportDoctor +from suds.client import Client + +bturl=sys.argv[1] +btuser=sys.argv[2] +btpass=sys.argv[3] +ticket=sys.argv[4] +bitbucketjson=sys.argv[5] +titlefile=sys.argv[6] + +with open (bitbucketjson, 'r') as fp: + jsondata = json.loads (fp.read()) + +with open (titlefile, 'r') as fp: + title = fp.read() + +regex = re.compile (r'^https://api\.bitbucket\.org/2\.0/repositories/([^/]+)/([^/]+)/pullrequests/([0-9]+)$') +match = regex.match (jsondata["links"]["self"]["href"]) + +if not match: + print "Bad JSON input (couldn't regex the pull request URL)" + quit() + +suds_import = Import ('http://schemas.xmlsoap.org/soap/encoding/', \ + 'http://schemas.xmlsoap.org/soap/encoding/') +suds_client = Client ('https://%s/api/soap/mantisconnect.php?wsdl' % bturl, \ + plugins=[ImportDoctor (suds_import)]) + +data={} +data["status"]={} +data["status"]["id"] = 60 +suds_client.service.mc_issue_update (btuser, btpass, ticket, data) +note = {} +note["text"] = "https://bitbucket.org/%s/%s/pull-request/%s" % (match.group(1), match.group(2), match.group(3)) +noteid = suds_client.service.mc_issue_note_add (btuser, btpass, ticket, note) +print "Pull request notice posted to mantisbt: https://%s/view.php?id=%s#c%s" % (bturl, ticket, noteid) diff -r 9aabb075337c -r b7f23f86cb2c pullrequest_template.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pullrequest_template.json Sun Jun 08 23:26:40 2014 +0300 @@ -0,0 +1,18 @@ +{ + "title": "{{TITLE}}", + "source": + { + "branch": + { + "name": "default" + }, + "repository": + { + "full_name": "{{SOURCE_REPO}}" + }, + "commit": + { + "hash": "{{HEAD}}" + } + } +}