SVN and Mantis Bug Tracker Integration (Linux/Python)

Hello fellow developers,

As announced recently, I am now working on a new version of MPfm for the Linux and Mac OS X platforms. I have installed Ubuntu and MonoDevelop for developing the Linux version of MPfm.

Because of this, I am now moving my private Subversion server into a minimal Gentoo Linux box inside VirtualBox. I can now host the SVN server inside any OS I want. I was previously using VisualSVN on Windows.

Since I didn’t want to install Mono on the Gentoo box to re-use my previous .NET console application, I have chosen to write the new Subversion/Mantis integration script using Python3.

Here is the post-commit file I am using:

#!/bin/sh

REPOS="$1"
REV="$2"

/usr/bin/python /var/svn/repos/MPfm/hooks/mantis.py "$REPOS" "$REV"

Here is the code for the Python3 script that posts the Subversion log to Mantis:

#!/usr/bin/python
# SVN/Mantis Integration Script 0.1

import sys
import subprocess as sub
import urllib.request
import urllib.parse

def main():

  # Check arguments
  if(len(sys.argv) != 3):
    print("The script needs 2 arguments")
    sys.exit(0)

  repo = sys.argv[1]
  rev = sys.argv[2]

  # Get SVN change log
  args = ['svnlook', 'changed', '-r', rev, repo]
  p = sub.Popen(args,stdout=sub.PIPE,stderr=sub.PIPE)
  outputChanged, errorsChanged = p.communicate()
  strOutputChanged = str(outputChanged, encoding='utf8')

  # Get SVN author
  args = ['svnlook', 'author', '-r', rev, repo]
  p = sub.Popen(args,stdout=sub.PIPE,stderr=sub.PIPE)
  outputAuthor, errorsAuthor = p.communicate()
  strOutputAuthor = str(outputAuthor, encoding='utf8').replace("\n", "")

  # Get SVN comment
  args = ['svnlook', 'log', '-r', rev, repo]
  p = sub.Popen(args,stdout=sub.PIPE,stderr=sub.PIPE)
  outputLog, errorsLog = p.communicate()
  strOutputLog = str(outputLog, encoding='utf8')

  # Add comment to Mantis issue
  url = "http://www.mp4m.org/mantis/checkin.php"
  logContent = "The user <b>" + strOutputAuthor + "</b> has checked in files related to this issue (revision <b>" + rev + "</b>).<br /><br /><b>Comment:</b><br />" + strOutputLog + "<br /><br /><b>Files modified:</b><br />" + strOutputChanged
  print(logContent)
  logContent = logContent.replace("\\n", "<br />")
  post_data = {"user":"svn", "log":logContent}
  post_data_encoded = urllib.parse.urlencode(post_data)
  request_object = urllib.request.Request(url, post_data_encoded)
  response = urllib.request.urlopen(request_object)
  html_string = response.read()
  print(html_string)

if __name__ == "__main__":
  main()

Do not forget to make both files executable by typing:

sudo chmod o+x filename.ext

Note: To complete the integration, you will also need the modified checkin.php file for Mantis, which you can find in the previous article.

If you have any questions, do not hesitate to ask!

 

Tags: , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>