From 8ebd2b639932a39d36d3435fba9250db7b63a460 Mon Sep 17 00:00:00 2001 From: "@pawelmitka" Date: Fri, 27 Aug 2021 12:23:35 +0200 Subject: [PATCH] refactor: avoid using env variables in action Signed-off-by: @pawelmitka --- .../githubCreateRepositoryWebhook.test.ts | 26 +++++++------------ .../github/githubCreateRepositoryWebhook.ts | 22 +++++----------- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubCreateRepositoryWebhook.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubCreateRepositoryWebhook.test.ts index 166ca15272..4b74799dbe 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubCreateRepositoryWebhook.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubCreateRepositoryWebhook.test.ts @@ -39,7 +39,7 @@ describe('github:repository:webhook:create', () => { input: { repoUrl: 'github.com?repo=repo&owner=owner', webhookUrl: 'https://example.com/payload', - webhookSecretEnv: 'SUPER_SECRET', + webhookSecret: 'aafdfdivierernfdk23f', }, workspacePath: 'lol', logger: getVoidLogger(), @@ -90,20 +90,12 @@ describe('github:repository:webhook:create', () => { ).rejects.toThrow(/No token available for host/); }); - it('should throw if environment variable in secret is invalid', async () => { - await expect(action.handler(mockContext)).rejects.toThrow( - /Environment variable: SUPER_SECRET does not exist./, - ); - }); - it('should call the githubApi for creating repository Webhook', async () => { const repoUrl = 'github.com?repo=repo&owner=owner'; const webhookUrl = 'https://example.com/payload'; - const webhookSecretEnv = 'SUPER_SECRET'; - const secret = 'aafdfdivierernfdk23f'; - process.env[webhookSecretEnv] = secret; + const webhookSecret = 'aafdfdivierernfdk23f'; const ctx = Object.assign({}, mockContext, { - input: { repoUrl, webhookUrl, webhookSecretEnv }, + input: { repoUrl, webhookUrl, webhookSecret }, }); await action.handler(ctx); @@ -115,7 +107,7 @@ describe('github:repository:webhook:create', () => { config: { url: webhookUrl, content_type: 'form', - secret, + secret: webhookSecret, insecure_ssl: '0', }, }); @@ -136,7 +128,7 @@ describe('github:repository:webhook:create', () => { config: { url: webhookUrl, content_type: 'form', - secret, + secret: webhookSecret, insecure_ssl: '0', }, }); @@ -157,7 +149,7 @@ describe('github:repository:webhook:create', () => { config: { url: webhookUrl, content_type: 'json', - secret, + secret: webhookSecret, insecure_ssl: '0', }, }); @@ -178,7 +170,7 @@ describe('github:repository:webhook:create', () => { config: { url: webhookUrl, content_type: 'form', - secret, + secret: webhookSecret, insecure_ssl: '1', }, }); @@ -199,7 +191,7 @@ describe('github:repository:webhook:create', () => { config: { url: webhookUrl, content_type: 'form', - secret, + secret: webhookSecret, insecure_ssl: '1', }, }); @@ -220,7 +212,7 @@ describe('github:repository:webhook:create', () => { config: { url: webhookUrl, content_type: 'form', - secret, + secret: webhookSecret, insecure_ssl: '0', }, }); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubCreateRepositoryWebhook.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubCreateRepositoryWebhook.ts index 32ebbcae31..cec68c09f2 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubCreateRepositoryWebhook.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubCreateRepositoryWebhook.ts @@ -39,7 +39,7 @@ export function createGithubCreateRepositoryWebhookAction(options: { return createTemplateAction<{ repoUrl: string; webhookUrl: string; - webhookSecretEnv?: string; + webhookSecret?: string; events?: string[]; active?: boolean; contentType?: ContentType; @@ -62,9 +62,9 @@ export function createGithubCreateRepositoryWebhookAction(options: { description: 'The URL to which the payloads will be delivered', type: 'string', }, - webhookSecretEnv: { - title: 'Environment variable containing Webhook Secret', - description: 'Name of the environment variable containing secret', + webhookSecret: { + title: 'Webhook Secret', + description: 'Webhook secret value', type: 'string', }, events: { @@ -99,7 +99,7 @@ export function createGithubCreateRepositoryWebhookAction(options: { const { repoUrl, webhookUrl, - webhookSecretEnv, + webhookSecret, events = ['push'], active = true, contentType = 'form', @@ -112,16 +112,6 @@ export function createGithubCreateRepositoryWebhookAction(options: { throw new InputError(`No owner provided for repo ${repoUrl}`); } - let secret; - if (webhookSecretEnv) { - secret = process.env[webhookSecretEnv]; - if (!secret) { - throw new InputError( - `Environment variable: ${webhookSecretEnv} does not exist.`, - ); - } - } - ctx.logger.info(`Creating webhook ${webhookUrl} for repo ${repoUrl}`); const credentialsProvider = credentialsProviders.get(host); @@ -159,7 +149,7 @@ export function createGithubCreateRepositoryWebhookAction(options: { config: { url: webhookUrl, content_type: contentType, - secret, + secret: webhookSecret, insecure_ssl, }, events,