@@ -2819,3 +2819,236 @@ describe("PR review-state workflow", () => {
28192819 } )
28202820 } )
28212821} )
2822+
2823+ describe ( "PR review-state workflow concurrency group (#1707)" , ( ) => {
2824+ // Minimal evaluator for the exact GitHub expression subset used by
2825+ // workflow.concurrency.group: top-level `||` chains, `&&`, loose `==`,
2826+ // `github.` property paths, `[0]` indexing (null parent -> null),
2827+ // parenthesized operands, and string/number literals. GitHub expressions
2828+ // use loose truthiness, so `||`/`&&` return operand values rather than
2829+ // booleans and missing properties evaluate to null instead of throwing.
2830+ interface GithubExpressionContext {
2831+ event_name : string
2832+ run_id : number
2833+ event : {
2834+ pull_request ?: { number : number }
2835+ issue ?: { number : number }
2836+ workflow_run ?: { pull_requests : Array < { number : number } > }
2837+ inputs ?: { pull_request_number : number }
2838+ }
2839+ }
2840+
2841+ function splitTopLevel ( expression : string , operator : "||" | "&&" | "==" ) : string [ ] {
2842+ const parts : string [ ] = [ ]
2843+ let depth = 0
2844+ let inString = false
2845+ let start = 0
2846+ for ( let index = 0 ; index < expression . length ; index ++ ) {
2847+ const char = expression [ index ]
2848+ if ( char === "'" ) {
2849+ inString = ! inString
2850+ } else if ( ! inString && char === "(" ) {
2851+ depth ++
2852+ } else if ( ! inString && char === ")" ) {
2853+ depth --
2854+ } else if ( ! inString && depth === 0 && expression . startsWith ( operator , index ) ) {
2855+ parts . push ( expression . slice ( start , index ) )
2856+ index += operator . length - 1
2857+ start = index + 1
2858+ }
2859+ }
2860+ parts . push ( expression . slice ( start ) )
2861+ return parts
2862+ }
2863+
2864+ function isTruthy ( value : unknown ) : boolean {
2865+ return value !== null && value !== undefined && value !== false && value !== 0 && value !== ""
2866+ }
2867+
2868+ function looseEquals ( left : unknown , right : unknown ) : boolean {
2869+ if ( left === null || left === undefined || right === null || right === undefined ) {
2870+ return ( left ?? null ) === ( right ?? null )
2871+ }
2872+ if ( typeof left === "string" && typeof right === "string" ) {
2873+ return left . toLowerCase ( ) === right . toLowerCase ( )
2874+ }
2875+ return left === right
2876+ }
2877+
2878+ function resolvePath ( path : string , github : GithubExpressionContext ) : unknown {
2879+ let value : unknown = github
2880+ for ( const segment of path . split ( "." ) ) {
2881+ if ( value === null || value === undefined ) {
2882+ return null
2883+ }
2884+ const match = / ^ ( [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * ) (?: \[ ( \d + ) \] ) ? $ / . exec ( segment )
2885+ if ( ! match ) {
2886+ throw new Error ( `Unsupported path segment: ${ segment } ` )
2887+ }
2888+ value = ( value as Record < string , unknown > ) [ match [ 1 ] ]
2889+ if ( match [ 2 ] !== undefined ) {
2890+ if ( ! Array . isArray ( value ) ) {
2891+ return null
2892+ }
2893+ value = value [ Number ( match [ 2 ] ) ] ?? null
2894+ }
2895+ }
2896+ return value ?? null
2897+ }
2898+
2899+ function evaluatePrimary ( expression : string , github : GithubExpressionContext ) : unknown {
2900+ const current = expression . trim ( )
2901+ if ( current . startsWith ( "(" ) ) {
2902+ // Strip the parentheses only when the first one wraps the whole expression.
2903+ let depth = 0
2904+ for ( let index = 0 ; index < current . length ; index ++ ) {
2905+ if ( current [ index ] === "(" ) {
2906+ depth ++
2907+ } else if ( current [ index ] === ")" ) {
2908+ depth --
2909+ }
2910+ if ( depth === 0 ) {
2911+ if ( index === current . length - 1 ) {
2912+ return evaluateExpression ( current . slice ( 1 , - 1 ) , github )
2913+ }
2914+ break
2915+ }
2916+ }
2917+ }
2918+ if ( current . startsWith ( "'" ) && current . endsWith ( "'" ) ) {
2919+ return current . slice ( 1 , - 1 )
2920+ }
2921+ if ( / ^ \d + $ / . test ( current ) ) {
2922+ return Number ( current )
2923+ }
2924+ if ( current . startsWith ( "github." ) ) {
2925+ return resolvePath ( current . slice ( "github." . length ) , github )
2926+ }
2927+ return evaluateExpression ( current , github )
2928+ }
2929+
2930+ function evaluateComparison ( expression : string , github : GithubExpressionContext ) : unknown {
2931+ const parts = splitTopLevel ( expression , "==" )
2932+ if ( parts . length === 1 ) {
2933+ return evaluatePrimary ( parts [ 0 ] , github )
2934+ }
2935+ return looseEquals ( evaluatePrimary ( parts [ 0 ] , github ) , evaluatePrimary ( parts [ 1 ] , github ) )
2936+ }
2937+
2938+ function evaluateAndChain ( expression : string , github : GithubExpressionContext ) : unknown {
2939+ let result : unknown = true
2940+ for ( const part of splitTopLevel ( expression , "&&" ) ) {
2941+ result = evaluateComparison ( part , github )
2942+ if ( ! isTruthy ( result ) ) {
2943+ return result
2944+ }
2945+ }
2946+ return result
2947+ }
2948+
2949+ function evaluateExpression ( expression : string , github : GithubExpressionContext ) : unknown {
2950+ let result : unknown = null
2951+ for ( const part of splitTopLevel ( expression , "||" ) ) {
2952+ result = evaluateAndChain ( part , github )
2953+ if ( isTruthy ( result ) ) {
2954+ return result
2955+ }
2956+ }
2957+ return result
2958+ }
2959+
2960+ function concurrencyGroupFor ( github : GithubExpressionContext ) : string {
2961+ const groupTemplate = workflow . concurrency . group as string
2962+ return groupTemplate . replace ( / \$ \{ \{ ( .+ ?) \} \} / s, ( _match , expression : string ) => {
2963+ const value = evaluateExpression ( expression , github )
2964+ return isTruthy ( value ) ? String ( value ) : ""
2965+ } )
2966+ }
2967+
2968+ function contextFor ( overrides : {
2969+ event_name : string
2970+ run_id ?: number
2971+ event ?: GithubExpressionContext [ "event" ]
2972+ } ) : GithubExpressionContext {
2973+ return {
2974+ event_name : overrides . event_name ,
2975+ run_id : overrides . run_id ?? 21057 ,
2976+ event : overrides . event ?? { } ,
2977+ }
2978+ }
2979+
2980+ it ( "keeps cancel-in-progress disabled" , ( ) => {
2981+ expect ( workflow . concurrency [ "cancel-in-progress" ] ) . toBe ( false )
2982+ } )
2983+
2984+ it ( "keys every per-PR event type for the same PR to the same group" , ( ) => {
2985+ const contexts = [
2986+ contextFor ( { event_name : "pull_request_target" , event : { pull_request : { number : 1664 } } } ) ,
2987+ contextFor ( { event_name : "pull_request_review" , event : { pull_request : { number : 1664 } } } ) ,
2988+ // CodeRabbit status comments arrive as issue_comment events on the PR.
2989+ contextFor ( { event_name : "issue_comment" , event : { issue : { number : 1664 } } } ) ,
2990+ contextFor ( { event_name : "workflow_dispatch" , event : { inputs : { pull_request_number : 1664 } } } ) ,
2991+ contextFor ( {
2992+ event_name : "workflow_run" ,
2993+ event : { workflow_run : { pull_requests : [ { number : 1664 } ] } } ,
2994+ } ) ,
2995+ ]
2996+
2997+ for ( const context of contexts ) {
2998+ expect ( concurrencyGroupFor ( context ) ) . toBe ( "label-pr-review-state-1664" )
2999+ }
3000+ } )
3001+
3002+ it ( "keys different PRs to different groups so unrelated triggers cannot starve a pending run" , ( ) => {
3003+ // The #1707 regression: with one shared group, a trigger for PR B superseded
3004+ // PR A's pending reconcile. Per-PR keys make that supersession impossible.
3005+ const prA = contextFor ( { event_name : "pull_request_target" , event : { pull_request : { number : 1664 } } } )
3006+ const prB = contextFor ( { event_name : "pull_request_target" , event : { pull_request : { number : 1707 } } } )
3007+
3008+ const groupA = concurrencyGroupFor ( prA )
3009+ const groupB = concurrencyGroupFor ( prB )
3010+ expect ( groupA ) . toBe ( "label-pr-review-state-1664" )
3011+ expect ( groupB ) . toBe ( "label-pr-review-state-1707" )
3012+ expect ( groupB ) . not . toBe ( groupA )
3013+ } )
3014+
3015+ it ( "keys issue comments by issue number, matching the PR group for the same numeric id" , ( ) => {
3016+ // Issues and PRs share one GitHub number sequence, so an issue-comment run
3017+ // can never collide with a different PR's group.
3018+ const issueComment = contextFor ( { event_name : "issue_comment" , event : { issue : { number : 1707 } } } )
3019+ const prEvent = contextFor ( { event_name : "pull_request_target" , event : { pull_request : { number : 1707 } } } )
3020+
3021+ expect ( concurrencyGroupFor ( issueComment ) ) . toBe ( concurrencyGroupFor ( prEvent ) )
3022+ } )
3023+
3024+ it ( "falls back to unique per-run groups for workflow runs without an associated PR" , ( ) => {
3025+ // Fork workflow_run events leave pull_requests empty, as do branch runs with
3026+ // no open PR. They must never be superseded by (or supersede) another run.
3027+ const forkRunA = contextFor ( {
3028+ event_name : "workflow_run" ,
3029+ run_id : 35477088534 ,
3030+ event : { workflow_run : { pull_requests : [ ] } } ,
3031+ } )
3032+ const forkRunB = contextFor ( {
3033+ event_name : "workflow_run" ,
3034+ run_id : 35477090001 ,
3035+ event : { workflow_run : { pull_requests : [ ] } } ,
3036+ } )
3037+
3038+ const groupA = concurrencyGroupFor ( forkRunA )
3039+ const groupB = concurrencyGroupFor ( forkRunB )
3040+ expect ( groupA ) . toBe ( "label-pr-review-state-35477088534" )
3041+ expect ( groupB ) . toBe ( "label-pr-review-state-35477090001" )
3042+ expect ( groupA ) . not . toBe ( groupB )
3043+ expect ( groupA ) . not . toBe ( "label-pr-review-state-1664" )
3044+ expect ( groupB ) . not . toBe ( "label-pr-review-state-1664" )
3045+ } )
3046+
3047+ it ( "shares a single sweep group for the hourly schedule and pushes to main" , ( ) => {
3048+ const scheduled = contextFor ( { event_name : "schedule" } )
3049+ const push = contextFor ( { event_name : "push" } )
3050+
3051+ expect ( concurrencyGroupFor ( scheduled ) ) . toBe ( "label-pr-review-state-sweep" )
3052+ expect ( concurrencyGroupFor ( push ) ) . toBe ( "label-pr-review-state-sweep" )
3053+ } )
3054+ } )
0 commit comments