Anonymous (owner)

Revisions

  • 4d0950 Mon Apr 27 03:46:49 -0700 2009
  • 68f54c Mon Apr 27 03:46:19 -0700 2009
gist: 102437 Download_button fork
public
Public Clone URL: git://gist.github.com/102437.git
Embed All Files: show embed
postCommitHook.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require 'rubygems'
require 'net/http'
require 'builder'
require 'cgi'
 
# Created by Damian Nicholson
# Product Manager
# hedgehoglab
# damian.nicholson@hedgehoglab.com
#
# NOTE: THIS SCRIPT IT TAILORED TO WORKING WITH SUBVERSION AND FIXX VERSION 1.7
 
# This script needs to ran as part of the post commit hook which is found in the hooks directory(/path_to_repository/repository/hooks/) of your
# Subersion repository.
 
# Copy the original template file called "post-commit.tmpl" and rename it to just "post-commit"
# Make this file execuatable (chmod +x post-commit)
# Making sure to keep it in the same directory as the other hook templates.
 
# Edit the post-commit file you've just made executable making sure to comment out the following lines at the bottom as these are merely examples
# of how your script can be run
# commit-email.pl "$REPOS" "$REV" commit-watchers@example.org
# log-commit.py --repository "$REPOS" --revision "$REV"
 
# Copy and paste in this line, making sure to change the path to where your script is located
# ruby /path_to_script/postCommitHook.rb $1 $2
 
# We recommend placing this script in the hooks directory of your repository, you also need to make this file executable (chmod +x postCommitHook.rb)
 
# Messages are only logged in fixx if they contain the ID of the issue they're referring to - it must be prefixed with a hashtag
# e.g. svn commit -m '#12 CSS Tweaks'
 
# SVNLOOK - Path to wherever your installation of subversion is
SVNLOOK = "/usr/local/bin/svnlook"
REPOS_PATH = ARGV[0]
REVISION = ARGV[1]
 
# IMPORTANT - Change the contents of this to suit your configuration
DETAILS = {
  # The url of your installation of fixx with port number if necessary
  :path => '0.0.0.0',
  :port => 8080,
  
  
  # FIXX AUTHENTICATION
  # You can put all your usernames and passwords in this hash though it is not recommended. You may wish to do so
  # if you want the Subversion messages to created by the person who commited the revision in Subversion
  
  # We have created a new user called svnbot specifically for this task who feeds all new messages into fixx as the
  # logs will show who originally changed the files anyway.
  :users => { 'svnbot' => 'password' }
}
 
 
# Path, revision and details are all passed in automatically, no need to change params for this function
def postToFixx(path, revision, details)
  
  # Running of svnlook commands, change to suit your respective SCM
  commit_author = `#{SVNLOOK} author #{path} -r #{revision}`
  commit_log = `#{SVNLOOK} log #{path} -r #{revision}`
  commit_changed = `#{SVNLOOK} changed #{path} -r #{revision}`
  
  # Regex to search for #issueId
  regex = /#\d+/
  
  # Scans the commit message for #issueId tag
  issue_id = commit_log.scan(regex)
  if !issue_id.empty?
    Net::HTTP.start(details[:path], details[:port]) { |http|
      
        commit_changes = commit_changed.inject([]) do |array, value|
          array << value.to_s + "\n"
          array
        end
        
        # Strips the hash tag
        stripped_issue = issue_id.to_s.delete('#').to_i
        req = Net::HTTP::Post.new("/api/issues/#{stripped_issue}/comments")
        
        # We use the same user for all messages hence why we're using only first entry
        
        # It is possible to do some subversion username - fixx username comparison here if you opted down the multiple user avenue
        req.basic_auth details[:users].keys[0], details[:users].values[0]
        req.set_content_type('application/xml')
        
        # Creating the xml to be posted to fixx
        xml = <<-END_XML
<comment>
<text>Commited by: #{CGI.escapeHTML(commit_author)} \n Commit message: #{CGI.escapeHTML(commit_log.gsub!(regex, ''))} \n Files changed: \n\n #{commit_changes}</text>
<createdAt>#{DateTime.now.to_s}</createdAt>
</comment>
END_XML
        req.body = xml.strip
        res = http.request(req)
        if res.is_a? Net::HTTPSuccess
          # OK
        else
          return false
        end
      }
  end
end
 
 
postToFixx(REPOS_PATH, REVISION, DETAILS)