From 4242f74c66fcd73c6326c651263c3b87bdf20b7d Mon Sep 17 00:00:00 2001 From: "yanqianyu.yqy" Date: Fri, 15 Nov 2024 16:51:06 +0800 Subject: [PATCH 1/9] feat: support atomgit bulkChangeFiles api --- .../code-api/src/atomgit/atomgit.service.ts | 33 +++++++++++++++++-- packages/code-api/src/atomgit/types.ts | 15 +++++++-- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index a300c54e..f1e3dae1 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, @@ -325,8 +326,36 @@ export class AtomGitAPIService implements ICodeAPIService { async getFiles(_repo: IRepositoryModel): Promise { return []; } - 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, + })), + projectId: repo!.projectId, + 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.'); diff --git a/packages/code-api/src/atomgit/types.ts b/packages/code-api/src/atomgit/types.ts index 467aedf4..5db132a2 100644 --- a/packages/code-api/src/atomgit/types.ts +++ b/packages/code-api/src/atomgit/types.ts @@ -40,9 +40,18 @@ export namespace API { } export interface ResponseCommit { - commit: { - sha: string; - }; + 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 { From 2965dd6df7d3b69b0efa1b7bf2dbe4f29d35a9b8 Mon Sep 17 00:00:00 2001 From: "yanqianyu.yqy" Date: Fri, 15 Nov 2024 17:06:16 +0800 Subject: [PATCH 2/9] fix: remove useless params in atomgit bulkChangeFiles --- packages/code-api/src/atomgit/atomgit.service.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index f1e3dae1..bc3e07b2 100644 --- a/packages/code-api/src/atomgit/atomgit.service.ts +++ b/packages/code-api/src/atomgit/atomgit.service.ts @@ -337,7 +337,6 @@ export class AtomGitAPIService implements ICodeAPIService { content: action.content, previous_path: action.file_path, })), - projectId: repo!.projectId, branch: header.branch, commit_message: header.commit_message, }, From 28729a154aeaa8454d2096d00a6963223c3a13fe Mon Sep 17 00:00:00 2001 From: "yanqianyu.yqy" Date: Fri, 15 Nov 2024 18:02:17 +0800 Subject: [PATCH 3/9] feat: support atomgit createBranch --- .../code-api/src/atomgit/atomgit.service.ts | 20 +++++++++++++++++-- packages/code-api/src/atomgit/types.ts | 8 ++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index bc3e07b2..cedba2e9 100644 --- a/packages/code-api/src/atomgit/atomgit.service.ts +++ b/packages/code-api/src/atomgit/atomgit.service.ts @@ -356,8 +356,24 @@ export class AtomGitAPIService implements ICodeAPIService { } 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: newBranch, + ref: ref, + }, + }); + + 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.'); diff --git a/packages/code-api/src/atomgit/types.ts b/packages/code-api/src/atomgit/types.ts index 5db132a2..4e250e31 100644 --- a/packages/code-api/src/atomgit/types.ts +++ b/packages/code-api/src/atomgit/types.ts @@ -58,4 +58,12 @@ export namespace API { name: string; default_branch: string; } + + export interface ResponseBranch { + object: { + sha: string; + type: string; + } + ref: string; + } } From 2dd1dc0bb5af0747207bfebbad9f8e0330387388 Mon Sep 17 00:00:00 2001 From: "yanqianyu.yqy" Date: Fri, 15 Nov 2024 18:13:13 +0800 Subject: [PATCH 4/9] fix: change response type from ResponseCommit to ResponseCommitInfo in atomgit service --- packages/code-api/src/atomgit/atomgit.service.ts | 2 +- packages/code-api/src/atomgit/types.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index cedba2e9..e3a8fc1c 100644 --- a/packages/code-api/src/atomgit/atomgit.service.ts +++ b/packages/code-api/src/atomgit/atomgit.service.ts @@ -327,7 +327,7 @@ export class AtomGitAPIService implements ICodeAPIService { return []; } async bulkChangeFiles(repo: IRepositoryModel, actions: FileAction[], header: FileActionHeader): Promise { - const res = await this.request( + const res = await this.request( `/repos/${this.getProjectPath(repo)}commits/create`, { data: { diff --git a/packages/code-api/src/atomgit/types.ts b/packages/code-api/src/atomgit/types.ts index 4e250e31..112d17e2 100644 --- a/packages/code-api/src/atomgit/types.ts +++ b/packages/code-api/src/atomgit/types.ts @@ -40,6 +40,12 @@ export namespace API { } export interface ResponseCommit { + commit: { + sha: string; + }; + } + + export interface ResponseCommitInfo { author_email: string; author_name: string; authored_date: string; From d9e005fa9c216a58a5e150d1b84b8de840616eb1 Mon Sep 17 00:00:00 2001 From: "yanqianyu.yqy" Date: Mon, 18 Nov 2024 19:46:39 +0800 Subject: [PATCH 5/9] feat: implement atomgit getBlobByCommitPath --- packages/code-api/src/atomgit/atomgit.service.ts | 13 +++++++++++-- packages/code-api/src/atomgit/types.ts | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index e3a8fc1c..049836ca 100644 --- a/packages/code-api/src/atomgit/atomgit.service.ts +++ b/packages/code-api/src/atomgit/atomgit.service.ts @@ -222,8 +222,17 @@ 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): Promise { + const infoAndBlobs = await this.request( + `/repos/${this.getProjectPath(repo)}/contents/file`, + { + params: { + path: path, + ref: commit + }, + }, + ); + return new Uint8Array(new TextEncoder().encode(infoAndBlobs?.content || '')); } async getBranches(repo: IRepositoryModel): Promise { if (!this.PRIVATE_TOKEN) { diff --git a/packages/code-api/src/atomgit/types.ts b/packages/code-api/src/atomgit/types.ts index 112d17e2..078828b8 100644 --- a/packages/code-api/src/atomgit/types.ts +++ b/packages/code-api/src/atomgit/types.ts @@ -72,4 +72,15 @@ export namespace API { } ref: string; } + + export interface ResponseInfoAndBlobs { + content: string; + download_url: string; + encoding: string; + name: string; + path: string; + sha: string; + size: number; + type: 'file' | string; + } } From 4611fd0087baaa90b4ac7792ae8e09a41a0d9f1c Mon Sep 17 00:00:00 2001 From: Yan Qianyu Date: Mon, 18 Nov 2024 21:43:59 +0800 Subject: [PATCH 6/9] feat: atomgit getUser & createBranch add prefix --- packages/code-api/src/atomgit/atomgit.service.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index 049836ca..59b9ce9a 100644 --- a/packages/code-api/src/atomgit/atomgit.service.ts +++ b/packages/code-api/src/atomgit/atomgit.service.ts @@ -337,7 +337,7 @@ export class AtomGitAPIService implements ICodeAPIService { } async bulkChangeFiles(repo: IRepositoryModel, actions: FileAction[], header: FileActionHeader): Promise { const res = await this.request( - `/repos/${this.getProjectPath(repo)}commits/create`, + `/repos/${this.getProjectPath(repo)}/commits/create`, { data: { actions: actions.map((action) => ({ @@ -369,8 +369,8 @@ export class AtomGitAPIService implements ICodeAPIService { const res = await this.request(`/repos/${this.getProjectPath(repo)}/git/refs`, { method: 'post', data: { - sha: newBranch, - ref: ref, + sha: ref, + ref: `refs/heads/${newBranch}`, }, }); @@ -385,7 +385,7 @@ export class AtomGitAPIService implements ICodeAPIService { return resBranch; } getUser(_repo: IRepositoryModel): Promise { - throw new Error('Method not implemented.'); + return {} as any; } public async getProject(repo: IRepositoryModel): Promise { From 6634535448c43a3f02ff3a8318d579dcb3ea5c6a Mon Sep 17 00:00:00 2001 From: Yan Qianyu Date: Tue, 19 Nov 2024 13:44:09 +0800 Subject: [PATCH 7/9] fix: remove prefix in createBranch --- packages/code-api/src/atomgit/atomgit.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index 59b9ce9a..db7fe623 100644 --- a/packages/code-api/src/atomgit/atomgit.service.ts +++ b/packages/code-api/src/atomgit/atomgit.service.ts @@ -370,7 +370,7 @@ export class AtomGitAPIService implements ICodeAPIService { method: 'post', data: { sha: ref, - ref: `refs/heads/${newBranch}`, + ref: newBranch, }, }); From b805651ae0ce631579e1fc312cc434aee2ecda9d Mon Sep 17 00:00:00 2001 From: "yanqianyu.yqy" Date: Tue, 19 Nov 2024 17:53:37 +0800 Subject: [PATCH 8/9] feat: support getFiles in atomgit --- .../code-api/src/atomgit/atomgit.service.ts | 27 ++++++++++++++++--- packages/code-api/src/atomgit/types.ts | 7 +++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index db7fe623..3cd72abc 100644 --- a/packages/code-api/src/atomgit/atomgit.service.ts +++ b/packages/code-api/src/atomgit/atomgit.service.ts @@ -223,7 +223,7 @@ export class AtomGitAPIService implements ICodeAPIService { return Buffer.from(content); } async getBlobByCommitPath(repo: IRepositoryModel, commit: string, path: string): Promise { - const infoAndBlobs = await this.request( + const res = await this.request( `/repos/${this.getProjectPath(repo)}/contents/file`, { params: { @@ -232,7 +232,18 @@ export class AtomGitAPIService implements ICodeAPIService { }, }, ); - return new Uint8Array(new TextEncoder().encode(infoAndBlobs?.content || '')); + + 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) { @@ -332,8 +343,16 @@ 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); } async bulkChangeFiles(repo: IRepositoryModel, actions: FileAction[], header: FileActionHeader): Promise { const res = await this.request( diff --git a/packages/code-api/src/atomgit/types.ts b/packages/code-api/src/atomgit/types.ts index 078828b8..cd04fa4a 100644 --- a/packages/code-api/src/atomgit/types.ts +++ b/packages/code-api/src/atomgit/types.ts @@ -83,4 +83,11 @@ export namespace API { size: number; type: 'file' | string; } + + export interface ResponseFileNames { + path: string; + sha: string; + mode: string; + type: 'tree' | 'blob' | string; + } } From c41d2e0834ae6a34c1d643b6828e8c426ec0a46c Mon Sep 17 00:00:00 2001 From: "yanqianyu.yqy" Date: Tue, 19 Nov 2024 18:05:46 +0800 Subject: [PATCH 9/9] feat: hidden error msg when create file in web-scm --- packages/code-api/src/atomgit/atomgit.service.ts | 9 +++++++-- packages/code-api/src/atomgit/types.ts | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index 3cd72abc..d7b7fb2f 100644 --- a/packages/code-api/src/atomgit/atomgit.service.ts +++ b/packages/code-api/src/atomgit/atomgit.service.ts @@ -136,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; @@ -163,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; } @@ -222,7 +226,7 @@ export class AtomGitAPIService implements ICodeAPIService { return Buffer.from(content); } - async getBlobByCommitPath(repo: IRepositoryModel, commit: string, path: string): Promise { + async getBlobByCommitPath(repo: IRepositoryModel, commit: string, path: string, options?: API.RequestResponseOptions): Promise { const res = await this.request( `/repos/${this.getProjectPath(repo)}/contents/file`, { @@ -231,6 +235,7 @@ export class AtomGitAPIService implements ICodeAPIService { ref: commit }, }, + options ); const { content, encoding, type } = res; diff --git a/packages/code-api/src/atomgit/types.ts b/packages/code-api/src/atomgit/types.ts index cd04fa4a..b8ddfcd8 100644 --- a/packages/code-api/src/atomgit/types.ts +++ b/packages/code-api/src/atomgit/types.ts @@ -90,4 +90,8 @@ export namespace API { mode: string; type: 'tree' | 'blob' | string; } + + export interface RequestResponseOptions { + errorOption?: boolean; + } }