added dry run to github.ts

Signed-off-by: Tavi Nolan <Tavi.Nolan@fmr.com>
This commit is contained in:
Tavi Nolan
2024-04-04 17:25:57 +01:00
parent 1d0a7ce72a
commit 7c2c8e4961
2 changed files with 59 additions and 14 deletions
@@ -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();
});
});
@@ -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,