修复远程仓库信息获取问题。

This commit is contained in:
Ivan Li 2021-02-23 20:19:39 +08:00
parent dfaee1fb56
commit ab6c56e55a
9 changed files with 62 additions and 17 deletions

View File

@ -9,3 +9,5 @@ db:
database: fennec
username: fennec
password:
workspaces:
root: '/Users/ivanli/Projects/fennec/workspaces'

View File

@ -11,7 +11,7 @@
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:debug": "DEBUG=simple-git,simple-git:* nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",

View File

@ -15,7 +15,7 @@ export class Branch implements BranchSummaryBranch {
}
@ObjectType()
export class BranchesList {
export class BranchList {
detached: boolean;
current: string;
all: string[];

View File

@ -13,7 +13,7 @@ export class LogFields {
}
@ObjectType()
export class LogsList implements LogResult<DefaultLogFields> {
export class LogList implements LogResult<DefaultLogFields> {
@Field(() => [LogFields])
all: LogFields[];
total: number;

View File

@ -3,9 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { Project } from '../projects/project.entity';
import { ReposResolver } from './repos.resolver';
import { ReposService } from './repos.service';
import { ConfigModule } from '@nestjs/config';
@Module({
imports: [TypeOrmModule.forFeature([Project])],
imports: [TypeOrmModule.forFeature([Project]), ConfigModule],
providers: [ReposResolver, ReposService],
})
export class ReposModule {}

View File

@ -1,21 +1,21 @@
import { Args, Query, Resolver } from '@nestjs/graphql';
import { ListLogsArgs } from './dtos/list-logs.args';
import { ReposService } from './repos.service';
import { LogsList } from './dtos/logs-list.model';
import { LogList } from './dtos/log-list.model';
import { ListBranchesArgs } from './dtos/list-branches.args';
import { BranchesList } from './dtos/branches-list.model';
import { BranchList } from './dtos/branch-list.model';
@Resolver()
export class ReposResolver {
con
@Query(() => LogsList)
constructor(private readonly service: ReposService) {}
@Query(() => LogList)
async listLogs(@Args('listLogsArgs') dto: ListLogsArgs) {
return await this.service.listLogs(dto);
}
@Query(() => BranchesList)
@Query(() => BranchList)
async ListBranchesArgs(
@Args('listBranchesArgs') dto: ListBranchesArgs,
): Promise<BranchesList> {
): Promise<BranchList> {
return await this.service.listBranches(dto).then((data) => {
return {
...data,

View File

@ -2,6 +2,11 @@ import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Project } from '../projects/project.entity';
import { ReposService } from './repos.service';
import { ConfigService } from '@nestjs/config';
import { rm, unlink } from 'fs/promises';
import { join } from 'path';
const workspacesRoot = '/Users/ivanli/Projects/fennec/workspaces';
describe('ReposService', () => {
let service: ReposService;
@ -24,6 +29,14 @@ describe('ReposService', () => {
provide: getRepositoryToken(Project),
useFactory: repositoryMockFactory,
},
{
provide: ConfigService,
useValue: {
get() {
return workspacesRoot;
},
},
},
],
}).compile();
@ -34,10 +47,13 @@ describe('ReposService', () => {
expect(service).toBeDefined();
});
describe('listLogs', () => {
beforeEach(async () => {
await rm(join(workspacesRoot, 'test1'), { recursive: true });
});
it('should be return logs', async () => {
const result = await service.listLogs({ projectId: '1' });
expect(result).toBeDefined();
}, 10_000);
}, 20_000);
});
describe('listBranch', () => {
it('should be return branches', async () => {

View File

@ -8,18 +8,35 @@ import { Repository } from 'typeorm';
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';
@Injectable()
export class ReposService {
constructor(
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
private readonly configService: ConfigService,
) {}
async getGit(project: Project) {
const workspacePath = join(__dirname, '../../workspaces', project.name);
await access(workspacePath, F_OK).catch(() => mkdir(workspacePath));
return gitP(workspacePath);
const workspacePath = join(
this.configService.get<string>('workspaces.root'),
project.name,
);
const firstInit = await access(workspacePath, F_OK)
.then(() => false)
.catch(async () => {
await mkdir(workspacePath);
return true;
});
const git = gitP(workspacePath);
if (firstInit) {
await git.init();
await git.addRemote('origin', project.sshUrl);
// await git.clone(project.sshUrl, workspacePath);
}
return git;
}
async listLogs(dto: ListLogsArgs) {
@ -27,8 +44,17 @@ export class ReposService {
id: dto.projectId,
});
const git = await this.getGit(project);
await git.fetch();
return git.log();
await git
.outputHandler((command, stdout, stderr) => {
stdout.pipe(process.stdout);
stderr.pipe(process.stderr);
})
.fetch();
// await git.checkoutBranch('master', 'origin/master');
return await git.log({
'--branches': dto.branch ?? '',
'--remotes': 'origin',
});
}
async listBranches(dto: ListBranchesArgs) {

View File