@@ -57,19 +57,19 @@ const isChangeRequestCloseCommand = (command: string) =>
5757// A create command anywhere else, as in `echo "gh pr create"`, is text, not a command that runs.
5858const COMMAND_START =
5959 "(?:^|[;&|({`\\n]|\\b(?:do|then|else)\\s)\\s*(?:(?:\\w+=\\S*|env|time|nohup|timeout\\s+\\S+)\\s+)*" ;
60- const CREATE_COMMANDS = sourceControlDefinitions . definitions . flatMap (
61- ( { kind , changeRequestCreateCommand : command } ) =>
62- command === undefined
63- ? [ ]
64- : // The subcommand must end its shell word: `gh pr create#x` runs another one.
65- [
66- {
67- kind ,
68- command,
69- run : new RegExp ( `${ COMMAND_START } ${ command . source } (?=$|[\\s;&|)])` , "gu" ) ,
70- } ,
71- ] ,
72- ) ;
60+ const CREATE_COMMANDS = sourceControlDefinitions . definitions . flatMap ( ( definition ) => {
61+ const command = definition . changeRequestCreateCommand ;
62+ return command === undefined
63+ ? [ ]
64+ : // The subcommand must end its shell word: `gh pr create#x` runs another one.
65+ [
66+ {
67+ definition ,
68+ command,
69+ run : new RegExp ( `${ COMMAND_START } ${ command . source } (?=$|[\\s;&|)])` , "gu" ) ,
70+ } ,
71+ ] ;
72+ } ) ;
7373const createCommandCount = ( code : string ) =>
7474 CREATE_COMMANDS . reduce ( ( count , { run } ) => count + [ ...code . matchAll ( run ) ] . length , 0 ) ;
7575// A push, directly or through a stack tool, that can start checks on a linked pull request.
@@ -144,17 +144,15 @@ const isPushCommand = (input: string) => PUSH_COMMAND.test(shellCode(shellScript
144144type CommandExecution = Extract < OrchestrationV2TurnItem , { readonly type : "command_execution" } > ;
145145
146146/**
147- * The change requests a shell command opened: URLs on lines of their own in the output of a
148- * command that runs a host's create command. One the command also names, as `gh pr view 12`
149- * does, existed before it ran. More URLs than create commands means something else printed
150- * some, such as `gh pr list`, so none are taken.
147+ * The change requests a shell command opened: the URLs its host's create command printed. One the
148+ * command also names, as `gh pr view 12` does, existed before it ran. More of a host's URLs than
149+ * its creates means something else printed some, such as `gh pr list`, so none of them are taken.
151150 */
152151export function changeRequestsCreatedBy ( input : string , output : string ) {
153152 if ( ! CREATE_COMMANDS . some ( ( { command } ) => command . test ( input ) ) ) return [ ] ;
154153 const script = shellScript ( input ) ;
155154 const code = shellCode ( script ) ;
156- const creates = createCommandCount ( code ) ;
157- if ( creates === 0 ) return [ ] ;
155+ if ( createCommandCount ( code ) === 0 ) return [ ] ;
158156 const otherCommands = code
159157 . split ( COMMAND_SEPARATOR )
160158 . filter ( ( part ) => createCommandCount ( part ) === 0 )
@@ -163,22 +161,26 @@ export function changeRequestsCreatedBy(input: string, output: string) {
163161 const otherScript = shellSegments ( script )
164162 . filter ( ( segment ) => createCommandCount ( shellCode ( segment ) ) === 0 )
165163 . join ( "\n" ) ;
166- const creators = new Set (
167- CREATE_COMMANDS . filter ( ( { run } ) => code . match ( run ) !== null ) . map ( ( { kind } ) => kind ) ,
168- ) ;
169- const created = new Map < string , ThreadPullRequestKey & { readonly url : string } > ( ) ;
170- for ( const [ , url = "" ] of output . matchAll ( URL_LINE ) ) {
171- const parsed = sourceControlDefinitions . parseChangeRequestUrl ( url ) ;
172- // Only the host whose create command ran can have printed a new change request.
173- if ( parsed === null || ! creators . has ( parsed . definition . kind ) ) continue ;
174- const { link } = parsed ;
175- const named = new RegExp ( `(?:^|(?<=[\\s#/=("']))${ link . number } (?=$|[\\s);&|/"'])` , "u" ) ;
176- // Quoted text in the other commands counts, as in `printf '.../pull/12'`.
177- if ( named . test ( otherScript ) ) continue ;
178- const key = normalizeThreadPullRequestKey ( link ) ;
179- created . set ( threadPullRequestKeyOf ( key ) , { ...key , url } ) ;
180- }
181- return created . size > creates ? [ ] : [ ...created . values ( ) ] ;
164+ const urlLines = Array . from ( output . matchAll ( URL_LINE ) , ( [ , url = "" ] ) => url ) ;
165+ return CREATE_COMMANDS . flatMap ( ( { definition, run } ) => {
166+ const creates = [ ...code . matchAll ( run ) ] . length ;
167+ if ( creates === 0 || definition . changeRequestPrintCommand ?. test ( otherCommands ) === true ) {
168+ return [ ] ;
169+ }
170+ const created = new Map < string , ThreadPullRequestKey & { readonly url : string } > ( ) ;
171+ for ( const url of definition . changeRequestCreateOutputUrls ?.( output ) ?? urlLines ) {
172+ const parsed = sourceControlDefinitions . parseChangeRequestUrl ( url ) ;
173+ // Only the host whose create command ran can have printed a new change request.
174+ if ( parsed === null || parsed . definition . kind !== definition . kind ) continue ;
175+ const { link } = parsed ;
176+ const named = new RegExp ( `(?:^|(?<=[\\s#/=("']))${ link . number } (?=$|[\\s);&|/"'])` , "u" ) ;
177+ // Quoted text in the other commands counts, as in `printf '.../pull/12'`.
178+ if ( named . test ( otherScript ) ) continue ;
179+ const key = normalizeThreadPullRequestKey ( link ) ;
180+ created . set ( threadPullRequestKeyOf ( key ) , { ...key , url } ) ;
181+ }
182+ return created . size > creates ? [ ] : [ ...created . values ( ) ] ;
183+ } ) ;
182184}
183185
184186const isPullRequestProviderError = Schema . is ( PullRequestProviderError ) ;
0 commit comments