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/githubPullRequest.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts index 20508494b8..485f7dec14 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts @@ -231,6 +231,18 @@ describe('createPublishGithubPullRequestAction', () => { ); expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123); }); + + it('sets correct outputs during dry run', 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 5efcc6e0bb..213d24eca7 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -149,6 +149,7 @@ export const createPublishGithubPullRequestAction = ( }>({ id: 'publish:github:pull-request', examples, + supportsDryRun: true, schema: { input: { required: ['repoUrl', 'title', 'description', 'branchName'], @@ -343,6 +344,16 @@ 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); + ctx.output('remoteUrl', repoUrl); + ctx.output('pullRequestNumber', 43); + ctx.logger.info(`Dry run complete`); + return; + } + try { const createOptions: createPullRequest.Options = { owner, 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..30bdcef20d 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts @@ -221,6 +221,29 @@ describe('github:repository:webhook:create', () => { }); }); + 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'; + + // Create the context object with the necessary properties for a dry run + const ctx = { + ...mockContext, + isDryRun: true, + input: { + ...mockContext.input, + repoUrl, + webhookUrl, + }, + }; + + // 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 () => { 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 984029076e..e188cdf0cb 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts @@ -55,6 +55,7 @@ export function createGithubWebhookAction(options: { id: 'github:webhook', description: 'Creates webhook for a repository on GitHub.', examples, + supportsDryRun: true, schema: { input: { type: 'object', @@ -148,6 +149,12 @@ export function createGithubWebhookAction(options: { }), ); + // If this is a dry run, log and return + if (ctx.isDryRun) { + ctx.logger.info(`Dry run complete`); + return; + } + try { const insecure_ssl = insecureSsl ? '1' : '0'; await client.rest.repos.createWebhook({