Skip to content

Commit fb73ce0

Browse files
committed
Support partial-build-extension defers when sources.length > 1
Previously partial builds that had more than one source (e.g. had different start paths for different branches) would fail. This commit ensures that if there are multiple sources that the contentAggregated phase is used to remove branches that should not be built.
1 parent 9873514 commit fb73ce0

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

lib/partial-build-extension.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ module.exports.register = function ({ config = {} }) {
4545
console.log(`${category}: version ${version} not previously built; reverting to full build`)
4646
return
4747
}
48+
const sourcesLength = playbook.content.sources.length
49+
if (sourcesLength !== 1) {
50+
console.log(`sources.length = ${sourcesLength} so deferring filtering till contentAggregated`)
51+
return
52+
}
4853
Object.assign(
4954
playbook.content.sources[0],
5055
isBranch ? { branches: [refname], tags: [] } : { branches: [], tags: [refname] }
@@ -56,6 +61,21 @@ module.exports.register = function ({ config = {} }) {
5661
Object.assign(asciidocAttrs, { 'primary-site-url': '.', 'primary-site-manifest-url': siteManifestUrl })
5762
this.updateVariables({ playbook })
5863
})
64+
this.once('contentAggregated', async ({ playbook, contentAggregate }) => {
65+
if (contentAggregate.length <= 1) {
66+
return
67+
}
68+
if (fs.existsSync(ospath.join(playbook.dir, '.full-build'), '')) {
69+
return
70+
}
71+
const indexToSave = contentAggregate.findIndex((c) => c.origins[0].branch === refname)
72+
if (indexToSave < 0) {
73+
throw new Error(`Could not find ${refname}`)
74+
}
75+
const toSave = contentAggregate[indexToSave]
76+
contentAggregate.splice(0, contentAggregate.length)
77+
contentAggregate.push(toSave)
78+
})
5979
}
6080

6181
function download (get, url) {

test/partial-build-extension-test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ describe('partial-build-extension', () => {
6262
it('should register listener if only refname key is set', () => {
6363
ext.register.call(generatorContext, { config: { refname: 'main' } })
6464
expect(generatorContext.playbookBuilt).to.be.instanceOf(Function)
65+
expect(generatorContext.contentAggregated).to.be.instanceOf(Function)
6566
})
6667

6768
it('should not register listener if refname key is not set', () => {
6869
ext.register.call(generatorContext, {})
6970
expect(generatorContext.playbookBuilt).to.be.undefined()
71+
expect(generatorContext.contentAggregated).to.be.undefined()
7072
})
7173
})
7274

@@ -215,6 +217,82 @@ describe('partial-build-extension', () => {
215217
})
216218
})
217219

220+
it('should not rewrite content sources when multiple sources', async () => {
221+
playbook.dir = WORK_DIR
222+
playbook.asciidoc.attributes = { 'primary-site-manifest-url': ospath.join(FIXTURES_DIR, 'site-manifest.json') }
223+
playbook.content.sources = [
224+
{
225+
url: '.',
226+
branches: ['main'],
227+
startPaths: ['new-start-path'],
228+
},
229+
{
230+
url: '.',
231+
branches: ['1.0.x'],
232+
startPaths: ['old-start-path'],
233+
},
234+
]
235+
ext.register.call(generatorContext, { config: { refname: 'HEAD', version: '7.0.0-SNAPSHOT' } })
236+
generatorContext.updateVariables({ playbook })
237+
await generatorContext.playbookBuilt(generatorContext.variables)
238+
expect(playbook.content.sources).to.eql([
239+
{
240+
url: '.',
241+
branches: ['main'],
242+
startPaths: ['new-start-path'],
243+
},
244+
{
245+
url: '.',
246+
branches: ['1.0.x'],
247+
startPaths: ['old-start-path'],
248+
},
249+
])
250+
})
251+
252+
it('should disable contentAggregated when full build', async () => {
253+
playbook.dir = WORK_DIR
254+
await fsp.writeFile(ospath.join(playbook.dir, '.full-build'), '')
255+
ext.register.call(generatorContext, { config: { refname: 'main', version: '7.0.0-SNAPSHOT' } })
256+
generatorContext.updateVariables({ playbook })
257+
const contentAggregate = [
258+
{
259+
origins: [{ branch: 'main' }],
260+
},
261+
{
262+
origins: [{ branch: '1.0.x' }],
263+
},
264+
]
265+
await generatorContext.contentAggregated({ playbook, contentAggregate })
266+
expect(contentAggregate.length).to.eql(2)
267+
})
268+
269+
it('contentAggregated should remove when activated', async () => {
270+
playbook.dir = WORK_DIR
271+
ext.register.call(generatorContext, { config: { refname: '1.0.x', version: '1.0.0-SNAPSHOT' } })
272+
generatorContext.updateVariables({ playbook })
273+
const contentAggregate = [
274+
{
275+
origins: [{ branch: 'main' }],
276+
},
277+
{
278+
origins: [{ branch: '1.0.x' }],
279+
},
280+
{
281+
origins: [{ branch: '2.0.x' }],
282+
},
283+
]
284+
await generatorContext.contentAggregated({ playbook, contentAggregate })
285+
expect(contentAggregate.length).to.eql(1)
286+
expect(contentAggregate[0].origins[0].branch).to.eql('1.0.x')
287+
})
288+
289+
it('contentAggregated should not remove when refname not specified', async () => {
290+
playbook.dir = WORK_DIR
291+
ext.register.call(generatorContext, { config: { version: '1.0.0-SNAPSHOT' } })
292+
generatorContext.updateVariables({ playbook })
293+
expect(generatorContext.contentAggregated).undefined()
294+
})
295+
218296
it('should error if refname is HEAD and version undefined', async () => {
219297
expect(await trapAsyncError(() => runScenario({ refname: 'HEAD' }))).to.throw(
220298
'When using author mode version is required. Specify config.version or env BUILD_VERSION'

0 commit comments

Comments
 (0)