From bdcb2990b321d2fc1e9379f637e3e6ab97f20126 Mon Sep 17 00:00:00 2001 From: Christopher Diaz Date: Wed, 19 Jun 2024 15:28:07 -0400 Subject: [PATCH 1/3] feat: support tag based policies Signed-off-by: Christopher Diaz --- .../src/actions/githubEnvironment.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts index 7f6f9f7fa3..e821ff0cd6 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts @@ -44,6 +44,7 @@ export function createGithubEnvironmentAction(options: { custom_branch_policies: boolean; }; customBranchPolicyNames?: string[]; + customTagPolicyNames?: string[]; environmentVariables?: { [key: string]: string }; secrets?: { [key: string]: string }; token?: string; @@ -94,6 +95,16 @@ export function createGithubEnvironmentAction(options: { type: 'string', }, }, + customTagPolicyNames: { + title: 'Custom Tag Policy Name', + description: `The name pattern that tags must match in order to deploy to the environment. + + 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.`, + type: 'array', + items: { + type: 'string', + }, + }, environmentVariables: { title: 'Environment Variables', description: `Environment variables attached to the deployment environment`, @@ -118,6 +129,7 @@ export function createGithubEnvironmentAction(options: { name, deploymentBranchPolicy, customBranchPolicyNames, + customTagPolicyNames, environmentVariables, secrets, token: providedToken, @@ -153,6 +165,19 @@ export function createGithubEnvironmentAction(options: { await client.rest.repos.createDeploymentBranchPolicy({ owner: owner, repo: repo, + type: 'branch', + environment_name: name, + name: item, + }); + } + } + + if (customTagPolicyNames) { + for (const item of customTagPolicyNames) { + await client.rest.repos.createDeploymentBranchPolicy({ + owner: owner, + repo: repo, + type: 'tag', environment_name: name, name: item, }); From 70c4b3625db012293b6f3c638e4f60eb61ecc5a5 Mon Sep 17 00:00:00 2001 From: Christopher Diaz Date: Wed, 19 Jun 2024 15:32:42 -0400 Subject: [PATCH 2/3] feat: support tag based policies Signed-off-by: Christopher Diaz --- .changeset/short-flowers-cry.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/short-flowers-cry.md diff --git a/.changeset/short-flowers-cry.md b/.changeset/short-flowers-cry.md new file mode 100644 index 0000000000..bf2c354465 --- /dev/null +++ b/.changeset/short-flowers-cry.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend-module-github': minor +--- + +Adds support for custom tag policies when creating GitHub environments. From fc6db7af1d84959b34b47655cda3c27b0a9baa63 Mon Sep 17 00:00:00 2001 From: Christopher Diaz Date: Thu, 20 Jun 2024 17:10:40 -0400 Subject: [PATCH 3/3] update tests and API reports Signed-off-by: Christopher Diaz --- .../api-report.md | 1 + .../githubEnvironment.examples.test.ts | 2 + .../src/actions/githubEnvironment.test.ts | 48 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/plugins/scaffolder-backend-module-github/api-report.md b/plugins/scaffolder-backend-module-github/api-report.md index beabdfaaf6..94b642bdbf 100644 --- a/plugins/scaffolder-backend-module-github/api-report.md +++ b/plugins/scaffolder-backend-module-github/api-report.md @@ -77,6 +77,7 @@ export function createGithubEnvironmentAction(options: { } | undefined; customBranchPolicyNames?: string[] | undefined; + customTagPolicyNames?: string[] | undefined; environmentVariables?: | { [key: string]: string; diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.examples.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.examples.test.ts index ecb4e9f092..9d4395eb7a 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.examples.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.examples.test.ts @@ -171,6 +171,7 @@ describe('github:environment:create examples', () => { repo: 'repository', environment_name: 'envname', name: 'main', + type: 'branch', }); expect( @@ -180,6 +181,7 @@ describe('github:environment:create examples', () => { repo: 'repository', environment_name: 'envname', name: '*.*.*', + type: 'branch', }); expect( diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.test.ts index 16872525b4..a8d8c4ef2e 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.test.ts @@ -121,6 +121,7 @@ describe('github:environment:create', () => { }, }); }); + it('should work specify deploymentBranchPolicy custom', async () => { await action.handler({ ...mockContext, @@ -153,6 +154,7 @@ describe('github:environment:create', () => { repo: 'repository', environment_name: 'envname', name: 'main', + type: 'branch', }); expect( mockOctokit.rest.repos.createDeploymentBranchPolicy, @@ -161,6 +163,52 @@ describe('github:environment:create', () => { repo: 'repository', environment_name: 'envname', name: '*.*.*', + type: 'branch', + }); + }); + + it('should work specify deploymentTagPolicy custom', async () => { + await action.handler({ + ...mockContext, + input: { + ...mockContext.input, + deploymentBranchPolicy: { + protected_branches: false, + custom_branch_policies: true, + }, + customTagPolicyNames: ['main', '*.*.*'], + }, + }); + + expect( + mockOctokit.rest.repos.createOrUpdateEnvironment, + ).toHaveBeenCalledWith({ + owner: 'owner', + repo: 'repository', + environment_name: 'envname', + deployment_branch_policy: { + protected_branches: false, + custom_branch_policies: true, + }, + }); + + expect( + mockOctokit.rest.repos.createDeploymentBranchPolicy, + ).toHaveBeenCalledWith({ + owner: 'owner', + repo: 'repository', + environment_name: 'envname', + name: 'main', + type: 'tag', + }); + expect( + mockOctokit.rest.repos.createDeploymentBranchPolicy, + ).toHaveBeenCalledWith({ + owner: 'owner', + repo: 'repository', + environment_name: 'envname', + name: '*.*.*', + type: 'tag', }); });