From 176f5c9eadb9403f8f7ed96de42048ff01608c12 Mon Sep 17 00:00:00 2001 From: Sagnik Haldar Date: Tue, 25 Jan 2022 10:17:00 +0530 Subject: [PATCH 1/2] added default config values --- __tests__/starfish.test.js | 14 ++------------ globals.js | 29 +++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/__tests__/starfish.test.js b/__tests__/starfish.test.js index 0e137be..15ec8c1 100644 --- a/__tests__/starfish.test.js +++ b/__tests__/starfish.test.js @@ -138,8 +138,8 @@ describe('getOrThrowIfMissingOrEmpty', () => { expect(() => getOrThrowIfMissingOrEmpty('configurationThatDoesNotExist')).toThrow(Error); }); it('should return the value of a configuration that exists in the environment', () => { - expect(() => getOrThrowIfMissingOrEmpty('TIMEZONE')).not.toThrow(Error); - expect(getOrThrowIfMissingOrEmpty('TIMEZONE')).toEqual(process.env.TIMEZONE); + expect(() => getOrThrowIfMissingOrEmpty('GITHUB_TOKEN')).not.toThrow(Error); + expect(getOrThrowIfMissingOrEmpty('GITHUB_TOKEN')).toEqual(process.env.GITHUB_TOKEN); }); }); @@ -164,16 +164,6 @@ describe('filterResponseForImportantEvents', () => { }); }); -describe('getOrThrowIfMissingOrEmpty', () => { - it('should throw an error if the configuration does not exist in the environment', () => { - expect(() => getOrThrowIfMissingOrEmpty('configurationThatDoesNotExist')).toThrow(Error); - }); - it('should return the value of a configuration that exists in the environment', () => { - expect(() => getOrThrowIfMissingOrEmpty('TIMEZONE')).not.toThrow(Error); - expect(getOrThrowIfMissingOrEmpty('TIMEZONE')).toEqual(process.env.TIMEZONE); - }); -}); - describe('fetchPageOfDataAndFilter', () => { const apiDomain = 'https://api.github.com'; const apiPath = '/users/octocat/events'; diff --git a/globals.js b/globals.js index ad8e4ed..4cc82d9 100644 --- a/globals.js +++ b/globals.js @@ -1,5 +1,15 @@ const { createLuxonDateTimeFromIso, createTimeZone } = require('./dateTimes'); +const defaultEnvironmentVariables = { + TIMEZONE: '', + CSV_COLUMN_NUMBER_FOR_GITHUB_ID: '0', + CSV_COLUMN_NUMBER_FOR_ALTERNATE_ID: '0', + IGNORE_SELFOWNED_EVENTS: 'false', + MINIMUM_NUMBER_OF_CONTRIBUTIONS: 1, + GITHUB_IMPORTANT_EVENTS: + 'CommitCommentEvent,IssueCommentEvent,IssuesEvent,PullRequestEvent,PullRequestReviewEvent,PullRequestReviewCommentEvent', +}; + function getOrThrowIfMissingOrEmpty(configField) { const value = process.env[configField]; if (!value) { @@ -11,6 +21,13 @@ function getOrThrowIfMissingOrEmpty(configField) { return value; } +function getDefaultEnvironemntValues(configField) { + const environmentValue = process.env[configField]; + const value = environmentValue ? environmentValue : defaultEnvironmentVariables[configField]; + + return value; +} + function getDateTimesFromArgv(timeZone) { console.info(`Using time zone: ${createTimeZone(timeZone).name}`); const startDate = process.argv[2]; @@ -23,15 +40,15 @@ function getDateTimesFromArgv(timeZone) { } const githubToken = Buffer.from(getOrThrowIfMissingOrEmpty('GITHUB_TOKEN')).toString('base64'); -const githubIdColumnNumber = getOrThrowIfMissingOrEmpty('CSV_COLUMN_NUMBER_FOR_GITHUB_ID'); -const alternateIdColumnNumber = getOrThrowIfMissingOrEmpty('CSV_COLUMN_NUMBER_FOR_ALTERNATE_ID'); -const minimumNumberOfContributions = process.env.MINIMUM_NUMBER_OF_CONTRIBUTIONS || 1; -const githubImportantEvents = getOrThrowIfMissingOrEmpty('GITHUB_IMPORTANT_EVENTS').split(','); -const timeZone = process.env.TIMEZONE; +const githubIdColumnNumber = getDefaultEnvironemntValues('CSV_COLUMN_NUMBER_FOR_GITHUB_ID'); +const alternateIdColumnNumber = getDefaultEnvironemntValues('CSV_COLUMN_NUMBER_FOR_ALTERNATE_ID'); +const minimumNumberOfContributions = getDefaultEnvironemntValues('MINIMUM_NUMBER_OF_CONTRIBUTIONS'); +const githubImportantEvents = getDefaultEnvironemntValues('GITHUB_IMPORTANT_EVENTS').split(','); +const timeZone = getDefaultEnvironemntValues('TIMEZONE'); const dateTimes = getDateTimesFromArgv(timeZone); const csvFilename = process.argv[4]; -const ignoreSelfOwnedEvents = (process.env.IGNORE_SELFOWNED_EVENTS || 'false').toLowerCase(); +const ignoreSelfOwnedEvents = getDefaultEnvironemntValues('IGNORE_SELFOWNED_EVENTS').toLowerCase(); console.info(`Configuration set to ignore self-owned events? ${ignoreSelfOwnedEvents}`); if (ignoreSelfOwnedEvents !== 'true' && ignoreSelfOwnedEvents !== 'false') { console.error('IGNORE_SELFOWNED_EVENTS must be "true" or "false"'); From 72b6b33bcacad071c58f38b7a2cdbe672c3dca93 Mon Sep 17 00:00:00 2001 From: SAGNIK HALDAR <56445132+SagnikH@users.noreply.github.com> Date: Tue, 25 Jan 2022 10:28:52 +0530 Subject: [PATCH 2/2] added default config variable note --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 144f4a6..de3ce46 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ Log in to GitHub and [register a new personal access token](https://github.com/s ### Next, Create a file named .env, copy the contents of the .env.template file into it, and add your values to the new file. +#### NOTE: We have provided default(hardcoded) environment variable values for easy onboarding inside the `globals.js` file where we handle our environment variables. The only thing you need to take care about is the `GITHUB_TOKEN`(access token) which is unique for every user in your .env file. This default implementation is provided for user convinience, rest you can modify the .env according to your needs. + - Paste the access token into `GITHUB_TOKEN`. - `TIMEZONE` allows you to specify which timezone Starfish should use to decide on which day a contribution happened. - The default is UTC, which works well for organizations with multiple locations. See the "Time zones" section for details.