@@ -27,6 +27,27 @@ 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 *
@@ -35,8 +56,9 @@ const SYNC_TS_PATH = path.join(PR_CHECKS_DIR, "sync.ts");
3556 */
3657export function scanGeneratedWorkflows (
3758 workflowDir : string ,
38- ) : Record < string , string > {
39- const actionVersions : Record < string , string > = { } ;
59+ ) : ScanWorkflowResult {
60+ const actionVersionsByWorkflow : ActionVersionsByWorkflow = { } ;
61+ const actionVersions : Record < ActionName , Set < ActionVersion > > = { } ;
4062
4163 const generatedFiles = fs
4264 . readdirSync ( workflowDir )
@@ -46,6 +68,10 @@ export function scanGeneratedWorkflows(
4668 for ( const filePath of generatedFiles ) {
4769 const content = fs . readFileSync ( filePath , "utf8" ) ;
4870
71+ // Get the basename of the workflow and drop the leading `__` from it.
72+ const workflowName = path . basename ( filePath ) . slice ( 2 ) ;
73+ actionVersionsByWorkflow [ workflowName ] = { } ;
74+
4975 // Find all action uses in the file, including potential comments
5076 // This pattern captures: action_name@version_with_possible_comment
5177 const pattern = / u s e s : \s + ( [ ^ / \s ] + \/ [ ^ @ \s ] + ) @ ( [ ^ @ \n ] + ) / g;
@@ -57,13 +83,26 @@ export function scanGeneratedWorkflows(
5783
5884 // Only track non-local actions (those with / but not starting with ./)
5985 if ( ! actionName . startsWith ( "./" ) ) {
60- // Assume that version numbers are consistent (this should be the case on a Dependabot update PR)
61- actionVersions [ actionName ] = versionWithComment ;
86+ // Record the version of the action for this workflow.
87+ actionVersionsByWorkflow [ workflowName ] [ actionName ] = versionWithComment ;
88+
89+ // Record this version for the action so that we can write it to `sync.ts` if needed.
90+ // Usually we'd expect there to be exactly one version per action across all workflows,
91+ // but sometimes these can go out-of-sync if e.g. a workflow is added in a branch and a
92+ // Dependabot PR is merged in the meantime that changes the version of an action that is
93+ // used in the new workflow. Different versions of the same action across different
94+ // workflows is not fundamentally a problem, but we may need to pick one for `sync.ts`
95+ // so we warn there if we have multiple versions of an action.
96+ if ( actionVersions [ actionName ] === undefined ) {
97+ actionVersions [ actionName ] = new Set ( [ versionWithComment ] ) ;
98+ } else if ( ! actionVersions [ actionName ] . has ( versionWithComment ) ) {
99+ actionVersions [ actionName ] . add ( versionWithComment ) ;
100+ }
62101 }
63102 }
64103 }
65104
66- return actionVersions ;
105+ return { actionVersions, actionVersionsByWorkflow } ;
67106}
68107
69108/**
@@ -79,7 +118,7 @@ export function scanGeneratedWorkflows(
79118 */
80119export function updateSyncTs (
81120 syncTsPath : string ,
82- actionVersions : Record < string , string > ,
121+ actionVersions : Record < ActionName , Set < ActionVersion > > ,
83122) : boolean {
84123 if ( ! fs . existsSync ( syncTsPath ) ) {
85124 throw new Error ( `Could not find ${ syncTsPath } ` ) ;
@@ -89,9 +128,18 @@ export function updateSyncTs(
89128 const originalContent = content ;
90129
91130 // Update hardcoded action versions
92- for ( const [ actionName , versionWithComment ] of Object . entries (
131+ for ( const [ actionName , versionsWithComments ] of Object . entries (
93132 actionVersions ,
94133 ) ) {
134+ // We usually expect `versionsWithComments` to contain one version. If there are multiple,
135+ // we arbitrarily pick one and warn about it.
136+ const versionsWithCommentsArray = Array . from ( versionsWithComments ) ;
137+ const versionWithComment = versionsWithCommentsArray [ 0 ] ;
138+ if ( versionsWithComments . size > 1 ) {
139+ console . warn (
140+ `Inconsistent versions for ${ actionName } . Arbitrarily picking '${ versionWithComment } '. All found: ${ versionsWithCommentsArray . join ( ", " ) } ` ,
141+ ) ;
142+ }
95143 // Split the scanned value into the ref (e.g. a commit SHA) and the optional
96144 // trailing version comment (e.g. `v6.0.3`).
97145 const ref = versionWithComment . includes ( "#" )
@@ -138,12 +186,12 @@ export function updateSyncTs(
138186 * Update action versions in template files in pr-checks/checks/
139187 *
140188 * @param checksDir - Path to pr-checks/checks directory
141- * @param actionVersions - Map of action names to versions (may include comments)
189+ * @param actionVersionsByWorkflow - Map of workflows to maps of action names to versions (may include comments)
142190 * @returns List of files that were modified
143191 */
144192export function updateTemplateFiles (
145193 checksDir : string ,
146- actionVersions : Record < string , string > ,
194+ actionVersionsByWorkflow : ActionVersionsByWorkflow ,
147195) : string [ ] {
148196 const modifiedFiles : string [ ] = [ ] ;
149197
@@ -156,6 +204,16 @@ export function updateTemplateFiles(
156204 let content = fs . readFileSync ( filePath , "utf8" ) ;
157205 const originalContent = content ;
158206
207+ const workflowName = path . basename ( filePath ) ;
208+ const actionVersions = actionVersionsByWorkflow [ workflowName ] ;
209+
210+ if ( actionVersions === undefined ) {
211+ console . info (
212+ `Found no action versions for workflow template '${ filePath } '.` ,
213+ ) ;
214+ continue ;
215+ }
216+
159217 // Update action versions
160218 for ( const [ actionName , versionWithComment ] of Object . entries (
161219 actionVersions ,
@@ -191,12 +249,15 @@ function main(): number {
191249 const verbose = values . verbose ?? false ;
192250
193251 console . info ( "Scanning generated workflows for latest action versions..." ) ;
194- const actionVersions = scanGeneratedWorkflows ( WORKFLOW_DIR ) ;
252+ const { actionVersions, actionVersionsByWorkflow } =
253+ scanGeneratedWorkflows ( WORKFLOW_DIR ) ;
195254
196255 if ( verbose ) {
197256 console . info ( "Found action versions:" ) ;
198- for ( const [ action , version ] of Object . entries ( actionVersions ) ) {
199- console . info ( ` ${ action } @${ version } ` ) ;
257+ for ( const [ action , versions ] of Object . entries ( actionVersions ) ) {
258+ for ( const version of versions ) {
259+ console . info ( ` ${ action } @${ version } ` ) ;
260+ }
200261 }
201262 }
202263
@@ -215,7 +276,10 @@ function main(): number {
215276 }
216277
217278 // Update template files
218- const templateModified = updateTemplateFiles ( CHECKS_DIR , actionVersions ) ;
279+ const templateModified = updateTemplateFiles (
280+ CHECKS_DIR ,
281+ actionVersionsByWorkflow ,
282+ ) ;
219283 modifiedFiles . push ( ...templateModified ) ;
220284
221285 if ( modifiedFiles . length > 0 ) {
0 commit comments