#!/usr/bin/env python3 # -*- coding: utf-8 -*- # ============================================================================== # Copyright and license info is available in the LICENSE file included with # the Server Deployment Package (SDP), and also available online: # https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE # ------------------------------------------------------------------------------ """ NAME: CheckFixes.py DESCRIPTION: This trigger is intended for use with P4DTG (Defect Tracking Replication) installations. It will allows fixes to be added or deleted depending on the values of job fields. Thus you can control workflow and disallow fixes if jobs are in a particular state. So if the field JiraStatus is closed, then you are not allowed to add or delete a fix. To install, add a line to your Perforce triggers table like the following: check-fixes fix-add //... "python /p4/common/bin/triggers/CheckFixes.py -p %serverport% -u perforce %change% " check-fixes fix-delete //... "python /p4/common/bin/triggers/CheckFixes.py -p %serverport% -u perforce %change% " or (if server is standard SDP and has appropriate environment defaults for P4PORT and P4USER): check-fixes fix-add //... "python /p4/common/bin/triggers/CheckFixes.py %change% " check-fixes fix-delete //... "python /p4/common/bin/triggers/CheckFixes.py %change% " You may need to provide the full path to python executable, or edit the path to the trigger. Also, don't forget to make the file executable. To configure, read and modify the following lines up to the comment that reads "END OF CONFIGURATION BLOCK". You may also need to modify the definition of which fields constitute a new job based on your jobspec. This is in the allowed_job() function. """ # Python 2.7/3.3 compatibility. from __future__ import print_function import sys import re import argparse import textwrap import P4Triggers import shutil import os ###################################################################### # CONFIGURATION BLOCK # The error message we give to the user MSG_CANT_CHANGE_JOB = """ You are not allowed to link changes to these jobs because of their state. Please change the state first in JIRA and try again. """ # The states where job change is allowed ALLOWED_CHANGE_STATES = ["Open", "In Work"] # The field to check STATE_FIELD = "JiraStatus" # END OF CONFIGURATION BLOCK ###################################################################### class CheckFixes(P4Triggers.P4Trigger): """See module doc string for details""" def __init__(self, *args, **kwargs): P4Triggers.P4Trigger.__init__(self, **kwargs) self.parse_args(__doc__, args) def add_parse_args(self, parser): """Specific args for this trigger - also calls super class to add common trigger args""" parser.add_argument('-d', '--delete', default=False, action="store_true", help="Whether this is Fix-Delete trigger. Default: False so Fix-Add assumed") parser.add_argument('change', help="User carrying out the command - %%user%% argument from triggers entry.") parser.add_argument('jobs', nargs='+', help="List of jobs - %%jobs%% argument from triggers entry.") super(CheckFixes, self).add_parse_args(parser) def run(self): """Runs trigger""" try: self.logger.debug("CheckFixes firing") self.setupP4() self.p4.connect() jobs = [] for jobname in self.options.jobs: jobs.append(self.p4.fetch_job(jobname)) errors = [] for job in jobs: if job['JiraStatus'] not in ALLOWED_CHANGE_STATES: errors.append("Job %s has state '%s'" % (job['Job'], job['JiraStatus'])) if errors: msg = MSG_CANT_CHANGE_JOB + str(errors) + "\n\n" self.message(msg) return 1 except Exception: return self.reportException() return 0 if __name__ == '__main__': """ Main Program""" trigger = CheckFixes(*sys.argv[1:]) sys.exit(trigger.run())