initial working changes for githubRepoCreate

Signed-off-by: Tavi Nolan <Tavi.Nolan@fmr.com>
This commit is contained in:
Tavi Nolan
2024-04-04 17:10:52 +01:00
parent 6dcd72ae3e
commit 07a6d7f153
2 changed files with 75 additions and 17 deletions
@@ -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();
});
});
@@ -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,