#!/usr/local/bin/ruby # # Copyright 2005 Perforce Software. All rights reserved. # # # ejsjobcommit form-commit job "job_commit.rb %user% %formfile% %action%" # if ARGV[0] == nil || ARGV[1] == nil then print "Trigger Error: Missing argument\n" exit 1 end require "P4Data.rb" require "P4Form.rb" require "P4Valid.rb" require "P4Logic.rb" require "P4Mail.rb" logfile = nil File.open( ##DEBUG P4LOGDIR + "jobcommit.log", ##DEBUG "a") do |logfile| ##DEBUG user=ARGV[0] jobfile=ARGV[1] action=ARGV[2] logfile.printf("User(%s) JobFile(%s) Action(%s)\n", ##DEBUG user, jobfile, action) ##DEBUG if action == nil then action = "unknown" else action.strip! end datestamp = Time.now.strftime("%Y/%m/%d %H:%M:%S") logfile.printf("%s User(%s) JobFile(%s) Action(%s)\n", ##DEBUG datestamp, user, jobfile, action) ##DEBUG # # Read in tmp file for revised/new job details # t_file = File.new(jobfile) t_form = P4Form.new(t_file) t_file.close # # Extract the jobid for comparing with any previous version of job # job = t_form.fields[P4JOBFIELD] logfile.printf("JobID: [%s]\n", job) ##DEBUG logfile.printf("#{P4REPORTEDDATEFIELD}: [%s]\n", t_form.fields[P4REPORTEDDATEFIELD]) ##DEBUG logfile.printf("#{P4MODIFIEDDATEFIELD}: [%s]\n", t_form.fields[P4MODIFIEDDATEFIELD]) ##DEBUG # SHOULD PROBABLY USE THIS FOR CHECKING FOR NEW headrevtxt = `#{P4_BIN} -u #{P4USER} -p #{P4PORT} fstat //spec/job/#{job}#{P4SPECSUFFIX} 2> /dev/null | egrep '^... headRev'` headrev = headrevtxt.chomp.split(/ */) spec_form = nil logfile.printf("\nheadrev.len %d, headrev[2] %d", headrev.length, headrev[2].to_i) ##DEBUG if headrev.length >= 3 && headrev[2].to_i > 1 then rev = headrev[2].to_i - 1 spec_form = P4Form.new(`#{P4_BIN} -u #{P4USER} -p #{P4PORT} print -q //spec/job/#{job}#{P4SPECSUFFIX}##{rev}`.split("\n")) logfile.printf("\n#{P4_BIN} -u #{P4USER} -p #{P4PORT} print -q //spec/job/#{job}#{P4SPECSUFFIX}##{rev}") end if spec_form == nil then ##DEBUG logfile.printf("\nspec_form is nil\n") ##DEBUG else ##DEBUG logfile.printf("\nspec_form retrieved\n") ##DEBUG end if t_form.fields[P4REPORTEDDATEFIELD] == t_form.fields[P4MODIFIEDDATEFIELD] then # # Process Related Field, this was deferred from job_in since # the jobid is not available until this point # related_jobs = extract_set(t_form.fields[P4RELATEDJOBSFIELD]) failed_list = [] related_jobs.each do |reljob| rejected = related_job(user, logfile, true, job, reljob, "") if rejected != "" then # # The related job update failed but this job cannot be cancelled # at this point, so add this information to the notification email # failed_list << reljob end end # # New Job notification # if spec_form == nil then notify = {"to" => [], "cc" => [] } notify["cc"] << t_form.fields[P4REPORTEDBYFIELD] dlist = t_form.fields[P4DESCRIPTIONFIELD].split(/[ \t]*\n[ \t\n]*/) if dlist[0] == nil || dlist[0].strip == "" then dlist.delete_at(0) end body = [] body << sprintf("%s: %s %s", t_form.fields[P4PRODUCTFIELD], t_form.fields["Type"], t_form.fields["Severity"]) body << "" if failed_list.length > 0 then body << sprintf("FAILED to set %s for: %s\n", P4RELATEDJOBSFIELD, failed_list.join(", ")) body << "" end body << dlist body.flatten! logfile.print "subject: added #{job}\n" notify_people(notify, job, "Added", t_form, body) end elsif action =~ /^submitted change [0-9][0-9]* (deleted|added)/ || action =~ /^pending change [0-9][0-9]* submitted/ then logfile.print "p4 fixes -j #{job}\n" ##DEBUG logfile.print `#{P4_BIN} -u #{P4USER} -p #{P4PORT} fixes -j #{job}` ##DEBUG fixes=`#{P4_BIN} -u #{P4USER} -p #{P4PORT} fixes -j #{job} | awk '{ print $5; }'`.split(/[ \t\n][ \t\n]*/) oldfixes=extract_set(t_form.fields[P4FIXESFIELD]) oldfixes.compact! oldfixes.uniq! oldfixes.sort! fixes.compact! fixes.uniq! fixes.sort! logfile.printf("\nFixes: %s\n", fixes.join(", ")) ##DEBUG logfile.printf("\nOldFixes: %s\n", oldfixes.join(", ")) ##DEBUG logfile.flush ##DEBUG # SHOULD PROBABLY USE THIS FOR CHECKING FOR NEW #headrevtxt = #`#{P4_BIN} -u #{P4USER} -p #{P4PORT} fstat //spec/job/#{job}#{P4SPECSUFFIX} 2> /dev/null | egrep '^... headRev'` #headrev = headrevtxt.chomp.split(/ */) #spec_form = nil #logfile.printf("\nheadrev.len %d, headrev[2] %d", headrev.length, headrev[2].to_i) ##DEBUG #if headrev.length >= 3 && headrev[2].to_i > 1 then #rev = headrev[2].to_i - 1 #spec_form = #P4Form.new(`#{P4_BIN} -u #{P4USER} -p #{P4PORT} print -q //spec/job/#{job}#{P4SPECSUFFIX}##{rev}`.split("\n")) #logfile.printf("\n#{P4_BIN} -u #{P4USER} -p #{P4PORT} print -q //spec/job/#{job}#{P4SPECSUFFIX}##{rev}") #logfile.printf("\nOldfixes: %s", oldfixes.join(", ")) #logfile.printf("\nNewfixes: %s", fixes.join(", ")) #end if spec_form != nil && (oldfixes != fixes || t_form.fields[P4STATUSFIELD].strip != spec_form.fields[P4STATUSFIELD].strip) then logfile.printf("\nTempFile:\n") ##DEBUG t_form.print(logfile) ##DEBUG logfile.printf("\nSpecDepot:\n") ##DEBUG spec_form.print(logfile) ##DEBUG t_form.fields.delete(P4FIXESFIELD) # Remove old fixes will update below # # Send the possibly revised job to p4 job -i # p4proc = IO.popen("p4 -u #{user} job -i", "w+") # Force user to be 'u' logfile.printf("Updating job:\n") ##DEBUG logfile.flush ##DEBUG t_form.print(p4proc) p4proc.printf("EJPUserID: %s\n", user) # Tag as EJP update, save UserID p4proc.printf("EJPRevNum: %d\n", rev) # Specify spec rev to use if fixes.length > 0 then logfile.printf("Fixes: %s\n", fixes.join(", ")) ##DEBUG p4proc.printf("Fixes: %s\n", fixes.join(", ")) end p4proc.close_write saved = false p4proc.each do |line| logfile.print line ##DEBUG logfile.flush ##DEBUG if line =~ /Job #{job} saved/ then saved = true end end p4proc.close_read if !saved then ##DEBUG logfile.printf("Unable to update job %s\n", job) ##DEBUG logfile.flush ##DEBUG end ##DEBUG else ##DEBUG logfile.printf("Skipping updating of job %s\n", job) ##DEBUG end else ##DEBUG logfile.printf("Skipping updating of job %s\n", job) ##DEBUG end end # close logfile ##DEBUG exit 0 # ALWAYS OK to proceed