feat(repos): 检出相关逻辑更改为传入任务信息而不是项目信息。

This commit is contained in:
Ivan
2021-03-04 17:02:07 +08:00
parent 33b09594f5
commit f39c801fc2
9 changed files with 169 additions and 96 deletions

View File

@@ -1,3 +1,5 @@
import { Pipeline } from './../pipelines/pipeline.entity';
import { PipelineTask } from './../pipeline-tasks/pipeline-task.entity';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { F_OK } from 'constants';
@@ -11,6 +13,7 @@ import { ListLogsArgs } from './dtos/list-logs.args';
import { ConfigService } from '@nestjs/config';
const DEFAULT_REMOTE_NAME = 'origin';
const INFO_PATH = '@info';
@Injectable()
export class ReposService {
constructor(
@@ -19,33 +22,28 @@ 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),
encodeURIComponent(project.name),
INFO_PATH,
);
}
async lockWorkspace(workspaceRoot: string) {
// TODO: 获取锁,失败抛错。
}
async getGit(project: Project, workspaceRoot?: string) {
if (!workspaceRoot) {
workspaceRoot = this.getWorkspaceRoot(project);
}
async getGit(project: Project, subDir = 'default') {
const workspaceRoot = this.getWorkspaceRoot(project, subDir);
await this.lockWorkspace(workspaceRoot);
const firstInit = await access(workspaceRoot, F_OK)
.then(() => false)
.catch(async () => {
await mkdir(workspaceRoot, { recursive: true });
return true;
});
await access(workspaceRoot, F_OK).catch(async () => {
await mkdir(workspaceRoot, { recursive: true });
});
const git = gitP(workspaceRoot);
if (firstInit) {
if (!(await git.checkIsRepo())) {
await git.init();
await git.addRemote(DEFAULT_REMOTE_NAME, project.sshUrl);
}
await git.fetch();
return git;
}
@@ -54,7 +52,6 @@ export class ReposService {
id: dto.projectId,
});
const git = await this.getGit(project);
await git.fetch();
return await git.log(
dto.branch ? ['--branches', dto.branch, '--'] : ['--all'],
);
@@ -68,26 +65,8 @@ export class ReposService {
return git.branch();
}
async checkoutBranch(project: Project, branch: string) {
const git = await this.getGit(project, branch);
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, commitNumber);
async checkout(task: PipelineTask, workspaceRoot: string) {
const git = await this.getGit(task.pipeline.project, workspaceRoot);
try {
await git.fetch(DEFAULT_REMOTE_NAME);
} catch (err) {
@@ -96,6 +75,20 @@ export class ReposService {
}
throw err;
}
await git.checkout([commitNumber]);
await git.checkout([task.commit]);
}
/**
* get workspace root absolute path
*
* ! example: `/var/tmp/fennec-workspaces-root/project/pipeline_name-commit_hash`
* @param task {PipelineTask} task (with pipeline and project info)
*/
getWorkspaceRootByTask(task: PipelineTask) {
return join(
this.configService.get<string>('workspaces.root'),
encodeURIComponent(task.pipeline.project.name),
encodeURIComponent(`${task.pipeline.name}-${task.commit}`),
);
}
}