@backstage/plugin-scaffolder-backend-module-github 0.5.1-next.0 → 0.5.1-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -0
- package/dist/actions/gitHelpers.cjs.js +90 -0
- package/dist/actions/gitHelpers.cjs.js.map +1 -0
- package/dist/actions/gitHubEnvironment.examples.cjs.js +340 -0
- package/dist/actions/gitHubEnvironment.examples.cjs.js.map +1 -0
- package/dist/actions/github.cjs.js +187 -0
- package/dist/actions/github.cjs.js.map +1 -0
- package/dist/actions/github.examples.cjs.js +60 -0
- package/dist/actions/github.examples.cjs.js.map +1 -0
- package/dist/actions/githubActionsDispatch.cjs.js +84 -0
- package/dist/actions/githubActionsDispatch.cjs.js.map +1 -0
- package/dist/actions/githubActionsDispatch.examples.cjs.js +66 -0
- package/dist/actions/githubActionsDispatch.examples.cjs.js.map +1 -0
- package/dist/actions/githubAutolinks.cjs.js +76 -0
- package/dist/actions/githubAutolinks.cjs.js.map +1 -0
- package/dist/actions/githubAutolinks.examples.cjs.js +30 -0
- package/dist/actions/githubAutolinks.examples.cjs.js.map +1 -0
- package/dist/actions/githubBranchProtection.cjs.js +97 -0
- package/dist/actions/githubBranchProtection.cjs.js.map +1 -0
- package/dist/actions/githubBranchProtection.examples.cjs.js +62 -0
- package/dist/actions/githubBranchProtection.examples.cjs.js.map +1 -0
- package/dist/actions/githubDeployKey.cjs.js +123 -0
- package/dist/actions/githubDeployKey.cjs.js.map +1 -0
- package/dist/actions/githubDeployKey.examples.cjs.js +30 -0
- package/dist/actions/githubDeployKey.examples.cjs.js.map +1 -0
- package/dist/actions/githubEnvironment.cjs.js +255 -0
- package/dist/actions/githubEnvironment.cjs.js.map +1 -0
- package/dist/actions/githubIssuesLabel.cjs.js +79 -0
- package/dist/actions/githubIssuesLabel.cjs.js.map +1 -0
- package/dist/actions/githubIssuesLabel.examples.cjs.js +46 -0
- package/dist/actions/githubIssuesLabel.examples.cjs.js.map +1 -0
- package/dist/actions/githubPagesEnable.cjs.js +88 -0
- package/dist/actions/githubPagesEnable.cjs.js.map +1 -0
- package/dist/actions/githubPagesEnable.examples.cjs.js +224 -0
- package/dist/actions/githubPagesEnable.examples.cjs.js.map +1 -0
- package/dist/actions/githubPullRequest.cjs.js +336 -0
- package/dist/actions/githubPullRequest.cjs.js.map +1 -0
- package/dist/actions/githubPullRequest.examples.cjs.js +260 -0
- package/dist/actions/githubPullRequest.examples.cjs.js.map +1 -0
- package/dist/actions/githubRepoCreate.cjs.js +130 -0
- package/dist/actions/githubRepoCreate.cjs.js.map +1 -0
- package/dist/actions/githubRepoCreate.examples.cjs.js +964 -0
- package/dist/actions/githubRepoCreate.examples.cjs.js.map +1 -0
- package/dist/actions/githubRepoPush.cjs.js +122 -0
- package/dist/actions/githubRepoPush.cjs.js.map +1 -0
- package/dist/actions/githubRepoPush.examples.cjs.js +57 -0
- package/dist/actions/githubRepoPush.examples.cjs.js.map +1 -0
- package/dist/actions/githubWebhook.cjs.js +135 -0
- package/dist/actions/githubWebhook.cjs.js.map +1 -0
- package/dist/actions/githubWebhook.examples.cjs.js +113 -0
- package/dist/actions/githubWebhook.examples.cjs.js.map +1 -0
- package/dist/actions/helpers.cjs.js +292 -0
- package/dist/actions/helpers.cjs.js.map +1 -0
- package/dist/actions/inputProperties.cjs.js +319 -0
- package/dist/actions/inputProperties.cjs.js.map +1 -0
- package/dist/actions/outputProperties.cjs.js +19 -0
- package/dist/actions/outputProperties.cjs.js.map +1 -0
- package/dist/index.cjs.js +31 -4306
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +33 -1
- package/dist/module.cjs.js +92 -0
- package/dist/module.cjs.js.map +1 -0
- package/package.json +11 -11
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errors = require('@backstage/errors');
|
|
4
|
+
var pluginScaffolderNode = require('@backstage/plugin-scaffolder-node');
|
|
5
|
+
var githubBranchProtection_examples = require('./githubBranchProtection.examples.cjs.js');
|
|
6
|
+
var inputProperties = require('./inputProperties.cjs.js');
|
|
7
|
+
var helpers = require('./helpers.cjs.js');
|
|
8
|
+
var octokit = require('octokit');
|
|
9
|
+
var gitHelpers = require('./gitHelpers.cjs.js');
|
|
10
|
+
|
|
11
|
+
function createGithubBranchProtectionAction(options) {
|
|
12
|
+
const { integrations } = options;
|
|
13
|
+
return pluginScaffolderNode.createTemplateAction({
|
|
14
|
+
id: "github:branch-protection:create",
|
|
15
|
+
description: "Configures Branch Protection",
|
|
16
|
+
examples: githubBranchProtection_examples.examples,
|
|
17
|
+
schema: {
|
|
18
|
+
input: {
|
|
19
|
+
type: "object",
|
|
20
|
+
required: ["repoUrl"],
|
|
21
|
+
properties: {
|
|
22
|
+
repoUrl: inputProperties.repoUrl,
|
|
23
|
+
branch: {
|
|
24
|
+
title: "Branch name",
|
|
25
|
+
description: `The branch to protect. Defaults to the repository's default branch`,
|
|
26
|
+
type: "string"
|
|
27
|
+
},
|
|
28
|
+
enforceAdmins: inputProperties.protectEnforceAdmins,
|
|
29
|
+
requiredApprovingReviewCount: inputProperties.requiredApprovingReviewCount,
|
|
30
|
+
requireCodeOwnerReviews: inputProperties.requireCodeOwnerReviews,
|
|
31
|
+
dismissStaleReviews: inputProperties.dismissStaleReviews,
|
|
32
|
+
bypassPullRequestAllowances: inputProperties.bypassPullRequestAllowances,
|
|
33
|
+
restrictions: inputProperties.restrictions,
|
|
34
|
+
requiredStatusCheckContexts: inputProperties.requiredStatusCheckContexts,
|
|
35
|
+
requireBranchesToBeUpToDate: inputProperties.requireBranchesToBeUpToDate,
|
|
36
|
+
requiredConversationResolution: inputProperties.requiredConversationResolution,
|
|
37
|
+
requireLastPushApproval: inputProperties.requireLastPushApproval,
|
|
38
|
+
requiredCommitSigning: inputProperties.requiredCommitSigning,
|
|
39
|
+
token: inputProperties.token
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
async handler(ctx) {
|
|
44
|
+
const {
|
|
45
|
+
repoUrl,
|
|
46
|
+
branch,
|
|
47
|
+
enforceAdmins = true,
|
|
48
|
+
requiredApprovingReviewCount = 1,
|
|
49
|
+
requireCodeOwnerReviews = false,
|
|
50
|
+
dismissStaleReviews = false,
|
|
51
|
+
bypassPullRequestAllowances,
|
|
52
|
+
restrictions,
|
|
53
|
+
requiredStatusCheckContexts = [],
|
|
54
|
+
requireBranchesToBeUpToDate = true,
|
|
55
|
+
requiredConversationResolution = false,
|
|
56
|
+
requireLastPushApproval = false,
|
|
57
|
+
requiredCommitSigning = false,
|
|
58
|
+
token: providedToken
|
|
59
|
+
} = ctx.input;
|
|
60
|
+
const octokitOptions = await helpers.getOctokitOptions({
|
|
61
|
+
integrations,
|
|
62
|
+
token: providedToken,
|
|
63
|
+
repoUrl
|
|
64
|
+
});
|
|
65
|
+
const client = new octokit.Octokit(octokitOptions);
|
|
66
|
+
const { owner, repo } = pluginScaffolderNode.parseRepoUrl(repoUrl, integrations);
|
|
67
|
+
if (!owner) {
|
|
68
|
+
throw new errors.InputError(`No owner provided for repo ${repoUrl}`);
|
|
69
|
+
}
|
|
70
|
+
const repository = await client.rest.repos.get({
|
|
71
|
+
owner,
|
|
72
|
+
repo
|
|
73
|
+
});
|
|
74
|
+
await gitHelpers.enableBranchProtectionOnDefaultRepoBranch({
|
|
75
|
+
repoName: repo,
|
|
76
|
+
client,
|
|
77
|
+
owner,
|
|
78
|
+
logger: ctx.logger,
|
|
79
|
+
requireCodeOwnerReviews,
|
|
80
|
+
bypassPullRequestAllowances,
|
|
81
|
+
requiredApprovingReviewCount,
|
|
82
|
+
restrictions,
|
|
83
|
+
requiredStatusCheckContexts,
|
|
84
|
+
requireBranchesToBeUpToDate,
|
|
85
|
+
requiredConversationResolution,
|
|
86
|
+
requireLastPushApproval,
|
|
87
|
+
defaultBranch: branch ?? repository.data.default_branch,
|
|
88
|
+
enforceAdmins,
|
|
89
|
+
dismissStaleReviews,
|
|
90
|
+
requiredCommitSigning
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
exports.createGithubBranchProtectionAction = createGithubBranchProtectionAction;
|
|
97
|
+
//# sourceMappingURL=githubBranchProtection.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"githubBranchProtection.cjs.js","sources":["../../src/actions/githubBranchProtection.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { InputError } from '@backstage/errors';\nimport {\n createTemplateAction,\n parseRepoUrl,\n} from '@backstage/plugin-scaffolder-node';\nimport { ScmIntegrationRegistry } from '@backstage/integration';\nimport { examples } from './githubBranchProtection.examples';\nimport * as inputProps from './inputProperties';\nimport { getOctokitOptions } from './helpers';\nimport { Octokit } from 'octokit';\nimport { enableBranchProtectionOnDefaultRepoBranch } from './gitHelpers';\n\n/**\n * Creates an `github:branch-protection:create` Scaffolder action that configured Branch Protection in a Github Repository.\n *\n * @public\n */\nexport function createGithubBranchProtectionAction(options: {\n integrations: ScmIntegrationRegistry;\n}) {\n const { integrations } = options;\n\n return createTemplateAction<{\n repoUrl: string;\n branch?: string;\n enforceAdmins?: boolean;\n requiredApprovingReviewCount?: number;\n requireCodeOwnerReviews?: boolean;\n dismissStaleReviews?: boolean;\n bypassPullRequestAllowances?:\n | {\n users?: string[];\n teams?: string[];\n apps?: string[];\n }\n | undefined;\n restrictions?:\n | {\n users: string[];\n teams: string[];\n apps?: string[];\n }\n | undefined;\n requiredStatusCheckContexts?: string[];\n requireBranchesToBeUpToDate?: boolean;\n requiredConversationResolution?: boolean;\n requireLastPushApproval?: boolean;\n requiredCommitSigning?: boolean;\n token?: string;\n }>({\n id: 'github:branch-protection:create',\n description: 'Configures Branch Protection',\n examples,\n schema: {\n input: {\n type: 'object',\n required: ['repoUrl'],\n properties: {\n repoUrl: inputProps.repoUrl,\n branch: {\n title: 'Branch name',\n description: `The branch to protect. Defaults to the repository's default branch`,\n type: 'string',\n },\n enforceAdmins: inputProps.protectEnforceAdmins,\n requiredApprovingReviewCount: inputProps.requiredApprovingReviewCount,\n requireCodeOwnerReviews: inputProps.requireCodeOwnerReviews,\n dismissStaleReviews: inputProps.dismissStaleReviews,\n bypassPullRequestAllowances: inputProps.bypassPullRequestAllowances,\n restrictions: inputProps.restrictions,\n requiredStatusCheckContexts: inputProps.requiredStatusCheckContexts,\n requireBranchesToBeUpToDate: inputProps.requireBranchesToBeUpToDate,\n requiredConversationResolution:\n inputProps.requiredConversationResolution,\n requireLastPushApproval: inputProps.requireLastPushApproval,\n requiredCommitSigning: inputProps.requiredCommitSigning,\n token: inputProps.token,\n },\n },\n },\n async handler(ctx) {\n const {\n repoUrl,\n branch,\n enforceAdmins = true,\n requiredApprovingReviewCount = 1,\n requireCodeOwnerReviews = false,\n dismissStaleReviews = false,\n bypassPullRequestAllowances,\n restrictions,\n requiredStatusCheckContexts = [],\n requireBranchesToBeUpToDate = true,\n requiredConversationResolution = false,\n requireLastPushApproval = false,\n requiredCommitSigning = false,\n token: providedToken,\n } = ctx.input;\n\n const octokitOptions = await getOctokitOptions({\n integrations,\n token: providedToken,\n repoUrl: repoUrl,\n });\n const client = new Octokit(octokitOptions);\n\n const { owner, repo } = parseRepoUrl(repoUrl, integrations);\n\n if (!owner) {\n throw new InputError(`No owner provided for repo ${repoUrl}`);\n }\n\n const repository = await client.rest.repos.get({\n owner: owner,\n repo: repo,\n });\n\n await enableBranchProtectionOnDefaultRepoBranch({\n repoName: repo,\n client,\n owner,\n logger: ctx.logger,\n requireCodeOwnerReviews,\n bypassPullRequestAllowances,\n requiredApprovingReviewCount,\n restrictions,\n requiredStatusCheckContexts,\n requireBranchesToBeUpToDate,\n requiredConversationResolution,\n requireLastPushApproval,\n defaultBranch: branch ?? repository.data.default_branch,\n enforceAdmins,\n dismissStaleReviews,\n requiredCommitSigning,\n });\n },\n });\n}\n"],"names":["createTemplateAction","examples","inputProps.repoUrl","inputProps.protectEnforceAdmins","inputProps.requiredApprovingReviewCount","inputProps.requireCodeOwnerReviews","inputProps.dismissStaleReviews","inputProps.bypassPullRequestAllowances","inputProps.restrictions","inputProps.requiredStatusCheckContexts","inputProps.requireBranchesToBeUpToDate","inputProps.requiredConversationResolution","inputProps.requireLastPushApproval","inputProps.requiredCommitSigning","inputProps.token","getOctokitOptions","Octokit","parseRepoUrl","InputError","enableBranchProtectionOnDefaultRepoBranch"],"mappings":";;;;;;;;;;AAiCO,SAAS,mCAAmC,OAEhD,EAAA;AACD,EAAM,MAAA,EAAE,cAAiB,GAAA,OAAA,CAAA;AAEzB,EAAA,OAAOA,yCA2BJ,CAAA;AAAA,IACD,EAAI,EAAA,iCAAA;AAAA,IACJ,WAAa,EAAA,8BAAA;AAAA,cACbC,wCAAA;AAAA,IACA,MAAQ,EAAA;AAAA,MACN,KAAO,EAAA;AAAA,QACL,IAAM,EAAA,QAAA;AAAA,QACN,QAAA,EAAU,CAAC,SAAS,CAAA;AAAA,QACpB,UAAY,EAAA;AAAA,UACV,SAASC,uBAAW;AAAA,UACpB,MAAQ,EAAA;AAAA,YACN,KAAO,EAAA,aAAA;AAAA,YACP,WAAa,EAAA,CAAA,kEAAA,CAAA;AAAA,YACb,IAAM,EAAA,QAAA;AAAA,WACR;AAAA,UACA,eAAeC,oCAAW;AAAA,UAC1B,8BAA8BC,4CAAW;AAAA,UACzC,yBAAyBC,uCAAW;AAAA,UACpC,qBAAqBC,mCAAW;AAAA,UAChC,6BAA6BC,2CAAW;AAAA,UACxC,cAAcC,4BAAW;AAAA,UAC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ACD,CAAA,CAAA;AAED,MAAA,MAAMC,oDAA0C,CAAA;AAAA,QAC9C,QAAU,EAAA,IAAA;AAAA,QACV,MAAA;AAAA,QACA,KAAA;AAAA,QACA,QAAQ,GAAI,CAAA,MAAA;AAAA,QACZ,uBAAA;AAAA,QACA,2BAAA;AAAA,QACA,4BAAA;AAAA,QACA,YAAA;AAAA,QACA,2BAAA;AAAA,QACA,2BAAA;AAAA,QACA,8BAAA;AAAA,QACA,uBAAA;AAAA,QACA,aAAA,EAAe,MAAU,IAAA,UAAA,CAAW,IAAK,CAAA,cAAA;AAAA,QACzC,aAAA;AAAA,QACA,mBAAA;AAAA,QACA,qBAAA;AAAA,OACD,CAAA,CAAA;AAAA,KACH;AAAA,GACD,CAAA,CAAA;AACH;;;;"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var yaml = require('yaml');
|
|
4
|
+
|
|
5
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var yaml__default = /*#__PURE__*/_interopDefaultCompat(yaml);
|
|
8
|
+
|
|
9
|
+
const examples = [
|
|
10
|
+
{
|
|
11
|
+
description: `GitHub Branch Protection for repository's default branch.`,
|
|
12
|
+
example: yaml__default.default.stringify({
|
|
13
|
+
steps: [
|
|
14
|
+
{
|
|
15
|
+
action: "github:branch-protection:create",
|
|
16
|
+
name: "Setup Branch Protection",
|
|
17
|
+
input: {
|
|
18
|
+
repoUrl: "github.com?repo=repo&owner=owner"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
})
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
description: `GitHub Branch Protection for a specific branch.`,
|
|
26
|
+
example: yaml__default.default.stringify({
|
|
27
|
+
steps: [
|
|
28
|
+
{
|
|
29
|
+
action: "github:branch-protection:create",
|
|
30
|
+
name: "Setup Branch Protection",
|
|
31
|
+
input: {
|
|
32
|
+
repoUrl: "github.com?repo=repo&owner=owner",
|
|
33
|
+
branch: "my-awesome-branch"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
})
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
description: `GitHub Branch Protection and required commit signing on default branch.`,
|
|
41
|
+
example: yaml__default.default.stringify({
|
|
42
|
+
steps: [
|
|
43
|
+
{
|
|
44
|
+
action: "github:branch-protection:create",
|
|
45
|
+
name: "Setup Branch Protection",
|
|
46
|
+
input: {
|
|
47
|
+
repoUrl: "github.com?repo=repo&owner=owner",
|
|
48
|
+
requireCodeOwnerReviews: true,
|
|
49
|
+
requiredStatusCheckContexts: ["test"],
|
|
50
|
+
dismissStaleReviews: true,
|
|
51
|
+
requireLastPushApproval: true,
|
|
52
|
+
requiredConversationResolution: true,
|
|
53
|
+
requiredCommitSigning: true
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
exports.examples = examples;
|
|
62
|
+
//# sourceMappingURL=githubBranchProtection.examples.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"githubBranchProtection.examples.cjs.js","sources":["../../src/actions/githubBranchProtection.examples.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { TemplateExample } from '@backstage/plugin-scaffolder-node';\nimport yaml from 'yaml';\n\nexport const examples: TemplateExample[] = [\n {\n description: `GitHub Branch Protection for repository's default branch.`,\n example: yaml.stringify({\n steps: [\n {\n action: 'github:branch-protection:create',\n name: 'Setup Branch Protection',\n input: {\n repoUrl: 'github.com?repo=repo&owner=owner',\n },\n },\n ],\n }),\n },\n {\n description: `GitHub Branch Protection for a specific branch.`,\n example: yaml.stringify({\n steps: [\n {\n action: 'github:branch-protection:create',\n name: 'Setup Branch Protection',\n input: {\n repoUrl: 'github.com?repo=repo&owner=owner',\n branch: 'my-awesome-branch',\n },\n },\n ],\n }),\n },\n {\n description: `GitHub Branch Protection and required commit signing on default branch.`,\n example: yaml.stringify({\n steps: [\n {\n action: 'github:branch-protection:create',\n name: 'Setup Branch Protection',\n input: {\n repoUrl: 'github.com?repo=repo&owner=owner',\n requireCodeOwnerReviews: true,\n requiredStatusCheckContexts: ['test'],\n dismissStaleReviews: true,\n requireLastPushApproval: true,\n requiredConversationResolution: true,\n requiredCommitSigning: true,\n },\n },\n ],\n }),\n },\n];\n"],"names":["yaml"],"mappings":";;;;;;;;AAmBO,MAAM,QAA8B,GAAA;AAAA,EACzC;AAAA,IACE,WAAa,EAAA,CAAA,yDAAA,CAAA;AAAA,IACb,OAAA,EAASA,sBAAK,SAAU,CAAA;AAAA,MACtB,KAAO,EAAA;AAAA,QACL;AAAA,UACE,MAAQ,EAAA,iCAAA;AAAA,UACR,IAAM,EAAA,yBAAA;AAAA,UACN,KAAO,EAAA;AAAA,YACL,OAAS,EAAA,kCAAA;AAAA,WACX;AAAA,SACF;AAAA,OACF;AAAA,KACD,CAAA;AAAA,GACH;AAAA,EACA;AAAA,IACE,WAAa,EAAA,CAAA,+CAAA,CAAA;AAAA,IACb,OAAA,EAASA,sBAAK,SAAU,CAAA;AAAA,MACtB,KAAO,EAAA;AAAA,QACL;AAAA,UACE,MAAQ,EAAA,iCAAA;AAAA,UACR,IAAM,EAAA,yBAAA;AAAA,UACN,KAAO,EAAA;AAAA,YACL,OAAS,EAAA,kCAAA;AAAA,YACT,MAAQ,EAAA,mBAAA;AAAA,WACV;AAAA,SACF;AAAA,OACF;AAAA,KACD,CAAA;AAAA,GACH;AAAA,EACA;AAAA,IACE,WAAa,EAAA,CAAA,uEAAA,CAAA;AAAA,IACb,OAAA,EAASA,sBAAK,SAAU,CAAA;AAAA,MACtB,KAAO,EAAA;AAAA,QACL;AAAA,UACE,MAAQ,EAAA,iCAAA;AAAA,UACR,IAAM,EAAA,yBAAA;AAAA,UACN,KAAO,EAAA;AAAA,YACL,OAAS,EAAA,kCAAA;AAAA,YACT,uBAAyB,EAAA,IAAA;AAAA,YACzB,2BAAA,EAA6B,CAAC,MAAM,CAAA;AAAA,YACpC,mBAAqB,EAAA,IAAA;AAAA,YACrB,uBAAyB,EAAA,IAAA;AAAA,YACzB,8BAAgC,EAAA,IAAA;AAAA,YAChC,qBAAuB,EAAA,IAAA;AAAA,WACzB;AAAA,SACF;AAAA,OACF;AAAA,KACD,CAAA;AAAA,GACH;AACF;;;;"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errors = require('@backstage/errors');
|
|
4
|
+
var pluginScaffolderNode = require('@backstage/plugin-scaffolder-node');
|
|
5
|
+
var helpers = require('./helpers.cjs.js');
|
|
6
|
+
var octokit = require('octokit');
|
|
7
|
+
var Sodium = require('libsodium-wrappers');
|
|
8
|
+
var githubDeployKey_examples = require('./githubDeployKey.examples.cjs.js');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
11
|
+
|
|
12
|
+
var Sodium__default = /*#__PURE__*/_interopDefaultCompat(Sodium);
|
|
13
|
+
|
|
14
|
+
function createGithubDeployKeyAction(options) {
|
|
15
|
+
const { integrations } = options;
|
|
16
|
+
return pluginScaffolderNode.createTemplateAction({
|
|
17
|
+
id: "github:deployKey:create",
|
|
18
|
+
description: "Creates and stores Deploy Keys",
|
|
19
|
+
examples: githubDeployKey_examples.examples,
|
|
20
|
+
schema: {
|
|
21
|
+
input: {
|
|
22
|
+
type: "object",
|
|
23
|
+
required: ["repoUrl", "publicKey", "privateKey", "deployKeyName"],
|
|
24
|
+
properties: {
|
|
25
|
+
repoUrl: {
|
|
26
|
+
title: "Repository Location",
|
|
27
|
+
description: `Accepts the format 'github.com?repo=reponame&owner=owner' where 'reponame' is the new repository name and 'owner' is an organization or username`,
|
|
28
|
+
type: "string"
|
|
29
|
+
},
|
|
30
|
+
publicKey: {
|
|
31
|
+
title: "SSH Public Key",
|
|
32
|
+
description: `Generated from ssh-keygen. Begins with 'ssh-rsa', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', 'ecdsa-sha2-nistp521', 'ssh-ed25519', 'sk-ecdsa-sha2-nistp256@openssh.com', or 'sk-ssh-ed25519@openssh.com'.`,
|
|
33
|
+
type: "string"
|
|
34
|
+
},
|
|
35
|
+
privateKey: {
|
|
36
|
+
title: "SSH Private Key",
|
|
37
|
+
description: `SSH Private Key generated from ssh-keygen`,
|
|
38
|
+
type: "string"
|
|
39
|
+
},
|
|
40
|
+
deployKeyName: {
|
|
41
|
+
title: "Deploy Key Name",
|
|
42
|
+
description: `Name of the Deploy Key`,
|
|
43
|
+
type: "string"
|
|
44
|
+
},
|
|
45
|
+
privateKeySecretName: {
|
|
46
|
+
title: "Private Key GitHub Secret Name",
|
|
47
|
+
description: `Name of the GitHub Secret to store the private key related to the Deploy Key. Defaults to: 'KEY_NAME_PRIVATE_KEY' where 'KEY_NAME' is the name of the Deploy Key`,
|
|
48
|
+
type: "string"
|
|
49
|
+
},
|
|
50
|
+
token: {
|
|
51
|
+
title: "Authentication Token",
|
|
52
|
+
type: "string",
|
|
53
|
+
description: "The token to use for authorization to GitHub"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
output: {
|
|
58
|
+
type: "object",
|
|
59
|
+
properties: {
|
|
60
|
+
privateKeySecretName: {
|
|
61
|
+
title: "The GitHub Action Repo Secret Name for the Private Key",
|
|
62
|
+
type: "string"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
async handler(ctx) {
|
|
68
|
+
const {
|
|
69
|
+
repoUrl,
|
|
70
|
+
publicKey,
|
|
71
|
+
privateKey,
|
|
72
|
+
deployKeyName,
|
|
73
|
+
privateKeySecretName = `${deployKeyName.split(" ").join("_").toLocaleUpperCase("en-US")}_PRIVATE_KEY`,
|
|
74
|
+
token: providedToken
|
|
75
|
+
} = ctx.input;
|
|
76
|
+
const octokitOptions = await helpers.getOctokitOptions({
|
|
77
|
+
integrations,
|
|
78
|
+
token: providedToken,
|
|
79
|
+
repoUrl
|
|
80
|
+
});
|
|
81
|
+
const { owner, repo } = pluginScaffolderNode.parseRepoUrl(repoUrl, integrations);
|
|
82
|
+
if (!owner) {
|
|
83
|
+
throw new errors.InputError(`No owner provided for repo ${repoUrl}`);
|
|
84
|
+
}
|
|
85
|
+
const client = new octokit.Octokit(octokitOptions);
|
|
86
|
+
await client.rest.repos.createDeployKey({
|
|
87
|
+
owner,
|
|
88
|
+
repo,
|
|
89
|
+
title: deployKeyName,
|
|
90
|
+
key: publicKey
|
|
91
|
+
});
|
|
92
|
+
const publicKeyResponse = await client.rest.actions.getRepoPublicKey({
|
|
93
|
+
owner,
|
|
94
|
+
repo
|
|
95
|
+
});
|
|
96
|
+
await Sodium__default.default.ready;
|
|
97
|
+
const binaryKey = Sodium__default.default.from_base64(
|
|
98
|
+
publicKeyResponse.data.key,
|
|
99
|
+
Sodium__default.default.base64_variants.ORIGINAL
|
|
100
|
+
);
|
|
101
|
+
const binarySecret = Sodium__default.default.from_string(privateKey);
|
|
102
|
+
const encryptedBinarySecret = Sodium__default.default.crypto_box_seal(
|
|
103
|
+
binarySecret,
|
|
104
|
+
binaryKey
|
|
105
|
+
);
|
|
106
|
+
const encryptedBase64Secret = Sodium__default.default.to_base64(
|
|
107
|
+
encryptedBinarySecret,
|
|
108
|
+
Sodium__default.default.base64_variants.ORIGINAL
|
|
109
|
+
);
|
|
110
|
+
await client.rest.actions.createOrUpdateRepoSecret({
|
|
111
|
+
owner,
|
|
112
|
+
repo,
|
|
113
|
+
secret_name: privateKeySecretName,
|
|
114
|
+
encrypted_value: encryptedBase64Secret,
|
|
115
|
+
key_id: publicKeyResponse.data.key_id
|
|
116
|
+
});
|
|
117
|
+
ctx.output("privateKeySecretName", privateKeySecretName);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
exports.createGithubDeployKeyAction = createGithubDeployKeyAction;
|
|
123
|
+
//# sourceMappingURL=githubDeployKey.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"githubDeployKey.cjs.js","sources":["../../src/actions/githubDeployKey.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { InputError } from '@backstage/errors';\nimport {\n createTemplateAction,\n parseRepoUrl,\n} from '@backstage/plugin-scaffolder-node';\nimport { ScmIntegrationRegistry } from '@backstage/integration';\nimport { getOctokitOptions } from './helpers';\nimport { Octokit } from 'octokit';\nimport Sodium from 'libsodium-wrappers';\nimport { examples } from './githubDeployKey.examples';\n\n/**\n * Creates an `github:deployKey:create` Scaffolder action that creates a Deploy Key\n *\n * @public\n */\nexport function createGithubDeployKeyAction(options: {\n integrations: ScmIntegrationRegistry;\n}) {\n const { integrations } = options;\n // For more information on how to define custom actions, see\n // https://backstage.io/docs/features/software-templates/writing-custom-actions\n return createTemplateAction<{\n repoUrl: string;\n publicKey: string;\n privateKey: string;\n deployKeyName: string;\n privateKeySecretName?: string;\n token?: string;\n }>({\n id: 'github:deployKey:create',\n description: 'Creates and stores Deploy Keys',\n examples,\n schema: {\n input: {\n type: 'object',\n required: ['repoUrl', 'publicKey', 'privateKey', 'deployKeyName'],\n properties: {\n repoUrl: {\n title: 'Repository Location',\n description: `Accepts the format 'github.com?repo=reponame&owner=owner' where 'reponame' is the new repository name and 'owner' is an organization or username`,\n type: 'string',\n },\n publicKey: {\n title: 'SSH Public Key',\n description: `Generated from ssh-keygen. Begins with 'ssh-rsa', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', 'ecdsa-sha2-nistp521', 'ssh-ed25519', 'sk-ecdsa-sha2-nistp256@openssh.com', or 'sk-ssh-ed25519@openssh.com'.`,\n type: 'string',\n },\n privateKey: {\n title: 'SSH Private Key',\n description: `SSH Private Key generated from ssh-keygen`,\n type: 'string',\n },\n deployKeyName: {\n title: 'Deploy Key Name',\n description: `Name of the Deploy Key`,\n type: 'string',\n },\n privateKeySecretName: {\n title: 'Private Key GitHub Secret Name',\n description: `Name of the GitHub Secret to store the private key related to the Deploy Key. Defaults to: 'KEY_NAME_PRIVATE_KEY' where 'KEY_NAME' is the name of the Deploy Key`,\n type: 'string',\n },\n token: {\n title: 'Authentication Token',\n type: 'string',\n description: 'The token to use for authorization to GitHub',\n },\n },\n },\n output: {\n type: 'object',\n properties: {\n privateKeySecretName: {\n title: 'The GitHub Action Repo Secret Name for the Private Key',\n type: 'string',\n },\n },\n },\n },\n async handler(ctx) {\n const {\n repoUrl,\n publicKey,\n privateKey,\n deployKeyName,\n privateKeySecretName = `${deployKeyName\n .split(' ')\n .join('_')\n .toLocaleUpperCase('en-US')}_PRIVATE_KEY`,\n token: providedToken,\n } = ctx.input;\n\n const octokitOptions = await getOctokitOptions({\n integrations,\n token: providedToken,\n repoUrl: repoUrl,\n });\n\n const { owner, repo } = parseRepoUrl(repoUrl, integrations);\n\n if (!owner) {\n throw new InputError(`No owner provided for repo ${repoUrl}`);\n }\n\n const client = new Octokit(octokitOptions);\n\n await client.rest.repos.createDeployKey({\n owner: owner,\n repo: repo,\n title: deployKeyName,\n key: publicKey,\n });\n const publicKeyResponse = await client.rest.actions.getRepoPublicKey({\n owner: owner,\n repo: repo,\n });\n\n await Sodium.ready;\n const binaryKey = Sodium.from_base64(\n publicKeyResponse.data.key,\n Sodium.base64_variants.ORIGINAL,\n );\n const binarySecret = Sodium.from_string(privateKey);\n const encryptedBinarySecret = Sodium.crypto_box_seal(\n binarySecret,\n binaryKey,\n );\n const encryptedBase64Secret = Sodium.to_base64(\n encryptedBinarySecret,\n Sodium.base64_variants.ORIGINAL,\n );\n\n await client.rest.actions.createOrUpdateRepoSecret({\n owner: owner,\n repo: repo,\n secret_name: privateKeySecretName,\n encrypted_value: encryptedBase64Secret,\n key_id: publicKeyResponse.data.key_id,\n });\n\n ctx.output('privateKeySecretName', privateKeySecretName);\n },\n });\n}\n"],"names":["createTemplateAction","examples","getOctokitOptions","parseRepoUrl","InputError","Octokit","Sodium"],"mappings":";;;;;;;;;;;;;AAgCO,SAAS,4BAA4B,OAEzC,EAAA;AACD,EAAM,MAAA,EAAE,cAAiB,GAAA,OAAA,CAAA;AAGzB,EAAA,OAAOA,yCAOJ,CAAA;AAAA,IACD,EAAI,EAAA,yBAAA;AAAA,IACJ,WAAa,EAAA,gCAAA;AAAA,cACbC,iCAAA;AAAA,IACA,MAAQ,EAAA;AAAA,MACN,KAAO,EAAA;AAAA,QACL,IAAM,EAAA,QAAA;AAAA,QACN,QAAU,EAAA,CAAC,SAAW,EAAA,WAAA,EAAa,cAAc,eAAe,CAAA;AAAA,QAChE,UAAY,EAAA;AAAA,UACV,OAAS,EAAA;AAAA,YACP,KAAO,EAAA,qBAAA;AAAA,YACP,WAAa,EAAA,CAAA,gJAAA,CAAA;AAAA,YACb,IAAM,EAAA,QAAA;AAAA,WACR;AAAA,UACA,SAAW,EAAA;AAAA,YACT,KAAO,EAAA,gBAAA;AAAA,YACP,WAAa,EAAA,CAAA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A,QACjD,KAAA;AAAA,QACA,IAAA;AAAA,QACA,WAAa,EAAA,oBAAA;AAAA,QACb,eAAiB,EAAA,qBAAA;AAAA,QACjB,MAAA,EAAQ,kBAAkB,IAAK,CAAA,MAAA;AAAA,OAChC,CAAA,CAAA;AAED,MAAI,GAAA,CAAA,MAAA,CAAO,wBAAwB,oBAAoB,CAAA,CAAA;AAAA,KACzD;AAAA,GACD,CAAA,CAAA;AACH;;;;"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var yaml = require('yaml');
|
|
4
|
+
|
|
5
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var yaml__default = /*#__PURE__*/_interopDefaultCompat(yaml);
|
|
8
|
+
|
|
9
|
+
const examples = [
|
|
10
|
+
{
|
|
11
|
+
description: "Example 1: Create and store a Deploy Key",
|
|
12
|
+
example: yaml__default.default.stringify({
|
|
13
|
+
steps: [
|
|
14
|
+
{
|
|
15
|
+
action: "github:deployKey:create",
|
|
16
|
+
name: "Create and store a Deploy Key",
|
|
17
|
+
input: {
|
|
18
|
+
repoUrl: "github.com?repo=repository&owner=owner",
|
|
19
|
+
publicKey: "pubkey",
|
|
20
|
+
privateKey: "privkey",
|
|
21
|
+
deployKeyName: "Push Tags"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
exports.examples = examples;
|
|
30
|
+
//# sourceMappingURL=githubDeployKey.examples.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"githubDeployKey.examples.cjs.js","sources":["../../src/actions/githubDeployKey.examples.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { TemplateExample } from '@backstage/plugin-scaffolder-node';\nimport yaml from 'yaml';\n\nexport const examples: TemplateExample[] = [\n {\n description: 'Example 1: Create and store a Deploy Key',\n example: yaml.stringify({\n steps: [\n {\n action: 'github:deployKey:create',\n name: 'Create and store a Deploy Key',\n input: {\n repoUrl: 'github.com?repo=repository&owner=owner',\n publicKey: 'pubkey',\n privateKey: 'privkey',\n deployKeyName: 'Push Tags',\n },\n },\n ],\n }),\n },\n];\n"],"names":["yaml"],"mappings":";;;;;;;;AAkBO,MAAM,QAA8B,GAAA;AAAA,EACzC;AAAA,IACE,WAAa,EAAA,0CAAA;AAAA,IACb,OAAA,EAASA,sBAAK,SAAU,CAAA;AAAA,MACtB,KAAO,EAAA;AAAA,QACL;AAAA,UACE,MAAQ,EAAA,yBAAA;AAAA,UACR,IAAM,EAAA,+BAAA;AAAA,UACN,KAAO,EAAA;AAAA,YACL,OAAS,EAAA,wCAAA;AAAA,YACT,SAAW,EAAA,QAAA;AAAA,YACX,UAAY,EAAA,SAAA;AAAA,YACZ,aAAe,EAAA,WAAA;AAAA,WACjB;AAAA,SACF;AAAA,OACF;AAAA,KACD,CAAA;AAAA,GACH;AACF;;;;"}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errors = require('@backstage/errors');
|
|
4
|
+
var pluginScaffolderNode = require('@backstage/plugin-scaffolder-node');
|
|
5
|
+
var helpers = require('./helpers.cjs.js');
|
|
6
|
+
var octokit = require('octokit');
|
|
7
|
+
var Sodium = require('libsodium-wrappers');
|
|
8
|
+
var gitHubEnvironment_examples = require('./gitHubEnvironment.examples.cjs.js');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
11
|
+
|
|
12
|
+
var Sodium__default = /*#__PURE__*/_interopDefaultCompat(Sodium);
|
|
13
|
+
|
|
14
|
+
function createGithubEnvironmentAction(options) {
|
|
15
|
+
const { integrations, catalogClient } = options;
|
|
16
|
+
return pluginScaffolderNode.createTemplateAction({
|
|
17
|
+
id: "github:environment:create",
|
|
18
|
+
description: "Creates Deployment Environments",
|
|
19
|
+
examples: gitHubEnvironment_examples.examples,
|
|
20
|
+
schema: {
|
|
21
|
+
input: {
|
|
22
|
+
type: "object",
|
|
23
|
+
required: ["repoUrl", "name"],
|
|
24
|
+
properties: {
|
|
25
|
+
repoUrl: {
|
|
26
|
+
title: "Repository Location",
|
|
27
|
+
description: `Accepts the format 'github.com?repo=reponame&owner=owner' where 'reponame' is the new repository name and 'owner' is an organization or username`,
|
|
28
|
+
type: "string"
|
|
29
|
+
},
|
|
30
|
+
name: {
|
|
31
|
+
title: "Environment Name",
|
|
32
|
+
description: `Name of the deployment environment to create`,
|
|
33
|
+
type: "string"
|
|
34
|
+
},
|
|
35
|
+
deploymentBranchPolicy: {
|
|
36
|
+
title: "Deployment Branch Policy",
|
|
37
|
+
description: `The type of deployment branch policy for this environment. To allow all branches to deploy, set to null.`,
|
|
38
|
+
type: "object",
|
|
39
|
+
required: ["protected_branches", "custom_branch_policies"],
|
|
40
|
+
properties: {
|
|
41
|
+
protected_branches: {
|
|
42
|
+
title: "Protected Branches",
|
|
43
|
+
description: `Whether only branches with branch protection rules can deploy to this environment. If protected_branches is true, custom_branch_policies must be false; if protected_branches is false, custom_branch_policies must be true.`,
|
|
44
|
+
type: "boolean"
|
|
45
|
+
},
|
|
46
|
+
custom_branch_policies: {
|
|
47
|
+
title: "Custom Branch Policies",
|
|
48
|
+
description: `Whether only branches that match the specified name patterns can deploy to this environment. If custom_branch_policies is true, protected_branches must be false; if custom_branch_policies is false, protected_branches must be true.`,
|
|
49
|
+
type: "boolean"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
customBranchPolicyNames: {
|
|
54
|
+
title: "Custom Branch Policy Name",
|
|
55
|
+
description: `The name pattern that branches must match in order to deploy to the environment.
|
|
56
|
+
|
|
57
|
+
Wildcard characters will not match /. For example, to match branches that begin with release/ and contain an additional single slash, use release/*/*. For more information about pattern matching syntax, see the Ruby File.fnmatch documentation.`,
|
|
58
|
+
type: "array",
|
|
59
|
+
items: {
|
|
60
|
+
type: "string"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
customTagPolicyNames: {
|
|
64
|
+
title: "Custom Tag Policy Name",
|
|
65
|
+
description: `The name pattern that tags must match in order to deploy to the environment.
|
|
66
|
+
|
|
67
|
+
Wildcard characters will not match /. For example, to match tags that begin with release/ and contain an additional single slash, use release/*/*. For more information about pattern matching syntax, see the Ruby File.fnmatch documentation.`,
|
|
68
|
+
type: "array",
|
|
69
|
+
items: {
|
|
70
|
+
type: "string"
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
environmentVariables: {
|
|
74
|
+
title: "Environment Variables",
|
|
75
|
+
description: `Environment variables attached to the deployment environment`,
|
|
76
|
+
type: "object"
|
|
77
|
+
},
|
|
78
|
+
secrets: {
|
|
79
|
+
title: "Deployment Secrets",
|
|
80
|
+
description: `Secrets attached to the deployment environment`,
|
|
81
|
+
type: "object"
|
|
82
|
+
},
|
|
83
|
+
token: {
|
|
84
|
+
title: "Authentication Token",
|
|
85
|
+
type: "string",
|
|
86
|
+
description: "The token to use for authorization to GitHub"
|
|
87
|
+
},
|
|
88
|
+
waitTimer: {
|
|
89
|
+
title: "Wait Timer",
|
|
90
|
+
type: "integer",
|
|
91
|
+
description: "The time to wait before creating or updating the environment (in milliseconds)"
|
|
92
|
+
},
|
|
93
|
+
preventSelfReview: {
|
|
94
|
+
title: "Prevent Self Review",
|
|
95
|
+
type: "boolean",
|
|
96
|
+
description: "Whether to prevent self-review for this environment"
|
|
97
|
+
},
|
|
98
|
+
reviewers: {
|
|
99
|
+
title: "Reviewers",
|
|
100
|
+
type: "array",
|
|
101
|
+
description: "Reviewers for this environment",
|
|
102
|
+
items: {
|
|
103
|
+
type: "string"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
async handler(ctx) {
|
|
110
|
+
const {
|
|
111
|
+
repoUrl,
|
|
112
|
+
name,
|
|
113
|
+
deploymentBranchPolicy,
|
|
114
|
+
customBranchPolicyNames,
|
|
115
|
+
customTagPolicyNames,
|
|
116
|
+
environmentVariables,
|
|
117
|
+
secrets,
|
|
118
|
+
token: providedToken,
|
|
119
|
+
waitTimer,
|
|
120
|
+
preventSelfReview,
|
|
121
|
+
reviewers
|
|
122
|
+
} = ctx.input;
|
|
123
|
+
await new Promise((resolve) => setTimeout(resolve, 2e3));
|
|
124
|
+
const octokitOptions = await helpers.getOctokitOptions({
|
|
125
|
+
integrations,
|
|
126
|
+
token: providedToken,
|
|
127
|
+
repoUrl
|
|
128
|
+
});
|
|
129
|
+
const { owner, repo } = pluginScaffolderNode.parseRepoUrl(repoUrl, integrations);
|
|
130
|
+
if (!owner) {
|
|
131
|
+
throw new errors.InputError(`No owner provided for repo ${repoUrl}`);
|
|
132
|
+
}
|
|
133
|
+
const client = new octokit.Octokit(octokitOptions);
|
|
134
|
+
const repository = await client.rest.repos.get({
|
|
135
|
+
owner,
|
|
136
|
+
repo
|
|
137
|
+
});
|
|
138
|
+
const githubReviewers = [];
|
|
139
|
+
if (reviewers) {
|
|
140
|
+
let reviewersEntityRefs = [];
|
|
141
|
+
const catalogResponse = await catalogClient?.getEntitiesByRefs({
|
|
142
|
+
entityRefs: reviewers
|
|
143
|
+
});
|
|
144
|
+
if (catalogResponse?.items?.length) {
|
|
145
|
+
reviewersEntityRefs = catalogResponse.items;
|
|
146
|
+
}
|
|
147
|
+
for (const reviewerEntityRef of reviewersEntityRefs) {
|
|
148
|
+
if (reviewerEntityRef?.kind === "User") {
|
|
149
|
+
try {
|
|
150
|
+
const user = await client.rest.users.getByUsername({
|
|
151
|
+
username: reviewerEntityRef.metadata.name
|
|
152
|
+
});
|
|
153
|
+
githubReviewers.push({
|
|
154
|
+
type: "User",
|
|
155
|
+
id: user.data.id
|
|
156
|
+
});
|
|
157
|
+
} catch (error) {
|
|
158
|
+
ctx.logger.error("User not found:", error);
|
|
159
|
+
}
|
|
160
|
+
} else if (reviewerEntityRef?.kind === "Group") {
|
|
161
|
+
try {
|
|
162
|
+
const team = await client.rest.teams.getByName({
|
|
163
|
+
org: owner,
|
|
164
|
+
team_slug: reviewerEntityRef.metadata.name
|
|
165
|
+
});
|
|
166
|
+
githubReviewers.push({
|
|
167
|
+
type: "Team",
|
|
168
|
+
id: team.data.id
|
|
169
|
+
});
|
|
170
|
+
} catch (error) {
|
|
171
|
+
ctx.logger.error("Team not found:", error);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
await client.rest.repos.createOrUpdateEnvironment({
|
|
177
|
+
owner,
|
|
178
|
+
repo,
|
|
179
|
+
environment_name: name,
|
|
180
|
+
deployment_branch_policy: deploymentBranchPolicy ?? null,
|
|
181
|
+
wait_timer: waitTimer ?? 0,
|
|
182
|
+
prevent_self_review: preventSelfReview ?? false,
|
|
183
|
+
reviewers: githubReviewers.length ? githubReviewers : null
|
|
184
|
+
});
|
|
185
|
+
if (customBranchPolicyNames) {
|
|
186
|
+
for (const item of customBranchPolicyNames) {
|
|
187
|
+
await client.rest.repos.createDeploymentBranchPolicy({
|
|
188
|
+
owner,
|
|
189
|
+
repo,
|
|
190
|
+
type: "branch",
|
|
191
|
+
environment_name: name,
|
|
192
|
+
name: item
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (customTagPolicyNames) {
|
|
197
|
+
for (const item of customTagPolicyNames) {
|
|
198
|
+
await client.rest.repos.createDeploymentBranchPolicy({
|
|
199
|
+
owner,
|
|
200
|
+
repo,
|
|
201
|
+
type: "tag",
|
|
202
|
+
environment_name: name,
|
|
203
|
+
name: item
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (const [key, value] of Object.entries(environmentVariables ?? {})) {
|
|
208
|
+
await client.rest.actions.createEnvironmentVariable({
|
|
209
|
+
repository_id: repository.data.id,
|
|
210
|
+
owner,
|
|
211
|
+
repo,
|
|
212
|
+
environment_name: name,
|
|
213
|
+
name: key,
|
|
214
|
+
value
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
if (secrets) {
|
|
218
|
+
const publicKeyResponse = await client.rest.actions.getEnvironmentPublicKey({
|
|
219
|
+
repository_id: repository.data.id,
|
|
220
|
+
owner,
|
|
221
|
+
repo,
|
|
222
|
+
environment_name: name
|
|
223
|
+
});
|
|
224
|
+
await Sodium__default.default.ready;
|
|
225
|
+
const binaryKey = Sodium__default.default.from_base64(
|
|
226
|
+
publicKeyResponse.data.key,
|
|
227
|
+
Sodium__default.default.base64_variants.ORIGINAL
|
|
228
|
+
);
|
|
229
|
+
for (const [key, value] of Object.entries(secrets)) {
|
|
230
|
+
const binarySecret = Sodium__default.default.from_string(value);
|
|
231
|
+
const encryptedBinarySecret = Sodium__default.default.crypto_box_seal(
|
|
232
|
+
binarySecret,
|
|
233
|
+
binaryKey
|
|
234
|
+
);
|
|
235
|
+
const encryptedBase64Secret = Sodium__default.default.to_base64(
|
|
236
|
+
encryptedBinarySecret,
|
|
237
|
+
Sodium__default.default.base64_variants.ORIGINAL
|
|
238
|
+
);
|
|
239
|
+
await client.rest.actions.createOrUpdateEnvironmentSecret({
|
|
240
|
+
repository_id: repository.data.id,
|
|
241
|
+
owner,
|
|
242
|
+
repo,
|
|
243
|
+
environment_name: name,
|
|
244
|
+
secret_name: key,
|
|
245
|
+
encrypted_value: encryptedBase64Secret,
|
|
246
|
+
key_id: publicKeyResponse.data.key_id
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
exports.createGithubEnvironmentAction = createGithubEnvironmentAction;
|
|
255
|
+
//# sourceMappingURL=githubEnvironment.cjs.js.map
|