6 Commits

9 changed files with 75 additions and 29 deletions

26
package-lock.json generated
View File

@@ -9087,14 +9087,6 @@
} }
} }
}, },
"mkdirp": {
"version": "0.5.5",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz",
"integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==",
"requires": {
"minimist": "^1.2.5"
}
},
"moment": { "moment": {
"version": "2.29.1", "version": "2.29.1",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
@@ -9126,6 +9118,16 @@
"on-finished": "^2.3.0", "on-finished": "^2.3.0",
"type-is": "^1.6.4", "type-is": "^1.6.4",
"xtend": "^4.0.0" "xtend": "^4.0.0"
},
"dependencies": {
"mkdirp": {
"version": "0.5.5",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz",
"integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==",
"requires": {
"minimist": "^1.2.5"
}
}
} }
}, },
"multimatch": { "multimatch": {
@@ -9489,6 +9491,14 @@
"minipass": "^2.9.0" "minipass": "^2.9.0"
} }
}, },
"mkdirp": {
"version": "0.5.5",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz",
"integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==",
"requires": {
"minimist": "^1.2.5"
}
},
"rimraf": { "rimraf": {
"version": "2.7.1", "version": "2.7.1",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz",

View File

@@ -3,6 +3,7 @@ import {
IsOptional, IsOptional,
IsString, IsString,
IsUrl, IsUrl,
Matches,
MaxLength, MaxLength,
MinLength, MinLength,
} from 'class-validator'; } from 'class-validator';
@@ -19,7 +20,9 @@ export class CreateProjectInput {
@MinLength(2) @MinLength(2)
comment: string; comment: string;
@IsUrl({ protocols: ['ssh'] }) @Matches(
/^(?:ssh:\/\/)?(?:[\w\d-_]+@)(?:[\w\d-_]+\.)*\w{2,10}(?::\d{1,5})?(?:\/[\w\d-_.]+)*/,
)
@MaxLength(256) @MaxLength(256)
sshUrl: string; sshUrl: string;

View File

@@ -0,0 +1,16 @@
import { InputType } from '@nestjs/graphql';
import { IsOptional, IsString, IsUUID } from 'class-validator';
@InputType()
export class CheckoutInput {
@IsUUID()
projectId: string;
@IsString()
@IsOptional()
branch?: string;
@IsString()
@IsOptional()
commitNumber?: string;
}

View File

@@ -1,4 +1,4 @@
import { InputType, ObjectType } from '@nestjs/graphql'; import { InputType } from '@nestjs/graphql';
import { IsOptional, IsString, IsUUID } from 'class-validator'; import { IsOptional, IsString, IsUUID } from 'class-validator';
@InputType() @InputType()

View File

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

View File

@@ -1,6 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { ReposResolver } from './repos.resolver'; import { ReposResolver } from './repos.resolver';
import { ReposService } from './repos.service'; import { ReposService } from './repos.service';
import { ProjectsService } from '../projects/projects.service';
describe('ReposResolver', () => { describe('ReposResolver', () => {
let resolver: ReposResolver; let resolver: ReposResolver;
@@ -13,6 +14,10 @@ describe('ReposResolver', () => {
provide: ReposService, provide: ReposService,
useValue: {}, useValue: {},
}, },
{
provide: ProjectsService,
useValue: {},
},
], ],
}).compile(); }).compile();

View File

@@ -1,19 +1,24 @@
import { Args, Query, Resolver } from '@nestjs/graphql'; import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { ListLogsArgs } from './dtos/list-logs.args'; import { ListLogsArgs } from './dtos/list-logs.args';
import { ReposService } from './repos.service'; import { ReposService } from './repos.service';
import { LogList } from './dtos/log-list.model'; import { LogList } from './dtos/log-list.model';
import { ListBranchesArgs } from './dtos/list-branches.args'; import { ListBranchesArgs } from './dtos/list-branches.args';
import { BranchList } from './dtos/branch-list.model'; import { BranchList } from './dtos/branch-list.model';
import { CheckoutInput } from './dtos/checkout.input';
import { ProjectsService } from '../projects/projects.service';
@Resolver() @Resolver()
export class ReposResolver { export class ReposResolver {
constructor(private readonly service: ReposService) {} constructor(
private readonly service: ReposService,
private readonly projectService: ProjectsService,
) {}
@Query(() => LogList) @Query(() => LogList)
async listLogs(@Args('listLogsArgs') dto: ListLogsArgs) { async listLogs(@Args('listLogsArgs') dto: ListLogsArgs) {
return await this.service.listLogs(dto); return await this.service.listLogs(dto);
} }
@Query(() => BranchList) @Query(() => BranchList)
async ListBranchesArgs( async listBranches(
@Args('listBranchesArgs') dto: ListBranchesArgs, @Args('listBranchesArgs') dto: ListBranchesArgs,
): Promise<BranchList> { ): Promise<BranchList> {
return await this.service.listBranches(dto).then((data) => { return await this.service.listBranches(dto).then((data) => {
@@ -23,4 +28,10 @@ export class ReposResolver {
}; };
}); });
} }
@Mutation(() => Boolean)
async checkout(@Args('checkoutInput') dto: CheckoutInput): Promise<true> {
const project = await this.projectService.findOne(dto.projectId);
await this.service.checkoutCommit(project, dto.commitNumber);
return true;
}
} }

View File

@@ -75,7 +75,7 @@ describe('ReposService', () => {
it('should be checkout', async () => { it('should be checkout', async () => {
await service.checkoutBranch(getTest1Project(), 'master'); await service.checkoutBranch(getTest1Project(), 'master');
const filePath = join( const filePath = join(
service.getWorkspaceRoot(getTest1Project()), service.getWorkspaceRoot(getTest1Project(), 'master'),
'README.md', 'README.md',
); );
const text = await readFile(filePath, { encoding: 'utf-8' }); const text = await readFile(filePath, { encoding: 'utf-8' });
@@ -86,7 +86,7 @@ describe('ReposService', () => {
await service.checkoutBranch(getTest1Project(), 'branch-a'); await service.checkoutBranch(getTest1Project(), 'branch-a');
await service.checkoutBranch(getTest1Project(), 'branch-b'); await service.checkoutBranch(getTest1Project(), 'branch-b');
const filePath = join( const filePath = join(
service.getWorkspaceRoot(getTest1Project()), service.getWorkspaceRoot(getTest1Project(), 'branch-b'),
'branch-b.md', 'branch-b.md',
); );
const text = await readFile(filePath, { encoding: 'utf-8' }); const text = await readFile(filePath, { encoding: 'utf-8' });
@@ -100,7 +100,7 @@ describe('ReposService', () => {
it('checkout the specified version', async () => { it('checkout the specified version', async () => {
await service.checkoutBranch(getTest1Project(), 'master'); await service.checkoutBranch(getTest1Project(), 'master');
const filePath = join( const filePath = join(
service.getWorkspaceRoot(getTest1Project()), service.getWorkspaceRoot(getTest1Project(), 'master'),
'README.md', 'README.md',
); );
const text = await readFile(filePath, { encoding: 'utf-8' }); const text = await readFile(filePath, { encoding: 'utf-8' });
@@ -112,7 +112,7 @@ describe('ReposService', () => {
it('should be checkout', async () => { it('should be checkout', async () => {
await service.checkoutCommit(getTest1Project(), '498c782685'); await service.checkoutCommit(getTest1Project(), '498c782685');
const filePath = join( const filePath = join(
service.getWorkspaceRoot(getTest1Project()), service.getWorkspaceRoot(getTest1Project(), '498c782685'),
'README.md', 'README.md',
); );
const text = await readFile(filePath, { encoding: 'utf-8' }); const text = await readFile(filePath, { encoding: 'utf-8' });
@@ -121,7 +121,7 @@ describe('ReposService', () => {
it('should be checkout right commit', async () => { it('should be checkout right commit', async () => {
await service.checkoutCommit(getTest1Project(), '7f7123fe5b'); await service.checkoutCommit(getTest1Project(), '7f7123fe5b');
const filePath = join( const filePath = join(
service.getWorkspaceRoot(getTest1Project()), service.getWorkspaceRoot(getTest1Project(), '7f7123fe5b'),
'README.md', 'README.md',
); );
const text = await readFile(filePath, { encoding: 'utf-8' }); const text = await readFile(filePath, { encoding: 'utf-8' });

View File

@@ -19,10 +19,11 @@ export class ReposService {
private readonly configService: ConfigService, private readonly configService: ConfigService,
) {} ) {}
getWorkspaceRoot(project: Project): string { getWorkspaceRoot(project: Project, subDir = ''): string {
return join( return join(
this.configService.get<string>('workspaces.root'), this.configService.get<string>('workspaces.root'),
project.name, project.name,
encodeURIComponent(subDir),
); );
} }
@@ -30,14 +31,14 @@ export class ReposService {
// TODO: 获取锁,失败抛错。 // TODO: 获取锁,失败抛错。
} }
async getGit(project: Project) { async getGit(project: Project, subDir = 'default') {
const workspaceRoot = this.getWorkspaceRoot(project); const workspaceRoot = this.getWorkspaceRoot(project, subDir);
await this.lockWorkspace(workspaceRoot); await this.lockWorkspace(workspaceRoot);
const firstInit = await access(workspaceRoot, F_OK) const firstInit = await access(workspaceRoot, F_OK)
.then(() => false) .then(() => false)
.catch(async () => { .catch(async () => {
await mkdir(workspaceRoot); await mkdir(workspaceRoot, { recursive: true });
return true; return true;
}); });
const git = gitP(workspaceRoot); const git = gitP(workspaceRoot);
@@ -54,10 +55,9 @@ export class ReposService {
}); });
const git = await this.getGit(project); const git = await this.getGit(project);
await git.fetch(); await git.fetch();
return await git.log({ return await git.log(
'--branches': dto.branch ?? '', dto.branch ? ['--branches', dto.branch, '--'] : ['--all'],
'--remotes': DEFAULT_REMOTE_NAME, );
});
} }
async listBranches(dto: ListBranchesArgs) { async listBranches(dto: ListBranchesArgs) {
@@ -69,7 +69,7 @@ export class ReposService {
} }
async checkoutBranch(project: Project, branch: string) { async checkoutBranch(project: Project, branch: string) {
const git = await this.getGit(project); const git = await this.getGit(project, branch);
try { try {
await git.fetch(DEFAULT_REMOTE_NAME, branch); await git.fetch(DEFAULT_REMOTE_NAME, branch);
} catch (err) { } catch (err) {
@@ -87,7 +87,7 @@ export class ReposService {
} }
async checkoutCommit(project: Project, commitNumber: string) { async checkoutCommit(project: Project, commitNumber: string) {
const git = await this.getGit(project); const git = await this.getGit(project, commitNumber);
try { try {
await git.fetch(DEFAULT_REMOTE_NAME); await git.fetch(DEFAULT_REMOTE_NAME);
} catch (err) { } catch (err) {