Add Commit Hash Output for Scaffolder

Signed-off-by: Lukas Fruntke (external expert on behalf of DB Netz) <lukas.fruntke-extern@deutschebahn.com>
This commit is contained in:
Lukas Fruntke (external expert on behalf of DB Netz)
2023-04-04 09:49:50 +02:00
parent 7e4942d1cd
commit 2b15cb4aa0
16 changed files with 45 additions and 22 deletions
+5
View File
@@ -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`
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': minor
---
**BREAKING** The git commit helper now returns the commitHash of the commit created
+1 -1
View File
@@ -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",
+1 -2
View File
@@ -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 */
@@ -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;
@@ -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(
@@ -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(),
@@ -91,7 +91,7 @@ export async function initRepoAndPush({
defaultBranch?: string;
commitMessage?: string;
gitAuthorInfo?: { name?: string; email?: string };
}): Promise<void> {
}): 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<void> {
}): 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 = {
@@ -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);
@@ -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);
},
@@ -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);
},
@@ -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);
},
@@ -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);
},
});
@@ -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);
},
@@ -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);
+2 -2
View File
@@ -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: