From 07a6d7f153ced344978a0eff2db35332f5c6c270 Mon Sep 17 00:00:00 2001 From: Tavi Nolan Date: Thu, 4 Apr 2024 17:10:52 +0100 Subject: [PATCH] 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,