From 9381838ed64749057c1bc5c63efc8e065f983756 Mon Sep 17 00:00:00 2001 From: Yan Qianyu <284989344@qq.com> Date: Tue, 19 Nov 2024 18:41:03 +0800 Subject: [PATCH 1/2] feat: atomgit bulk change files apis (#227) * feat: support atomgit bulkChangeFiles api * fix: remove useless params in atomgit bulkChangeFiles * feat: support atomgit createBranch * fix: change response type from ResponseCommit to ResponseCommitInfo in atomgit service * feat: implement atomgit getBlobByCommitPath * feat: atomgit getUser & createBranch add prefix * fix: remove prefix in createBranch * feat: support getFiles in atomgit * feat: hidden error msg when create file in web-scm --- .../code-api/src/atomgit/atomgit.service.ts | 97 +++++++++++++++++-- packages/code-api/src/atomgit/types.ts | 45 +++++++++ 2 files changed, 132 insertions(+), 10 deletions(-) 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; + } } From 152bf694eed97514843689eb3e4e8efe4bf9138e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 19 Nov 2024 10:46:42 +0000 Subject: [PATCH 2/2] v2.3.4 --- lerna.json | 2 +- packages/cli/package.json | 2 +- packages/code-api/package.json | 2 +- packages/code-service/package.json | 2 +- packages/common/package.json | 2 +- packages/core/package.json | 2 +- packages/i18n/package.json | 2 +- packages/plugin/package.json | 2 +- packages/registry/package.json | 2 +- packages/startup/package.json | 2 +- packages/sumi-core/package.json | 2 +- packages/toolkit/package.json | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lerna.json b/lerna.json index b6cd0797..eaedebed 100644 --- a/lerna.json +++ b/lerna.json @@ -1,4 +1,4 @@ { "$schema": "node_modules/lerna/schemas/lerna-schema.json", - "version": "2.3.3" + "version": "2.3.4" } \ No newline at end of file diff --git a/packages/cli/package.json b/packages/cli/package.json index d6d2fe83..dd2ccd53 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-cli", - "version": "2.3.3", + "version": "2.3.4", "description": "@codeblitzjs/ide-cli", "main": "lib/commander.js", "files": [ diff --git a/packages/code-api/package.json b/packages/code-api/package.json index 6f2b8545..d2ea1de4 100644 --- a/packages/code-api/package.json +++ b/packages/code-api/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-code-api", - "version": "2.3.3", + "version": "2.3.4", "description": "@codeblitzjs/ide-code-api", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/packages/code-service/package.json b/packages/code-service/package.json index b222a5d5..aee053aa 100644 --- a/packages/code-service/package.json +++ b/packages/code-service/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-code-service", - "version": "2.3.3", + "version": "2.3.4", "description": "@codeblitzjs/ide-code-service", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/packages/common/package.json b/packages/common/package.json index 1753a8e0..04cd733c 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-common", - "version": "2.3.3", + "version": "2.3.4", "description": "@codeblitzjs/ide-common", "main": "lib/index.js", "typing": "lib/index.d.ts", diff --git a/packages/core/package.json b/packages/core/package.json index 7d23c629..5412cbd9 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-core", - "version": "2.3.3", + "version": "2.3.4", "description": "@codeblitzjs/ide-core", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/packages/i18n/package.json b/packages/i18n/package.json index e68370c0..bbc519f1 100644 --- a/packages/i18n/package.json +++ b/packages/i18n/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-i18n", - "version": "2.3.3", + "version": "2.3.4", "description": "@codeblitzjs/ide-i18n", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/packages/plugin/package.json b/packages/plugin/package.json index 4bbcebfd..b34d2f17 100644 --- a/packages/plugin/package.json +++ b/packages/plugin/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-plugin", - "version": "2.3.3", + "version": "2.3.4", "description": "@codeblitzjs/ide-plugin", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/packages/registry/package.json b/packages/registry/package.json index 083c2d16..e8ba62e2 100644 --- a/packages/registry/package.json +++ b/packages/registry/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-registry", - "version": "2.3.3", + "version": "2.3.4", "description": "@codeblitzjs/ide-registry", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/packages/startup/package.json b/packages/startup/package.json index 8ee8d17b..b5b17dec 100644 --- a/packages/startup/package.json +++ b/packages/startup/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-startup", - "version": "2.3.3", + "version": "2.3.4", "description": "@codeblitzjs/ide-startup", "main": "lib/index.js", "typing": "types/index.d.ts", diff --git a/packages/sumi-core/package.json b/packages/sumi-core/package.json index 30e5b465..df0c6a9a 100644 --- a/packages/sumi-core/package.json +++ b/packages/sumi-core/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-sumi-core", - "version": "2.3.3", + "version": "2.3.4", "description": "core", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index 03843d06..36c33c84 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@codeblitzjs/ide-toolkit", - "version": "2.3.3", + "version": "2.3.4", "description": "@codeblitzjs/ide-toolkit", "main": "index.js", "files": [