{"id":1887,"date":"2010-12-28T10:23:00","date_gmt":"2010-12-28T09:23:00","guid":{"rendered":"http:\/\/www.devco.net\/?p=1887"},"modified":"2010-12-28T10:30:26","modified_gmt":"2010-12-28T09:30:26","slug":"git_hook_for_redmine_tickets","status":"publish","type":"post","link":"https:\/\/www.devco.net\/archives\/2010\/12\/28\/git_hook_for_redmine_tickets.php","title":{"rendered":"Git hook for redmine tickets"},"content":{"rendered":"
I use git and like to have a ticket for every bit of work I do on my projects. I also have a preferred way to prefix commit messages, something like:<\/p>\n
<\/code><\/p>\n Where Fix the broken thing<\/em> is the ticket subject in RedMine.<\/p>\n It’s not sophisticated or complex but it shows a nice one-liner in GitHub which I like.<\/p>\n To make sure I always stay consistent I have a simple git hook to tweak the commit message:<\/p>\n <\/code><\/p>\n It requires that I name my branches something like <ticket number>_anything_else<\/em> which might be too limited but works for me.<\/p>\n This will prepare my commit message with the ticket subject before I commit. I don’t do much off-line coding so generally don’t have to worry about connectivity checks.<\/p>\n","protected":false},"excerpt":{"rendered":" I use git and like to have a ticket for every bit of work I do on my projects. I also have a preferred way to prefix commit messages, something like: 123 – Fix this broken thing Description of commits here Where Fix the broken thing is the ticket subject in RedMine. It’s not sophisticated […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","footnotes":""},"categories":[1],"tags":[121,94,13],"_links":{"self":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1887"}],"collection":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/comments?post=1887"}],"version-history":[{"count":7,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1887\/revisions"}],"predecessor-version":[{"id":1893,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1887\/revisions\/1893"}],"wp:attachment":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/media?parent=1887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/categories?post=1887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/tags?post=1887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/p>\n
\r\n123 - Fix this broken thing\r\n\r\nDescription of commits here\r\n<\/pre>\n
<\/p>\n
\r\n#!\/usr\/bin\/env ruby\r\n\r\nrequire 'rubygems'\r\nrequire 'httparty'\r\n\r\nREDMINEURL=\"http:\/\/your.redmine\"\r\n\r\nmsg = File.read(ARGV[0])\r\n\r\n# If the msg starts with number we've been here already\r\n# and no doubt someone is amending existing log entries\r\nexit if msg =~\/^\\d\/\r\n\r\nbegin\r\n if msg =~ \/On branch (\\d+)(.+?)\\n\/\r\n branch = $1\r\n ticketurl = \"#{REDMINEURL}\/issues\/#{branch}.json\"\r\n puts \"Fetching #{ticketurl}\"\r\n headers = {\"User-Agent\" => \"meh\"}\r\n\r\n ticket = HTTParty.get(ticketurl, {:headers => headers})\r\n\r\n msg = \"%d - %s\\n\\n%s\" % [ branch, ticket[\"issue\"][\"subject\"], msg ]\r\n\r\n File.open(ARGV[0], \"w\") {|f| f.print msg}\r\n end\r\nrescue Exception => e\r\n puts \"git hook failed: #{e.class}: #{e}\"\r\n sleep 2\r\n exit\r\nend\r\n<\/pre>\n