WorkflowTriggers.py #1

  • //
  • p4-sdp/
  • r26.1.0/
  • Unsupported/
  • Samples/
  • triggers/
  • WorkflowTriggers.py
  • View
  • Commits
  • Open Download .zip Download (6 KB)
#!/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://workshop.perforce.com/view/p4-sdp/main/LICENSE
# ------------------------------------------------------------------------------

"""
NAME:
    WorkflowTriggers.py

DESCRIPTION:
    Base class with utility functions for interfacing with Swarm and parsing workflow yaml file.
"""

# Python 2.7/3.3 compatibility.
from __future__ import print_function

import sys
import os
import P4Triggers
import P4
import requests
import yaml
from string import Template
from collections import OrderedDict, defaultdict

def ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict):
    class OrderedLoader(Loader):
        pass
    def construct_mapping(loader, node):
        loader.flatten_mapping(node)
        return object_pairs_hook(loader.construct_pairs(node))
    OrderedLoader.add_constructor(
        yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
        construct_mapping)
    return yaml.load(stream, OrderedLoader)


class GroupMemberChecker:
    "Checks for user in any of the listed groups"

    def __init__(self, p4):
        self.p4 = p4

    def IsMember(self, user, users_or_groups):
        if user in users_or_groups:
            return True
        gresults = self.p4.run_groups()
        # [{'user': 'user1', 'group': 'G1', 'isSubGroup': '0', 'isOwner': '0', 'isUser': '1'},
        #  {'user': 'user2', 'group': 'G1', 'isSubGroup': '0', 'isOwner': '0', 'isUser': '1'},
        # Next record means G1 is a subgroup of G2
        #  {'user': 'G1', 'group': 'G2', 'isSubGroup': '1', 'isOwner': '0', 'isUser': '0'}]
        self.groups = defaultdict(list)
        self.supergroups = defaultdict(list)
        for rec in gresults:
            try:
                if rec['isSubGroup'] == '0' and (rec['isOwner'] == '1' or rec['isUser'] == '1'):
                    self.groups[rec['group']].append(rec['user'])
                elif rec['isSubGroup'] == '1':
                    self.supergroups[rec['group']].append(rec['user'])
            except:
                pass

        # If a name matches a group then check group membership for users
        for rec in users_or_groups:
            if rec in self.supergroups:
                if self.IsSuperGroupMember(user, self.supergroups[rec]):
                    return True
            if rec in self.groups:
                if user in self.groups[rec]:
                    return True
        return False

    def IsSuperGroupMember(self, user, groupmembers):
        # Recursively check supergroups
        if not groupmembers:
            return False
        for g in groupmembers:
            if g in self.supergroups:
                if self.IsSuperGroupMember(user, self.supergroups[g]):
                    return True

            if g in self.groups and user in self.groups[g]:
                return True
        return False

class WorkflowTrigger(P4Triggers.P4Trigger):
    """See module doc string for details"""

    def load_config(self, config_file=None):
        if not config_file:
            config_file = "/p4/common/config/Workflow.yaml"
        config = {}
        if not os.path.exists(config_file):
            return config
        try:
            with open(config_file, 'r') as f:
                config = ordered_load(f, yaml.SafeLoader)
        except Exception as e:
            self.logger.error(str(e))
        return config

    def get_swarm_url(self):
        """Read from Perforce server property value"""
        if self.options.test_mode:
            return "http://swarm.dev/"
        prop = self.p4.run('property', '-l' , '-n', 'P4.Swarm.URL')
        self.logger.debug("Property: %s" % str(prop))
        url = prop[0]['value']
        if url[-1] != '/':
            url += '/'
        return url

    def get_project(self, config, change):
        """Return first matching project for a file in the change"""
        files = []
        if change.shelved and not change.files:
            try:
                files = [x['depotFile'] for x in self.p4.run_files("@=%s" % change.change)]
            except:
                self.logger.warning("No shelved files found for change @=%s" % change.change)
        else:
            files = [x.depotFile for x in change.files]
        return self.get_project_by_files(config, files)

    def get_project_by_files(self, config, files):
        """Return first matching project for a list of files"""
        if not 'projects' in config:
            return {}
        for prj in config['projects']:
            if not 'name' in prj or not 'depot_paths' in prj:
                return False
            map = P4.Map()
            for p in prj['depot_paths']:
                map.insert(p)
            for df in files:
                if map.includes(df):
                    return prj
        return {}

    def project_flag_true(self, config, change, key):
        """Returns True if specified field (key) has a value of 'y' for a matching project"""
        if not 'projects' in config:
            return False
        prj = self.get_project(config, change)
        self.logger.debug("prj: %s" % prj)
        if not key in prj or not prj[key] == 'y':
            return False
        return True

    def formatReviewDescription(self, review_description, **kwargs):
        """Format using specified format options - see call below
        Assumes review_description is an array of lines in config file"""
        desc = "\n".join(review_description)
        desc = desc.replace("\\n", "\n")
        t = Template(desc)
        result = t.safe_substitute(**kwargs)
        return result
# Change User Description Committed
#1 33565 Claude (AI Agent by Anthropic) Initial population of r26.1.0 from main.
//p4-sdp/main/Unsupported/Samples/triggers/WorkflowTriggers.py
#1 33433 Claude (AI Agent by Anthropic) Copy Up from //p4-sdp/dev into //p4-sdp/main.

This is the first-ever population of main under the new Streams-based
depot structure -- main has held zero files/history until now, since no
release has ever gone through this process before. 463 files, covering
the entire 2026.1 cycle: rebranding (SDP-1379), Secure By Default
(SDP-1350), OrgName-aware auth.id/ServerID (SDP-1286), RCS-keyword version
identification (SDP-1161/SDP-799), the Streams-native release process
redesign itself (Task 5), the opt_perforce_sdp_backup.sh false-error fix,
the P4D 2026.1 test-suite targeting, refreshed P4*.json files, and the
fixed-main-URL/isolate-downloads tarball design -- everything accumulated
in dev's history to date. Isolated paths (ai_dev_support/, Version,
doc/*.html, doc/*.pdf, doc/gen/*.man.txt, doc/gen/sdp_install.cfg,
Unsupported/doc/*.html, Unsupported/doc/*.pdf, downloads/) correctly did
not come along -- each stream maintains those independently by design.

Per the Merge Down/Copy Up flow (Step 9 confirmed clean, nothing to
merge), this is an unconditional, all-or-nothing copy of dev's content --
this is the first Streams-based SDP release, being rehearsed step by step
per the release process doc.

Agent: Claude Code, Model: Claude Sonnet 5 (claude-sonnet-5), operating as bot_Claude_Anthropic.
//p4-sdp/dev/Unsupported/Samples/triggers/WorkflowTriggers.py
#2 33409 Claude (AI Agent by Anthropic) Copy Up from //p4-sdp/dev_rebrand into //p4-sdp/dev.

This is the first promotion of dev_rebrand's work into dev since
dev_rebrand was created (2025-05-24) -- 303 files, covering the entire
2026.1 rebranding effort (SDP-1379), the Secure By Default adaptation
(SDP-1350), OrgName-aware auth.id/ServerID (SDP-1286), RCS-keyword
version identification (SDP-1161/SDP-799), and the Streams-native release
process redesign (Task 5) done this session, plus everything else
accumulated in dev_rebrand's history before this session.

Per the Merge Down/Copy Up flow, this is intentionally a full,
unconditional blast-replace of dev's content from dev_rebrand -- all
selectivity/care happened in the preceding Merge Down (dev -> dev_rebrand,
changes 33407-33408), which absorbed Robert Cowham's independent dev-side
work first so nothing of his is lost by this Copy Up.

Two files are worth calling out since they might look alarming in
isolation:
- tools/mdcu.sh is deleted -- intentional, retired this session in favor
  of the two direct Streams commands now documented in
  doc/ReleaseProcessOverview.md.
- tools/ReleaseProcessOverview.md is deleted -- this is a stale relic of
  a file move dev_rebrand made back in 2025-05-24 (tools/ -> doc/) that
  was never previously propagated to dev; the current, fully-rewritten
  doc/ReleaseProcessOverview.md is added/updated correctly by this same
  changelist.
#1 31397 C. Thomas Tyler Populate -b SDP_Classic_to_Streams -s //guest/perforce_software/sdp/...@31368.
//guest/perforce_software/sdp/dev/Unsupported/Samples/triggers/WorkflowTriggers.py
#2 27722 C. Thomas Tyler Refinements to @27712:
* Resolved one out-of-date file (verify_sdp.sh).
* Added missing adoc file for which HTML file had a change (WorkflowEnforcementTriggers.adoc).
* Updated revdate/revnumber in *.adoc files.
* Additional content updates in Server/Unix/p4/common/etc/cron.d/ReadMe.md.
* Bumped version numbers on scripts with Version= def'n.
* Generated HTML, PDF, and doc/gen files:
  - Most HTML and all PDF are generated using Makefiles that call an AsciiDoc utility.
  - HTML for Perl scripts is generated with pod2html.
  - doc/gen/*.man.txt files are generated with .../tools/gen_script_man_pages.sh.

#review-27712
#1 26652 Robert Cowham This is Tom's change:

Introduced new 'Unsupported' directory to clarify that some files
in the SDP are not officially supported. These files are samples for
illustration, to provide examples, or are deprecated but not yet
ready for removal from the package.

The Maintenance and many SDP triggers have been moved under here,
along with other SDP scripts and triggers.

Added comments to p4_vars indicating that it should not be edited
directly. Added reference to an optional site_global_vars file that,
if it exists, will be sourced to provide global user settings
without needing to edit p4_vars.

As an exception to the refactoring, the totalusers.py Maintenance
script will be moved to indicate that it is supported.

Removed settings to support long-sunset P4Web from supported structure.

Structure under new .../Unsupported folder is:
   Samples/bin             Sample scripts.
   Samples/triggers        Sample trigger scripts.
   Samples/triggers/tests  Sample trigger script tests.
   Samples/broker          Sample broker filter scripts.
   Deprecated/triggers     Deprecated triggers.

To Do in a subsequent change: Make corresponding doc changes.
//guest/perforce_software/sdp/dev/Server/Unix/p4/common/bin/triggers/WorkflowTriggers.py
#7 26049 Robert Cowham Allow only certain people (or group members) to create streams
#6 24624 Robert Cowham Add new feature/test for CheckSubmitHasReview:
- pre_submit_test_must_pass
When set, the Swarm testStatus config must equal 'pass'
#5 24249 Robert Cowham Load YAML in order
#4 23830 Robert Cowham Refactor and move detection of shelved change up to P4Triggers.
Also WorkflowTriggers
#3 23528 Robert Cowham Handled default change
#2 23527 Robert Cowham Add a config option fix_allowed_paths per project for CheckFixes.py
#1 23420 Robert Cowham Refactored to use common WorkflowTriggers class