File tree Expand file tree Collapse file tree
main/groovy/net/gleske/jervis/tools
test/groovy/net/gleske/jervis/tools Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -147,6 +147,20 @@ class FilterByContext {
147147 */
148148 Map context = [:]
149149
150+ /**
151+ Tracks known metadata so that users can extend filtering with custom
152+ metadata.
153+ */
154+ private List known_metadata = [
155+ ' pr' ,
156+ ' branch' ,
157+ ' tag' ,
158+ ' push' ,
159+ ' cron' ,
160+ ' manually' ,
161+ ' pr_comment' ,
162+ ]
163+
150164 /**
151165 A list of user-provided filters where a filter can be a single string or
152166 a filter map.
@@ -300,7 +314,7 @@ class FilterByContext {
300314 else if ((this . context. metadata[filterKey] in Boolean ) || (userExpression in Boolean )) {
301315 return (userExpression == (context == filterKey))
302316 }
303- else if (context != filterKey) {
317+ else if ((context in known_metadata) && context != filterKey) {
304318 return false
305319 }
306320 // String is the only other case
Original file line number Diff line number Diff line change @@ -1184,4 +1184,26 @@ class FilterByContextTest extends GroovyTestCase {
11841184 shouldFilter. allowBuild(' invalid' )
11851185 }
11861186 }
1187+ @Test public void test_FilterByContext_allowBuild_with_custom_metadata () {
1188+ // stage_name is custom arbitrary data example in this test
1189+ Map context = [
1190+ trigger : ' ' ,
1191+ context : ' branch' ,
1192+ metadata : [
1193+ pr : false ,
1194+ branch : ' main' ,
1195+ tag : ' ' ,
1196+ push : false ,
1197+ cron : false ,
1198+ manually : ' ' ,
1199+ pr_comment : ' ' ,
1200+ stage_name : ' Some stage' ,
1201+ ]
1202+ ]
1203+ Map filters = [branch : ' main' , stage_name : ' /Some.*/' , combined : true ]
1204+ FilterByContext shouldFilter = new FilterByContext (context, filters)
1205+ assert shouldFilter. allowBuild == true
1206+ assert shouldFilter. allowBuild(stage_name : ' Some stage2' ) == false
1207+ assert shouldFilter. allowBuild(stage_name : ' Some stage' ) == true
1208+ }
11871209}
Original file line number Diff line number Diff line change 1616/**
1717 Get a build environment context map meant to be used with build trigger
1818 filtering by context. This is used by the FilterByContext class typically.
19+
20+ If you wish to extend the metadata available to filtering builds, then define
21+ a NonCPS variable named vars/adminExtendBuildContextMap.groovy which takes a
22+ context HashMap as an argument and returns the enhanced context HashMap.
23+
24+ Example vars/adminExtendBuildContextMap.groovy
25+ Map call(Map context) {
26+ context.metadata.your_field = 'value'
27+ return context
28+ }
1929 */
2030
2131import hudson.model.Cause
@@ -91,7 +101,8 @@ Map call() {
91101 push : false ,
92102 cron : false ,
93103 manually : ' ' ,
94- pr_comment : ' '
104+ pr_comment : ' ' ,
105+ stage_name : (env. STAGE_NAME ?: ' ' ),
95106 ]
96107 ]
97108
@@ -134,5 +145,9 @@ Map call() {
134145 context. context = ' branch'
135146 }
136147
148+ if (hasGlobalVar(' adminExtendBuildContextMap' )) {
149+ context = adminExtendBuildContextMap(context)
150+ }
151+
137152 return context
138153}
You can’t perform that action at this time.
0 commit comments