Preventing Job Closure on Submit

Using a submit trigger to validate job status

PROBLEM

I would like to prevent jobs from being closed automatically when they are linked to a changelist being submitted.


SOLUTION

The following shell script example finds any jobs attached to a submitted changelist and reasserts the current job status. Jobs can still be closed with the p4 fix and p4 job commands.

  1. The trigger assumes that job identifiers are of the format "job[0-9]{6}". If your jobs have a different identifier format, you need to modify the script accordingly.

  2. The trigger assumes that your job spec has a "Status" field and that all possible status values are single words (no spaces).

  3. If you enter a changelist description that matches this pattern "^ job[0-9]{6} closed #", the script assumes that this is an attached job being closed and attempts to act on it. Although unlikely, you should be aware of the possibility.
There are certainly other potential problems and/or shortcomings. You should test the script thoroughly and modify it as necessary before using it in a production environment. You need to set the path to P4, the user, port, and password in the script:
#!/bin/sh

# Suppresses the default behavior whereby jobs are automatically closed
# if they are associated with a submitted change.
# Set the path to p4, the port, user, and password if necessary.

P4CMD="p4 -p  -u  -P "

CHANGE=$1

# Step over all closing jobs in the changelist being submitted.

for JOB in `$P4CMD change -o -s $CHANGE | \
   grep -E "^	job[0-9]{6} closed	#" | cut -d ' ' -f 1`

do

   # Obtain the current status of the job and reassert it.

   STATUS=`$P4CMD job -o $JOB | \
      grep -E "^Status:	" | cut -d '	' -f 2`
   $P4CMD fix -s $STATUS -c $CHANGE $JOB

done

exit 0
You also need to add this entry to the triggers table:
preventJobClosure change-submit //depot/... "preventJobClosure.sh %change%"
Note:
This is an example only, and should be modified and tested thoroughly before being used in a production server.