refactor: DRY on input and output properties

Signed-off-by: Marco Crivellaro <marco.crive@gmail.com>
This commit is contained in:
Marco Crivellaro
2022-06-21 14:23:08 +01:00
parent 4e8787ac57
commit e150231eb9
5 changed files with 248 additions and 331 deletions
@@ -22,6 +22,8 @@ import { Octokit } from 'octokit';
import { createTemplateAction } from '../../createTemplateAction';
import { parseRepoUrl } from '../publish/util';
import { getOctokitOptions } from './helpers';
import * as inputProps from './inputProperties';
import * as outputProps from './outputProperties';
/**
* Creates a new action that initializes a git repository
@@ -67,123 +69,26 @@ export function createGithubRepoCreateAction(options: {
type: 'object',
required: ['repoUrl'],
properties: {
repoUrl: {
title: 'Repository Location',
description: `Accepts the format 'github.com?repo=reponame&owner=owner' where 'reponame' is the new repository name and 'owner' is an organization or username`,
type: 'string',
},
description: {
title: 'Repository Description',
type: 'string',
},
access: {
title: 'Repository Access',
description: `Sets an admin collaborator on the repository. Can either be a user reference different from 'owner' in 'repoUrl' or team reference, eg. 'org/team-name'`,
type: 'string',
},
requireCodeOwnerReviews: {
title: 'Require CODEOWNER Reviews?',
description:
'Require an approved review in PR including files with a designated Code Owner',
type: 'boolean',
},
requiredStatusCheckContexts: {
title: 'Required Status Check Contexts',
description:
'The list of status checks to require in order to merge into this branch',
type: 'array',
items: {
type: 'string',
},
},
repoVisibility: {
title: 'Repository Visibility',
type: 'string',
enum: ['private', 'public', 'internal'],
},
deleteBranchOnMerge: {
title: 'Delete Branch On Merge',
type: 'boolean',
description: `Delete the branch after merging the PR. The default value is 'false'`,
},
gitAuthorName: {
title: 'Default Author Name',
type: 'string',
description: `Sets the default author name for the commit. The default value is 'Scaffolder'`,
},
gitAuthorEmail: {
title: 'Default Author Email',
type: 'string',
description: `Sets the default author email for the commit.`,
},
allowMergeCommit: {
title: 'Allow Merge Commits',
type: 'boolean',
description: `Allow merge commits. The default value is 'true'`,
},
allowSquashMerge: {
title: 'Allow Squash Merges',
type: 'boolean',
description: `Allow squash merges. The default value is 'true'`,
},
allowRebaseMerge: {
title: 'Allow Rebase Merges',
type: 'boolean',
description: `Allow rebase merges. The default value is 'true'`,
},
collaborators: {
title: 'Collaborators',
description: 'Provide additional users or teams with permissions',
type: 'array',
items: {
type: 'object',
additionalProperties: false,
required: ['access'],
properties: {
access: {
type: 'string',
description: 'The type of access for the user',
enum: ['push', 'pull', 'admin', 'maintain', 'triage'],
},
user: {
type: 'string',
description:
'The name of the user that will be added as a collaborator',
},
team: {
type: 'string',
description:
'The name of the team that will be added as a collaborator',
},
},
oneOf: [{ required: ['user'] }, { required: ['team'] }],
},
},
token: {
title: 'Authentication Token',
type: 'string',
description: 'The token to use for authorization to GitHub',
},
topics: {
title: 'Topics',
type: 'array',
items: {
type: 'string',
},
},
repoUrl: inputProps.repoUrl,
description: inputProps.description,
access: inputProps.access,
requireCodeOwnerReviews: inputProps.requireCodeOwnerReviews,
requiredStatusCheckContexts: inputProps.requiredStatusCheckContexts,
repoVisibility: inputProps.repoVisibility,
deleteBranchOnMerge: inputProps.deleteBranchOnMerge,
allowMergeCommit: inputProps.allowMergeCommit,
allowSquashMerge: inputProps.allowSquashMerge,
allowRebaseMerge: inputProps.allowRebaseMerge,
collaborators: inputProps.collaborators,
token: inputProps.token,
topics: inputProps.topics,
},
},
output: {
type: 'object',
properties: {
remoteUrl: {
title: 'A URL to the repository with the provider',
type: 'string',
},
repoContentsUrl: {
title: 'A URL to the root of the repository',
type: 'string',
},
remoteUrl: outputProps.remoteUrl,
repoContentsUrl: outputProps.repoContentsUrl,
},
},
},
@@ -212,7 +117,7 @@ export function createGithubRepoCreateAction(options: {
integrations,
credentialsProvider: githubCredentialsProvider,
token: providedToken,
repoUrl,
repoUrl: repoUrl,
});
const client = new Octokit(octokitOptions);
@@ -27,6 +27,8 @@ import {
} from '../helpers';
import { getRepoSourceDirectory, parseRepoUrl } from '../publish/util';
import { getOctokitOptions } from './helpers';
import * as inputProps from './inputProperties';
import * as outputProps from './outputProperties';
/**
* Creates a new action that initializes a git repository of the content in the workspace
@@ -62,82 +64,23 @@ export function createGithubRepoPushAction(options: {
type: 'object',
required: ['repoUrl'],
properties: {
repoUrl: {
title: 'Repository Location',
description: `Accepts the format 'github.com?repo=reponame&owner=owner' where 'reponame' is the new repository name and 'owner' is an organization or username`,
type: 'string',
},
requireCodeOwnerReviews: {
title: 'Require CODEOWNER Reviews?',
description:
'Require an approved review in PR including files with a designated Code Owner',
type: 'boolean',
},
requiredStatusCheckContexts: {
title: 'Required Status Check Contexts',
description:
'The list of status checks to require in order to merge into this branch',
type: 'array',
items: {
type: 'string',
},
},
defaultBranch: {
title: 'Default Branch',
type: 'string',
description: `Sets the default branch on the repository. The default value is 'master'`,
},
protectDefaultBranch: {
title: 'Protect Default Branch',
type: 'boolean',
description: `Protect the default branch after creating the repository. The default value is 'true'`,
},
gitCommitMessage: {
title: 'Git Commit Message',
type: 'string',
description: `Sets the commit message on the repository. The default value is 'initial commit'`,
},
gitAuthorName: {
title: 'Default Author Name',
type: 'string',
description: `Sets the default author name for the commit. The default value is 'Scaffolder'`,
},
gitAuthorEmail: {
title: 'Default Author Email',
type: 'string',
description: `Sets the default author email for the commit.`,
},
sourcePath: {
title: 'Source Path',
description:
'Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the repository.',
type: 'string',
},
token: {
title: 'Authentication Token',
type: 'string',
description: 'The token to use for authorization to GitHub',
},
topics: {
title: 'Topics',
type: 'array',
items: {
type: 'string',
},
},
repoUrl: inputProps.repoUrl,
requireCodeOwnerReviews: inputProps.requireCodeOwnerReviews,
requiredStatusCheckContexts: inputProps.requiredStatusCheckContexts,
defaultBranch: inputProps.defaultBranch,
protectDefaultBranch: inputProps.protectDefaultBranch,
gitCommitMessage: inputProps.gitCommitMessage,
gitAuthorName: inputProps.gitAuthorName,
gitAuthorEmail: inputProps.gitAuthorEmail,
sourcePath: inputProps.sourcePath,
token: inputProps.token,
},
},
output: {
type: 'object',
properties: {
remoteUrl: {
title: 'A URL to the repository with the provider',
type: 'string',
},
repoContentsUrl: {
title: 'A URL to the root of the repository',
type: 'string',
},
remoteUrl: outputProps.remoteUrl,
repoContentsUrl: outputProps.repoContentsUrl,
},
},
},
@@ -0,0 +1,161 @@
/*
* Copyright 2021 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.
*/
const repoUrl = {
title: 'Repository Location',
description: `Accepts the format 'github.com?repo=reponame&owner=owner' where 'reponame' is the new repository name and 'owner' is an organization or username`,
type: 'string',
};
const description = {
title: 'Repository Description',
type: 'string',
};
const access = {
title: 'Repository Access',
description: `Sets an admin collaborator on the repository. Can either be a user reference different from 'owner' in 'repoUrl' or team reference, eg. 'org/team-name'`,
type: 'string',
};
const requireCodeOwnerReviews = {
title: 'Require CODEOWNER Reviews?',
description:
'Require an approved review in PR including files with a designated Code Owner',
type: 'boolean',
};
const requiredStatusCheckContexts = {
title: 'Required Status Check Contexts',
description:
'The list of status checks to require in order to merge into this branch',
type: 'array',
items: {
type: 'string',
},
};
const repoVisibility = {
title: 'Repository Visibility',
type: 'string',
enum: ['private', 'public', 'internal'],
};
const deleteBranchOnMerge = {
title: 'Delete Branch On Merge',
type: 'boolean',
description: `Delete the branch after merging the PR. The default value is 'false'`,
};
const gitAuthorName = {
title: 'Default Author Name',
type: 'string',
description: `Sets the default author name for the commit. The default value is 'Scaffolder'`,
};
const gitAuthorEmail = {
title: 'Default Author Email',
type: 'string',
description: `Sets the default author email for the commit.`,
};
const allowMergeCommit = {
title: 'Allow Merge Commits',
type: 'boolean',
description: `Allow merge commits. The default value is 'true'`,
};
const allowSquashMerge = {
title: 'Allow Squash Merges',
type: 'boolean',
description: `Allow squash merges. The default value is 'true'`,
};
const allowRebaseMerge = {
title: 'Allow Rebase Merges',
type: 'boolean',
description: `Allow rebase merges. The default value is 'true'`,
};
const collaborators = {
title: 'Collaborators',
description: 'Provide additional users or teams with permissions',
type: 'array',
items: {
type: 'object',
additionalProperties: false,
required: ['access'],
properties: {
access: {
type: 'string',
description: 'The type of access for the user',
enum: ['push', 'pull', 'admin', 'maintain', 'triage'],
},
user: {
type: 'string',
description:
'The name of the user that will be added as a collaborator',
},
team: {
type: 'string',
description:
'The name of the team that will be added as a collaborator',
},
},
oneOf: [{ required: ['user'] }, { required: ['team'] }],
},
};
const token = {
title: 'Authentication Token',
type: 'string',
description: 'The token to use for authorization to GitHub',
};
const topics = {
title: 'Topics',
type: 'array',
items: {
type: 'string',
},
};
const defaultBranch = {
title: 'Default Branch',
type: 'string',
description: `Sets the default branch on the repository. The default value is 'master'`,
};
const protectDefaultBranch = {
title: 'Protect Default Branch',
type: 'boolean',
description: `Protect the default branch after creating the repository. The default value is 'true'`,
};
const gitCommitMessage = {
title: 'Git Commit Message',
type: 'string',
description: `Sets the commit message on the repository. The default value is 'initial commit'`,
};
const sourcePath = {
title: 'Source Path',
description:
'Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the repository.',
type: 'string',
};
export { access };
export { allowMergeCommit };
export { allowRebaseMerge };
export { allowSquashMerge };
export { collaborators };
export { defaultBranch };
export { deleteBranchOnMerge };
export { description };
export { gitAuthorEmail };
export { gitAuthorName };
export { gitCommitMessage };
export { protectDefaultBranch };
export { repoUrl };
export { repoVisibility };
export { requireCodeOwnerReviews };
export { requiredStatusCheckContexts };
export { sourcePath };
export { token };
export { topics };
@@ -0,0 +1,27 @@
/*
* Copyright 2021 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.
*/
const remoteUrl = {
title: 'A URL to the repository with the provider',
type: 'string',
};
const repoContentsUrl = {
title: 'A URL to the root of the repository',
type: 'string',
};
export { remoteUrl };
export { repoContentsUrl };
@@ -13,20 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Config } from '@backstage/config';
import { assertError, InputError } from '@backstage/errors';
import {
GithubCredentialsProvider,
ScmIntegrationRegistry,
} from '@backstage/integration';
import { Octokit } from 'octokit';
import { createTemplateAction } from '../../createTemplateAction';
import { getOctokitOptions } from '../github/helpers';
import * as inputProps from '../github/inputProperties';
import * as outputProps from '../github/outputProperties';
import {
enableBranchProtectionOnDefaultRepoBranch,
initRepoAndPush,
} from '../helpers';
import { getRepoSourceDirectory, parseRepoUrl } from './util';
import { createTemplateAction } from '../../createTemplateAction';
import { Config } from '@backstage/config';
import { assertError, InputError } from '@backstage/errors';
import { getOctokitOptions } from '../github/helpers';
import { Octokit } from 'octokit';
/**
* Creates a new action that initializes a git repository of the content in the workspace
@@ -84,153 +86,32 @@ export function createPublishGithubAction(options: {
type: 'object',
required: ['repoUrl'],
properties: {
repoUrl: {
title: 'Repository Location',
description: `Accepts the format 'github.com?repo=reponame&owner=owner' where 'reponame' is the new repository name and 'owner' is an organization or username`,
type: 'string',
},
description: {
title: 'Repository Description',
type: 'string',
},
access: {
title: 'Repository Access',
description: `Sets an admin collaborator on the repository. Can either be a user reference different from 'owner' in 'repoUrl' or team reference, eg. 'org/team-name'`,
type: 'string',
},
requireCodeOwnerReviews: {
title: 'Require CODEOWNER Reviews?',
description:
'Require an approved review in PR including files with a designated Code Owner',
type: 'boolean',
},
requiredStatusCheckContexts: {
title: 'Required Status Check Contexts',
description:
'The list of status checks to require in order to merge into this branch',
type: 'array',
items: {
type: 'string',
},
},
repoVisibility: {
title: 'Repository Visibility',
type: 'string',
enum: ['private', 'public', 'internal'],
},
defaultBranch: {
title: 'Default Branch',
type: 'string',
description: `Sets the default branch on the repository. The default value is 'master'`,
},
protectDefaultBranch: {
title: 'Protect Default Branch',
type: 'boolean',
description: `Protect the default branch after creating the repository. The default value is 'true'`,
},
deleteBranchOnMerge: {
title: 'Delete Branch On Merge',
type: 'boolean',
description: `Delete the branch after merging the PR. The default value is 'false'`,
},
gitCommitMessage: {
title: 'Git Commit Message',
type: 'string',
description: `Sets the commit message on the repository. The default value is 'initial commit'`,
},
gitAuthorName: {
title: 'Default Author Name',
type: 'string',
description: `Sets the default author name for the commit. The default value is 'Scaffolder'`,
},
gitAuthorEmail: {
title: 'Default Author Email',
type: 'string',
description: `Sets the default author email for the commit.`,
},
allowMergeCommit: {
title: 'Allow Merge Commits',
type: 'boolean',
description: `Allow merge commits. The default value is 'true'`,
},
allowSquashMerge: {
title: 'Allow Squash Merges',
type: 'boolean',
description: `Allow squash merges. The default value is 'true'`,
},
allowRebaseMerge: {
title: 'Allow Rebase Merges',
type: 'boolean',
description: `Allow rebase merges. The default value is 'true'`,
},
sourcePath: {
title: 'Source Path',
description:
'Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the repository.',
type: 'string',
},
collaborators: {
title: 'Collaborators',
description: 'Provide additional users or teams with permissions',
type: 'array',
items: {
type: 'object',
additionalProperties: false,
required: ['access'],
properties: {
access: {
type: 'string',
description: 'The type of access for the user',
enum: ['push', 'pull', 'admin', 'maintain', 'triage'],
},
user: {
type: 'string',
description:
'The name of the user that will be added as a collaborator',
},
username: {
type: 'string',
description:
'Deprecated. Use the `team` or `user` field instead.',
},
team: {
type: 'string',
description:
'The name of the team that will be added as a collaborator',
},
},
oneOf: [
{ required: ['user'] },
{ required: ['username'] },
{ required: ['team'] },
],
},
},
token: {
title: 'Authentication Token',
type: 'string',
description: 'The token to use for authorization to GitHub',
},
topics: {
title: 'Topics',
type: 'array',
items: {
type: 'string',
},
},
repoUrl: inputProps.repoUrl,
description: inputProps.description,
access: inputProps.access,
requireCodeOwnerReviews: inputProps.requireCodeOwnerReviews,
requiredStatusCheckContexts: inputProps.requiredStatusCheckContexts,
repoVisibility: inputProps.repoVisibility,
defaultBranch: inputProps.defaultBranch,
protectDefaultBranch: inputProps.protectDefaultBranch,
deleteBranchOnMerge: inputProps.deleteBranchOnMerge,
gitCommitMessage: inputProps.gitCommitMessage,
gitAuthorName: inputProps.gitAuthorName,
gitAuthorEmail: inputProps.gitAuthorEmail,
allowMergeCommit: inputProps.allowMergeCommit,
allowSquashMerge: inputProps.allowSquashMerge,
allowRebaseMerge: inputProps.allowRebaseMerge,
sourcePath: inputProps.sourcePath,
collaborators: inputProps.collaborators,
token: inputProps.token,
topics: inputProps.topics,
},
},
output: {
type: 'object',
properties: {
remoteUrl: {
title: 'A URL to the repository with the provider',
type: 'string',
},
repoContentsUrl: {
title: 'A URL to the root of the repository',
type: 'string',
},
remoteUrl: outputProps.remoteUrl,
repoContentsUrl: outputProps.repoContentsUrl,
},
},
},