From 2b15cb4aa0a6afe04b379e55b707118f49d9e867 Mon Sep 17 00:00:00 2001 From: "Lukas Fruntke (external expert on behalf of DB Netz)" Date: Tue, 4 Apr 2023 09:49:50 +0200 Subject: [PATCH] Add Commit Hash Output for Scaffolder Signed-off-by: Lukas Fruntke (external expert on behalf of DB Netz) --- .changeset/dull-birds-lie.md | 5 +++++ .changeset/quick-plants-live.md | 5 +++++ packages/backend-common/package.json | 2 +- packages/backend-common/src/scm/git.ts | 3 +-- .../src/features/Patch/PatchBody.tsx | 2 +- .../scaffolder/actions/builtin/github/helpers.ts | 6 ++++-- .../src/scaffolder/actions/builtin/helpers.test.ts | 6 ++++-- .../src/scaffolder/actions/builtin/helpers.ts | 13 ++++++++----- .../src/scaffolder/actions/builtin/publish/azure.ts | 3 ++- .../scaffolder/actions/builtin/publish/bitbucket.ts | 3 ++- .../actions/builtin/publish/bitbucketCloud.ts | 3 ++- .../actions/builtin/publish/bitbucketServer.ts | 3 ++- .../scaffolder/actions/builtin/publish/gerrit.ts | 3 ++- .../scaffolder/actions/builtin/publish/github.ts | 3 ++- .../scaffolder/actions/builtin/publish/gitlab.ts | 3 ++- yarn.lock | 4 ++-- 16 files changed, 45 insertions(+), 22 deletions(-) create mode 100644 .changeset/dull-birds-lie.md create mode 100644 .changeset/quick-plants-live.md diff --git a/.changeset/dull-birds-lie.md b/.changeset/dull-birds-lie.md new file mode 100644 index 0000000000..addb988fb8 --- /dev/null +++ b/.changeset/dull-birds-lie.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': minor +--- + +**BREAKING** The non-PR/MR Git Actions now return the commitHash of the commit pushed as a new output called `commitHash` diff --git a/.changeset/quick-plants-live.md b/.changeset/quick-plants-live.md new file mode 100644 index 0000000000..0cfba91314 --- /dev/null +++ b/.changeset/quick-plants-live.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-common': minor +--- + +**BREAKING** The git commit helper now returns the commitHash of the commit created diff --git a/packages/backend-common/package.json b/packages/backend-common/package.json index 4b7789a04d..373d664aa9 100644 --- a/packages/backend-common/package.json +++ b/packages/backend-common/package.json @@ -82,7 +82,7 @@ "fs-extra": "10.1.0", "git-url-parse": "^13.0.0", "helmet": "^6.0.0", - "isomorphic-git": "^1.8.0", + "isomorphic-git": "^1.21.0", "jose": "^4.6.0", "keyv": "^4.5.2", "knex": "^2.0.0", diff --git a/packages/backend-common/src/scm/git.ts b/packages/backend-common/src/scm/git.ts index 1c33b89ef6..8317c77d1e 100644 --- a/packages/backend-common/src/scm/git.ts +++ b/packages/backend-common/src/scm/git.ts @@ -111,8 +111,7 @@ export class Git { this.config.logger?.info( `Committing file to repo {dir=${dir},message=${message}}`, ); - - return git.commit({ fs, dir, message, author, committer }); + return await git.commit({ fs, dir, message, author, committer }); } /** https://isomorphic-git.org/docs/en/clone */ diff --git a/plugins/git-release-manager/src/features/Patch/PatchBody.tsx b/plugins/git-release-manager/src/features/Patch/PatchBody.tsx index c5429e389a..790b704487 100644 --- a/plugins/git-release-manager/src/features/Patch/PatchBody.tsx +++ b/plugins/git-release-manager/src/features/Patch/PatchBody.tsx @@ -182,7 +182,7 @@ export const PatchBody = ({ // The selected patch commit's sha is included in the commit message, // which means it's part of a previous patch releaseBranchCommit.commit.message.includes( - getPatchCommitSuffix({ commitSha: commit.sha }), + getPatchCommitSuffix({ commitHash: commit.sha }), ), ); const hasNoParent = !commit.firstParentSha; diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/helpers.ts index 75c8f25d8b..ab5710facd 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/helpers.ts @@ -293,7 +293,7 @@ export async function initRepoPushAndProtect( gitAuthorEmail?: string, dismissStaleReviews?: boolean, requiredCommitSigning?: boolean, -) { +): Promise<{ commitHash: string }> { const gitAuthorInfo = { name: gitAuthorName ? gitAuthorName @@ -307,7 +307,7 @@ export async function initRepoPushAndProtect( ? gitCommitMessage : config.getOptionalString('scaffolder.defaultCommitMessage'); - await initRepoAndPush({ + const commitHash = await initRepoAndPush({ dir: getRepoSourceDirectory(workspacePath, sourcePath), remoteUrl, defaultBranch, @@ -346,6 +346,8 @@ export async function initRepoPushAndProtect( ); } } + + return { commitHash }; } function extractCollaboratorName( diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/helpers.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/helpers.test.ts index b5a375d83d..63053725f3 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/helpers.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/helpers.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Git, getVoidLogger } from '@backstage/backend-common'; +import { getVoidLogger, Git } from '@backstage/backend-common'; import { commitAndPushRepo, initRepoAndPush } from './helpers'; jest.mock('@backstage/backend-common', () => ({ @@ -23,7 +23,9 @@ jest.mock('@backstage/backend-common', () => ({ init: jest.fn(), add: jest.fn(), checkout: jest.fn(), - commit: jest.fn(), + commit: jest.fn().mockResolvedValue({ + commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6', + }), fetch: jest.fn(), addRemote: jest.fn(), push: jest.fn(), diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/helpers.ts index 8bed279a1b..0ea86000d1 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/helpers.ts @@ -91,7 +91,7 @@ export async function initRepoAndPush({ defaultBranch?: string; commitMessage?: string; gitAuthorInfo?: { name?: string; email?: string }; -}): Promise { +}): Promise<{ commitHash: string }> { const git = Git.fromAuth({ ...auth, logger, @@ -110,13 +110,12 @@ export async function initRepoAndPush({ email: gitAuthorInfo?.email ?? 'scaffolder@backstage.io', }; - await git.commit({ + const commitHash = await git.commit({ dir, message: commitMessage, author: authorInfo, committer: authorInfo, }); - await git.addRemote({ dir, url: remoteUrl, @@ -127,6 +126,8 @@ export async function initRepoAndPush({ dir, remote: 'origin', }); + + return { commitHash }; } export async function commitAndPushRepo({ @@ -148,7 +149,7 @@ export async function commitAndPushRepo({ gitAuthorInfo?: { name?: string; email?: string }; branch?: string; remoteRef?: string; -}): Promise { +}): Promise<{ commitHash: string }> { const git = Git.fromAuth({ ...auth, logger, @@ -164,7 +165,7 @@ export async function commitAndPushRepo({ email: gitAuthorInfo?.email ?? 'scaffolder@backstage.io', }; - await git.commit({ + const commitHash = await git.commit({ dir, message: commitMessage, author: authorInfo, @@ -176,6 +177,8 @@ export async function commitAndPushRepo({ remote: 'origin', remoteRef: remoteRef ?? `refs/heads/${branch}`, }); + + return { commitHash }; } type BranchProtectionOptions = { diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/azure.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/azure.ts index 083c18c94f..6686ea3dba 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/azure.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/azure.ts @@ -183,7 +183,7 @@ export function createPublishAzureAction(options: { : config.getOptionalString('scaffolder.defaultAuthor.email'), }; - await initRepoAndPush({ + const commitResult = await initRepoAndPush({ dir: getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath), remoteUrl, defaultBranch, @@ -198,6 +198,7 @@ export function createPublishAzureAction(options: { gitAuthorInfo, }); + ctx.output('commitHash', commitResult?.commitHash); ctx.output('remoteUrl', remoteUrl); ctx.output('repoContentsUrl', repoContentsUrl); ctx.output('repositoryId', repositoryId); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucket.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucket.ts index 74e5e8c1ab..72c7f065ab 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucket.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucket.ts @@ -391,7 +391,7 @@ export function createPublishBitbucketAction(options: { }; } - await initRepoAndPush({ + const commitResult = await initRepoAndPush({ dir: getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath), remoteUrl, auth, @@ -407,6 +407,7 @@ export function createPublishBitbucketAction(options: { await performEnableLFS({ authorization, host, project, repo }); } + ctx.output('commitHash', commitResult?.commitHash); ctx.output('remoteUrl', remoteUrl); ctx.output('repoContentsUrl', repoContentsUrl); }, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketCloud.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketCloud.ts index 2c17bf2dbb..419abdd8f0 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketCloud.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketCloud.ts @@ -262,7 +262,7 @@ export function createPublishBitbucketCloudAction(options: { }; } - await initRepoAndPush({ + const commitResult = await initRepoAndPush({ dir: getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath), remoteUrl, auth, @@ -274,6 +274,7 @@ export function createPublishBitbucketCloudAction(options: { gitAuthorInfo, }); + ctx.output('commitHash', commitResult?.commitHash); ctx.output('remoteUrl', remoteUrl); ctx.output('repoContentsUrl', repoContentsUrl); }, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.ts index f2f581eadb..c990d14086 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.ts @@ -279,7 +279,7 @@ export function createPublishBitbucketServerAction(options: { password: authConfig.password!, }; - await initRepoAndPush({ + const commitResult = await initRepoAndPush({ dir: getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath), remoteUrl, auth, @@ -295,6 +295,7 @@ export function createPublishBitbucketServerAction(options: { await performEnableLFS({ authorization, host, project, repo }); } + ctx.output('commitHash', commitResult?.commitHash); ctx.output('remoteUrl', remoteUrl); ctx.output('repoContentsUrl', repoContentsUrl); }, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.ts index b5bcc31c62..e72d1d9011 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.ts @@ -201,7 +201,7 @@ export function createPublishGerritAction(options: { }; const remoteUrl = `${integrationConfig.config.cloneUrl}/a/${repo}`; - await initRepoAndPush({ + const commitResult = await initRepoAndPush({ dir: getRepoSourceDirectory(ctx.workspacePath, sourcePath), remoteUrl, auth, @@ -213,6 +213,7 @@ export function createPublishGerritAction(options: { const repoContentsUrl = `${integrationConfig.config.gitilesBaseUrl}/${repo}/+/refs/heads/${defaultBranch}`; ctx.output('remoteUrl', remoteUrl); + ctx.output('commitHash', commitResult?.commitHash); ctx.output('repoContentsUrl', repoContentsUrl); }, }); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts index 185239c73f..cefb57270c 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts @@ -236,7 +236,7 @@ export function createPublishGithubAction(options: { const remoteUrl = newRepo.clone_url; const repoContentsUrl = `${newRepo.html_url}/blob/${defaultBranch}`; - await initRepoPushAndProtect( + const commitResult = await initRepoPushAndProtect( remoteUrl, octokitOptions.auth, ctx.workspacePath, @@ -263,6 +263,7 @@ export function createPublishGithubAction(options: { requiredCommitSigning, ); + ctx.output('commitHash', commitResult?.commitHash); ctx.output('remoteUrl', remoteUrl); ctx.output('repoContentsUrl', repoContentsUrl); }, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlab.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlab.ts index 2e7520b77f..4b2f7ee727 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlab.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlab.ts @@ -212,7 +212,7 @@ export function createPublishGitlabAction(options: { ? gitAuthorEmail : config.getOptionalString('scaffolder.defaultAuthor.email'), }; - await initRepoAndPush({ + const commitResult = await initRepoAndPush({ dir: getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath), remoteUrl: http_url_to_repo as string, defaultBranch, @@ -227,6 +227,7 @@ export function createPublishGitlabAction(options: { gitAuthorInfo, }); + ctx.output('commitHash', commitResult?.commitHash); ctx.output('remoteUrl', remoteUrl); ctx.output('repoContentsUrl', repoContentsUrl); ctx.output('projectId', projectId); diff --git a/yarn.lock b/yarn.lock index 589db99d4e..eb25118c8f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3525,7 +3525,7 @@ __metadata: git-url-parse: ^13.0.0 helmet: ^6.0.0 http-errors: ^2.0.0 - isomorphic-git: ^1.8.0 + isomorphic-git: ^1.21.0 jose: ^4.6.0 keyv: ^4.5.2 knex: ^2.0.0 @@ -27103,7 +27103,7 @@ __metadata: languageName: node linkType: hard -"isomorphic-git@npm:^1.8.0": +"isomorphic-git@npm:^1.21.0, isomorphic-git@npm:^1.8.0": version: 1.23.0 resolution: "isomorphic-git@npm:1.23.0" dependencies: