feat(repos): 新增 检出指定 commit 功能。
This commit is contained in:
@ -3,26 +3,30 @@ import { getRepositoryToken } from '@nestjs/typeorm';
|
||||
import { Project } from '../projects/project.entity';
|
||||
import { ReposService } from './repos.service';
|
||||
import { ConfigService, ConfigModule } from '@nestjs/config';
|
||||
import { rm } from 'fs/promises';
|
||||
import { readFile, rm } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import configuration from '../commons/config/configuration';
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
|
||||
const getTest1Project = () =>
|
||||
({
|
||||
id: '1',
|
||||
sshUrl: 'ssh://gitea@git.ivanli.cc:7018/Fennec/test-1.git',
|
||||
name: 'test-1',
|
||||
} as Project);
|
||||
|
||||
describe('ReposService', () => {
|
||||
let service: ReposService;
|
||||
const repositoryMockFactory = jest.fn(() => ({
|
||||
findOneOrFail: jest.fn(
|
||||
(entity): Project => ({
|
||||
id: '1',
|
||||
// sshUrl: 'ssh://gitea@git.ivanli.cc:7018/ivan/test1.git',
|
||||
sshUrl: 'ssh://gitea@git.ivanli.cc:7018/Fennec/fennec-fe.git',
|
||||
name: 'test1',
|
||||
...getTest1Project(),
|
||||
...entity,
|
||||
}),
|
||||
),
|
||||
}));
|
||||
let workspacesRoot: string;
|
||||
afterEach(async () => {
|
||||
await rm(join(workspacesRoot, 'test1'), {
|
||||
await rm(service.getWorkspaceRoot(getTest1Project()), {
|
||||
recursive: true,
|
||||
}).catch(() => undefined);
|
||||
});
|
||||
@ -43,17 +47,20 @@ describe('ReposService', () => {
|
||||
}).compile();
|
||||
|
||||
service = module.get<ReposService>(ReposService);
|
||||
const configServer = module.get<ConfigService>(ConfigService);
|
||||
workspacesRoot = configServer.get<string>('workspaces.root');
|
||||
|
||||
await rm(join(workspacesRoot, 'test1'), {
|
||||
await rm(service.getWorkspaceRoot(getTest1Project()), {
|
||||
recursive: true,
|
||||
}).catch(() => undefined);
|
||||
}).catch((err) => {
|
||||
console.log('!!!!', err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
it('getWorkspaceRoot', () => {
|
||||
expect(service.getWorkspaceRoot(getTest1Project())).toBeDefined();
|
||||
});
|
||||
describe('listLogs', () => {
|
||||
it('should be return logs', async () => {
|
||||
const result = await service.listLogs({ projectId: '1' });
|
||||
@ -66,4 +73,61 @@ describe('ReposService', () => {
|
||||
expect(result).toBeDefined();
|
||||
}, 10_000);
|
||||
});
|
||||
describe('checkoutBranch', () => {
|
||||
it('should be checkout', async () => {
|
||||
await service.checkoutBranch(getTest1Project(), 'master');
|
||||
const filePath = join(
|
||||
service.getWorkspaceRoot(getTest1Project()),
|
||||
'README.md',
|
||||
);
|
||||
const text = await readFile(filePath, { encoding: 'utf-8' });
|
||||
expect(text).toMatch(/Commit 1/gi);
|
||||
}, 30_000);
|
||||
it('multiplexing workspace', async () => {
|
||||
await service.checkoutBranch(getTest1Project(), 'master');
|
||||
await service.checkoutBranch(getTest1Project(), 'branch-a');
|
||||
await service.checkoutBranch(getTest1Project(), 'branch-b');
|
||||
const filePath = join(
|
||||
service.getWorkspaceRoot(getTest1Project()),
|
||||
'branch-b.md',
|
||||
);
|
||||
const text = await readFile(filePath, { encoding: 'utf-8' });
|
||||
expect(text).toMatch(/Commit branch b/gi);
|
||||
}, 30_000);
|
||||
it('nonexistent branch', async () => {
|
||||
return expect(
|
||||
service.checkoutBranch(getTest1Project(), 'nonexistent'),
|
||||
).rejects.toBeInstanceOf(NotFoundException);
|
||||
}, 30_000);
|
||||
it('checkout the specified version', async () => {
|
||||
await service.checkoutBranch(getTest1Project(), 'master');
|
||||
const filePath = join(
|
||||
service.getWorkspaceRoot(getTest1Project()),
|
||||
'README.md',
|
||||
);
|
||||
const text = await readFile(filePath, { encoding: 'utf-8' });
|
||||
expect(text).toMatch(/Commit 1/gi);
|
||||
}, 30_000);
|
||||
});
|
||||
|
||||
describe('checkoutCommit', () => {
|
||||
it('should be checkout', async () => {
|
||||
await service.checkoutCommit(getTest1Project(), '498c782685');
|
||||
const filePath = join(
|
||||
service.getWorkspaceRoot(getTest1Project()),
|
||||
'README.md',
|
||||
);
|
||||
const text = await readFile(filePath, { encoding: 'utf-8' });
|
||||
expect(text).toMatch(/Commit 1/gi);
|
||||
});
|
||||
it('should be checkout right commit', async () => {
|
||||
await service.checkoutCommit(getTest1Project(), '7f7123fe5b');
|
||||
const filePath = join(
|
||||
service.getWorkspaceRoot(getTest1Project()),
|
||||
'README.md',
|
||||
);
|
||||
const text = await readFile(filePath, { encoding: 'utf-8' });
|
||||
expect(text).toMatch(/(?!Commit 1)/gi);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { F_OK } from 'constants';
|
||||
import { access, mkdir } from 'fs/promises';
|
||||
@ -9,8 +9,8 @@ import { Project } from '../projects/project.entity';
|
||||
import { ListBranchesArgs } from './dtos/list-branches.args';
|
||||
import { ListLogsArgs } from './dtos/list-logs.args';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { log } from 'console';
|
||||
|
||||
const DEFAULT_REMOTE_NAME = 'origin';
|
||||
@Injectable()
|
||||
export class ReposService {
|
||||
constructor(
|
||||
@ -19,21 +19,25 @@ export class ReposService {
|
||||
private readonly configService: ConfigService,
|
||||
) {}
|
||||
|
||||
async getGit(project: Project) {
|
||||
const workspacePath = join(
|
||||
getWorkspaceRoot(project: Project): string {
|
||||
return join(
|
||||
this.configService.get<string>('workspaces.root'),
|
||||
project.name,
|
||||
);
|
||||
const firstInit = await access(workspacePath, F_OK)
|
||||
}
|
||||
|
||||
async getGit(project: Project) {
|
||||
const workspaceRoot = this.getWorkspaceRoot(project);
|
||||
const firstInit = await access(workspaceRoot, F_OK)
|
||||
.then(() => false)
|
||||
.catch(async () => {
|
||||
await mkdir(workspacePath);
|
||||
await mkdir(workspaceRoot);
|
||||
return true;
|
||||
});
|
||||
const git = gitP(workspacePath);
|
||||
const git = gitP(workspaceRoot);
|
||||
if (firstInit) {
|
||||
await git.init();
|
||||
await git.addRemote('origin', project.sshUrl);
|
||||
await git.addRemote(DEFAULT_REMOTE_NAME, project.sshUrl);
|
||||
}
|
||||
return git;
|
||||
}
|
||||
@ -46,7 +50,7 @@ export class ReposService {
|
||||
await git.fetch();
|
||||
return await git.log({
|
||||
'--branches': dto.branch ?? '',
|
||||
'--remotes': 'origin',
|
||||
'--remotes': DEFAULT_REMOTE_NAME,
|
||||
});
|
||||
}
|
||||
|
||||
@ -57,4 +61,34 @@ export class ReposService {
|
||||
const git = await this.getGit(project);
|
||||
return git.branch();
|
||||
}
|
||||
|
||||
async checkoutBranch(project: Project, branch: string) {
|
||||
const git = await this.getGit(project);
|
||||
try {
|
||||
await git.fetch(DEFAULT_REMOTE_NAME, branch);
|
||||
} catch (err) {
|
||||
if (err.message.includes("couldn't find remote ref nonexistent")) {
|
||||
throw new NotFoundException(err.message);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
await git.checkout([
|
||||
'-B',
|
||||
branch,
|
||||
'--track',
|
||||
`${DEFAULT_REMOTE_NAME}/${branch}`,
|
||||
]);
|
||||
}
|
||||
|
||||
async checkoutCommit(project: Project, commitNumber: string) {
|
||||
const git = await this.getGit(project);
|
||||
try {
|
||||
await git.checkout([commitNumber]);
|
||||
} catch (err) {
|
||||
if (err.message.includes("couldn't find remote ref nonexistent")) {
|
||||
throw new NotFoundException(err.message);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user