From c339c30d8796052ecda056ff3642232e8ab2d674 Mon Sep 17 00:00:00 2001 From: Neil Kalman Date: Thu, 6 May 2021 17:06:34 +0000 Subject: [PATCH] feat(config): add github token config we should use a github app authentication, but for now, this is fine while we develop this. Later, we should have the github app, but that's complicated enough to be it's own pull request --- env.schema.json | 2 +- .../__snapshots__/config.service.spec.ts.snap | 1 + server/src/config/achievibit-config.model.ts | 8 ++++++++ server/src/config/config.service.spec.ts | 20 +++++-------------- server/src/config/config.service.ts | 4 +++- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/env.schema.json b/env.schema.json index 2d157794..5adca274 100644 --- a/env.schema.json +++ b/env.schema.json @@ -1 +1 @@ -{"properties":{"port":{"description":"Set server port","type":"number"},"dbUrl":{"description":"DB connection URL. Expects a mongodb db for connections","format":"url","type":"string"},"webhookProxyUrl":{"description":"Used to create a custom repeatable smee webhook url instead of Generating a random one","format":"url","type":"string","pattern":"^https:\\/\\/(?:www\\.)?smee\\.io\\/[a-zA-Z0-9_-]+\\/?"},"webhookDestinationUrl":{"description":"proxy should sent events to this url for achievibit","pattern":"^([\\w]+)?(\\/[\\w-]+)*$","type":"string"},"saveToFile":{"description":"Create a file made out of the internal config. This is mostly for merging command line, environment, and file variables to a single instance","type":"boolean"},"deletePRsHealthId":{"description":"cron job monitoring id","type":"string"}},"type":"object","required":["port","webhookProxyUrl","webhookDestinationUrl","saveToFile"]} +{"properties":{"port":{"description":"Set server port","type":"number"},"dbUrl":{"description":"DB connection URL. Expects a mongodb db for connections","format":"url","type":"string"},"webhookProxyUrl":{"description":"Used to create a custom repeatable smee webhook url instead of Generating a random one","format":"url","type":"string","pattern":"^https:\\/\\/(?:www\\.)?smee\\.io\\/[a-zA-Z0-9_-]+\\/?"},"webhookDestinationUrl":{"description":"proxy should sent events to this url for achievibit","pattern":"^([\\w]+)?(\\/[\\w-]+)*$","type":"string"},"saveToFile":{"description":"Create a file made out of the internal config. This is mostly for merging command line, environment, and file variables to a single instance","type":"boolean"},"deletePRsHealthId":{"description":"cron job monitoring id","type":"string"},"githubAccessToken":{"description":"GitHub Access Token","type":"string"}},"type":"object","required":["port","webhookProxyUrl","webhookDestinationUrl","saveToFile"]} diff --git a/server/src/config/__snapshots__/config.service.spec.ts.snap b/server/src/config/__snapshots__/config.service.spec.ts.snap index 201a8a07..b57639cf 100644 --- a/server/src/config/__snapshots__/config.service.spec.ts.snap +++ b/server/src/config/__snapshots__/config.service.spec.ts.snap @@ -4,6 +4,7 @@ exports[`ConfigService Forced Singleton and baypass should set default values to Object { "dbUrl": undefined, "deletePRsHealthId": undefined, + "githubAccessToken": undefined, "nodeEnv": "development", "port": 10101, "webhookDestinationUrl": "events", diff --git a/server/src/config/achievibit-config.model.ts b/server/src/config/achievibit-config.model.ts index 37a6dbe1..3bd53b9a 100644 --- a/server/src/config/achievibit-config.model.ts +++ b/server/src/config/achievibit-config.model.ts @@ -84,6 +84,14 @@ export class AchievibitConfig { ]) deletePRsHealthId; + @Expose() + @IsString() + @IsOptional() + @Validate(JsonSchema, [ + 'GitHub Access Token' + ]) + githubAccessToken: string; + constructor(partial: Partial = {}) { Object.assign(this, partial); } diff --git a/server/src/config/config.service.spec.ts b/server/src/config/config.service.spec.ts index e3295ceb..8399c29f 100644 --- a/server/src/config/config.service.spec.ts +++ b/server/src/config/config.service.spec.ts @@ -10,7 +10,10 @@ import { } from './achievibit-config.model'; import { ConfigService } from './config.service'; -// not a jest component so testing it as a node module +// TODO@Thatkookooguy: #354 manual mock this to test events when created +jest.mock('smee-client'); + +// testing it as a node module and not as a nestjs component describe('ConfigService', () => { describe('Forced Singleton and baypass', () => { @@ -21,10 +24,6 @@ describe('ConfigService', () => { }); }); - afterEach(() => { - configService.closeEvents(); - }); - it('should be defined', () => { expect(configService).toBeDefined(); }); @@ -52,8 +51,6 @@ describe('ConfigService', () => { expect(productionService.smee).toBeUndefined(); expect(productionService.events).toBeUndefined(); - - productionService.closeEvents(); }); it('should initial smee and events on development', () => { @@ -62,9 +59,7 @@ describe('ConfigService', () => { }); expect(productionService.smee).toBeDefined(); - expect(productionService.events).toBeDefined(); - - productionService.closeEvents(); + expect(productionService.smee.start).toHaveBeenCalledTimes(1); }); }); @@ -81,8 +76,6 @@ describe('ConfigService', () => { const serviceWrapper = () => new ConfigService({ nodeEnv }); expect(serviceWrapper().toPlainObject).toBeDefined(); - - serviceWrapper().closeEvents(); }); }) .value(); @@ -133,8 +126,6 @@ describe('ConfigService', () => { const service = new ConfigService({ dbUrl: undefined }); expect(service.dbUrl).toBeUndefined(); - - service.closeEvents(); }); it('should ACCEPT localhost mongodb URL', () => { @@ -178,7 +169,6 @@ describe('ConfigService', () => { webhookProxyUrl: smeeProtocolUrl })) .forEach((configService, smeeProtocolUrl) => { - configService.closeEvents(); expect(configService.webhookProxyUrl).toBe(smeeProtocolUrl); }) .value(); diff --git a/server/src/config/config.service.ts b/server/src/config/config.service.ts index 80370544..afdfd617 100644 --- a/server/src/config/config.service.ts +++ b/server/src/config/config.service.ts @@ -36,6 +36,7 @@ const appRoot = findRoot(__dirname, (dir) => { const environment = get(process, 'env.NODE_ENV', 'development'); const eventLogger: Logger = new Logger('SmeeEvents'); (eventLogger as any).info = eventLogger.log; +const defaultConfigFilePath = join(appRoot, 'defaults.env.json'); const configFilePath = join(appRoot, `${ environment }.env.json`); const packageDetails = new ApiInfo(readJSONSync(join(appRoot, 'package.json'))); @@ -49,7 +50,8 @@ nconf parseValues: true, transform: transformToLowerCase }) - .file({ file: configFilePath }); + .file('defaults', { file: defaultConfigFilePath }) + .file('environment', { file: configFilePath }); let smee: SmeeClient; let events: any;