From cbbde942d76a07e6ea84fd2fb646472e398f5f04 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 14:55:54 +0100 Subject: [PATCH] updates to githubWebhook Signed-off-by: Tavi Nolan --- .../src/actions/githubWebhook.test.ts | 36 +++++++------------ .../src/actions/githubWebhook.ts | 15 ++++---- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts index 367f19d083..6c0335c509 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts @@ -222,37 +222,27 @@ describe('github:repository:webhook:create', () => { }); }); - it('should call the githubApi for creating repository Webhook with dry run', async () => { + it('should not call the githubApi for creating repository Webhook with dry run', async () => { + // Define constants for reused string literals const repoUrl = 'github.com?repo=repo&owner=owner'; const webhookUrl = 'https://example.com/payload'; - const ctx = Object.assign({}, mockContext, { - input: { repoUrl, webhookUrl }, - }); - ctx.isDryRun = true; - await action.handler(ctx); - const webhookSecret = 'yet_another_secret'; - await action.handler({ + // Create the context object with the necessary properties for a dry run + const ctx = { ...mockContext, + isDryRun: true, input: { ...mockContext.input, - webhookSecret, - events: ['push', 'pull_request'], + repoUrl, + webhookUrl, }, - }); + }; - expect(mockOctokit.rest.repos.createWebhook).toHaveBeenCalledWith({ - owner: 'owner', - repo: 'repo', - events: ['push', 'pull_request'], - active: true, - config: { - url: webhookUrl, - content_type: 'form', - secret: webhookSecret, - insecure_ssl: '0', - }, - }); + // Call the handler with the context + await action.handler(ctx); + + // Check that the createWebhook method was not called + expect(mockOctokit.rest.repos.createWebhook).not.toHaveBeenCalled(); }); it('should validate input', async () => { diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts index aa8f542df7..e0e124a1c0 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts @@ -149,14 +149,15 @@ export function createGithubWebhookAction(options: { }), ); + // If this is a dry run, log the arguments and exit if (ctx.isDryRun) { - ctx.logger.info( - `Dry run arguments: ${{ - webhookSecret, - events, - ...ctx.input, - }}`, - ); + const dryRunArgs = { + webhookSecret, + events, + ...ctx.input, + }; + + ctx.logger.info(`Dry run completed`); return; }