version:2.1orbs:python:circleci/python@2.1.1workflows:main:jobs:- python/test- deploy:name:Deployrequires:[python/test ]context:[no-my-context ]#Feature and Dev work use a dev context available to any branch## Jobs included for valid config, replace with your own.#jobs:deploy:executor:python/defaultsteps:- run:echo "Hello Governance!"
packageorgimportfuture.keywords#Eachfileisasinglepolicypolicy_name["project_context_protection"]valid_context_names:={"my-context","this-context"}#UUIDforprojecttoapplyruletoin_scope_projects:={"788dd296-2fca-4718-82f8-07db1637a58e"}#Mayactivate1tomanyrulesenable_rule["use_project_context_only"]{in_scope_projects[data.meta.project_id]#applythisruleonlyifourIDmatchedthecurrentevaluatedproject(ignoreoherprojects)}#RulesdefaulttoSOFT_FAIL/WARNING,butcanbeahard_fail/BLOCKhard_fail["use_project_context_only"]#Customruleisaregofunctionthatiteratesconfig.ymlobjecttocomparebranchwithcontextuse.use_project_context_only[reason]{somewfName,workflowininput.workflowssomejobinworkflow.jobssomejobName,jobInfoinjobsomecontextinto_array(jobInfo.context)#Enforceasinglecontextname#notregex.match("this-projects-context-name",context)#OR#Enforcelistofcontextsallowednotcontext_is_only_mine(context)#displaythisfailurereasontouser.reason:=sprintf("You may not use ANOTHER PROJECT'S context: %s. Offending workflow.job: `%s.%s`",[context,wfName,job_name(jobName,jobInfo)])}#Helperfunctions#Convertvaluetoarayifitisn'toneto_array(value):=[value]if{notis_array(value)}else:=value#Use`name`ifprovided,otherwiseusekeynamejob_name(jobName,jobInfo):=jobInfo.nameif{jobInfo.name}else:=jobName#returnstrueifjobonlyhasbranch.onlyanditismaincontext_is_only_mine(context):=true{contextinvalid_context_names}else:=false