Add examples for publish:bitbucket scaffolder actions

Signed-off-by: subash <subashraina54@gmail.com>
This commit is contained in:
subash
2023-08-25 10:13:25 +05:30
parent 28e9e62e22
commit ded27b83ea
4 changed files with 572 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Add examples for `publish:bitbucket` scaffolder actions.
@@ -0,0 +1,369 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
jest.mock('../helpers', () => {
return {
initRepoAndPush: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
commitAndPushRepo: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
};
});
import { createPublishBitbucketAction } from './bitbucket';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { setupRequestMockHandlers } from '@backstage/backend-test-utils';
import { ScmIntegrations } from '@backstage/integration';
import { ConfigReader } from '@backstage/config';
import { getVoidLogger } from '@backstage/backend-common';
import { PassThrough } from 'stream';
import { initRepoAndPush } from '../helpers';
import yaml from 'yaml';
import { examples } from './bitbucket.examples';
describe('publish:bitbucket', () => {
const config = new ConfigReader({
integrations: {
bitbucket: [
{
host: 'bitbucket.org',
token: 'tokenlols',
},
{
host: 'hosted.bitbucket.com',
token: 'thing',
apiBaseUrl: 'https://hosted.bitbucket.com/rest/api/1.0',
},
{
host: 'notoken.bitbucket.com',
},
],
},
});
const integrations = ScmIntegrations.fromConfig(config);
const action = createPublishBitbucketAction({ integrations, config });
const mockContext = {
input: {
repoUrl: 'bitbucket.org?workspace=workspace&project=project&repo=repo',
repoVisibility: 'private' as const,
},
workspacePath: 'lol',
logger: getVoidLogger(),
logStream: new PassThrough(),
output: jest.fn(),
createTemporaryDirectory: jest.fn(),
};
const server = setupServer();
setupRequestMockHandlers(server);
beforeEach(() => {
jest.resetAllMocks();
});
it('should call initAndPush with the correct values', async () => {
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/workspace/repo',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json({
links: {
html: {
href: 'https://bitbucket.org/workspace/repo',
},
clone: [
{
name: 'https',
href: 'https://bitbucket.org/workspace/cloneurl',
},
],
},
}),
),
),
);
await action.handler({
...mockContext,
input: yaml.parse(examples[0].example).steps[0].input,
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContext.workspacePath,
remoteUrl: 'https://bitbucket.org/workspace/cloneurl',
defaultBranch: 'master',
auth: { username: 'x-token-auth', password: 'tokenlols' },
logger: mockContext.logger,
commitMessage: 'initial commit',
gitAuthorInfo: {},
});
});
it('should call initAndPush with the correct default branch', async () => {
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/workspace/repo',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json({
links: {
html: {
href: 'https://bitbucket.org/workspace/repo',
},
clone: [
{
name: 'https',
href: 'https://bitbucket.org/workspace/cloneurl',
},
],
},
}),
),
),
);
await action.handler({
...mockContext,
input: yaml.parse(examples[3].example).steps[0].input,
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContext.workspacePath,
remoteUrl: 'https://bitbucket.org/workspace/cloneurl',
defaultBranch: 'main',
auth: { username: 'x-token-auth', password: 'tokenlols' },
logger: mockContext.logger,
commitMessage: 'initial commit',
gitAuthorInfo: {},
});
});
it('should call initAndPush with the specified source path', async () => {
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/workspace/repo',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json({
links: {
html: {
href: 'https://bitbucket.org/workspace/repo',
},
clone: [
{
name: 'https',
href: 'https://bitbucket.org/workspace/cloneurl',
},
],
},
}),
),
),
);
await action.handler({
...mockContext,
input: yaml.parse(examples[4].example).steps[0].input,
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: `${mockContext.workspacePath}/repoRoot`,
remoteUrl: 'https://bitbucket.org/workspace/cloneurl',
auth: { username: 'x-token-auth', password: 'tokenlols' },
logger: mockContext.logger,
defaultBranch: 'main',
commitMessage: 'initial commit',
gitAuthorInfo: {},
});
});
it('should call initAndPush with the authentication token', async () => {
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/workspace/repo',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json({
links: {
html: {
href: 'https://bitbucket.org/workspace/repo',
},
clone: [
{
name: 'https',
href: 'https://bitbucket.org/workspace/cloneurl',
},
],
},
}),
),
),
);
await action.handler({
...mockContext,
input: yaml.parse(examples[6].example).steps[0].input,
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContext.workspacePath,
remoteUrl: 'https://bitbucket.org/workspace/cloneurl',
auth: { username: 'x-token-auth', password: 'your-auth-token' },
logger: mockContext.logger,
defaultBranch: 'main',
commitMessage: 'initial commit',
gitAuthorInfo: {},
});
});
it('should call initAndPush with the custom commit message', async () => {
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/workspace/repo',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json({
links: {
html: {
href: 'https://bitbucket.org/workspace/repo',
},
clone: [
{
name: 'https',
href: 'https://bitbucket.org/workspace/cloneurl',
},
],
},
}),
),
),
);
await action.handler({
...mockContext,
input: yaml.parse(examples[7].example).steps[0].input,
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContext.workspacePath,
remoteUrl: 'https://bitbucket.org/workspace/cloneurl',
auth: { username: 'x-token-auth', password: 'your-auth-token' },
logger: mockContext.logger,
defaultBranch: 'main',
commitMessage: 'Initial commit with custom message',
gitAuthorInfo: { email: undefined, name: undefined },
});
});
it('should call initAndPush with the custom author name and email for the commit.', async () => {
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/workspace/repo',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json({
links: {
html: {
href: 'https://bitbucket.org/workspace/repo',
},
clone: [
{
name: 'https',
href: 'https://bitbucket.org/workspace/cloneurl',
},
],
},
}),
),
),
);
await action.handler({
...mockContext,
input: yaml.parse(examples[8].example).steps[0].input,
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContext.workspacePath,
remoteUrl: 'https://bitbucket.org/workspace/cloneurl',
auth: { username: 'x-token-auth', password: 'your-auth-token' },
logger: mockContext.logger,
defaultBranch: 'main',
commitMessage: 'Initial commit with custom message',
gitAuthorInfo: { email: 'your.email@example.com', name: 'Your Name' },
});
});
describe('LFS for hosted bitbucket', () => {
const repoCreationResponse = {
links: {
self: [
{
href: 'https://bitbucket.mycompany.com/projects/project/repos/repo',
},
],
clone: [
{
name: 'http',
href: 'https://bitbucket.mycompany.com/scm/project/repo',
},
],
},
};
it('should call the correct APIs to enable LFS if requested and the host is hosted bitbucket', async () => {
expect.assertions(1);
server.use(
rest.post(
'https://hosted.bitbucket.com/rest/api/1.0/projects/project/repos',
(_, res, ctx) => {
return res(
ctx.status(201),
ctx.set('Content-Type', 'application/json'),
ctx.json(repoCreationResponse),
);
},
),
rest.put(
'https://hosted.bitbucket.com/rest/git-lfs/admin/projects/project/repos/repo/enabled',
(req, res, ctx) => {
expect(req.headers.get('Authorization')).toBe('Bearer thing');
return res(ctx.status(204));
},
),
);
await action.handler({
...mockContext,
input: yaml.parse(examples[5].example).steps[0].input,
});
});
});
});
@@ -0,0 +1,196 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TemplateExample } from '@backstage/plugin-scaffolder-node';
import yaml from 'yaml';
export const examples: TemplateExample[] = [
{
description:
'Initializes a git repository of contents in workspace and publish it to Bitbucket with default configuration.',
example: yaml.stringify({
steps: [
{
id: 'publish',
action: 'publish:bitbucket',
name: 'Publish to Bitbucket',
input: {
repoUrl:
'bitbucket.org?repo=repo&workspace=workspace&project=project',
},
},
],
}),
},
{
description: 'Add a description.',
example: yaml.stringify({
steps: [
{
id: 'publish',
action: 'publish:bitbucket',
name: 'Publish to Bitbucket',
input: {
repoUrl:
'bitbucket.org?repo=repo&workspace=workspace&project=project',
description: 'Initialize a git repository',
},
},
],
}),
},
{
description: 'Change visibility of the repository.',
example: yaml.stringify({
steps: [
{
id: 'publish',
action: 'publish:bitbucket',
name: 'Publish to Bitbucket',
input: {
repoUrl:
'bitbucket.org?repo=repo&workspace=workspace&project=project',
description: 'Initialize a git repository',
repoVisibility: 'public',
},
},
],
}),
},
{
description: 'Set the default branch.',
example: yaml.stringify({
steps: [
{
id: 'publish',
action: 'publish:bitbucket',
name: 'Publish to Bitbucket',
input: {
repoUrl:
'bitbucket.org?repo=repo&workspace=workspace&project=project',
description: 'Initialize a git repository',
repoVisibility: 'public',
defaultBranch: 'main',
},
},
],
}),
},
{
description: 'Specify a source path within the workspace.',
example: yaml.stringify({
steps: [
{
id: 'publish',
action: 'publish:bitbucket',
name: 'Publish to Bitbucket',
input: {
repoUrl:
'bitbucket.org?repo=repo&workspace=workspace&project=project',
description: 'Initialize a git repository',
repoVisibility: 'public',
defaultBranch: 'main',
sourcePath: './repoRoot',
},
},
],
}),
},
{
description: 'Enable LFS for the repository.',
example: yaml.stringify({
steps: [
{
id: 'publish',
action: 'publish:bitbucket',
name: 'Publish to Bitbucket',
input: {
repoUrl: 'hosted.bitbucket.com?project=project&repo=repo',
description: 'Initialize a git repository',
repoVisibility: 'public',
defaultBranch: 'main',
sourcePath: './repoRoot',
enableLFS: true,
},
},
],
}),
},
{
description: 'Provide an authentication token for Bitbucket.',
example: yaml.stringify({
steps: [
{
id: 'publish',
action: 'publish:bitbucket',
name: 'Publish to Bitbucket',
input: {
repoUrl:
'bitbucket.org?repo=repo&workspace=workspace&project=project',
description: 'Initialize a git repository',
repoVisibility: 'public',
defaultBranch: 'main',
token: 'your-auth-token',
},
},
],
}),
},
{
description: 'Set a custom commit message.',
example: yaml.stringify({
steps: [
{
id: 'publish',
action: 'publish:bitbucket',
name: 'Publish to Bitbucket',
input: {
repoUrl:
'bitbucket.org?repo=repo&workspace=workspace&project=project',
description: 'Initialize a git repository',
repoVisibility: 'public',
defaultBranch: 'main',
token: 'your-auth-token',
gitCommitMessage: 'Initial commit with custom message',
},
},
],
}),
},
{
description: 'Set a custom author name and email for the commit.',
example: yaml.stringify({
steps: [
{
id: 'publish',
action: 'publish:bitbucket',
name: 'Publish to Bitbucket',
input: {
repoUrl:
'bitbucket.org?repo=repo&workspace=workspace&project=project',
description: 'Initialize a git repository',
repoVisibility: 'public',
defaultBranch: 'main',
token: 'your-auth-token',
gitCommitMessage: 'Initial commit with custom message',
gitAuthorName: 'Your Name',
gitAuthorEmail: 'your.email@example.com',
},
},
],
}),
},
];
@@ -24,6 +24,7 @@ import fetch, { Response, RequestInit } from 'node-fetch';
import { initRepoAndPush } from '../helpers';
import { getRepoSourceDirectory, parseRepoUrl } from './util';
import { Config } from '@backstage/config';
import { examples } from './bitbucket.examples';
const createBitbucketCloudRepository = async (opts: {
workspace: string;
@@ -221,6 +222,7 @@ export function createPublishBitbucketAction(options: {
id: 'publish:bitbucket',
description:
'Initializes a git repository of the content in the workspace, and publishes it to Bitbucket.',
examples,
schema: {
input: {
type: 'object',