#!/usr/local/bin/ruby # # USAGE: changeid.rb %user% %formfile%" # Restricts new job creation to P4AUTHUSER # changes "new" jobids to be the value in # DTG_DTISSUE # # To add the trigger to the Peforce server, add the following line to the # trigger table: # jobId form-in job "/changeid.rb %user% %formfile%" # # Define which user is allowed to create new jobs and edit ro fields P4AUTHUSER = "p4dtguser" ############################################## # Everything after this should be fine as is # ############################################## if ARGV[0] == nil || ARGV[1] == nil then print "Trigger Error: Missing argument\n" exit 1 end # # P4 Form Data Class # class P4Form # # A form is composed of a set of fields interspersed with # comments delimited by the # character # # Fields are of the following two varieties: # ^field: value # # ^field: # [\t ]value... # def initialize( formdata ) @fields = {} infield=false curfield=nil curvalue=nil formdata.each do |line| line.chomp! case line when /^#/ # ignore comment lines when /^[^\t ]/ # begin field if infield && curvalue != nil && curvalue.strip != "" then @fields[curfield] = curvalue end infield=true curfield = line[/^[^:]*/].strip curvalue = line.sub(/^[^:]*:[\t ]*/, "") when /^[\t ]/ # continue field if infield then curvalue = "#{curvalue}\n#{line}" else # unknown end else # unknown end end # If in a field, make sure to save it if infield && curvalue != nil && curvalue.strip != "" then @fields[curfield] = curvalue end end attr_reader :fields def print(obj) for f in @fields.keys do obj.printf("%s: %s\n\n", f, fields[f]) end end end user=ARGV[0] jobfile=ARGV[1] t_file = File.new(jobfile, "r") t_form = P4Form.new(t_file) t_file.close job = t_form.fields["Job"] if job == "new" then if user != P4AUTHUSER then $stdout.printf("You are not authorized to create new jobs\n") exit 1 # Not authorized to create new jobs else t_form.fields["Job"] = t_form.fields["DTG_DTISSUE"] t_file = File.new(jobfile, "w") t_form.print(t_file) end end exit 0 # OK to proceed