diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index a300c54e..d7b7fb2f 100644 --- a/packages/code-api/src/atomgit/atomgit.service.ts +++ b/packages/code-api/src/atomgit/atomgit.service.ts @@ -13,6 +13,7 @@ import { EntryParam, FileAction, FileActionHeader, + FileActionResult, GetEntryInfoParam, GitlensBlame, ICodeAPIService, @@ -135,7 +136,7 @@ export class AtomGitAPIService implements ICodeAPIService { }); } - protected async request(path: string, options?: RequestOptions): Promise { + protected async request(path: string, options?: RequestOptions, responseOptions?: API.RequestResponseOptions): Promise { try { const { headers, ...rest } = options || {}; const privateToken = this.PRIVATE_TOKEN; @@ -162,6 +163,10 @@ export class AtomGitAPIService implements ICodeAPIService { } else if (status === 404) { messageKey = 'error.resource-not-found'; } + if (responseOptions?.errorOption === false) { + console.log(err); + return undefined as any; + } this.showErrorMessage(messageKey, status); throw err; } @@ -221,8 +226,29 @@ export class AtomGitAPIService implements ICodeAPIService { return Buffer.from(content); } - getBlobByCommitPath(_repo: IRepositoryModel, _commit: string, _path: string, _options?: any): Promise { - throw new Error('Method not implemented.'); + async getBlobByCommitPath(repo: IRepositoryModel, commit: string, path: string, options?: API.RequestResponseOptions): Promise { + const res = await this.request( + `/repos/${this.getProjectPath(repo)}/contents/file`, + { + params: { + path: path, + ref: commit + }, + }, + options + ); + + const { content, encoding, type } = res; + + if (type !== 'file') { + throw new Error(`${path} is not a file.`); + } + + if (encoding === 'base64') { + return Buffer.from(decodeURIComponent(escape(atob(content)))); + } + + return Buffer.from(content); } async getBranches(repo: IRepositoryModel): Promise { if (!this.PRIVATE_TOKEN) { @@ -322,17 +348,68 @@ export class AtomGitAPIService implements ICodeAPIService { getCommitCompare(_repo: IRepositoryModel, _from: string, _to: string): Promise { throw new Error('Method not implemented.'); } - async getFiles(_repo: IRepositoryModel): Promise { - return []; + async getFiles(repo: IRepositoryModel): Promise { + const fileList = await this.request( + `/repos/${this.getProjectPath(repo)}/trees/${repo.commit}`, + { + params: { + recursive: 'true', + }, + }, + ); + return (fileList || []).filter(f => f.type === 'blob').map((f) => f.path); } - bulkChangeFiles(_repo: IRepositoryModel, _actions: FileAction[], _header: FileActionHeader): Promise { - throw new Error('Method not implemented.'); + async bulkChangeFiles(repo: IRepositoryModel, actions: FileAction[], header: FileActionHeader): Promise { + const res = await this.request( + `/repos/${this.getProjectPath(repo)}/commits/create`, + { + data: { + actions: actions.map((action) => ({ + action: action.action_type.toLocaleLowerCase(), + file_path: action.file_path, + content: action.content, + previous_path: action.file_path, + })), + branch: header.branch, + commit_message: header.commit_message, + }, + method: 'post', + }, + ); + const resCommit = { + branch_created: false, + branch: header.branch, + commit_id: res.id, + file_name: '', + ...res, + }; + // 没有提交ID 说明提交失败 + if (res.id) { + return [resCommit] as FileActionResult[]; + } + return []; } - createBranch(_repo: IRepositoryModel, _newBranch: string, _ref: string): Promise { - throw new Error('Method not implemented.'); + async createBranch(repo: IRepositoryModel, newBranch: string, ref: string): Promise { + const res = await this.request(`/repos/${this.getProjectPath(repo)}/git/refs`, { + method: 'post', + data: { + sha: ref, + ref: newBranch, + }, + }); + + const resBranch: Branch = { + commit: { + id: res.object?.sha, + }, + name: res.ref, + ref: res.ref, + } + + return resBranch; } getUser(_repo: IRepositoryModel): Promise { - throw new Error('Method not implemented.'); + return {} as any; } public async getProject(repo: IRepositoryModel): Promise { diff --git a/packages/code-api/src/atomgit/types.ts b/packages/code-api/src/atomgit/types.ts index 467aedf4..b8ddfcd8 100644 --- a/packages/code-api/src/atomgit/types.ts +++ b/packages/code-api/src/atomgit/types.ts @@ -45,8 +45,53 @@ export namespace API { }; } + export interface ResponseCommitInfo { + author_email: string; + author_name: string; + authored_date: string; + committed_date: string; + committer_email: string; + committer_name: string; + created_at: string; + id: string; + message: string; + parent_ids: string[]; + short_id: string; + title: string; + } + export interface ResponseRepoInfo { name: string; default_branch: string; } + + export interface ResponseBranch { + object: { + sha: string; + type: string; + } + ref: string; + } + + export interface ResponseInfoAndBlobs { + content: string; + download_url: string; + encoding: string; + name: string; + path: string; + sha: string; + size: number; + type: 'file' | string; + } + + export interface ResponseFileNames { + path: string; + sha: string; + mode: string; + type: 'tree' | 'blob' | string; + } + + export interface RequestResponseOptions { + errorOption?: boolean; + } }