Skip to content

Commit 8f03383

Browse files
authored
fix(3384): Failure to get user info from scm should not throw error (#101)
1 parent 40b6301 commit 8f03383

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33
/* eslint-disable no-underscore-dangle */
44
const Hoek = require('@hapi/hoek');
55
const Joi = require('joi');
6+
const logger = require('screwdriver-logger');
67
const dataSchema = require('screwdriver-data-schema');
78
const { getAnnotations } = require('./lib/helper');
89

910
const repoManifestAnnotation = 'screwdriver.cd/repoManifest';
1011

12+
const DEFAULT_AUTHOR = {
13+
id: '',
14+
avatar: 'https://cd.screwdriver.cd/assets/unknown_user.png',
15+
name: 'n/a',
16+
username: 'n/a',
17+
url: 'https://cd.screwdriver.cd/'
18+
};
19+
1120
/**
1221
* Validate the config using the schema
1322
* @method validate
@@ -328,7 +337,23 @@ class ScmBase {
328337
*/
329338
decorateAuthor(config) {
330339
return validate(config, dataSchema.plugins.scm.decorateAuthor)
331-
.then(validAuthor => this._decorateAuthor(this.getConfig(validAuthor)))
340+
.then(async validAuthor => {
341+
let author;
342+
343+
try {
344+
author = await this._decorateAuthor(this.getConfig(validAuthor));
345+
} catch (err) {
346+
logger.error('Failed to decorateAuthor: ', err);
347+
348+
author = {
349+
...DEFAULT_AUTHOR,
350+
name: config.username,
351+
username: config.username
352+
};
353+
}
354+
355+
return author;
356+
})
332357
.then(decoratedAuthor => validate(decoratedAuthor, dataSchema.core.scm.user));
333358
}
334359

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"dependencies": {
4848
"@hapi/hoek": "^11.0.7",
4949
"joi": "^17.13.3",
50-
"screwdriver-data-schema": "^25.0.0"
50+
"screwdriver-data-schema": "^25.0.0",
51+
"screwdriver-logger": "^3.0.0"
5152
}
5253
}

test/index.test.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,38 @@ describe('index test', () => {
596596
it('returns not implemented', () =>
597597
instance
598598
.decorateAuthor(config)
599-
.then(() => {
600-
assert.fail('you will never get dis');
599+
.then(author => {
600+
assert.deepEqual(
601+
{
602+
id: '',
603+
avatar: 'https://cd.screwdriver.cd/assets/unknown_user.png',
604+
name: config.username,
605+
username: config.username,
606+
url: 'https://cd.screwdriver.cd/'
607+
},
608+
author
609+
);
601610
})
602611
.catch(err => {
603612
assert.equal(err.message, 'Not implemented');
604613
}));
614+
615+
it('returns default author when there is a failure get author from scm', () => {
616+
instance._decorateAuthor = () => Promise.reject(new Error('User profile does not exist'));
617+
618+
return instance.decorateAuthor(config).then(author => {
619+
assert.deepEqual(
620+
{
621+
id: '',
622+
avatar: 'https://cd.screwdriver.cd/assets/unknown_user.png',
623+
name: config.username,
624+
username: config.username,
625+
url: 'https://cd.screwdriver.cd/'
626+
},
627+
author
628+
);
629+
});
630+
});
605631
});
606632

607633
describe('getPermissons', () => {

0 commit comments

Comments
 (0)