Git Connection With Redmine Issue Utility

Git Connection With Redmine Issue Utility

For a large scale project, it is important to have a decent project management tool. Also, for the sake of development ease and testing process I would suggest any developer team to use project management tools well integrated into their development process.

Redmine is a open source software which serves as a project management tool where you can assign roles to different users, arrange work schedule, create Gantt charts as well as have a decent bug tracking system. In Redmine, issues can be bug, feature or support tickets. In theory and most everyday cases, ticker creator is responsible for testing and odds are that he is not a developer who has access to source repositories to check the code. Assigned developer will take a look at the issue and continue his work with regarding to that ticket. When there is an addition to the source code, it is important to notify the ticker creator. Instead of going back on Redmine and doing this manually, how can we set up a framework such that when there is a new push to repository, it will automatically update the ticket description for the follow up?

The answer is using the hooks build in Git version tracking system. Hooks are predefined processes, scripts, that run after a certain action regarding to the git repository. To have a start up, I have developed a “post-update” for connecting my git repository updates with their respective issues in Redmine. This project is at its beginning era for me as for now, it partially relies on the user. My goal is to set up my hooks such that there will be no commit without an issue in Redmine system. This will enable the project developers to create a readable project history which can be easily used by the project managers and can also populate a know-how database for future projects.

Currently, my “post-update” checks Redmine database to see if there is an open ticket for the coming commit to the git repository. I have set a rule for the commits, every commit message have to start with “Issue:” then go on with the usual commit message. My hook will parse the issue number first to see if the commit message is in laid out format, if not it will send a feedback to the user stating that the system will accept the commit, however there will be no action in the Redmine system. Verifying that use used the correct format and entered a issue number, then we will check to see if that numbered issue actually exists. If it doesn’t again the system currently accepts the commit but sends a message stating there is no connection with the issue number in Redmine framework. After passing these two tests, post-update hook will connect to the Redmine database and update the issue description with the commit details, including links to commit history and difference links to the user. Also, my system will make the older description for the issue look like quotations so that we can have a full history on the issue. Here is the post-update script that I have developed:

#!/bin/sh
#Author: Egemen Gozoglu egemen@egemengozoglu.com
#Date: 08.07.2011

#Color Codes
BOLD="33[1;39m"
NORMAL="33[0;39m"
RED="33[31m"

#Database variables
db_socket=/home/egemen/redmine-1.2.0-0/mysql/tmp/mysql.sock
db_username=bitnami
db_name=bitnami_redmine
db_password=3e79743af9
server_ip=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'`

commit_message=$(git show --format=format:%s | head -1)
commit_id=$(git rev-parse HEAD)

issue_id_tmp=$(echo $commit_message | awk -F " " '{print $1}')
issue_id=$(echo $issue_id_tmp | awk -F ":" '{print $2}')

if [ "$issue_id" = ''  ];then
    echo $BOLD$RED"Invalid issue number. No Redmine update will be made."$NORMAL
	echo $BOLD$RED"Please start your commmit message like the following format:"$NORMAL;
	echo $BOLD$RED"Issue: ....."$NORMAL
	echo $BOLD$RED"Ony push to your repository will be processed."$NORMAL
	exit 1;
fi

count_issue=`mysql --socket=$db_socket -u $db_username $db_name --password=$db_password -e "SELECT COUNT(*) from issues WHERE id = $issue_id"`

check_issue=$(echo $count_issue | awk -F " " '{print $2}')

if [ "$check_issue" = '0'  ];then
    echo $BOLD$RED"No issue with is found with the issue number you provided!"$NORMAL
	echo $BOLD$RED"No Redmine update will be made."$NORMAL
	echo $BOLD$RED"Ony push to your repository will be processed."$NORMAL
	exit 1;
fi

commit_date=$(date);

old_desc=`mysql --skip-column-names --socket=$db_socket -u $db_username $db_name --password=$db_password -e "SELECT description FROM issues WHERE id = $issue_id"`

project_id_tmp=`mysql --socket=$db_socket -u $db_username $db_name --password=$db_password -e "SELECT project_id FROM issues WHERE id = $issue_id"`

project_id=`echo $project_id_tmp | awk -F "project_id " '{print $2}'`

project_html_tmp=`mysql --socket=$db_socket -u $db_username $db_name --password=$db_password -e "SELECT identifier FROM projects WHERE id = $project_id"`

project_html=`echo $project_html_tmp | awk -F "identifier " '{print $2}'`

mysql --socket=$db_socket -u $db_username $db_name --password=$db_password << EOFMYSQL
UPDATE issues SET description = 'Commit Date: $commit_date n Commit History: "$commit_id":http://$server_ip:8080/redmine/projects/$project_html/repository/revisions/$commit_id n Diff: "$commit_id":http://$server_ip:8080/redmine/projects/$project_html/repository/revisions/$commit_id/diff n Commit Message: $commit_message nn $old_desc ' WHERE id = $issue_id;
EOFMYSQL

You need to adjust the database socket, database username, database name and database password for your own system. For generating links, the system automatically gets the server’s IP address. Also, you need to pay attention to one detail about Redmine, Redmine only fetches repository data when you actually click the “Repository” tab for your project. So, when you update your project and go to issue directly and try to click on the commit hash link right away, Redmine will fail to show you the page. Once you click on the repository tab all the links regarding to previous commits work perfectly well. This problem is related to Redmine and it can be solved by adding cronjobs to your system. For more information you can check the following link:

http://www.redmine.org/projects/redmine/wiki/FAQ

So far, this project for me is just beginning, I am hoping to develop this more based on my needs and feedback I receive, so please feel free to contact me about this project! For those of you who wants to get the snapshot of the code you can go to my Github repository by following the link below:

https://github.com/egemengozoglu/egemen_repo/blob/master/Redmine_Git_Auto_Issue/post-update

Enjoy!

2 thoughts on “Git Connection With Redmine Issue Utility

Leave a Reply

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