From 8eb75423a54a1f2fcb05c60b32bb7db2c1d37cd8 Mon Sep 17 00:00:00 2001 From: Efren Aguilar Date: Thu, 29 Aug 2024 15:13:43 -0700 Subject: [PATCH] Add custom prop functionality Signed-off-by: Efren Aguilar --- .../src/actions/github.test.ts | 57 +++++++++++++++++++ .../src/actions/github.ts | 4 ++ .../src/actions/githubRepoCreate.test.ts | 57 +++++++++++++++++++ .../src/actions/githubRepoCreate.ts | 4 ++ .../src/actions/helpers.ts | 3 + .../src/actions/inputProperties.ts | 8 +++ 6 files changed, 133 insertions(+) diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts index 31ec7ecc90..3af922eb4f 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts @@ -287,6 +287,36 @@ describe('publish:github', () => { has_projects: false, has_issues: false, }); + + await action.handler({ + ...mockContext, + input: { + ...mockContext.input, + customProperties: { + foo: 'bar', + foo2: 'bar2', + }, + }, + }); + + expect(mockOctokit.rest.repos.createInOrg).toHaveBeenCalledWith({ + description: 'description', + name: 'repo', + org: 'owner', + private: true, + delete_branch_on_merge: false, + allow_squash_merge: true, + squash_merge_commit_title: 'COMMIT_OR_PR_TITLE', + squash_merge_commit_message: 'COMMIT_MESSAGES', + allow_merge_commit: true, + allow_rebase_merge: true, + allow_auto_merge: false, + visibility: 'private', + custom_properties: { + foo: 'bar', + foo2: 'bar2', + }, + }); }); it('should call the githubApis with the correct values for createForAuthenticatedUser', async () => { @@ -412,6 +442,33 @@ describe('publish:github', () => { has_projects: false, has_issues: false, }); + + // Custom properties on user repos should be ignored + await action.handler({ + ...mockContext, + input: { + ...mockContext.input, + customProperties: { + foo: 'bar', + foo2: 'bar2', + }, + }, + }); + + expect( + mockOctokit.rest.repos.createForAuthenticatedUser, + ).toHaveBeenCalledWith({ + description: 'description', + name: 'repo', + private: true, + delete_branch_on_merge: false, + allow_squash_merge: true, + squash_merge_commit_title: 'COMMIT_OR_PR_TITLE', + squash_merge_commit_message: 'COMMIT_MESSAGES', + allow_merge_commit: true, + allow_rebase_merge: true, + allow_auto_merge: false, + }); }); it('should call initRepoAndPush with the correct values', async () => { diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.ts b/plugins/scaffolder-backend-module-github/src/actions/github.ts index c8b22de7e7..982c68d80f 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.ts @@ -115,6 +115,7 @@ export function createPublishGithubAction(options: { includeClaimKeys?: string[]; }; requiredCommitSigning?: boolean; + customProperties?: { [key: string]: string }; }>({ id: 'publish:github', description: @@ -164,6 +165,7 @@ export function createPublishGithubAction(options: { secrets: inputProps.secrets, oidcCustomization: inputProps.oidcCustomization, requiredCommitSigning: inputProps.requiredCommitSigning, + customProperties: inputProps.customProperties, }, }, output: { @@ -213,6 +215,7 @@ export function createPublishGithubAction(options: { secrets, oidcCustomization, token: providedToken, + customProperties, requiredCommitSigning = false, } = ctx.input; @@ -253,6 +256,7 @@ export function createPublishGithubAction(options: { repoVariables, secrets, oidcCustomization, + customProperties, ctx.logger, ); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts index 1750acdb9a..66d54366c5 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts @@ -235,6 +235,36 @@ describe('github:repo:create', () => { has_projects: false, has_issues: false, }); + + await action.handler({ + ...mockContext, + input: { + ...mockContext.input, + customProperties: { + foo: 'bar', + foo2: 'bar2', + }, + }, + }); + + expect(mockOctokit.rest.repos.createInOrg).toHaveBeenCalledWith({ + description: 'description', + name: 'repo', + org: 'owner', + private: true, + delete_branch_on_merge: false, + allow_squash_merge: true, + squash_merge_commit_title: 'COMMIT_OR_PR_TITLE', + squash_merge_commit_message: 'COMMIT_MESSAGES', + allow_merge_commit: true, + allow_rebase_merge: true, + allow_auto_merge: false, + visibility: 'private', + custom_properties: { + foo: 'bar', + foo2: 'bar2', + }, + }); }); it('should call the githubApis with the correct values for createForAuthenticatedUser', async () => { @@ -360,6 +390,33 @@ describe('github:repo:create', () => { has_projects: false, has_issues: false, }); + + // Custom properties on user repos should be ignored + await action.handler({ + ...mockContext, + input: { + ...mockContext.input, + customProperties: { + foo: 'bar', + foo2: 'bar2', + }, + }, + }); + + expect( + mockOctokit.rest.repos.createForAuthenticatedUser, + ).toHaveBeenCalledWith({ + description: 'description', + name: 'repo', + private: true, + delete_branch_on_merge: false, + allow_squash_merge: true, + squash_merge_commit_title: 'COMMIT_OR_PR_TITLE', + squash_merge_commit_message: 'COMMIT_MESSAGES', + allow_merge_commit: true, + allow_rebase_merge: true, + allow_auto_merge: false, + }); }); it('should add access for the team when it starts with the owner', async () => { diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts index 52bf9be9c0..1c3d035ee3 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts @@ -100,6 +100,7 @@ export function createGithubRepoCreateAction(options: { includeClaimKeys?: string[]; }; requireCommitSigning?: boolean; + customProperties?: { [key: string]: string }; }>({ id: 'github:repo:create', description: 'Creates a GitHub repository.', @@ -139,6 +140,7 @@ export function createGithubRepoCreateAction(options: { secrets: inputProps.secrets, oidcCustomization: inputProps.oidcCustomization, requiredCommitSigning: inputProps.requiredCommitSigning, + customProperties: inputProps.customProperties, }, }, output: { @@ -171,6 +173,7 @@ export function createGithubRepoCreateAction(options: { repoVariables, secrets, oidcCustomization, + customProperties, token: providedToken, } = ctx.input; @@ -211,6 +214,7 @@ export function createGithubRepoCreateAction(options: { repoVariables, secrets, oidcCustomization, + customProperties, ctx.logger, ); diff --git a/plugins/scaffolder-backend-module-github/src/actions/helpers.ts b/plugins/scaffolder-backend-module-github/src/actions/helpers.ts index eec6f73f92..0ae1cad3fb 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/helpers.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/helpers.ts @@ -148,6 +148,7 @@ export async function createGithubRepoWithCollaboratorsAndTopics( includeClaimKeys?: string[]; } | undefined, + customProperties: { [key: string]: string } | undefined, logger: LoggerService, ) { // eslint-disable-next-line testing-library/no-await-sync-queries @@ -179,6 +180,8 @@ export async function createGithubRepoWithCollaboratorsAndTopics( has_projects: hasProjects, has_wiki: hasWiki, has_issues: hasIssues, + // Custom properties only available on org repos + custom_properties: customProperties, }) : client.rest.repos.createForAuthenticatedUser({ name: repo, diff --git a/plugins/scaffolder-backend-module-github/src/actions/inputProperties.ts b/plugins/scaffolder-backend-module-github/src/actions/inputProperties.ts index f6fc592b72..4a52c1d69f 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/inputProperties.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/inputProperties.ts @@ -304,6 +304,13 @@ const oidcCustomization = { }, }; +const customProperties = { + title: 'Custom Repository Properties', + description: + 'Custom properties to be added to the repository (note, this only works for organization repositories)', + type: 'object', +}; + export { access }; export { allowMergeCommit }; export { allowRebaseMerge }; @@ -342,3 +349,4 @@ export { requiredCommitSigning }; export { repoVariables }; export { secrets }; export { oidcCustomization }; +export { customProperties };