mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(core): Set source control repository to track remote if ready (#9532)
This commit is contained in:
parent
ec0373f666
commit
dbaac82f79
|
@ -176,6 +176,7 @@ export class SourceControlGitService {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await this.git.addRemote(SOURCE_CONTROL_ORIGIN, sourceControlPreferences.repositoryUrl);
|
await this.git.addRemote(SOURCE_CONTROL_ORIGIN, sourceControlPreferences.repositoryUrl);
|
||||||
|
this.logger.debug(`Git remote added: ${sourceControlPreferences.repositoryUrl}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if ((error as Error).message.includes('remote origin already exists')) {
|
if ((error as Error).message.includes('remote origin already exists')) {
|
||||||
this.logger.debug(`Git remote already exists: ${(error as Error).message}`);
|
this.logger.debug(`Git remote already exists: ${(error as Error).message}`);
|
||||||
|
@ -189,6 +190,9 @@ export class SourceControlGitService {
|
||||||
: SOURCE_CONTROL_DEFAULT_NAME,
|
: SOURCE_CONTROL_DEFAULT_NAME,
|
||||||
user.email ?? SOURCE_CONTROL_DEFAULT_EMAIL,
|
user.email ?? SOURCE_CONTROL_DEFAULT_EMAIL,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await this.trackRemoteIfReady(sourceControlPreferences.branchName);
|
||||||
|
|
||||||
if (sourceControlPreferences.initRepo) {
|
if (sourceControlPreferences.initRepo) {
|
||||||
try {
|
try {
|
||||||
const branches = await this.getBranches();
|
const branches = await this.getBranches();
|
||||||
|
@ -201,6 +205,28 @@ export class SourceControlGitService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If this is a new local repository being set up after remote is ready,
|
||||||
|
* then set this local to start tracking remote's target branch.
|
||||||
|
*/
|
||||||
|
private async trackRemoteIfReady(targetBranch: string) {
|
||||||
|
if (!this.git) return;
|
||||||
|
|
||||||
|
await this.fetch();
|
||||||
|
|
||||||
|
const { currentBranch, branches: remoteBranches } = await this.getBranches();
|
||||||
|
|
||||||
|
if (!currentBranch && remoteBranches.some((b) => b === targetBranch)) {
|
||||||
|
await this.git.checkout(targetBranch);
|
||||||
|
|
||||||
|
const upstream = [SOURCE_CONTROL_ORIGIN, targetBranch].join('/');
|
||||||
|
|
||||||
|
await this.git.branch([`--set-upstream-to=${upstream}`, targetBranch]);
|
||||||
|
|
||||||
|
this.logger.info('Set local git repository to track remote', { upstream });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async setGitUserDetails(name: string, email: string): Promise<void> {
|
async setGitUserDetails(name: string, email: string): Promise<void> {
|
||||||
if (!this.git) {
|
if (!this.git) {
|
||||||
throw new ApplicationError('Git is not initialized (setGitUserDetails)');
|
throw new ApplicationError('Git is not initialized (setGitUserDetails)');
|
||||||
|
|
39
packages/cli/test/unit/GitService.test.ts
Normal file
39
packages/cli/test/unit/GitService.test.ts
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import { SourceControlGitService } from '@/environments/sourceControl/sourceControlGit.service.ee';
|
||||||
|
import { mock } from 'jest-mock-extended';
|
||||||
|
import type { SourceControlPreferences } from '@/environments/sourceControl/types/sourceControlPreferences';
|
||||||
|
import type { User } from '@/databases/entities/User';
|
||||||
|
import type { SimpleGit } from 'simple-git';
|
||||||
|
|
||||||
|
describe('GitService', () => {
|
||||||
|
describe('initRepository', () => {
|
||||||
|
describe('when local repo is set up after remote is ready', () => {
|
||||||
|
it('should track remote', async () => {
|
||||||
|
/**
|
||||||
|
* Arrange
|
||||||
|
*/
|
||||||
|
const gitService = new SourceControlGitService(mock(), mock(), mock());
|
||||||
|
const prefs = mock<SourceControlPreferences>({ branchName: 'main' });
|
||||||
|
const user = mock<User>();
|
||||||
|
const git = mock<SimpleGit>();
|
||||||
|
const checkoutSpy = jest.spyOn(git, 'checkout');
|
||||||
|
const branchSpy = jest.spyOn(git, 'branch');
|
||||||
|
gitService.git = git;
|
||||||
|
jest.spyOn(gitService, 'setGitSshCommand').mockResolvedValue();
|
||||||
|
jest
|
||||||
|
.spyOn(gitService, 'getBranches')
|
||||||
|
.mockResolvedValue({ currentBranch: '', branches: ['main'] });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Act
|
||||||
|
*/
|
||||||
|
await gitService.initRepository(prefs, user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert
|
||||||
|
*/
|
||||||
|
expect(checkoutSpy).toHaveBeenCalledWith('main');
|
||||||
|
expect(branchSpy).toHaveBeenCalledWith(['--set-upstream-to=origin/main', 'main']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue