@@ -8,8 +8,53 @@ function hasPrefix(filename, prefixes) {
88 return prefixes . some ( ( prefix ) => filename . startsWith ( prefix ) ) ;
99}
1010
11+ function extractSlugs ( paths ) {
12+ const slugs = new Set ( ) ;
13+ for ( const filename of paths ) {
14+ const postMatch = filename . match ( / ^ c o n t e n t \/ p o s t s \/ ( [ ^ / ] + ) \. m d $ / ) ;
15+ if ( postMatch ) {
16+ slugs . add ( postMatch [ 1 ] ) ;
17+ continue ;
18+ }
19+ const uploadMatch = filename . match ( / ^ p u b l i c \/ u p l o a d s \/ ( [ ^ / ] + ) \/ / ) ;
20+ if ( uploadMatch && ! / ^ \d { 4 } $ / . test ( uploadMatch [ 1 ] ) ) {
21+ slugs . add ( uploadMatch [ 1 ] ) ;
22+ continue ;
23+ }
24+ const personMatch = filename . match ( / ^ c o n t e n t \/ p e o p l e \/ ( [ ^ / ] + ) \. j s o n $ / ) ;
25+ if ( personMatch ) {
26+ slugs . add ( personMatch [ 1 ] ) ;
27+ continue ;
28+ }
29+ const memberPhotoMatch = filename . match (
30+ / ^ p u b l i c \/ t e a m \/ m e m b e r s \/ ( [ ^ / ] + ) \/ / ,
31+ ) ;
32+ if ( memberPhotoMatch ) {
33+ slugs . add ( memberPhotoMatch [ 1 ] ) ;
34+ }
35+ }
36+ return [ ...slugs ] . sort ( ) ;
37+ }
38+
39+ function buildSummaryTitle ( kind , slugs ) {
40+ const prefixByKind = {
41+ blog : "Keystatic (Blog)" ,
42+ people : "Keystatic (People)" ,
43+ mixed : "Keystatic (Editorial)" ,
44+ ineligible : "Keystatic (Manual review)" ,
45+ empty : "Keystatic" ,
46+ } ;
47+ const prefix = prefixByKind [ kind ] || "Keystatic" ;
48+ if ( slugs . length === 0 ) {
49+ return `${ prefix } : publish changes` ;
50+ }
51+ const shown = slugs . slice ( 0 , 3 ) . join ( ", " ) ;
52+ const suffix = slugs . length > 3 ? ` (+${ slugs . length - 3 } more)` : "" ;
53+ return `${ prefix } : ${ shown } ${ suffix } ` ;
54+ }
55+
1156export function classifyPaths ( inputPaths ) {
12- const paths = [ ...new Set ( inputPaths ) ] . sort ( ) ;
57+ const paths = [ ...new Set ( inputPaths . filter ( Boolean ) ) ] . sort ( ) ;
1358 const blogPaths = paths . filter ( ( filename ) =>
1459 hasPrefix ( filename , BLOG_PREFIXES ) ,
1560 ) ;
@@ -28,12 +73,19 @@ export function classifyPaths(inputPaths) {
2873 else if ( peoplePaths . length > 0 ) kind = "people" ;
2974 else if ( blogPaths . length > 0 ) kind = "blog" ;
3075
76+ const eligible = paths . length > 0 && ineligiblePaths . length === 0 ;
77+ const hasPeople = peoplePaths . length > 0 ;
78+ const slugs = extractSlugs ( paths ) ;
79+
3180 return {
32- eligible : paths . length > 0 && ineligiblePaths . length === 0 ,
33- hasPeople : peoplePaths . length > 0 ,
81+ autoMergeBlog : eligible && kind === "blog" ,
82+ eligible,
83+ hasPeople,
3484 kind,
3585 paths,
3686 ineligiblePaths,
87+ slugs,
88+ summaryTitle : buildSummaryTitle ( kind , slugs ) ,
3789 } ;
3890}
3991
@@ -66,31 +118,52 @@ function writeOutputs(result) {
66118 const outputPath = process . env . GITHUB_OUTPUT ;
67119 if ( ! outputPath ) return ;
68120
121+ const safeTitle = result . summaryTitle . replace ( / [ \r \n ] + / g, " " ) . trim ( ) ;
69122 fs . appendFileSync (
70123 outputPath ,
71124 `${ [
125+ `auto_merge_blog=${ result . autoMergeBlog } ` ,
72126 `eligible=${ result . eligible } ` ,
73127 `has_people=${ result . hasPeople } ` ,
74128 `kind=${ result . kind } ` ,
75129 `ineligible_count=${ result . ineligiblePaths . length } ` ,
76130 `ineligible_paths=${ result . ineligiblePaths . join ( ", " ) } ` ,
131+ `summary_title=${ safeTitle } ` ,
77132 ] . join ( "\n" ) } \n`,
78133 ) ;
79134}
80135
81136function selfTest ( ) {
82- assert . deepEqual ( classifyPaths ( [ "content/posts/hello.md" ] ) . kind , "blog" ) ;
83- assert . deepEqual (
84- classifyPaths ( [
85- "content/people/alex.json" ,
86- "public/team/members/alex/photo.jpg" ,
87- ] ) . kind ,
88- "people" ,
89- ) ;
90- assert . deepEqual (
91- classifyPaths ( [ "content/posts/hello.md" , "content/people/alex.json" ] ) . kind ,
92- "mixed" ,
93- ) ;
137+ const blogOnly = classifyPaths ( [
138+ "content/posts/hello.md" ,
139+ "public/uploads/hello/cover.jpg" ,
140+ ] ) ;
141+ assert . equal ( blogOnly . kind , "blog" ) ;
142+ assert . equal ( blogOnly . eligible , true ) ;
143+ assert . equal ( blogOnly . hasPeople , false ) ;
144+ assert . equal ( blogOnly . autoMergeBlog , true ) ;
145+ assert . equal ( blogOnly . summaryTitle , "Keystatic (Blog): hello" ) ;
146+
147+ const peopleOnly = classifyPaths ( [
148+ "content/people/alex.json" ,
149+ "public/team/members/alex/photo.jpg" ,
150+ ] ) ;
151+ assert . equal ( peopleOnly . kind , "people" ) ;
152+ assert . equal ( peopleOnly . eligible , true ) ;
153+ assert . equal ( peopleOnly . hasPeople , true ) ;
154+ assert . equal ( peopleOnly . autoMergeBlog , false ) ;
155+ assert . equal ( peopleOnly . summaryTitle , "Keystatic (People): alex" ) ;
156+
157+ const mixed = classifyPaths ( [
158+ "content/posts/hello.md" ,
159+ "content/people/alex.json" ,
160+ ] ) ;
161+ assert . equal ( mixed . kind , "mixed" ) ;
162+ assert . equal ( mixed . eligible , true ) ;
163+ assert . equal ( mixed . hasPeople , true ) ;
164+ assert . equal ( mixed . autoMergeBlog , false ) ;
165+ assert . equal ( mixed . summaryTitle , "Keystatic (Editorial): alex, hello" ) ;
166+
94167 assert . equal (
95168 classifyPaths ( [ "content/posts/hello.md" , "src/app/page.tsx" ] ) . eligible ,
96169 false ,
@@ -128,8 +201,17 @@ if (args[0] === "--self-test") {
128201 const result = classifyGitHubFiles ( parsed , expectedCount ) ;
129202 writeOutputs ( result ) ;
130203 console . log ( JSON . stringify ( result , null , 2 ) ) ;
204+ } else if ( args [ 0 ] === "--paths-file" && args [ 1 ] ) {
205+ const lines = fs
206+ . readFileSync ( args [ 1 ] , "utf8" )
207+ . split ( / \r ? \n / )
208+ . map ( ( line ) => line . trim ( ) )
209+ . filter ( Boolean ) ;
210+ const result = classifyPaths ( lines ) ;
211+ writeOutputs ( result ) ;
212+ console . log ( JSON . stringify ( result , null , 2 ) ) ;
131213} else {
132214 throw new Error (
133- "Usage: classify-keystatic-changes.mjs --self-test | --github-json <file>" ,
215+ "Usage: classify-keystatic-changes.mjs --self-test | --github-json <file> | --paths-file <file> " ,
134216 ) ;
135217}
0 commit comments