From 58edd8846a41fb8fe0023d04f23e7d3913a78df4 Mon Sep 17 00:00:00 2001 From: Jithin Emmanuel Date: Wed, 20 Nov 2019 10:34:19 -0800 Subject: [PATCH 1/2] fix: refactor logging --- index.js | 50 ++++++++++++++++++++++++++-------------------- test/index.test.js | 7 ++++++- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index cfbf25e..72d360d 100644 --- a/index.js +++ b/index.js @@ -35,6 +35,12 @@ const DESCRIPTION_MAP = { RUNNING: 'Testing your code...', QUEUED: 'Looking for a place to park...' }; +const logger = winston.createLogger({ + level: process.env.LOG_LEVEL || 'info', + transports: [ + new (winston.transports.Console)({ timestamp: true }) + ] +}); /** * Get repo information @@ -167,7 +173,7 @@ class GithubScm extends Scm { repoFullName = repo.data.full_name; } catch (err) { - winston.error('Failed to lookupScmUri: ', err); + logger.error('Failed to lookupScmUri: ', err); throw err; } } @@ -218,7 +224,7 @@ class GithubScm extends Scm { return screwdriverHook; } catch (err) { - winston.error('Failed to findWebhook: ', err); + logger.error('Failed to findWebhook: ', err); throw err; } } @@ -262,7 +268,7 @@ class GithubScm extends Scm { return hooks.data; } catch (err) { - winston.error('Failed to createWebhook: ', err); + logger.error('Failed to createWebhook: ', err); throw err; } } @@ -512,7 +518,7 @@ class GithubScm extends Scm { userProfile: pullRequest.user.html_url })); } catch (err) { - winston.error('Failed to getOpenedPRs: ', err); + logger.error('Failed to getOpenedPRs: ', err); throw err; } } @@ -552,14 +558,14 @@ class GithubScm extends Scm { } catch (err) { // Suspended user if (err.message.match(/suspend/i)) { - winston.info( + logger.info( `User's account suspended for ${config.scmUri}, ` + 'it will be removed from pipeline admins.'); return { admin: false, push: false, pull: false }; } - winston.error('Failed to getPermissions: ', err); + logger.error('Failed to getPermissions: ', err); throw err; } } @@ -600,7 +606,7 @@ class GithubScm extends Scm { return result; } catch (err) { - winston.error('Failed to getOrgPermissions: ', err); + logger.error('Failed to getOrgPermissions: ', err); throw err; } } @@ -644,7 +650,7 @@ class GithubScm extends Scm { return branch.data.commit.sha; } catch (err) { - winston.error('Failed to getCommitSha: ', err); + logger.error('Failed to getCommitSha: ', err); throw err; } } @@ -693,7 +699,7 @@ class GithubScm extends Scm { } throw new Error(`Cannot handle ${refObj.data.object.type} type`); } catch (err) { - winston.error('Failed to getCommitRefSha: ', err); + logger.error('Failed to getCommitRefSha: ', err); throw err; } } @@ -741,7 +747,7 @@ class GithubScm extends Scm { return status ? status.data : undefined; } catch (err) { if (err.status !== 422) { - winston.error('Failed to updateCommitStatus: ', err); + logger.error('Failed to updateCommitStatus: ', err); throw err; } @@ -791,7 +797,7 @@ class GithubScm extends Scm { return Buffer.from(file.data.content, file.data.encoding).toString(); } catch (err) { - winston.error('Failed to getFile: ', err); + logger.error('Failed to getFile: ', err); throw err; } } @@ -833,7 +839,7 @@ class GithubScm extends Scm { throw new Error(`Cannot find repository ${checkoutUrl}`); } - winston.error('Failed to getRepoId: ', err); + logger.error('Failed to getRepoId: ', err); throw new Error(err); } } @@ -863,7 +869,7 @@ class GithubScm extends Scm { url: user.data.html_url }; } catch (err) { - winston.error('Failed to decorateAuthor: ', err); + logger.error('Failed to decorateAuthor: ', err); throw err; } } @@ -930,7 +936,7 @@ class GithubScm extends Scm { url: commit.data.html_url }; } catch (err) { - winston.error('Failed to decorateCommit: ', err); + logger.error('Failed to decorateCommit: ', err); throw err; } } @@ -1028,7 +1034,7 @@ class GithubScm extends Scm { return files.map(file => file.filename); } catch (err) { - winston.error('Failed to getChangedFiles: ', err); + logger.error('Failed to getChangedFiles: ', err); return []; } @@ -1150,7 +1156,7 @@ class GithubScm extends Scm { const refType = hoek.reach(webhookPayload, 'ref_type'); if (refType !== 'tag') { - winston.info('%s event of %s is not available yet in scm-github plugin', + logger.info('%s event of %s is not available yet in scm-github plugin', type, refType); return null; @@ -1169,7 +1175,7 @@ class GithubScm extends Scm { } default: - winston.info('%s event is not available yet in scm-github plugin', type); + logger.info('%s event is not available yet in scm-github plugin', type); return null; } @@ -1281,7 +1287,7 @@ class GithubScm extends Scm { baseBranch: pullRequestInfo.data.base.ref }; } catch (err) { - winston.error('Failed to getPrInfo: ', err); + logger.error('Failed to getPrInfo: ', err); throw err; } } @@ -1321,7 +1327,7 @@ class GithubScm extends Scm { username: pullRequestComment.data.user.login }; } catch (err) { - winston.error('Failed to addPRComment: ', err); + logger.error('Failed to addPRComment: ', err); return null; } @@ -1360,7 +1366,7 @@ class GithubScm extends Scm { return result.checkoutUrl.startsWith(checkoutSshHost); } catch (err) { - winston.error('Failed to run canHandleWebhook', err); + logger.error('Failed to run canHandleWebhook', err); return false; } @@ -1399,7 +1405,7 @@ class GithubScm extends Scm { return branches.map(branch => ({ name: hoek.reach(branch, 'name') })); } catch (err) { - winston.error('Failed to findBranches: ', err); + logger.error('Failed to findBranches: ', err); throw err; } } @@ -1423,7 +1429,7 @@ class GithubScm extends Scm { page: 1, token: config.token }).catch((err) => { - winston.error('Failed to getBranchList: ', err); + logger.error('Failed to getBranchList: ', err); throw err; }); } diff --git a/test/index.test.js b/test/index.test.js index 7a016b5..4530449 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -87,7 +87,12 @@ describe('index', function () { }; mockery.registerMock('@octokit/rest', githubMockClass); - mockery.registerMock('winston', winstonMock); + mockery.registerMock('winston', { + createLogger: () => winstonMock, + transports: { + Console: sinon.stub() + } + }); // eslint-disable-next-line global-require GithubScm = require('../'); From 76385ebfeda357e55ff05eab98ee114e278d6ff6 Mon Sep 17 00:00:00 2001 From: Jithin Emmanuel Date: Wed, 20 Nov 2019 17:13:27 -0800 Subject: [PATCH 2/2] fix: use screwdriver-logger --- index.js | 8 +------- package.json | 4 ++-- test/index.test.js | 7 +------ 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 72d360d..4cb9dbc 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ const joi = require('joi'); const schema = require('screwdriver-data-schema'); const Scm = require('screwdriver-scm-base'); const crypto = require('crypto'); -const winston = require('winston'); +const logger = require('screwdriver-logger'); const DEFAULT_AUTHOR = { avatar: 'https://cd.screwdriver.cd/assets/unknown_user.png', name: 'n/a', @@ -35,12 +35,6 @@ const DESCRIPTION_MAP = { RUNNING: 'Testing your code...', QUEUED: 'Looking for a place to park...' }; -const logger = winston.createLogger({ - level: process.env.LOG_LEVEL || 'info', - transports: [ - new (winston.transports.Console)({ timestamp: true }) - ] -}); /** * Get repo information diff --git a/package.json b/package.json index a67e3c5..e7b0d2d 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "chai": "^3.5.0", "eslint": "^4.19.1", "eslint-config-screwdriver": "^3.0.1", - "jenkins-mocha": "^7.0.0", + "jenkins-mocha": "^8.0.0", "mockery": "^2.0.0", "sinon": "^7.2.7" }, @@ -46,7 +46,7 @@ "joi": "^13.7.0", "screwdriver-data-schema": "^18.46.13", "screwdriver-scm-base": "^5.2.0", - "winston": "^3.2.1" + "screwdriver-logger": "^1.0.0" }, "release": { "debug": false, diff --git a/test/index.test.js b/test/index.test.js index 4530449..a1227af 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -87,12 +87,7 @@ describe('index', function () { }; mockery.registerMock('@octokit/rest', githubMockClass); - mockery.registerMock('winston', { - createLogger: () => winstonMock, - transports: { - Console: sinon.stub() - } - }); + mockery.registerMock('screwdriver-logger', winstonMock); // eslint-disable-next-line global-require GithubScm = require('../');