Skip to content

Commit

Permalink
Merge pull request #25333 from codingdiaz/tag-based-github-env-protec…
Browse files Browse the repository at this point in the history
…tions

feat: support tag based policies
  • Loading branch information
benjdlambert committed Jun 24, 2024
2 parents 172f473 + fc6db7a commit b9cc3c9
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-flowers-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-github': minor
---

Adds support for custom tag policies when creating GitHub environments.
1 change: 1 addition & 0 deletions plugins/scaffolder-backend-module-github/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export function createGithubEnvironmentAction(options: {
}
| undefined;
customBranchPolicyNames?: string[] | undefined;
customTagPolicyNames?: string[] | undefined;
environmentVariables?:
| {
[key: string]: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ describe('github:environment:create examples', () => {
repo: 'repository',
environment_name: 'envname',
name: 'main',
type: 'branch',
});

expect(
Expand All @@ -180,6 +181,7 @@ describe('github:environment:create examples', () => {
repo: 'repository',
environment_name: 'envname',
name: '*.*.*',
type: 'branch',
});

expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe('github:environment:create', () => {
},
});
});

it('should work specify deploymentBranchPolicy custom', async () => {
await action.handler({
...mockContext,
Expand Down Expand Up @@ -153,6 +154,52 @@ describe('github:environment:create', () => {
repo: 'repository',
environment_name: 'envname',
name: 'main',
type: 'branch',
});
expect(
mockOctokit.rest.repos.createDeploymentBranchPolicy,
).toHaveBeenCalledWith({
owner: 'owner',
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,
Expand All @@ -161,6 +208,7 @@ describe('github:environment:create', () => {
repo: 'repository',
environment_name: 'envname',
name: '*.*.*',
type: 'tag',
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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`,
Expand All @@ -118,6 +129,7 @@ export function createGithubEnvironmentAction(options: {
name,
deploymentBranchPolicy,
customBranchPolicyNames,
customTagPolicyNames,
environmentVariables,
secrets,
token: providedToken,
Expand Down Expand Up @@ -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,
});
Expand Down

0 comments on commit b9cc3c9

Please sign in to comment.