Skip to content

Commit d1af965

Browse files
authored
Merge pull request #26293 from github/repo-sync
Repo sync
2 parents 147f1b1 + eb55f0f commit d1af965

File tree

4 files changed

+77
-54
lines changed

4 files changed

+77
-54
lines changed

src/ghes-releases/scripts/remove-deprecated-frontmatter.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
// Returns false when no changes were made to the frontmatter,
2+
// true when changes were made.
13
export default function removeDeprecatedFrontmatter(
24
file,
35
frontmatterVersions,
46
releaseToDeprecate,
57
nextOldestRelease
68
) {
79
// skip files with no Enterprise Server versions frontmatter
8-
if (!frontmatterVersions) return
9-
if (!frontmatterVersions.ghes) return
10+
if (!frontmatterVersions) return false
11+
if (!frontmatterVersions.ghes) return false
1012

1113
const ghesRange = frontmatterVersions.ghes
1214

1315
// skip files with versions frontmatter that already applies to all enterprise-server releases
14-
if (ghesRange === '*') return
16+
if (ghesRange === '*') return false
1517

1618
// if the release to deprecate is 2.13, and the FM is either '>=2.13', '>2.13', or '>=2.14',
1719
// we can safely change the FM to ghes: '*'
@@ -22,7 +24,7 @@ export default function removeDeprecatedFrontmatter(
2224

2325
if (appliesToAllSupportedGhesReleases) {
2426
frontmatterVersions.ghes = '*'
25-
return
27+
return true
2628
}
2729

2830
// if the release to deprecate is 2.13, and the FM is either '=2.13', '<2.13', '<=2.13', or '<2.14',
@@ -39,9 +41,10 @@ export default function removeDeprecatedFrontmatter(
3941
console.log(
4042
`Warning! ${file} has frontmatter versioning that will make it never appear when ${releaseToDeprecate} is deprecated. The article should probably be removed.`
4143
)
42-
return
44+
return false
4345
}
4446

4547
delete frontmatterVersions.ghes
48+
return true
4649
}
4750
}

src/ghes-releases/scripts/remove-liquid-statements.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ const tokenize = (str) => {
2222
// src/ghes-releases/tests/remove-liquid-statements.js.
2323
export default function removeLiquidStatements(content, release, nextOldestRelease, file) {
2424
let newContent = content
25+
let contentChanged = false
2526

2627
// Get an array of ifversion blocks with their content included.
2728
const blocks = getLiquidConditionalsWithContent(newContent, 'ifversion')
28-
if (!blocks.length) return newContent
29+
if (!blocks.length) return { newContent, contentChanged }
2930

3031
// Decorate those blocks with more GHES versioning information.
3132
const versionBlocks = getVersionBlocks(blocks)
@@ -233,6 +234,7 @@ export default function removeLiquidStatements(content, release, nextOldestRelea
233234
// in the general content and return the updated general content.
234235
versionBlocks.forEach((versionBlock) => {
235236
if (versionBlock.action !== 'none') {
237+
contentChanged = true
236238
const newBlockContent = versionBlock.newContent.replaceAll(/\n\n\n+?/g, '\n\n')
237239

238240
newContent = newContent
@@ -246,7 +248,7 @@ export default function removeLiquidStatements(content, release, nextOldestRelea
246248
}
247249
})
248250

249-
return newContent
251+
return { newContent, contentChanged }
250252
}
251253

252254
// Hack to use a regex with lastIndexOf.

src/ghes-releases/scripts/remove-version-markup.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,37 +60,55 @@ async function main() {
6060
for (const file of allFiles) {
6161
const oldContents = fs.readFileSync(file, 'utf8')
6262
const { content, data } = frontmatter(oldContents)
63-
63+
let fileChanged = false
6464
// update frontmatter versions prop
65-
removeDeprecatedFrontmatter(file, data.versions, release, nextOldestRelease)
65+
fileChanged ||= removeDeprecatedFrontmatter(file, data.versions, release, nextOldestRelease)
6666

6767
// update liquid statements in content and data
68-
const newContent = removeLiquidStatements(content, release, nextOldestRelease, file)
68+
const { newContent, contentChanged } = removeLiquidStatements(
69+
content,
70+
release,
71+
nextOldestRelease,
72+
file
73+
)
74+
fileChanged ||= contentChanged
6975

7076
// update liquid statements in content frontmatter (like intro and title)
7177
for (const key in data) {
7278
const value = data[key]
7379
if (typeof value === 'string' && value.includes('{% ifversion')) {
74-
const newValue = removeLiquidStatements(value, release, nextOldestRelease, file)
75-
data[key] = newValue
80+
const { newContent, contentChanged } = removeLiquidStatements(
81+
value,
82+
release,
83+
nextOldestRelease,
84+
file
85+
)
86+
fileChanged ||= contentChanged
87+
data[key] = newContent
7688
}
7789
}
7890

79-
// make sure any intro fields that exist and are empty return an empty string, not null
80-
if (typeof data.intro !== 'undefined' && !data.intro) {
81-
data.intro = ''
82-
}
91+
// When stringifying frontmatter, the frontmatter is also formatted.
92+
// This means that even if there were no Liquid versioning changes,
93+
// the frontmatter may still be modified to modify line breaks or quotes.
94+
// This an already difficult PR noisier to review. This prevents writing
95+
// the file unless there are versioning changes made.
96+
if (fileChanged) {
97+
// make sure any intro fields that exist and are empty return an empty string, not null
98+
if (typeof data.intro !== 'undefined' && !data.intro) {
99+
data.intro = ''
100+
}
101+
// put it all back together
102+
const newContents = frontmatter.stringify(newContent, data, { lineWidth: 10000 })
83103

84-
// put it all back together
85-
const newContents = frontmatter.stringify(newContent, data, { lineWidth: 10000 })
104+
// if the content file is now empty, remove it
105+
if (newContents.replace(/\s/g, '').length === 0) {
106+
fs.unlinkSync(file)
107+
continue
108+
}
86109

87-
// if the content file is now empty, remove it
88-
if (newContents.replace(/\s/g, '').length === 0) {
89-
fs.unlinkSync(file)
90-
continue
110+
fs.writeFileSync(file, newContents)
91111
}
92-
93-
fs.writeFileSync(file, newContents)
94112
}
95113

96114
console.log(`Removed GHES ${release} markup from content and data files! Review and run tests.`)

src/ghes-releases/tests/remove-liquid-statements.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ function processFrontmatter(contents, file) {
3939

4040
describe('removing liquid statements only', () => {
4141
test('removes liquid statements that specify "greater than version to deprecate"', async () => {
42-
let contents = await fs.readFile(greaterThan, 'utf8')
43-
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
44-
const $ = cheerio.load(contents)
42+
const content = await fs.readFile(greaterThan, 'utf8')
43+
const { newContent } = removeLiquidStatements(content, versionToDeprecate, nextOldestVersion)
44+
const $ = cheerio.load(newContent)
4545
expect($('.example1').text().trim()).toBe(`{% ifversion ghes %}\n
4646
Alpha\n\n{% endif %}`)
4747
expect($('.example2').text().trim()).toBe(`{% ifversion fpt or ghes %}\n
@@ -65,9 +65,9 @@ Alpha\n\n{% else %}\n\nBravo\n\n{% ifversion ghes > 2.16 %}\n\nCharlie\n
6565
{% else %}\n\nBravo\n\n{% endif %}`)
6666
})
6767
test('removes liquid statements that specify all known versions, including some nested conditionals"', async () => {
68-
let contents = await fs.readFile(unnecessary, 'utf8')
69-
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
70-
const $ = cheerio.load(contents)
68+
const content = await fs.readFile(unnecessary, 'utf8')
69+
const { newContent } = removeLiquidStatements(content, versionToDeprecate, nextOldestVersion)
70+
const $ = cheerio.load(newContent)
7171
expect($('.example1').text().trim()).toBe(`Alpha`)
7272
expect($('.example2').text().trim()).toBe(
7373
`Alpha\n {% ifversion fpt or ghec %}\n Bravo\n {% endif %}`
@@ -84,9 +84,9 @@ Alpha\n\n{% else %}\n\nBravo\n\n{% ifversion ghes > 2.16 %}\n\nCharlie\n
8484
})
8585

8686
test('removes liquid statements that specify "and greater than version to deprecate"', async () => {
87-
let contents = await fs.readFile(andGreaterThan1, 'utf8')
88-
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
89-
const $ = cheerio.load(contents)
87+
const content = await fs.readFile(andGreaterThan1, 'utf8')
88+
const { newContent } = removeLiquidStatements(content, versionToDeprecate, nextOldestVersion)
89+
const $ = cheerio.load(newContent)
9090
expect($('.example1').text().trim()).toBe(
9191
'{% ifversion not fpt and ghes %}\n\nAlpha\n\n{% endif %}'
9292
)
@@ -102,9 +102,9 @@ Alpha\n\n{% ifversion ghes > 2.16 %}\n\nBravo\n\n{% endif %}\n\n{% else %}\n\nCh
102102
})
103103

104104
test('removes liquid statements that specify "and greater than version to deprecate" (alternate format)', async () => {
105-
let contents = await fs.readFile(andGreaterThan2, 'utf8')
106-
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
107-
const $ = cheerio.load(contents)
105+
const content = await fs.readFile(andGreaterThan2, 'utf8')
106+
const { newContent } = removeLiquidStatements(content, versionToDeprecate, nextOldestVersion)
107+
const $ = cheerio.load(newContent)
108108
expect($('.example1').text().trim()).toBe('{% ifversion ghes < 2.16 %}\n\nAlpha\n\n{% endif %}')
109109
expect($('.example2').text().trim()).toBe(`{% ifversion ghes < 2.16 %}\n\nAlpha\n\n{% else %}\n
110110
Bravo\n\n{% endif %}`)
@@ -117,9 +117,9 @@ Alpha\n\n{% ifversion not fpt %}\n\nBravo\n\n{% endif %}\n\n{% else %}\n\nCharli
117117
})
118118

119119
test('removes liquid statements that specify "not equals version to deprecate"', async () => {
120-
let contents = await fs.readFile(notEquals, 'utf8')
121-
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
122-
const $ = cheerio.load(contents)
120+
const content = await fs.readFile(notEquals, 'utf8')
121+
const { newContent } = removeLiquidStatements(content, versionToDeprecate, nextOldestVersion)
122+
const $ = cheerio.load(newContent)
123123
expect($('.example1').text().trim()).toBe('{% ifversion ghes %}\n\nAlpha\n\n{% endif %}')
124124
expect($('.example2').text().trim()).toBe('{% ifversion fpt or ghes %}\n\nAlpha\n\n{% endif %}')
125125
expect($('.example3').text().trim()).toBe(`{% ifversion fpt %}\n
@@ -136,9 +136,9 @@ Alpha\n\n{% endif %}`)
136136

137137
describe('removing liquid statements and content', () => {
138138
test('removes interior content and liquid statements that specify "equals version to deprecate"', async () => {
139-
let contents = await fs.readFile(equals, 'utf8')
140-
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
141-
const $ = cheerio.load(contents)
139+
const content = await fs.readFile(equals, 'utf8')
140+
const { newContent } = removeLiquidStatements(content, versionToDeprecate, nextOldestVersion)
141+
const $ = cheerio.load(newContent)
142142
expect($('.example1').text().trim()).toBe('')
143143
expect($('.example2').text().trim()).toBe('{% ifversion not fpt %}\n\nAlpha\n\n{% endif %}')
144144
expect($('.example3').text().trim()).toBe(`{% ifversion fpt %}\n
@@ -152,9 +152,9 @@ Alpha\n\n{% else %}\n\nCharlie\n\n{% endif %}`)
152152
})
153153

154154
test('removes interior content and liquid statements that specify "less than next oldest than version to deprecate"', async () => {
155-
let contents = await fs.readFile(lessThanNextOldest, 'utf8')
156-
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
157-
const $ = cheerio.load(contents)
155+
const content = await fs.readFile(lessThanNextOldest, 'utf8')
156+
const { newContent } = removeLiquidStatements(content, versionToDeprecate, nextOldestVersion)
157+
const $ = cheerio.load(newContent)
158158
expect($('.example1').text().trim()).toBe('Alpha')
159159
expect($('.example2').text().trim()).toBe(
160160
'Alpha\n\n{% ifversion fpt %}\n\nBravo\n\n{% endif %}'
@@ -193,19 +193,19 @@ describe('updating frontmatter', () => {
193193

194194
describe('whitespace', () => {
195195
test('does not add newlines when whitespace control is used', async () => {
196-
let contents = await fs.readFile(whitespace, 'utf8')
197-
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
198-
const $ = cheerio.load(contents)
196+
const content = await fs.readFile(whitespace, 'utf8')
197+
const { newContent } = removeLiquidStatements(content, versionToDeprecate, nextOldestVersion)
198+
const $ = cheerio.load(newContent)
199199
expect($('.example1').text()).toBe('\n{% ifversion ghes %}\n Alpha\n{% endif %}\n')
200200
expect($('.example2').text()).toBe('\n{%- ifversion ghes %}\n Alpha\n{% endif %}\n')
201201
expect($('.example3').text()).toBe('\n{% ifversion fpt or ghes %}\n Alpha\n{%- endif %}\n')
202202
expect($('.example4').text()).toBe('\n{%- ifversion fpt or ghes %}\n Alpha\n{%- endif %}\n')
203203
})
204204

205205
test('does not add newlines when no newlines are present', async () => {
206-
let contents = await fs.readFile(whitespace, 'utf8')
207-
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
208-
const $ = cheerio.load(contents)
206+
const content = await fs.readFile(whitespace, 'utf8')
207+
const { newContent } = removeLiquidStatements(content, versionToDeprecate, nextOldestVersion)
208+
const $ = cheerio.load(newContent)
209209
expect($('.example5').text()).toBe('\n{% ifversion ghes %}\n Alpha\n{% endif %}\n')
210210
expect($('.example6').text()).toBe(
211211
'\n Alpha\n{% ifversion fpt or ghes %}\n Bravo\n{% endif %}\n Charlie\n'
@@ -214,9 +214,9 @@ describe('whitespace', () => {
214214
})
215215

216216
test('only remove newlines when tag starts at beginning of line', async () => {
217-
let contents = await fs.readFile(whitespace, 'utf8')
218-
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
219-
const $ = cheerio.load(contents)
217+
const content = await fs.readFile(whitespace, 'utf8')
218+
const { newContent } = removeLiquidStatements(content, versionToDeprecate, nextOldestVersion)
219+
const $ = cheerio.load(newContent)
220220
expect($('.example8').text()).toBe('\nAlpha\nBravo\n')
221221
expect($('.example9').text()).toBe('\nAlpha\nBravo\n')
222222
expect($('.example10').text()).toBe('\nPre\nBravo\n')

0 commit comments

Comments
 (0)