@@ -27,16 +27,39 @@ const CHECKS_DIR = path.join(PR_CHECKS_DIR, "checks");
2727const WORKFLOW_DIR = path . join ( REPO_ROOT , ".github" , "workflows" ) ;
2828const SYNC_TS_PATH = path . join ( PR_CHECKS_DIR , "sync.ts" ) ;
2929
30+ /** Type alias for action names. */
31+ type ActionName = string ;
32+
33+ /** Type alias for action versions. May include a comment. */
34+ type ActionVersion = string ;
35+
36+ /** Mappings of {@link ActionName}s to {@link ActionVersion}s. */
37+ type ActionVersions = Record < ActionName , ActionVersion > ;
38+
39+ /** Mappings of workflow names to {@link ActionVersions}. */
40+ type ActionVersionsByWorkflow = Record < string , ActionVersions > ;
41+
42+ /** The result of {@link scanGeneratedWorkflows}. */
43+ type ScanWorkflowResult = {
44+ /** A mapping of {@link ActionName}s to all encountered {@link ActionVersion}s. */
45+ actionVersions : Record < ActionName , Set < ActionVersion > > ;
46+
47+ /** A mapping of workflow names to {@link ActionVersions}. */
48+ actionVersionsByWorkflow : ActionVersionsByWorkflow ;
49+ } ;
50+
3051/**
3152 * Scan generated workflow files to extract the latest action versions.
3253 *
3354 * @param workflowDir - Path to .github/workflows directory
34- * @returns Map from action names to their latest versions (including comments)
55+ *
56+ * @returns A {@link ScanWorkflowResult} object containing information about the extracted action versions.
3557 */
3658export function scanGeneratedWorkflows (
3759 workflowDir : string ,
38- ) : Record < string , string > {
39- const actionVersions : Record < string , string > = { } ;
60+ ) : ScanWorkflowResult {
61+ const actionVersionsByWorkflow : ActionVersionsByWorkflow = { } ;
62+ const actionVersions : Record < ActionName , Set < ActionVersion > > = { } ;
4063
4164 const generatedFiles = fs
4265 . readdirSync ( workflowDir )
@@ -46,6 +69,10 @@ export function scanGeneratedWorkflows(
4669 for ( const filePath of generatedFiles ) {
4770 const content = fs . readFileSync ( filePath , "utf8" ) ;
4871
72+ // Get the basename of the workflow and drop the leading `__` from it.
73+ const workflowName = path . basename ( filePath ) . slice ( 2 ) ;
74+ actionVersionsByWorkflow [ workflowName ] = { } ;
75+
4976 // Find all action uses in the file, including potential comments
5077 // This pattern captures: action_name@version_with_possible_comment
5178 const pattern = / u s e s : \s + ( [ ^ / \s ] + \/ [ ^ @ \s ] + ) @ ( [ ^ @ \n ] + ) / g;
@@ -57,13 +84,26 @@ export function scanGeneratedWorkflows(
5784
5885 // Only track non-local actions (those with / but not starting with ./)
5986 if ( ! actionName . startsWith ( "./" ) ) {
60- // Assume that version numbers are consistent (this should be the case on a Dependabot update PR)
61- actionVersions [ actionName ] = versionWithComment ;
87+ // Record the version of the action for this workflow.
88+ actionVersionsByWorkflow [ workflowName ] [ actionName ] = versionWithComment ;
89+
90+ // Record this version for the action so that we can write it to `sync.ts` if needed.
91+ // Usually we'd expect there to be exactly one version per action across all workflows,
92+ // but sometimes these can go out-of-sync if e.g. a workflow is added in a branch and a
93+ // Dependabot PR is merged in the meantime that changes the version of an action that is
94+ // used in the new workflow. Different versions of the same action across different
95+ // workflows is not fundamentally a problem, but we may need to pick one for `sync.ts`
96+ // so we warn there if we have multiple versions of an action.
97+ if ( actionVersions [ actionName ] === undefined ) {
98+ actionVersions [ actionName ] = new Set ( [ versionWithComment ] ) ;
99+ } else if ( ! actionVersions [ actionName ] . has ( versionWithComment ) ) {
100+ actionVersions [ actionName ] . add ( versionWithComment ) ;
101+ }
62102 }
63103 }
64104 }
65105
66- return actionVersions ;
106+ return { actionVersions, actionVersionsByWorkflow } ;
67107}
68108
69109/**
@@ -79,7 +119,7 @@ export function scanGeneratedWorkflows(
79119 */
80120export function updateSyncTs (
81121 syncTsPath : string ,
82- actionVersions : Record < string , string > ,
122+ actionVersions : Record < ActionName , Set < ActionVersion > > ,
83123) : boolean {
84124 if ( ! fs . existsSync ( syncTsPath ) ) {
85125 throw new Error ( `Could not find ${ syncTsPath } ` ) ;
@@ -89,9 +129,18 @@ export function updateSyncTs(
89129 const originalContent = content ;
90130
91131 // Update hardcoded action versions
92- for ( const [ actionName , versionWithComment ] of Object . entries (
132+ for ( const [ actionName , versionsWithComments ] of Object . entries (
93133 actionVersions ,
94134 ) ) {
135+ // We usually expect `versionsWithComments` to contain one version. If there are multiple,
136+ // we arbitrarily pick one and warn about it.
137+ const versionsWithCommentsArray = Array . from ( versionsWithComments ) ;
138+ const versionWithComment = versionsWithCommentsArray [ 0 ] ;
139+ if ( versionsWithComments . size > 1 ) {
140+ console . warn (
141+ `Inconsistent versions for ${ actionName } . Arbitrarily picking '${ versionWithComment } '. All found: ${ versionsWithCommentsArray . join ( ", " ) } ` ,
142+ ) ;
143+ }
95144 // Split the scanned value into the ref (e.g. a commit SHA) and the optional
96145 // trailing version comment (e.g. `v6.0.3`).
97146 const ref = versionWithComment . includes ( "#" )
@@ -138,12 +187,12 @@ export function updateSyncTs(
138187 * Update action versions in template files in pr-checks/checks/
139188 *
140189 * @param checksDir - Path to pr-checks/checks directory
141- * @param actionVersions - Map of action names to versions (may include comments)
190+ * @param actionVersionsByWorkflow - Map of workflows to maps of action names to versions (may include comments)
142191 * @returns List of files that were modified
143192 */
144193export function updateTemplateFiles (
145194 checksDir : string ,
146- actionVersions : Record < string , string > ,
195+ actionVersionsByWorkflow : ActionVersionsByWorkflow ,
147196) : string [ ] {
148197 const modifiedFiles : string [ ] = [ ] ;
149198
@@ -156,6 +205,16 @@ export function updateTemplateFiles(
156205 let content = fs . readFileSync ( filePath , "utf8" ) ;
157206 const originalContent = content ;
158207
208+ const workflowName = path . basename ( filePath ) ;
209+ const actionVersions = actionVersionsByWorkflow [ workflowName ] ;
210+
211+ if ( actionVersions === undefined ) {
212+ console . info (
213+ `Found no action versions for workflow template '${ filePath } '.` ,
214+ ) ;
215+ continue ;
216+ }
217+
159218 // Update action versions
160219 for ( const [ actionName , versionWithComment ] of Object . entries (
161220 actionVersions ,
@@ -191,12 +250,15 @@ function main(): number {
191250 const verbose = values . verbose ?? false ;
192251
193252 console . info ( "Scanning generated workflows for latest action versions..." ) ;
194- const actionVersions = scanGeneratedWorkflows ( WORKFLOW_DIR ) ;
253+ const { actionVersions, actionVersionsByWorkflow } =
254+ scanGeneratedWorkflows ( WORKFLOW_DIR ) ;
195255
196256 if ( verbose ) {
197257 console . info ( "Found action versions:" ) ;
198- for ( const [ action , version ] of Object . entries ( actionVersions ) ) {
199- console . info ( ` ${ action } @${ version } ` ) ;
258+ for ( const [ action , versions ] of Object . entries ( actionVersions ) ) {
259+ for ( const version of versions ) {
260+ console . info ( ` ${ action } @${ version } ` ) ;
261+ }
200262 }
201263 }
202264
@@ -215,7 +277,10 @@ function main(): number {
215277 }
216278
217279 // Update template files
218- const templateModified = updateTemplateFiles ( CHECKS_DIR , actionVersions ) ;
280+ const templateModified = updateTemplateFiles (
281+ CHECKS_DIR ,
282+ actionVersionsByWorkflow ,
283+ ) ;
219284 modifiedFiles . push ( ...templateModified ) ;
220285
221286 if ( modifiedFiles . length > 0 ) {
0 commit comments