({
projectId,
workspaceId,
message,
}: {
projectId: string;
workspaceId?: string;
message: string;
})
| 2504 | }; |
| 2505 | |
| 2506 | export const commitAndPushToGitRepoAction = async ({ |
| 2507 | projectId, |
| 2508 | workspaceId, |
| 2509 | message, |
| 2510 | }: { |
| 2511 | projectId: string; |
| 2512 | workspaceId?: string; |
| 2513 | message: string; |
| 2514 | }): Promise<CommitToGitRepoResult> => { |
| 2515 | await assertBranchOnOrigin('push'); |
| 2516 | const repo = await getGitRepository({ workspaceId, projectId }); |
| 2517 | |
| 2518 | // Validate credentials before committing to prevent orphaned local commits |
| 2519 | // when the subsequent push would fail due to authentication issues. |
| 2520 | try { |
| 2521 | await validateGitCredentials({ credentialsId: repo.credentialsId, uri: repo.uri }); |
| 2522 | } catch (e) { |
| 2523 | const errorMessage = e instanceof Error ? e.message : 'Authentication failed'; |
| 2524 | return { errors: [errorMessage] }; |
| 2525 | } |
| 2526 | |
| 2527 | try { |
| 2528 | // Flush DB changes to disk before committing |
| 2529 | await repoFileWatcherRegistry.flushNow(repo._id); |
| 2530 | await GitVCS.setAuthor(); |
| 2531 | await GitVCS.commit(message); |
| 2532 | |
| 2533 | let providerName = 'custom'; |
| 2534 | if (repo.credentialsId) { |
| 2535 | const credentials = await services.gitCredentials.getById(repo.credentialsId); |
| 2536 | invariant(credentials, 'Git Credentials not found'); |
| 2537 | providerName = credentials.provider; |
| 2538 | } |
| 2539 | |
| 2540 | trackAnalyticsEvent(AnalyticsEvent.vcsAction, { |
| 2541 | ...vcsEventProperties('git', 'commit'), |
| 2542 | providerName, |
| 2543 | repoId: repo._id, |
| 2544 | }); |
| 2545 | } catch (e) { |
| 2546 | const message = e instanceof Error ? e.message : 'Error while committing changes'; |
| 2547 | return { errors: [message] }; |
| 2548 | } |
| 2549 | |
| 2550 | let canPush = false; |
| 2551 | try { |
| 2552 | canPush = await GitVCS.canPush(repo.credentialsId); |
| 2553 | } catch (err) { |
| 2554 | // The commit above already succeeded — a failure here is just the |
| 2555 | // pushability check, so persist that the commit is still unpushed. |
| 2556 | await services.gitRepository.update(repo, { |
| 2557 | hasUnpushedChanges: true, |
| 2558 | }); |
| 2559 | |
| 2560 | if (err instanceof Errors.HttpError) { |
| 2561 | return { |
| 2562 | errors: [`${err.message}, ${err.data.response}`], |
| 2563 | pushFailedAfterCommit: true, |
no test coverage detected