From 030fd36b03e5d13bfe54f369aa59a331b50e1523 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Mon, 11 Mar 2024 22:55:16 +0000 Subject: [PATCH 01/23] Enabling scaffolder dry run Signed-off-by: Tavi Nolan --- .../src/actions/githubPullRequest.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts index b4aa03b602..243c871724 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -14,24 +14,25 @@ * limitations under the License. */ -import path from 'path'; +import { CustomErrorBase, InputError } from '@backstage/errors'; import { GithubCredentialsProvider, ScmIntegrationRegistry, } from '@backstage/integration'; import { + SerializedFile, createTemplateAction, parseRepoUrl, - SerializedFile, serializeDirectoryContents, } from '@backstage/plugin-scaffolder-node'; + +import { Logger } from 'winston'; import { Octokit } from 'octokit'; -import { CustomErrorBase, InputError } from '@backstage/errors'; -import { resolveSafeChildPath } from '@backstage/backend-common'; import { createPullRequest } from 'octokit-plugin-create-pull-request'; -import { getOctokitOptions } from './helpers'; import { examples } from './githubPullRequest.examples'; -import { LoggerService } from '@backstage/backend-plugin-api'; +import { getOctokitOptions } from './helpers'; +import path from 'path'; +import { resolveSafeChildPath } from '@backstage/backend-common'; export type Encoding = 'utf-8' | 'base64'; @@ -139,6 +140,7 @@ export const createPublishGithubPullRequestAction = ( }>({ id: 'publish:github:pull-request', examples, + supportsDryRun: true, schema: { input: { required: ['repoUrl', 'title', 'description', 'branchName'], From 9f3fdcc3828ccb7c0c73554b1c86b088c4d08f2a Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Wed, 3 Apr 2024 13:28:07 +0100 Subject: [PATCH 02/23] initial commit, add dry run to githubPullRequest Signed-off-by: Tavi Nolan --- .../src/actions/githubPullRequest.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts index 243c871724..12ff50afc4 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -321,6 +321,22 @@ export const createPublishGithubPullRequestAction = ( ]), ); + if (ctx.isDryRun) { + ctx.logger.info( + `Dry run arguments: ${{ + repoUrl, + branchName, + title, + description, + ...ctx.input, + }}`, + ); + ctx.output('targetBranchName', branchName); + ctx.output('remoteUrl', repoUrl); + ctx.output('pullRequestNumber', 42); + return; + } + try { const createOptions: createPullRequest.Options = { owner, From 27dbbdf72a1c442cd3e1434c47ceb038751d766d Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Wed, 3 Apr 2024 13:41:54 +0100 Subject: [PATCH 03/23] fixed imports Signed-off-by: Tavi Nolan --- .../src/actions/githubPullRequest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts index 12ff50afc4..a1606a0d95 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -333,7 +333,7 @@ export const createPublishGithubPullRequestAction = ( ); ctx.output('targetBranchName', branchName); ctx.output('remoteUrl', repoUrl); - ctx.output('pullRequestNumber', 42); + ctx.output('pullRequestNumber', 43); return; } From dae724d913693ca77360979ce1dc128bad17c9f3 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Wed, 3 Apr 2024 14:03:59 +0100 Subject: [PATCH 04/23] add dry run to githubWebhook Signed-off-by: Tavi Nolan --- .../src/actions/githubWebhook.ts | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts index 984029076e..e8bf179e75 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts @@ -18,15 +18,16 @@ import { GithubCredentialsProvider, ScmIntegrationRegistry, } from '@backstage/integration'; +import { InputError, assertError } from '@backstage/errors'; import { createTemplateAction, parseRepoUrl, } from '@backstage/plugin-scaffolder-node'; -import { emitterEventNames } from '@octokit/webhooks'; -import { assertError, InputError } from '@backstage/errors'; + import { Octokit } from 'octokit'; -import { getOctokitOptions } from './helpers'; +import { emitterEventNames } from '@octokit/webhooks'; import { examples } from './githubWebhook.examples'; +import { getOctokitOptions } from './helpers'; /** * Creates new action that creates a webhook for a repository on GitHub. @@ -148,6 +149,19 @@ export function createGithubWebhookAction(options: { }), ); + if (ctx.isDryRun) { + ctx.logger.info( + `Dry run arguments: ${{ + repoUrl, + webhookUrl, + webhookSecret, + events, + ...ctx.input, + }}`, + ); + return; + } + try { const insecure_ssl = insecureSsl ? '1' : '0'; await client.rest.repos.createWebhook({ From f0ae76208118652b61115462a9d858f7cc8a0161 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 13:49:49 +0100 Subject: [PATCH 05/23] working test for githubPullRequest Signed-off-by: Tavi Nolan --- .../src/actions/githubPullRequest.test.ts | 31 +++++++++++++------ .../src/actions/githubPullRequest.ts | 4 --- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts index c9d1498187..86197285f2 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts @@ -14,20 +14,21 @@ * limitations under the License. */ -import { createRootLogger } from '@backstage/backend-common'; -import { ConfigReader } from '@backstage/config'; -import { - GithubCredentialsProvider, - ScmIntegrations, -} from '@backstage/integration'; import { ActionContext, TemplateAction, } from '@backstage/plugin-scaffolder-node'; -import fs from 'fs-extra'; -import { createPublishGithubPullRequestAction } from './githubPullRequest'; -import { createMockDirectory } from '@backstage/backend-test-utils'; +import { + GithubCredentialsProvider, + ScmIntegrations, +} from '@backstage/integration'; + +import { ConfigReader } from '@backstage/config'; import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; +import { createMockDirectory } from '@backstage/backend-test-utils'; +import { createPublishGithubPullRequestAction } from './githubPullRequest'; +import { createRootLogger } from '@backstage/backend-common'; +import fs from 'fs-extra'; // Make sure root logger is initialized ahead of FS mock createRootLogger(); @@ -227,6 +228,18 @@ describe('createPublishGithubPullRequestAction', () => { ); expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123); }); + + it('handles dry run correctly', async () => { + ctx.isDryRun = true; + await instance.handler(ctx); + + expect(ctx.output).toHaveBeenCalledWith('targetBranchName', 'new-app'); + expect(ctx.output).toHaveBeenCalledWith( + 'remoteUrl', + 'github.com?owner=myorg&repo=myrepo', + ); + expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 43); + }); }); describe('with sourcePath', () => { diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts index a1606a0d95..e058bfcd94 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -324,10 +324,6 @@ export const createPublishGithubPullRequestAction = ( if (ctx.isDryRun) { ctx.logger.info( `Dry run arguments: ${{ - repoUrl, - branchName, - title, - description, ...ctx.input, }}`, ); From 8de90c83587ada096900730bf8b7709d0148f0d2 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 14:14:55 +0100 Subject: [PATCH 06/23] testing for githubWebhook.test.ts Signed-off-by: Tavi Nolan --- .../src/actions/githubWebhook.test.ts | 40 +++++++++++++++++-- .../src/actions/githubWebhook.ts | 2 - 2 files changed, 37 insertions(+), 5 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 cbdcee6af1..367f19d083 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts @@ -14,15 +14,16 @@ * limitations under the License. */ -import { createGithubWebhookAction } from './githubWebhook'; import { - ScmIntegrations, DefaultGithubCredentialsProvider, GithubCredentialsProvider, + ScmIntegrations, } from '@backstage/integration'; -import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; + import { ConfigReader } from '@backstage/config'; import { TemplateAction } from '@backstage/plugin-scaffolder-node'; +import { createGithubWebhookAction } from './githubWebhook'; +import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; const mockOctokit = { rest: { @@ -221,6 +222,39 @@ describe('github:repository:webhook:create', () => { }); }); + it('should call the githubApi for creating repository Webhook with dry run', async () => { + 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({ + ...mockContext, + input: { + ...mockContext.input, + webhookSecret, + events: ['push', 'pull_request'], + }, + }); + + 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', + }, + }); + }); + it('should validate input', async () => { const Validator = require('jsonschema').Validator; const v = new Validator(); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts index e8bf179e75..aa8f542df7 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts @@ -152,8 +152,6 @@ export function createGithubWebhookAction(options: { if (ctx.isDryRun) { ctx.logger.info( `Dry run arguments: ${{ - repoUrl, - webhookUrl, webhookSecret, events, ...ctx.input, From cbbde942d76a07e6ea84fd2fb646472e398f5f04 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 14:55:54 +0100 Subject: [PATCH 07/23] 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; } From 38b1a35a83f549dc8ff598972febf878461be6bc Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 16:21:03 +0100 Subject: [PATCH 08/23] updated githubPullRequest Signed-off-by: Tavi Nolan --- .../src/actions/githubPullRequest.test.ts | 2 +- .../src/actions/githubPullRequest.ts | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts index 86197285f2..150ed39e50 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts @@ -229,7 +229,7 @@ describe('createPublishGithubPullRequestAction', () => { expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123); }); - it('handles dry run correctly', async () => { + it('sets correct outputs during dry run', async () => { ctx.isDryRun = true; await instance.handler(ctx); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts index e058bfcd94..99e442f74a 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -322,14 +322,11 @@ export const createPublishGithubPullRequestAction = ( ); if (ctx.isDryRun) { - ctx.logger.info( - `Dry run arguments: ${{ - ...ctx.input, - }}`, - ); + ctx.logger.info(`Performing dry run of creating pull request`); ctx.output('targetBranchName', branchName); ctx.output('remoteUrl', repoUrl); ctx.output('pullRequestNumber', 43); + ctx.logger.info(`Dry run complete`); return; } From 6dcd72ae3e7d00190708e100300a42dc1a263445 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 16:25:40 +0100 Subject: [PATCH 09/23] updated githubWebhook variable and comment Signed-off-by: Tavi Nolan --- .../src/actions/githubWebhook.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts index e0e124a1c0..9b3ccdac07 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts @@ -149,14 +149,8 @@ export function createGithubWebhookAction(options: { }), ); - // If this is a dry run, log the arguments and exit + // If this is a dry run, log and return if (ctx.isDryRun) { - const dryRunArgs = { - webhookSecret, - events, - ...ctx.input, - }; - ctx.logger.info(`Dry run completed`); return; } From 07a6d7f153ced344978a0eff2db35332f5c6c270 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 17:10:52 +0100 Subject: [PATCH 10/23] initial working changes for githubRepoCreate Signed-off-by: Tavi Nolan --- .../src/actions/githubRepoCreate.test.ts | 67 ++++++++++++++++--- .../src/actions/githubRepoCreate.ts | 25 ++++--- 2 files changed, 75 insertions(+), 17 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts index 1750acdb9a..edd7bb7c9d 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts @@ -14,8 +14,18 @@ * limitations under the License. */ +import { + DefaultGithubCredentialsProvider, + GithubCredentialsProvider, + ScmIntegrations, +} from '@backstage/integration'; + +import { ConfigReader } from '@backstage/config'; import { TemplateAction } from '@backstage/plugin-scaffolder-node'; +import { createGithubRepoCreateAction } from './githubRepoCreate'; import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; +import { entityRefToName } from './gitHelpers'; +import { when } from 'jest-when'; jest.mock('./gitHelpers', () => { return { @@ -24,15 +34,6 @@ jest.mock('./gitHelpers', () => { }; }); -import { ConfigReader } from '@backstage/config'; -import { - DefaultGithubCredentialsProvider, - GithubCredentialsProvider, - ScmIntegrations, -} from '@backstage/integration'; -import { createGithubRepoCreateAction } from './githubRepoCreate'; -import { entityRefToName } from './gitHelpers'; - const publicKey = '2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU='; const mockOctokit = { @@ -697,4 +698,52 @@ describe('github:repo:create', () => { 'https://github.com/clone/url.git', ); }); + + it('should NOT call the githubApis with the correct values for createInOrg during dry run', async () => { + mockOctokit.rest.users.getByUsername.mockResolvedValue({ + data: { type: 'Organization' }, + }); + + mockOctokit.rest.teams.getByName.mockResolvedValue({ + data: { + name: 'blam', + id: 42, + }, + }); + + mockOctokit.rest.repos.createInOrg.mockResolvedValue({ data: {} }); + + mockContext.isDryRun = true; + await action.handler(mockContext); + expect(mockOctokit.rest.repos.createInOrg).not.toHaveBeenCalledWith({ + description: 'description', + name: 'repo', + org: 'owner', + private: true, + delete_branch_on_merge: false, + allow_squash_merge: true, + squash_merge_commit_title: 'COMMIT_OR_PR_TITLE', + squash_merge_commit_message: 'COMMIT_MESSAGES', + allow_merge_commit: true, + allow_rebase_merge: true, + allow_auto_merge: false, + visibility: 'private', + }); + }); + + it('should not call the githubApis with the correct values for createForAuthenticatedUser during dry run', async () => { + mockOctokit.rest.users.getByUsername.mockResolvedValue({ + data: { type: 'User' }, + }); + + mockOctokit.rest.repos.createForAuthenticatedUser.mockResolvedValue({ + data: {}, + }); + + mockContext.isDryRun = true; + await action.handler(mockContext); + expect( + mockOctokit.rest.repos.createForAuthenticatedUser, + ).not.toHaveBeenCalled(); + }); }); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts index 52bf9be9c0..175aae46aa 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts @@ -14,22 +14,24 @@ * limitations under the License. */ -import { InputError } from '@backstage/errors'; +import * as inputProps from './inputProperties'; +import * as outputProps from './outputProperties'; + import { GithubCredentialsProvider, ScmIntegrationRegistry, } from '@backstage/integration'; -import { Octokit } from 'octokit'; -import { - createTemplateAction, - parseRepoUrl, -} from '@backstage/plugin-scaffolder-node'; import { createGithubRepoWithCollaboratorsAndTopics, getOctokitOptions, } from './helpers'; -import * as inputProps from './inputProperties'; -import * as outputProps from './outputProperties'; +import { + createTemplateAction, + parseRepoUrl, +} from '@backstage/plugin-scaffolder-node'; + +import { InputError } from '@backstage/errors'; +import { Octokit } from 'octokit'; import { examples } from './githubRepoCreate.examples'; /** @@ -188,6 +190,13 @@ export function createGithubRepoCreateAction(options: { throw new InputError('Invalid repository owner provided in repoUrl'); } + if (ctx.isDryRun) { + ctx.logger.info(`Performing dry run of creating repository`); + ctx.output('remoteUrl', repoUrl); + ctx.logger.info(`Dry run complete`); + return; + } + const newRepo = await createGithubRepoWithCollaboratorsAndTopics( client, repo, From 1d0a7ce72a9c53f23d617ebc90fd3c1d97aa0893 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 17:19:28 +0100 Subject: [PATCH 11/23] minor fixes and test name changes for githubRepoCreate Signed-off-by: Tavi Nolan --- .../src/actions/githubRepoCreate.test.ts | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts index edd7bb7c9d..3940a71c5d 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts @@ -699,7 +699,7 @@ describe('github:repo:create', () => { ); }); - it('should NOT call the githubApis with the correct values for createInOrg during dry run', async () => { + it('should not call createInOrg during dry run', async () => { mockOctokit.rest.users.getByUsername.mockResolvedValue({ data: { type: 'Organization' }, }); @@ -715,23 +715,10 @@ describe('github:repo:create', () => { mockContext.isDryRun = true; await action.handler(mockContext); - expect(mockOctokit.rest.repos.createInOrg).not.toHaveBeenCalledWith({ - description: 'description', - name: 'repo', - org: 'owner', - private: true, - delete_branch_on_merge: false, - allow_squash_merge: true, - squash_merge_commit_title: 'COMMIT_OR_PR_TITLE', - squash_merge_commit_message: 'COMMIT_MESSAGES', - allow_merge_commit: true, - allow_rebase_merge: true, - allow_auto_merge: false, - visibility: 'private', - }); + expect(mockOctokit.rest.repos.createInOrg).not.toHaveBeenCalled(); }); - it('should not call the githubApis with the correct values for createForAuthenticatedUser during dry run', async () => { + it('should not call createForAuthenticatedUser during dry run', async () => { mockOctokit.rest.users.getByUsername.mockResolvedValue({ data: { type: 'User' }, }); @@ -740,7 +727,6 @@ describe('github:repo:create', () => { data: {}, }); - mockContext.isDryRun = true; await action.handler(mockContext); expect( mockOctokit.rest.repos.createForAuthenticatedUser, From 7c2c8e4961b04366cebfc28b45f737831a8c71bf Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 17:25:57 +0100 Subject: [PATCH 12/23] added dry run to github.ts Signed-off-by: Tavi Nolan --- .../src/actions/github.test.ts | 46 +++++++++++++++++-- .../src/actions/github.ts | 27 +++++++---- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts index 9eda9a2eb0..4802fd9078 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts @@ -33,21 +33,23 @@ jest.mock('@backstage/plugin-scaffolder-node', () => { }; }); -import { TemplateAction } from '@backstage/plugin-scaffolder-node'; -import { ConfigReader } from '@backstage/config'; -import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; import { DefaultGithubCredentialsProvider, GithubCredentialsProvider, ScmIntegrations, } from '@backstage/integration'; -import { createPublishGithubAction } from './github'; -import { initRepoAndPush } from '@backstage/plugin-scaffolder-node'; import { enableBranchProtectionOnDefaultRepoBranch, entityRefToName, } from './gitHelpers'; +import { ConfigReader } from '@backstage/config'; +import { TemplateAction } from '@backstage/plugin-scaffolder-node'; +import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; +import { createPublishGithubAction } from './github'; +import { initRepoAndPush } from '@backstage/plugin-scaffolder-node'; +import { when } from 'jest-when'; + const publicKey = '2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU='; const initRepoAndPushMocked = initRepoAndPush as jest.Mock< @@ -2064,4 +2066,38 @@ describe('publish:github', () => { requiredCommitSigning: false, }); }); + + it('should not call createInOrg during dry run', async () => { + mockOctokit.rest.users.getByUsername.mockResolvedValue({ + data: { type: 'Organization' }, + }); + + mockOctokit.rest.teams.getByName.mockResolvedValue({ + data: { + name: 'blam', + id: 42, + }, + }); + + mockOctokit.rest.repos.createInOrg.mockResolvedValue({ data: {} }); + + mockContext.isDryRun = true; + await action.handler(mockContext); + expect(mockOctokit.rest.repos.createInOrg).not.toHaveBeenCalled(); + }); + + it('should not call createForAuthenticatedUser during dry run', async () => { + mockOctokit.rest.users.getByUsername.mockResolvedValue({ + data: { type: 'User' }, + }); + + mockOctokit.rest.repos.createForAuthenticatedUser.mockResolvedValue({ + data: {}, + }); + + await action.handler(mockContext); + expect( + mockOctokit.rest.repos.createForAuthenticatedUser, + ).not.toHaveBeenCalled(); + }); }); diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.ts b/plugins/scaffolder-backend-module-github/src/actions/github.ts index f32b65bc9a..923190465a 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.ts @@ -14,24 +14,26 @@ * limitations under the License. */ -import { Config } from '@backstage/config'; -import { InputError } from '@backstage/errors'; +import * as inputProps from './inputProperties'; +import * as outputProps from './outputProperties'; + import { GithubCredentialsProvider, ScmIntegrationRegistry, } from '@backstage/integration'; -import { Octokit } from 'octokit'; -import { - createTemplateAction, - parseRepoUrl, -} from '@backstage/plugin-scaffolder-node'; import { createGithubRepoWithCollaboratorsAndTopics, getOctokitOptions, initRepoPushAndProtect, } from './helpers'; -import * as inputProps from './inputProperties'; -import * as outputProps from './outputProperties'; +import { + createTemplateAction, + parseRepoUrl, +} from '@backstage/plugin-scaffolder-node'; + +import { Config } from '@backstage/config'; +import { InputError } from '@backstage/errors'; +import { Octokit } from 'octokit'; import { examples } from './github.examples'; /** @@ -227,6 +229,13 @@ export function createPublishGithubAction(options: { throw new InputError('Invalid repository owner provided in repoUrl'); } + if (ctx.isDryRun) { + ctx.logger.info(`Performing dry run of creating repository`); + ctx.output('remoteUrl', repoUrl); + ctx.logger.info(`Dry run complete`); + return; + } + const newRepo = await createGithubRepoWithCollaboratorsAndTopics( client, repo, From d66ca81a3836786eb646f39ce5d3ce241c83db66 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 17:38:42 +0100 Subject: [PATCH 13/23] fixed outputs for github and githubRepoCreate, added dry run for githubRepoPush Signed-off-by: Tavi Nolan --- .../src/actions/github.test.ts | 18 ++++++++++++++++ .../src/actions/github.ts | 4 +++- .../src/actions/githubRepoCreate.test.ts | 8 +++++++ .../src/actions/githubRepoCreate.ts | 2 +- .../src/actions/githubRepoPush.ts | 21 ++++++++++++++----- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts index 4802fd9078..a346781428 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts @@ -2083,6 +2083,15 @@ describe('publish:github', () => { mockContext.isDryRun = true; await action.handler(mockContext); + expect(mockContext.output).toHaveBeenCalledWith('commitHash', 'commitHash'); + expect(mockContext.output).toHaveBeenCalledWith( + 'remoteUrl', + 'www.example.com', + ); + expect(mockContext.output).toHaveBeenCalledWith( + 'repoContentsUrl', + 'www.example.com/contents', + ); expect(mockOctokit.rest.repos.createInOrg).not.toHaveBeenCalled(); }); @@ -2096,6 +2105,15 @@ describe('publish:github', () => { }); await action.handler(mockContext); + expect(mockContext.output).toHaveBeenCalledWith('commitHash', 'commitHash'); + expect(mockContext.output).toHaveBeenCalledWith( + 'remoteUrl', + 'www.example.com', + ); + expect(mockContext.output).toHaveBeenCalledWith( + 'repoContentsUrl', + 'www.example.com/contents', + ); expect( mockOctokit.rest.repos.createForAuthenticatedUser, ).not.toHaveBeenCalled(); diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.ts b/plugins/scaffolder-backend-module-github/src/actions/github.ts index 923190465a..ab7375daaa 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.ts @@ -231,7 +231,9 @@ export function createPublishGithubAction(options: { if (ctx.isDryRun) { ctx.logger.info(`Performing dry run of creating repository`); - ctx.output('remoteUrl', repoUrl); + ctx.output('commitHash', 'commitHash'); + ctx.output('remoteUrl', 'www.example.com'); + ctx.output('repoContentsUrl', 'www.example.com/contents'); ctx.logger.info(`Dry run complete`); return; } diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts index 3940a71c5d..ebd82f0f06 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts @@ -715,6 +715,10 @@ describe('github:repo:create', () => { mockContext.isDryRun = true; await action.handler(mockContext); + expect(mockContext.output).toHaveBeenCalledWith( + 'remoteUrl', + 'www.example.com', + ); expect(mockOctokit.rest.repos.createInOrg).not.toHaveBeenCalled(); }); @@ -728,6 +732,10 @@ describe('github:repo:create', () => { }); await action.handler(mockContext); + expect(mockContext.output).toHaveBeenCalledWith( + 'remoteUrl', + 'www.example.com', + ); expect( mockOctokit.rest.repos.createForAuthenticatedUser, ).not.toHaveBeenCalled(); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts index 175aae46aa..80edb219df 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts @@ -192,7 +192,7 @@ export function createGithubRepoCreateAction(options: { if (ctx.isDryRun) { ctx.logger.info(`Performing dry run of creating repository`); - ctx.output('remoteUrl', repoUrl); + ctx.output('remoteUrl', 'www.example.com'); ctx.logger.info(`Dry run complete`); return; } diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts index 5b9db78f48..5b10226b3d 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts @@ -14,20 +14,22 @@ * limitations under the License. */ -import { Config } from '@backstage/config'; -import { InputError } from '@backstage/errors'; +import * as inputProps from './inputProperties'; +import * as outputProps from './outputProperties'; + import { GithubCredentialsProvider, ScmIntegrationRegistry, } from '@backstage/integration'; -import { Octokit } from 'octokit'; import { createTemplateAction, parseRepoUrl, } from '@backstage/plugin-scaffolder-node'; import { getOctokitOptions, initRepoPushAndProtect } from './helpers'; -import * as inputProps from './inputProperties'; -import * as outputProps from './outputProperties'; + +import { Config } from '@backstage/config'; +import { InputError } from '@backstage/errors'; +import { Octokit } from 'octokit'; import { examples } from './githubRepoPush.examples'; /** @@ -156,6 +158,15 @@ export function createGithubRepoPushAction(options: { const remoteUrl = targetRepo.data.clone_url; const repoContentsUrl = `${targetRepo.data.html_url}/blob/${defaultBranch}`; + if (ctx.isDryRun) { + ctx.logger.info(`Performing dry run of creating pull request`); + ctx.output('remoteUrl', 'www.example.com'); + ctx.output('repoContentsUrl', 'www.example.com/content'); + ctx.output('commitHash', 'commitHash'); + ctx.logger.info(`Dry run complete`); + return; + } + const { commitHash } = await initRepoPushAndProtect( remoteUrl, octokitOptions.auth, From 088a586cccd6065984f1b6dfb71d6c8401ab37e7 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Mon, 8 Apr 2024 10:13:00 +0100 Subject: [PATCH 14/23] enabling dry run in githubWebhook Signed-off-by: Tavi Nolan --- .../src/actions/githubWebhook.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts index 9b3ccdac07..69988f512e 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts @@ -56,6 +56,7 @@ export function createGithubWebhookAction(options: { id: 'github:webhook', description: 'Creates webhook for a repository on GitHub.', examples, + supportsDryRun: true, schema: { input: { type: 'object', From 425d9d5a43b9a787f9e2c3719bc7234e96b8832d Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 11 Apr 2024 11:11:19 +0100 Subject: [PATCH 15/23] log output change Signed-off-by: Tavi Nolan --- .../src/actions/githubWebhook.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts index 69988f512e..91db9c0534 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts @@ -152,7 +152,7 @@ export function createGithubWebhookAction(options: { // If this is a dry run, log and return if (ctx.isDryRun) { - ctx.logger.info(`Dry run completed`); + ctx.logger.info(`Dry run complete`); return; } From 41f70d21d75c1946bf5936b8e5ad748bdbdd3989 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 11 Apr 2024 11:15:39 +0100 Subject: [PATCH 16/23] reverting changes to github action Signed-off-by: Tavi Nolan --- .../src/actions/github.test.ts | 52 ------------------- .../src/actions/github.ts | 9 ---- 2 files changed, 61 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts index a346781428..73fa7fbcd0 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts @@ -2066,56 +2066,4 @@ describe('publish:github', () => { requiredCommitSigning: false, }); }); - - it('should not call createInOrg during dry run', async () => { - mockOctokit.rest.users.getByUsername.mockResolvedValue({ - data: { type: 'Organization' }, - }); - - mockOctokit.rest.teams.getByName.mockResolvedValue({ - data: { - name: 'blam', - id: 42, - }, - }); - - mockOctokit.rest.repos.createInOrg.mockResolvedValue({ data: {} }); - - mockContext.isDryRun = true; - await action.handler(mockContext); - expect(mockContext.output).toHaveBeenCalledWith('commitHash', 'commitHash'); - expect(mockContext.output).toHaveBeenCalledWith( - 'remoteUrl', - 'www.example.com', - ); - expect(mockContext.output).toHaveBeenCalledWith( - 'repoContentsUrl', - 'www.example.com/contents', - ); - expect(mockOctokit.rest.repos.createInOrg).not.toHaveBeenCalled(); - }); - - it('should not call createForAuthenticatedUser during dry run', async () => { - mockOctokit.rest.users.getByUsername.mockResolvedValue({ - data: { type: 'User' }, - }); - - mockOctokit.rest.repos.createForAuthenticatedUser.mockResolvedValue({ - data: {}, - }); - - await action.handler(mockContext); - expect(mockContext.output).toHaveBeenCalledWith('commitHash', 'commitHash'); - expect(mockContext.output).toHaveBeenCalledWith( - 'remoteUrl', - 'www.example.com', - ); - expect(mockContext.output).toHaveBeenCalledWith( - 'repoContentsUrl', - 'www.example.com/contents', - ); - expect( - mockOctokit.rest.repos.createForAuthenticatedUser, - ).not.toHaveBeenCalled(); - }); }); diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.ts b/plugins/scaffolder-backend-module-github/src/actions/github.ts index ab7375daaa..63284002cc 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.ts @@ -229,15 +229,6 @@ export function createPublishGithubAction(options: { throw new InputError('Invalid repository owner provided in repoUrl'); } - if (ctx.isDryRun) { - ctx.logger.info(`Performing dry run of creating repository`); - ctx.output('commitHash', 'commitHash'); - ctx.output('remoteUrl', 'www.example.com'); - ctx.output('repoContentsUrl', 'www.example.com/contents'); - ctx.logger.info(`Dry run complete`); - return; - } - const newRepo = await createGithubRepoWithCollaboratorsAndTopics( client, repo, From a0a3c055cd7e4e0caec07024182e75d9262da27f Mon Sep 17 00:00:00 2001 From: "Nolan, Tavi" Date: Thu, 11 Apr 2024 11:18:36 +0100 Subject: [PATCH 17/23] Update github.test.ts Signed-off-by: Nolan, Tavi --- .../src/actions/github.test.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts index 73fa7fbcd0..58c43616e4 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts @@ -33,23 +33,22 @@ jest.mock('@backstage/plugin-scaffolder-node', () => { }; }); +import { TemplateAction } from '@backstage/plugin-scaffolder-node'; +import { ConfigReader } from '@backstage/config'; +import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; import { DefaultGithubCredentialsProvider, GithubCredentialsProvider, ScmIntegrations, } from '@backstage/integration'; +import { when } from 'jest-when'; +import { createPublishGithubAction } from './github'; +import { initRepoAndPush } from '@backstage/plugin-scaffolder-node'; import { enableBranchProtectionOnDefaultRepoBranch, entityRefToName, } from './gitHelpers'; -import { ConfigReader } from '@backstage/config'; -import { TemplateAction } from '@backstage/plugin-scaffolder-node'; -import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; -import { createPublishGithubAction } from './github'; -import { initRepoAndPush } from '@backstage/plugin-scaffolder-node'; -import { when } from 'jest-when'; - const publicKey = '2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU='; const initRepoAndPushMocked = initRepoAndPush as jest.Mock< From c465caddc59a7aa4c6bf181151c99f0d4f3e5d90 Mon Sep 17 00:00:00 2001 From: "Nolan, Tavi" Date: Thu, 11 Apr 2024 11:18:59 +0100 Subject: [PATCH 18/23] Update github.ts Signed-off-by: Nolan, Tavi --- .../src/actions/github.ts | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.ts b/plugins/scaffolder-backend-module-github/src/actions/github.ts index 63284002cc..f32b65bc9a 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.ts @@ -14,26 +14,24 @@ * limitations under the License. */ -import * as inputProps from './inputProperties'; -import * as outputProps from './outputProperties'; - +import { Config } from '@backstage/config'; +import { InputError } from '@backstage/errors'; import { GithubCredentialsProvider, ScmIntegrationRegistry, } from '@backstage/integration'; +import { Octokit } from 'octokit'; +import { + createTemplateAction, + parseRepoUrl, +} from '@backstage/plugin-scaffolder-node'; import { createGithubRepoWithCollaboratorsAndTopics, getOctokitOptions, initRepoPushAndProtect, } from './helpers'; -import { - createTemplateAction, - parseRepoUrl, -} from '@backstage/plugin-scaffolder-node'; - -import { Config } from '@backstage/config'; -import { InputError } from '@backstage/errors'; -import { Octokit } from 'octokit'; +import * as inputProps from './inputProperties'; +import * as outputProps from './outputProperties'; import { examples } from './github.examples'; /** From 565f2a5eba3c42b44fd0dd74617b1af19dd337ad Mon Sep 17 00:00:00 2001 From: "Nolan, Tavi" Date: Thu, 11 Apr 2024 11:19:32 +0100 Subject: [PATCH 19/23] Update githubRepoCreate.test.ts Signed-off-by: Nolan, Tavi --- .../src/actions/githubRepoCreate.test.ts | 62 +++---------------- 1 file changed, 10 insertions(+), 52 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts index ebd82f0f06..df2dd29eb8 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts @@ -14,18 +14,8 @@ * limitations under the License. */ -import { - DefaultGithubCredentialsProvider, - GithubCredentialsProvider, - ScmIntegrations, -} from '@backstage/integration'; - -import { ConfigReader } from '@backstage/config'; import { TemplateAction } from '@backstage/plugin-scaffolder-node'; -import { createGithubRepoCreateAction } from './githubRepoCreate'; import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; -import { entityRefToName } from './gitHelpers'; -import { when } from 'jest-when'; jest.mock('./gitHelpers', () => { return { @@ -34,6 +24,16 @@ jest.mock('./gitHelpers', () => { }; }); +import { ConfigReader } from '@backstage/config'; +import { + DefaultGithubCredentialsProvider, + GithubCredentialsProvider, + ScmIntegrations, +} from '@backstage/integration'; +import { when } from 'jest-when'; +import { createGithubRepoCreateAction } from './githubRepoCreate'; +import { entityRefToName } from './gitHelpers'; + const publicKey = '2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU='; const mockOctokit = { @@ -698,46 +698,4 @@ describe('github:repo:create', () => { 'https://github.com/clone/url.git', ); }); - - it('should not call createInOrg during dry run', async () => { - mockOctokit.rest.users.getByUsername.mockResolvedValue({ - data: { type: 'Organization' }, - }); - - mockOctokit.rest.teams.getByName.mockResolvedValue({ - data: { - name: 'blam', - id: 42, - }, - }); - - mockOctokit.rest.repos.createInOrg.mockResolvedValue({ data: {} }); - - mockContext.isDryRun = true; - await action.handler(mockContext); - expect(mockContext.output).toHaveBeenCalledWith( - 'remoteUrl', - 'www.example.com', - ); - expect(mockOctokit.rest.repos.createInOrg).not.toHaveBeenCalled(); - }); - - it('should not call createForAuthenticatedUser during dry run', async () => { - mockOctokit.rest.users.getByUsername.mockResolvedValue({ - data: { type: 'User' }, - }); - - mockOctokit.rest.repos.createForAuthenticatedUser.mockResolvedValue({ - data: {}, - }); - - await action.handler(mockContext); - expect(mockContext.output).toHaveBeenCalledWith( - 'remoteUrl', - 'www.example.com', - ); - expect( - mockOctokit.rest.repos.createForAuthenticatedUser, - ).not.toHaveBeenCalled(); - }); }); From e381232758f330fdb7b3af2735f73f5d08332f42 Mon Sep 17 00:00:00 2001 From: "Nolan, Tavi" Date: Thu, 11 Apr 2024 11:19:51 +0100 Subject: [PATCH 20/23] Update githubRepoCreate.ts Signed-off-by: Nolan, Tavi --- .../src/actions/githubRepoCreate.ts | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts index 80edb219df..52bf9be9c0 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts @@ -14,24 +14,22 @@ * limitations under the License. */ -import * as inputProps from './inputProperties'; -import * as outputProps from './outputProperties'; - +import { InputError } from '@backstage/errors'; import { GithubCredentialsProvider, ScmIntegrationRegistry, } from '@backstage/integration'; -import { - createGithubRepoWithCollaboratorsAndTopics, - getOctokitOptions, -} from './helpers'; +import { Octokit } from 'octokit'; import { createTemplateAction, parseRepoUrl, } from '@backstage/plugin-scaffolder-node'; - -import { InputError } from '@backstage/errors'; -import { Octokit } from 'octokit'; +import { + createGithubRepoWithCollaboratorsAndTopics, + getOctokitOptions, +} from './helpers'; +import * as inputProps from './inputProperties'; +import * as outputProps from './outputProperties'; import { examples } from './githubRepoCreate.examples'; /** @@ -190,13 +188,6 @@ export function createGithubRepoCreateAction(options: { throw new InputError('Invalid repository owner provided in repoUrl'); } - if (ctx.isDryRun) { - ctx.logger.info(`Performing dry run of creating repository`); - ctx.output('remoteUrl', 'www.example.com'); - ctx.logger.info(`Dry run complete`); - return; - } - const newRepo = await createGithubRepoWithCollaboratorsAndTopics( client, repo, From d1428a0d36a2238c0ee35c01f10c4f90fb2330b6 Mon Sep 17 00:00:00 2001 From: "Nolan, Tavi" Date: Thu, 11 Apr 2024 11:20:11 +0100 Subject: [PATCH 21/23] Update githubRepoPush.ts Signed-off-by: Nolan, Tavi --- .../src/actions/githubRepoPush.ts | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts index 5b10226b3d..5b9db78f48 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts @@ -14,22 +14,20 @@ * limitations under the License. */ -import * as inputProps from './inputProperties'; -import * as outputProps from './outputProperties'; - +import { Config } from '@backstage/config'; +import { InputError } from '@backstage/errors'; import { GithubCredentialsProvider, ScmIntegrationRegistry, } from '@backstage/integration'; +import { Octokit } from 'octokit'; import { createTemplateAction, parseRepoUrl, } from '@backstage/plugin-scaffolder-node'; import { getOctokitOptions, initRepoPushAndProtect } from './helpers'; - -import { Config } from '@backstage/config'; -import { InputError } from '@backstage/errors'; -import { Octokit } from 'octokit'; +import * as inputProps from './inputProperties'; +import * as outputProps from './outputProperties'; import { examples } from './githubRepoPush.examples'; /** @@ -158,15 +156,6 @@ export function createGithubRepoPushAction(options: { const remoteUrl = targetRepo.data.clone_url; const repoContentsUrl = `${targetRepo.data.html_url}/blob/${defaultBranch}`; - if (ctx.isDryRun) { - ctx.logger.info(`Performing dry run of creating pull request`); - ctx.output('remoteUrl', 'www.example.com'); - ctx.output('repoContentsUrl', 'www.example.com/content'); - ctx.output('commitHash', 'commitHash'); - ctx.logger.info(`Dry run complete`); - return; - } - const { commitHash } = await initRepoPushAndProtect( remoteUrl, octokitOptions.auth, From ed30d2cb7a4744b532630d5005f05cfe23d74d8b Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Wed, 17 Apr 2024 15:38:21 +0100 Subject: [PATCH 22/23] Fixed imports Signed-off-by: Tavi Nolan --- .../src/actions/githubPullRequest.ts | 16 ++++++++-------- .../src/actions/githubWebhook.ts | 7 +++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts index 99e442f74a..de70f56399 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -14,25 +14,24 @@ * limitations under the License. */ -import { CustomErrorBase, InputError } from '@backstage/errors'; +import path from 'path'; import { GithubCredentialsProvider, ScmIntegrationRegistry, } from '@backstage/integration'; import { - SerializedFile, createTemplateAction, parseRepoUrl, + SerializedFile, serializeDirectoryContents, } from '@backstage/plugin-scaffolder-node'; - -import { Logger } from 'winston'; import { Octokit } from 'octokit'; -import { createPullRequest } from 'octokit-plugin-create-pull-request'; -import { examples } from './githubPullRequest.examples'; -import { getOctokitOptions } from './helpers'; -import path from 'path'; +import { CustomErrorBase, InputError } from '@backstage/errors'; import { resolveSafeChildPath } from '@backstage/backend-common'; +import { createPullRequest } from 'octokit-plugin-create-pull-request'; +import { getOctokitOptions } from './helpers'; +import { examples } from './githubPullRequest.examples'; +import { LoggerService } from '@backstage/backend-plugin-api'; export type Encoding = 'utf-8' | 'base64'; @@ -321,6 +320,7 @@ export const createPublishGithubPullRequestAction = ( ]), ); + // If this is a dry run, log and return if (ctx.isDryRun) { ctx.logger.info(`Performing dry run of creating pull request`); ctx.output('targetBranchName', branchName); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts index 91db9c0534..e188cdf0cb 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts @@ -18,16 +18,15 @@ import { GithubCredentialsProvider, ScmIntegrationRegistry, } from '@backstage/integration'; -import { InputError, assertError } from '@backstage/errors'; import { createTemplateAction, parseRepoUrl, } from '@backstage/plugin-scaffolder-node'; - -import { Octokit } from 'octokit'; import { emitterEventNames } from '@octokit/webhooks'; -import { examples } from './githubWebhook.examples'; +import { assertError, InputError } from '@backstage/errors'; +import { Octokit } from 'octokit'; import { getOctokitOptions } from './helpers'; +import { examples } from './githubWebhook.examples'; /** * Creates new action that creates a webhook for a repository on GitHub. From f145a0458b8030c98e4b896c1d6a274eaec1287d Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Wed, 17 Apr 2024 15:48:51 +0100 Subject: [PATCH 23/23] Fixed test imports, added changeset Signed-off-by: Tavi Nolan --- .changeset/cyan-paws-beg.md | 5 +++++ .../src/actions/github.test.ts | 1 - .../src/actions/githubPullRequest.test.ts | 19 +++++++++---------- .../src/actions/githubPullRequest.ts | 2 +- .../src/actions/githubRepoCreate.test.ts | 1 - .../src/actions/githubWebhook.test.ts | 7 +++---- 6 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 .changeset/cyan-paws-beg.md diff --git a/.changeset/cyan-paws-beg.md b/.changeset/cyan-paws-beg.md new file mode 100644 index 0000000000..87c2b746a4 --- /dev/null +++ b/.changeset/cyan-paws-beg.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend-module-github': patch +--- + +Added handling for dry run to githubPullRequest and githubWebhook and added tests for this functionality diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts index 58c43616e4..9eda9a2eb0 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts @@ -41,7 +41,6 @@ import { GithubCredentialsProvider, ScmIntegrations, } from '@backstage/integration'; -import { when } from 'jest-when'; import { createPublishGithubAction } from './github'; import { initRepoAndPush } from '@backstage/plugin-scaffolder-node'; import { diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts index 150ed39e50..613ebc7c59 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts @@ -14,21 +14,20 @@ * limitations under the License. */ -import { - ActionContext, - TemplateAction, -} from '@backstage/plugin-scaffolder-node'; +import { createRootLogger } from '@backstage/backend-common'; +import { ConfigReader } from '@backstage/config'; import { GithubCredentialsProvider, ScmIntegrations, } from '@backstage/integration'; - -import { ConfigReader } from '@backstage/config'; -import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; -import { createMockDirectory } from '@backstage/backend-test-utils'; -import { createPublishGithubPullRequestAction } from './githubPullRequest'; -import { createRootLogger } from '@backstage/backend-common'; +import { + ActionContext, + TemplateAction, +} from '@backstage/plugin-scaffolder-node'; import fs from 'fs-extra'; +import { createPublishGithubPullRequestAction } from './githubPullRequest'; +import { createMockDirectory } from '@backstage/backend-test-utils'; +import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; // Make sure root logger is initialized ahead of FS mock createRootLogger(); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts index de70f56399..ce199d5f56 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -26,7 +26,7 @@ import { serializeDirectoryContents, } from '@backstage/plugin-scaffolder-node'; import { Octokit } from 'octokit'; -import { CustomErrorBase, InputError } from '@backstage/errors'; +import { InputError, CustomErrorBase } from '@backstage/errors'; import { resolveSafeChildPath } from '@backstage/backend-common'; import { createPullRequest } from 'octokit-plugin-create-pull-request'; import { getOctokitOptions } from './helpers'; diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts index df2dd29eb8..1750acdb9a 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts @@ -30,7 +30,6 @@ import { GithubCredentialsProvider, ScmIntegrations, } from '@backstage/integration'; -import { when } from 'jest-when'; import { createGithubRepoCreateAction } from './githubRepoCreate'; import { entityRefToName } from './gitHelpers'; 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 6c0335c509..30bdcef20d 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts @@ -14,16 +14,15 @@ * limitations under the License. */ +import { createGithubWebhookAction } from './githubWebhook'; import { + ScmIntegrations, DefaultGithubCredentialsProvider, GithubCredentialsProvider, - ScmIntegrations, } from '@backstage/integration'; - +import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; import { ConfigReader } from '@backstage/config'; import { TemplateAction } from '@backstage/plugin-scaffolder-node'; -import { createGithubWebhookAction } from './githubWebhook'; -import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; const mockOctokit = { rest: {