Skip to content

Commit

Permalink
feat: atomgit bulk change files apis (#227)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yanqianyu authored Nov 19, 2024
1 parent 88503a8 commit 9381838
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 10 deletions.
97 changes: 87 additions & 10 deletions packages/code-api/src/atomgit/atomgit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
EntryParam,
FileAction,
FileActionHeader,
FileActionResult,
GetEntryInfoParam,
GitlensBlame,
ICodeAPIService,
Expand Down Expand Up @@ -135,7 +136,7 @@ export class AtomGitAPIService implements ICodeAPIService {
});
}

protected async request<T>(path: string, options?: RequestOptions): Promise<T> {
protected async request<T>(path: string, options?: RequestOptions, responseOptions?: API.RequestResponseOptions): Promise<T> {
try {
const { headers, ...rest } = options || {};
const privateToken = this.PRIVATE_TOKEN;
Expand All @@ -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;
}
Expand Down Expand Up @@ -221,8 +226,29 @@ export class AtomGitAPIService implements ICodeAPIService {

return Buffer.from(content);
}
getBlobByCommitPath(_repo: IRepositoryModel, _commit: string, _path: string, _options?: any): Promise<Uint8Array> {
throw new Error('Method not implemented.');
async getBlobByCommitPath(repo: IRepositoryModel, commit: string, path: string, options?: API.RequestResponseOptions): Promise<Uint8Array> {
const res = await this.request<API.ResponseInfoAndBlobs>(
`/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<BranchOrTag[]> {
if (!this.PRIVATE_TOKEN) {
Expand Down Expand Up @@ -322,17 +348,68 @@ export class AtomGitAPIService implements ICodeAPIService {
getCommitCompare(_repo: IRepositoryModel, _from: string, _to: string): Promise<CommitFileChange[]> {
throw new Error('Method not implemented.');
}
async getFiles(_repo: IRepositoryModel): Promise<string[]> {
return [];
async getFiles(repo: IRepositoryModel): Promise<string[]> {
const fileList = await this.request<API.ResponseFileNames[]>(
`/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<any> {
throw new Error('Method not implemented.');
async bulkChangeFiles(repo: IRepositoryModel, actions: FileAction[], header: FileActionHeader): Promise<FileActionResult[]> {
const res = await this.request<API.ResponseCommitInfo>(
`/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<Branch> {
throw new Error('Method not implemented.');
async createBranch(repo: IRepositoryModel, newBranch: string, ref: string): Promise<Branch> {
const res = await this.request<API.ResponseBranch>(`/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<any> {
throw new Error('Method not implemented.');
return {} as any;
}

public async getProject(repo: IRepositoryModel): Promise<Project> {
Expand Down
45 changes: 45 additions & 0 deletions packages/code-api/src/atomgit/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 9381838

Please sign in to comment.