#!/usr/bin/env ruby # # Triggers: # sendrequest change-commit ... "/path/to/file/sendrequest.rb # %serverport% %change% %triggerMeta_trigger%" # # sendrequest shelve-commit ... "/path/to/file/sendrequest.rb # %serverport% %change% %triggerMeta_trigger%" # # sendrequest form-commit ... "/path/to/file/sendrequest.rb # %serverport% %formfile% %triggerMeta_trigger%" # # Description: # Sends a POST request to the URLs specified in the Perforce server key # which corresponds to the trigger type that this script is invoked with. # # If this script is invoked as a change-commit trigger: # Looks for a JSON array of valid URLs in the 'webhook-change-commit' key. # Sends a JSON string containing the user who submitted the changelist, # the changelist description, and an array of the filenames of the files # that were changed. # # If this script is invoked as a shelve-commit trigger: # Looks for URLs in the 'webhook-shelve-commit' key. # Sends information is same format as change-commit trigger. # # If this script is invoked as a form-commit trigger: # Looks for a JSON array of valid URLs in the 'webhook-form-commit' key. # Sends a JSON string containing the contents of the form. # SET THESE OR TRIGGER WILL NOT WORK p4bin = '/usr/local/bin/p4' user = 'example' passwd = 'example' # ALL SET require 'net/http' require 'json' begin if ARGV.length != 3 print 'Usage: sendrequest.rb ' puts '%serverport% (%change% | %formfile%) %triggerMeta_trigger%' exit 1 end serverport = ARGV[0] triggertype = ARGV[2] p4 = "#{p4bin} -p #{serverport} -u #{user} -P #{passwd}" params = nil if (triggertype == 'change-commit' || triggertype == 'shelve-commit') change = ARGV[1] describe_output = `#{p4} -R describe -s #{change}` formatted = Marshal.load(describe_output) change_user = formatted['user'] change_desc = formatted['desc'] files = [] i = 0 loop do filename = formatted["depotFile#{i}"] break if (nil == filename) files << filename i += 1 end params = {'content' => "{ 'user' => #{change_user}, 'description' => #{change_desc}, 'files' => #{files} }" } elsif (triggertype == 'form-commit') formfile = ARGV[1] form = File.read(formfile) params = {'content' => form} else puts "Trigger must be change-commit, shelve-commit or form-commit." exit 1 end begin urls = JSON.parse(`#{p4} key webhook-#{triggertype}`) json_headers = {"Content-Type" => "application/json", "Accept" => "application/json"} urls.each do |url| uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) begin response = http.post(uri.path, params.to_json, json_headers) #puts response rescue ArgumentError end end rescue JSON::ParserError end end exit 0
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 10132 | jim_brower | Moved sendrequest.rb and added webhook-wrapper | ||
//guest/jim_brower/triggers/sendrequest.rb | |||||
#2 | 10033 | jim_brower |
sendrequest.rb now supports the three requested trigger types. URLs are extracted from specific keys on the Perforce server. Currently the user must create the keys manually before using the trigger. The program will not explode if the corresponding key is empty. |
||
#1 | 10028 | jim_brower |
Added the first version of the 'webhook' trigger. Correctly POSTs to my test rails app, but the array of files is currently a string. |