Skip to content

Commit a51937e

Browse files
committed
fix(publish): catch error when trying to merge non-existent configuration from master
fixes #846
1 parent f289718 commit a51937e

File tree

2 files changed

+137
-12
lines changed

2 files changed

+137
-12
lines changed

src/remotepublish.cmd.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,24 @@ class RemotePublishCommand extends AbstractCommand {
213213

214214
if (this._filter) {
215215
this.log.debug('filtering');
216-
const content = await GitUtils.getRawContent('.', 'master', 'helix-config.yaml');
217-
218-
const other = await new HelixConfig()
219-
.withSource(content.toString())
220-
.init();
221-
222-
this.log.debug(`this: ${Array.from(this.config.strains.keys()).join(', ')}`);
223-
this.log.debug(`other: ${Array.from(other.strains.keys()).join(', ')}`);
224-
const merged = other.merge(this.config, this._filter);
225-
this.log.debug(Array.from(merged.strains.keys()).join(', '));
226-
227-
this._helixConfig = merged;
216+
try {
217+
const content = await GitUtils.getRawContent('.', 'master', 'helix-config.yaml');
218+
219+
const other = await new HelixConfig()
220+
.withSource(content.toString())
221+
.init();
222+
223+
this.log.debug(`this: ${Array.from(this.config.strains.keys()).join(', ')}`);
224+
this.log.debug(`other: ${Array.from(other.strains.keys()).join(', ')}`);
225+
const merged = other.merge(this.config, this._filter);
226+
this.log.debug(Array.from(merged.strains.keys()).join(', '));
227+
228+
this._helixConfig = merged;
229+
} catch (e) {
230+
this.log.error(`Cannot merge configuration from master. Do you have a helix-config.yaml commited in the master branch?
231+
${e}`);
232+
throw new Error('Unable to merge configurations for selective publishing');
233+
}
228234
}
229235

230236
return request.post(this._publishAPI, {

test/testRemotePublishCmd.filter.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,122 @@ describe('hlx publish --remote (with filters)', () => {
145145
await fse.remove(testRoot);
146146
});
147147
});
148+
149+
describe('hlx publish --remote (with filters, but without config)', () => {
150+
let RemotePublishCommand;
151+
let writeDictItem;
152+
let purgeAll;
153+
let testRoot;
154+
let pwd;
155+
let publishedstrains;
156+
let remote;
157+
158+
beforeEach('Setting up Fake Server', async function bef() {
159+
this.timeout(5000);
160+
writeDictItem = sinon.fake.resolves(true);
161+
purgeAll = sinon.fake.resolves(true);
162+
163+
RemotePublishCommand = proxyquire('../src/remotepublish.cmd', {
164+
'@adobe/fastly-native-promises': () => ({
165+
transact: fn => fn(3),
166+
writeDictItem,
167+
purgeAll,
168+
}),
169+
});
170+
171+
172+
// ensure to reset nock to avoid conflicts with PollyJS
173+
nock.restore();
174+
nock.cleanAll();
175+
nock.activate();
176+
177+
nock('https://adobeioruntime.net')
178+
.post('/api/v1/web/helix/default/publish', (body) => {
179+
publishedstrains = body.configuration.strains.reduce((o, strain) => {
180+
if (strain.origin) {
181+
// eslint-disable-next-line no-param-reassign
182+
o[strain.name] = strain.origin.hostname === 'www.adobe.io' ? 'branch' : 'master';
183+
}
184+
if (strain.package) {
185+
// eslint-disable-next-line no-param-reassign
186+
o[strain.name] = strain.package;
187+
}
188+
return o;
189+
}, {});
190+
return true;
191+
})
192+
.reply(200, {})
193+
.post('/api/v1/web/helix/default/addlogger')
194+
.reply(200, {});
195+
196+
// set up a fake git repo.
197+
testRoot = await createTestRoot();
198+
await fse.copy(path.resolve(__dirname, 'fixtures/filtered-master.yaml'), path.resolve(testRoot, 'wrong-helix-config.yaml'));
199+
200+
// throw a Javascript error when any shell.js command encounters an error
201+
shell.config.fatal = true;
202+
203+
// init git repo
204+
pwd = shell.pwd();
205+
shell.cd(testRoot);
206+
shell.exec('git init');
207+
shell.exec('git add -A');
208+
shell.exec('git commit -m"initial commit."');
209+
210+
// set up command
211+
remote = await new RemotePublishCommand(makeLogger({ logLevel: 'debug' }))
212+
.withWskAuth('fakeauth')
213+
.withWskNamespace('fakename')
214+
.withFastlyAuth('fake_auth')
215+
.withFastlyNamespace('fake_name')
216+
.withWskHost('doesn.t.matter')
217+
.withPublishAPI('https://adobeioruntime.net/api/v1/web/helix/default/publish')
218+
.withConfigFile(path.resolve(__dirname, 'fixtures/filtered.yaml'))
219+
.withDryRun(false);
220+
});
221+
222+
it('publishing without filters takes everything from branch', async () => {
223+
await remote.run();
224+
225+
assert.deepEqual(publishedstrains, {
226+
default: 'branch',
227+
'both-api': 'branch',
228+
'branch-foo': 'branch',
229+
'branch-bar': 'branch',
230+
'only-branch': 'branch',
231+
});
232+
});
233+
234+
it('publishing with only fails', async () => {
235+
remote = remote.withFilter('branch-*', undefined);
236+
try {
237+
await remote.run();
238+
assert.fail('this should fail');
239+
} catch (e) {
240+
if (e instanceof assert.AssertionError) {
241+
throw e;
242+
}
243+
assert.equal(e.message, 'Error while running the Publish command');
244+
}
245+
});
246+
247+
it('publishing with exclude fails', async () => {
248+
remote = remote.withFilter(undefined, 'branch-*');
249+
try {
250+
await remote.run();
251+
assert.fail('this should fail');
252+
} catch (e) {
253+
if (e instanceof assert.AssertionError) {
254+
throw e;
255+
}
256+
assert.equal(e.message, 'Error while running the Publish command');
257+
}
258+
});
259+
260+
261+
afterEach(async () => {
262+
nock.restore();
263+
shell.cd(pwd);
264+
await fse.remove(testRoot);
265+
});
266+
});

0 commit comments

Comments
 (0)