Compare commits
1 Commits
ea4ca724e3
...
feat-repo+
Author | SHA1 | Date | |
---|---|---|---|
|
7ea8505873 |
@@ -1,7 +1,6 @@
|
||||
env: dev
|
||||
http:
|
||||
port: 7122
|
||||
|
||||
db:
|
||||
postgres:
|
||||
host: 192.168.31.194
|
||||
@@ -9,5 +8,9 @@ db:
|
||||
database: fennec
|
||||
username: fennec
|
||||
password:
|
||||
redis:
|
||||
mq:
|
||||
host: localhost
|
||||
port: 6379
|
||||
workspaces:
|
||||
root: '/Users/ivanli/Projects/fennec/workspaces'
|
26
package-lock.json
generated
26
package-lock.json
generated
@@ -9087,6 +9087,14 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"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": {
|
||||
"version": "2.29.1",
|
||||
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
|
||||
@@ -9118,16 +9126,6 @@
|
||||
"on-finished": "^2.3.0",
|
||||
"type-is": "^1.6.4",
|
||||
"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": {
|
||||
@@ -9491,14 +9489,6 @@
|
||||
"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": {
|
||||
"version": "2.7.1",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz",
|
||||
|
@@ -8,6 +8,7 @@ import { AppService } from './app.service';
|
||||
import { ProjectsModule } from './projects/projects.module';
|
||||
import { ReposModule } from './repos/repos.module';
|
||||
import configuration from './commons/config/configuration';
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -37,6 +38,16 @@ import configuration from './commons/config/configuration';
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
BullModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
useFactory: async (configService: ConfigService) => ({
|
||||
redis: {
|
||||
host: configService.get<string>('redis.mq.host', 'localhost'),
|
||||
port: configService.get<number>('redis.mq.port', 6379),
|
||||
},
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
ProjectsModule,
|
||||
ReposModule,
|
||||
],
|
||||
|
@@ -3,7 +3,6 @@ import {
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsUrl,
|
||||
Matches,
|
||||
MaxLength,
|
||||
MinLength,
|
||||
} from 'class-validator';
|
||||
@@ -20,9 +19,7 @@ export class CreateProjectInput {
|
||||
@MinLength(2)
|
||||
comment: string;
|
||||
|
||||
@Matches(
|
||||
/^(?:ssh:\/\/)?(?:[\w\d-_]+@)(?:[\w\d-_]+\.)*\w{2,10}(?::\d{1,5})?(?:\/[\w\d-_.]+)*/,
|
||||
)
|
||||
@IsUrl({ protocols: ['ssh'] })
|
||||
@MaxLength(256)
|
||||
sshUrl: string;
|
||||
|
||||
|
@@ -1,16 +0,0 @@
|
||||
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;
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
import { InputType } from '@nestjs/graphql';
|
||||
import { InputType, ObjectType } from '@nestjs/graphql';
|
||||
import { IsOptional, IsString, IsUUID } from 'class-validator';
|
||||
|
||||
@InputType()
|
||||
|
1
src/repos/repos.constants.ts
Normal file
1
src/repos/repos.constants.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const WORKSPACE_ACTION = 'workspace-action';
|
@@ -4,10 +4,17 @@ import { Project } from '../projects/project.entity';
|
||||
import { ReposResolver } from './repos.resolver';
|
||||
import { ReposService } from './repos.service';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { ProjectsModule } from '../projects/projects.module';
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
import { WORKSPACE_ACTION } from './repos.constants';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Project]), ConfigModule, ProjectsModule],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Project]),
|
||||
ConfigModule,
|
||||
BullModule.registerQueue({
|
||||
name: WORKSPACE_ACTION,
|
||||
}),
|
||||
],
|
||||
providers: [ReposResolver, ReposService],
|
||||
})
|
||||
export class ReposModule {}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { ReposResolver } from './repos.resolver';
|
||||
import { ReposService } from './repos.service';
|
||||
import { ProjectsService } from '../projects/projects.service';
|
||||
|
||||
describe('ReposResolver', () => {
|
||||
let resolver: ReposResolver;
|
||||
@@ -14,10 +13,6 @@ describe('ReposResolver', () => {
|
||||
provide: ReposService,
|
||||
useValue: {},
|
||||
},
|
||||
{
|
||||
provide: ProjectsService,
|
||||
useValue: {},
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
|
@@ -1,24 +1,19 @@
|
||||
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
|
||||
import { Args, Query, Resolver } from '@nestjs/graphql';
|
||||
import { ListLogsArgs } from './dtos/list-logs.args';
|
||||
import { ReposService } from './repos.service';
|
||||
import { LogList } from './dtos/log-list.model';
|
||||
import { ListBranchesArgs } from './dtos/list-branches.args';
|
||||
import { BranchList } from './dtos/branch-list.model';
|
||||
import { CheckoutInput } from './dtos/checkout.input';
|
||||
import { ProjectsService } from '../projects/projects.service';
|
||||
|
||||
@Resolver()
|
||||
export class ReposResolver {
|
||||
constructor(
|
||||
private readonly service: ReposService,
|
||||
private readonly projectService: ProjectsService,
|
||||
) {}
|
||||
constructor(private readonly service: ReposService) {}
|
||||
@Query(() => LogList)
|
||||
async listLogs(@Args('listLogsArgs') dto: ListLogsArgs) {
|
||||
return await this.service.listLogs(dto);
|
||||
}
|
||||
@Query(() => BranchList)
|
||||
async listBranches(
|
||||
async ListBranchesArgs(
|
||||
@Args('listBranchesArgs') dto: ListBranchesArgs,
|
||||
): Promise<BranchList> {
|
||||
return await this.service.listBranches(dto).then((data) => {
|
||||
@@ -28,10 +23,4 @@ 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;
|
||||
}
|
||||
}
|
||||
|
@@ -75,7 +75,7 @@ describe('ReposService', () => {
|
||||
it('should be checkout', async () => {
|
||||
await service.checkoutBranch(getTest1Project(), 'master');
|
||||
const filePath = join(
|
||||
service.getWorkspaceRoot(getTest1Project(), 'master'),
|
||||
service.getWorkspaceRoot(getTest1Project()),
|
||||
'README.md',
|
||||
);
|
||||
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-b');
|
||||
const filePath = join(
|
||||
service.getWorkspaceRoot(getTest1Project(), 'branch-b'),
|
||||
service.getWorkspaceRoot(getTest1Project()),
|
||||
'branch-b.md',
|
||||
);
|
||||
const text = await readFile(filePath, { encoding: 'utf-8' });
|
||||
@@ -100,7 +100,7 @@ describe('ReposService', () => {
|
||||
it('checkout the specified version', async () => {
|
||||
await service.checkoutBranch(getTest1Project(), 'master');
|
||||
const filePath = join(
|
||||
service.getWorkspaceRoot(getTest1Project(), 'master'),
|
||||
service.getWorkspaceRoot(getTest1Project()),
|
||||
'README.md',
|
||||
);
|
||||
const text = await readFile(filePath, { encoding: 'utf-8' });
|
||||
@@ -112,7 +112,7 @@ describe('ReposService', () => {
|
||||
it('should be checkout', async () => {
|
||||
await service.checkoutCommit(getTest1Project(), '498c782685');
|
||||
const filePath = join(
|
||||
service.getWorkspaceRoot(getTest1Project(), '498c782685'),
|
||||
service.getWorkspaceRoot(getTest1Project()),
|
||||
'README.md',
|
||||
);
|
||||
const text = await readFile(filePath, { encoding: 'utf-8' });
|
||||
@@ -121,7 +121,7 @@ describe('ReposService', () => {
|
||||
it('should be checkout right commit', async () => {
|
||||
await service.checkoutCommit(getTest1Project(), '7f7123fe5b');
|
||||
const filePath = join(
|
||||
service.getWorkspaceRoot(getTest1Project(), '7f7123fe5b'),
|
||||
service.getWorkspaceRoot(getTest1Project()),
|
||||
'README.md',
|
||||
);
|
||||
const text = await readFile(filePath, { encoding: 'utf-8' });
|
||||
|
@@ -19,11 +19,10 @@ export class ReposService {
|
||||
private readonly configService: ConfigService,
|
||||
) {}
|
||||
|
||||
getWorkspaceRoot(project: Project, subDir = ''): string {
|
||||
getWorkspaceRoot(project: Project): string {
|
||||
return join(
|
||||
this.configService.get<string>('workspaces.root'),
|
||||
project.name,
|
||||
encodeURIComponent(subDir),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -31,14 +30,14 @@ export class ReposService {
|
||||
// TODO: 获取锁,失败抛错。
|
||||
}
|
||||
|
||||
async getGit(project: Project, subDir = 'default') {
|
||||
const workspaceRoot = this.getWorkspaceRoot(project, subDir);
|
||||
async getGit(project: Project) {
|
||||
const workspaceRoot = this.getWorkspaceRoot(project);
|
||||
await this.lockWorkspace(workspaceRoot);
|
||||
|
||||
const firstInit = await access(workspaceRoot, F_OK)
|
||||
.then(() => false)
|
||||
.catch(async () => {
|
||||
await mkdir(workspaceRoot, { recursive: true });
|
||||
await mkdir(workspaceRoot);
|
||||
return true;
|
||||
});
|
||||
const git = gitP(workspaceRoot);
|
||||
@@ -55,9 +54,10 @@ export class ReposService {
|
||||
});
|
||||
const git = await this.getGit(project);
|
||||
await git.fetch();
|
||||
return await git.log(
|
||||
dto.branch ? ['--branches', dto.branch, '--'] : ['--all'],
|
||||
);
|
||||
return await git.log({
|
||||
'--branches': dto.branch ?? '',
|
||||
'--remotes': DEFAULT_REMOTE_NAME,
|
||||
});
|
||||
}
|
||||
|
||||
async listBranches(dto: ListBranchesArgs) {
|
||||
@@ -69,7 +69,7 @@ export class ReposService {
|
||||
}
|
||||
|
||||
async checkoutBranch(project: Project, branch: string) {
|
||||
const git = await this.getGit(project, branch);
|
||||
const git = await this.getGit(project);
|
||||
try {
|
||||
await git.fetch(DEFAULT_REMOTE_NAME, branch);
|
||||
} catch (err) {
|
||||
@@ -87,7 +87,7 @@ export class ReposService {
|
||||
}
|
||||
|
||||
async checkoutCommit(project: Project, commitNumber: string) {
|
||||
const git = await this.getGit(project, commitNumber);
|
||||
const git = await this.getGit(project);
|
||||
try {
|
||||
await git.fetch(DEFAULT_REMOTE_NAME);
|
||||
} catch (err) {
|
||||
|
8
src/repos/workspace-action.consumer.ts
Normal file
8
src/repos/workspace-action.consumer.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Process, Processor } from '@nestjs/bull';
|
||||
import { WORKSPACE_ACTION } from './repos.constants';
|
||||
|
||||
@Processor(WORKSPACE_ACTION)
|
||||
export class WorkspaceActionConsumer {
|
||||
@Process()
|
||||
async dispatch() {}
|
||||
}
|
5
src/repos/workspace-action.model.ts
Normal file
5
src/repos/workspace-action.model.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { WorkspaceActions } from './workspace-actions.enum';
|
||||
|
||||
export class WorkspaceAction {
|
||||
action: WorkspaceActions;
|
||||
}
|
6
src/repos/workspace-actions.enum.ts
Normal file
6
src/repos/workspace-actions.enum.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export enum WorkspaceActions {
|
||||
checkoutBranch = 'checkoutBranch',
|
||||
checkoutCommit = 'checkoutCommit',
|
||||
listLogs = 'listLogs',
|
||||
listBranches = 'listBranches',
|
||||
}
|
Reference in New Issue
Block a user