#!/bin/bash VERSION=1.2.0 #------------------------------------------------------------------------------ # job-new.sh # # This script simplifies the process of using "named" jobs with a Perforce # server. A "named" job is one that does not use Perforce's built-in job # numbering mechanism, which simply names jobs 'job######', incrementing the # numeric value. # # The idea of job-new.sh is that you can name your jobs something more # meaningful, like MyProj-3, ACME-15, or even ACME-15-3 (where the latter # implies that ACME-15-3 is a subtask of ACME-15). By default with # Perforce, jobs named anything other than the default must be manually # named and incremented. With this trigger, you simply name your job # something like "PRJ-new", and the "-new" suffix is replaced with the # next available number. So the first time you create a job with # "Job: PRJ-new", it will be saved as PRJ-1. The next time it will be # PRJ-2. # # This is vaguely like what Perforce does by default when you simply use # "Job: new", except that this numbering scheme does not use left-padding # of zeros, and the built-in prefix of "job" is not used. # # This trigger script is not appropriate to use if Perforce is # integrated with a defect tracker such as JIRA for Bugzilla. With such # systems, Perforce jobs mirror issue names from the defect tracker. # # This script must be enabled as a trigger by the Perforce server, with # an entry like the following needed in the Triggers table. # Triggers: # JobName form-in job "/path/to/job-new.sh %formfile%" # # This script also performs some rudimentary job name vetting, to be # sure the name is in lin with Workshop standards. # Per Thomas Gray: # # If no Project is specified, and the job name is "new", then job creation should succeed as normal. # If no Project is specified, and any name other than 'new' is specified, job creation should fail. # If a Project id is specified, but that Project has no stored prefix, job creation should fail if a job name of anything other than "new" was specified. # If a Project id is specified, and the Project has a stored prefix, job creation should fail if that the job is not created with the name "[prefix]-new". # If a Project id is specified, and the Project has a stored prefix, and the prefix matches the expected value and the new job name is "[prefix]-new", then job creation should succeed. # # # # Limitations: # * This assumes old jobs aren't deleted; it will re-use job numbers # if jobs are deleted. # * This dynamically checks existing jobs to determine the next # available job number. The current implementation is not # scalable to large numbers of jobs. (TO DO: Use a 'p4 # key' to recall the highest number for a given project and # the key value). # * Mutex locking is needed. This implementations allows for the # theoretical possibility of a race condition where to users # creating new jobs at the same time could collide. (TO DO: # Use a 'p4 key' to implement a semaphore to prevent the # unlikely chance of a race condition). # # This script has the following runtime dependencies: # This assumes the Perforce context and environment is appropriate, i.e.: # * The preferred 'p4' executable is in the path. # # * P4PORT is set appropriately. # # * P4USER is defined as a currently logged in user (as is typical # for Perforce trigger scripts which operate on the Perforce server # machine). # # * The BASH_VERSION must be 4.0 or higher, new enough to support the # =~ (regular expression matching) syntax. This is ubiquitous for # modern Linux distros. On Mac OSX, at least up thru 10.10.3/Yosemite, # a newer bash must be acquired. As one of several examples of # how to do this, see: http://clubmate.fi/upgrade-to-bash-4-in-mac-os-x/ # Declarations set -u set -e THISSCRIPT=${0##*/} # Usage: bail "message" [optional_exit_code] function bail () { echo -e "\n$THISSCRIPT v$VERSION: Error: ${1:-Unknown Error}\n" exit ${2:-1} } [[ $# -eq 1 ]] || bail "Bad usage: Missing %formfile% argument." declare oldFormFile=$1 declare newFormFile="${oldFormFile}.NEW.$$.$RANDOM.tmp" declare -i nextJobNum=1 declare thisJobNum=0 declare -i highestJobNum=0 declare -i changed=0 declare job= [[ -r $oldFormFile ]] || bail "Error: Missing form file [$oldFormFile]." while read line; do [[ $line == "#"* ]] && continue [[ $line != "Job:"* ]] && continue job=${line#Job:} job=$(echo $job) [[ $job == new ]] && continue [[ $job =~ job[0-9]{6}$ ]] && continue [[ $job =~ ^.+-[1-9][0-9]*$ ]] && continue if [[ $job =~ ^.+-new$ ]]; then changed=1 projName=${line#Job:} projName=${projName%-new} projName=$(echo $projName) jobSearchCmd="p4 -ztag -F %Job% jobs -e 'Job=${projName}*'" for job in $(eval $jobSearchCmd); do thisJobNum=${job##*-} # Confirm '$thisJobNum' really is purly numeric, so we can do math on it. if [[ $thisJobNum =~ ^[1-9]+$ ]]; then [[ $thisJobNum -gt $highestJobNum ]] && highestJobNum=$thisJobNum fi done nextJobNum=$(($highestJobNum+1)) else bail "Malformd job name [$job]. Sample valid values are:\n\tnew\n\tjob123456\n\tPRJ-33\n\tPRJ-new\n\tPRJ-2-new\n\n" fi done < $oldFormFile if [[ $changed -eq 1 ]]; then sed "s:${projName}-new:${projName}-${nextJobNum}:g" $oldFormFile > $newFormFile ||\ bail "Failed to update job number." /bin/mv -f "$newFormFile" "$oldFormFile" || bail "Failed to update job number." fi /bin/rm -f "$newFormFile" exit 0
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 12556 | C. Thomas Tyler |
job-new.sh v1.2.0: * Incorporated Lester's feedback. * Moved VERSION tag nearer to top of the file. * Tweaked BASH_VERSION comment to give Mac folks guidance on how to workaround Mac OSX having only an ancinet bash by default. #review-12557 @thomas_gray @lester_cheung |
||
#3 | 12539 | C. Thomas Tyler |
Implemened features to transform job-new.sh into a general purpose job name validation script, in addition to the original mission of incrementing '-new' features, per requirements from Thomas Gray. Updated test suite accordingly. Also simplified the test suite, which now uses the P4PORT 'rsh hack' to avoid needing to fire up a test server. #review-12540 @thomas_gray @lester_cheung |
||
#2 | 12094 | C. Thomas Tyler |
Added version tags to job-new.sh and run-tests.sh. Removed debug statements from job-new.sh. Updated .p4ignore to avoid adding log file. Updated test suite to run multi-pass test. Added APP_DATA setting to env.sh. |
||
#1 | 12093 | C. Thomas Tyler | Added job-new.sh and friends. |