From b9701ed428baaa9263ee1a400c994d304572c8e8 Mon Sep 17 00:00:00 2001 From: Kishore <42832651+kishore03109@users.noreply.github.com> Date: Mon, 4 Dec 2023 03:45:11 +0800 Subject: [PATCH] feat(quickie): only gitfile should have quickie --- src/services/db/GitHubService.ts | 172 ++++++++++++++++++++++++- src/services/db/GithubCommitService.ts | 19 +-- src/services/db/RepoService.ts | 60 +++++---- 3 files changed, 206 insertions(+), 45 deletions(-) diff --git a/src/services/db/GitHubService.ts b/src/services/db/GitHubService.ts index 285183a80..4973ee212 100644 --- a/src/services/db/GitHubService.ts +++ b/src/services/db/GitHubService.ts @@ -559,8 +559,7 @@ export default class GitHubService { async updateTree( sessionData: UserWithSiteSessionData, githubSessionData: GithubSessionData, - { gitTree, message }: { gitTree: any; message: any }, - isStaging: boolean + { gitTree, message }: { gitTree: any; message: any } ) { const { accessToken, siteName, isomerUserId: userId } = sessionData const { treeSha, currentCommitSha } = githubSessionData.getGithubState() @@ -604,16 +603,175 @@ export default class GitHubService { return newCommitSha } - async updateRepoState( + async deleteDirectory( sessionData: UserWithSiteSessionData, { - commitSha, - branchName = STAGING_BRANCH, - }: { commitSha: any; branchName?: string } + directoryName, + message, + githubSessionData, + }: { + directoryName: string + message: string + githubSessionData: GithubSessionData + } + ): Promise { + // GitHub flow + const gitTree = await this.getTree(sessionData, githubSessionData, { + isRecursive: true, + }) + + // Retrieve removed items and set their sha to null + const newGitTree = gitTree + .filter( + (item) => + item.path.startsWith(`${directoryName}/`) && item.type !== "tree" + ) + .map((item) => ({ + ...item, + sha: null, + })) + + const newCommitSha = await this.updateTree(sessionData, githubSessionData, { + gitTree: newGitTree, + message, + }) + + await this.updateRepoState(sessionData, { + commitSha: newCommitSha, + }) + } + + async moveFiles( + sessionData: UserWithSiteSessionData, + githubSessionData: GithubSessionData, + oldPath: string, + newPath: string, + targetFiles: string[], + message?: string + ): Promise { + const gitTree = await this.getTree(sessionData, githubSessionData, { + isRecursive: true, + }) + const newGitTree: any[] = [] + + gitTree.forEach((item: any) => { + if (item.path.startsWith(`${newPath}/`) && item.type !== "tree") { + const fileName = item.path + .split(`${newPath}/`) + .slice(1) + .join(`${newPath}/`) + if (targetFiles.includes(fileName)) { + // Conflicting file + throw new ConflictError("File already exists in target directory") + } + } + if (item.path.startsWith(`${oldPath}/`) && item.type !== "tree") { + const fileName = item.path + .split(`${oldPath}/`) + .slice(1) + .join(`${oldPath}/`) + if (targetFiles.includes(fileName)) { + // Add file to target directory + newGitTree.push({ + ...item, + path: `${newPath}/${fileName}`, + }) + // Delete old file + newGitTree.push({ + ...item, + sha: null, + }) + } + } + }) + + const newCommitSha = await this.updateTree(sessionData, githubSessionData, { + gitTree: newGitTree, + message, + }) + + await this.updateRepoState(sessionData, { + commitSha: newCommitSha, + }) + + return { newSha: newCommitSha } + } + + async renameSinglePath( + sessionData: UserWithSiteSessionData, + githubSessionData: GithubSessionData, + oldPath: string, + newPath: string, + message?: string, + isStaging = true + ): Promise { + const gitTree = await this.getTree( + sessionData, + githubSessionData, + { + isRecursive: true, + }, + !!isStaging + ) + const newGitTree: any[] = [] + const isMovingDirectory = + gitTree.find((item: any) => item.path === oldPath)?.type === "tree" || + false + + gitTree.forEach((item: any) => { + if (isMovingDirectory) { + if (item.path === newPath && item.type === "tree") { + throw new ConflictError("Target directory already exists") + } else if (item.path === oldPath && item.type === "tree") { + // Rename old subdirectory to new name + newGitTree.push({ + ...item, + path: newPath, + }) + } else if ( + item.path.startsWith(`${oldPath}/`) && + item.type !== "tree" + ) { + // Delete old files + newGitTree.push({ + ...item, + sha: null, + }) + } + } else if (item.path === newPath && item.type !== "tree") { + throw new ConflictError("Target file already exists") + } else if (item.path === oldPath && item.type !== "tree") { + // Add file to new directory + newGitTree.push({ + ...item, + path: newPath, + }) + // Delete old file + newGitTree.push({ + ...item, + sha: null, + }) + } + }) + + const newCommitSha = await this.updateTree(sessionData, githubSessionData, { + gitTree: newGitTree, + message, + }) + await this.updateRepoState(sessionData, { + commitSha: newCommitSha, + }) + + return { newSha: newCommitSha } + } + + async updateRepoState( + sessionData: UserWithSiteSessionData, + { commitSha }: { commitSha: any } ) { const { accessToken } = sessionData const { siteName } = sessionData - const refEndpoint = `${siteName}/git/refs/heads/${branchName}` + const refEndpoint = `${siteName}/git/refs/heads/${STAGING_BRANCH}` const headers = { Authorization: `token ${accessToken}`, } diff --git a/src/services/db/GithubCommitService.ts b/src/services/db/GithubCommitService.ts index 102639c69..3d0e386a0 100644 --- a/src/services/db/GithubCommitService.ts +++ b/src/services/db/GithubCommitService.ts @@ -149,15 +149,10 @@ export default class GitHubCommitService extends GitHubService { sha: null, })) - const newCommitSha = await this.updateTree( - sessionData, - githubSessionData, - { - gitTree: newGitTree, - message, - }, - !!isStaging - ) + const newCommitSha = await this.updateTree(sessionData, githubSessionData, { + gitTree: newGitTree, + message, + }) const deletePromises = [ this.updateRepoState(sessionData, { @@ -280,8 +275,7 @@ export default class GitHubCommitService extends GitHubService { { gitTree: newGitTree, message, - }, - !!isStaging + } ) await super.updateRepoState(sessionData, { commitSha: newCommitSha, @@ -362,8 +356,7 @@ export default class GitHubCommitService extends GitHubService { { gitTree: newGitTree, message, - }, - !!isStaging + } ) await super.updateRepoState(sessionData, { diff --git a/src/services/db/RepoService.ts b/src/services/db/RepoService.ts index 7e386e04a..dc79a5b1f 100644 --- a/src/services/db/RepoService.ts +++ b/src/services/db/RepoService.ts @@ -79,7 +79,6 @@ interface RepoServiceParams { isomerRepoAxiosInstance: AxiosCacheInstance gitFileSystemService: GitFileSystemService gitFileCommitService: GitFileCommitService - gitHubCommitService: GitHubCommitService } export default class RepoService extends GitHubService { @@ -87,18 +86,14 @@ export default class RepoService extends GitHubService { private readonly gitFileCommitService: GitFileCommitService - private readonly githubCommitService: GitHubCommitService - constructor({ isomerRepoAxiosInstance, gitFileSystemService, gitFileCommitService, - gitHubCommitService, }: RepoServiceParams) { super({ axiosInstance: isomerRepoAxiosInstance }) this.gitFileSystemService = gitFileSystemService this.gitFileCommitService = gitFileCommitService - this.githubCommitService = gitHubCommitService } getCommitDiff(siteName: string, base?: string, head?: string) { @@ -209,11 +204,12 @@ export default class RepoService extends GitHubService { isMedia, }) } - return this.githubCommitService.create(sessionData, { + return super.create(sessionData, { content, fileName, directoryName, isMedia, + branchName: STAGING_BRANCH, }) } @@ -419,11 +415,12 @@ export default class RepoService extends GitHubService { }) } - return this.githubCommitService.update(sessionData, { + return super.update(sessionData, { fileContent, sha, fileName, directoryName, + branchName: STAGING_BRANCH, }) } @@ -451,10 +448,29 @@ export default class RepoService extends GitHubService { return } - await this.githubCommitService.deleteDirectory(sessionData, { - directoryName, + // GitHub flow + const gitTree = await this.getTree(sessionData, githubSessionData, { + isRecursive: true, + }) + + // Retrieve removed items and set their sha to null + const newGitTree = gitTree + .filter( + (item) => + item.path.startsWith(`${directoryName}/`) && item.type !== "tree" + ) + .map((item) => ({ + ...item, + sha: null, + })) + + const newCommitSha = await this.updateTree(sessionData, githubSessionData, { + gitTree: newGitTree, message, - githubSessionData, + }) + + await this.updateRepoState(sessionData, { + commitSha: newCommitSha, }) } @@ -486,7 +502,7 @@ export default class RepoService extends GitHubService { } // GitHub flow - await this.githubCommitService.delete(sessionData, { + await super.delete(sessionData, { sha, fileName, directoryName, @@ -514,7 +530,7 @@ export default class RepoService extends GitHubService { message ) } - return this.githubCommitService.renameSinglePath( + return super.renameSinglePath( sessionData, githubSessionData, oldPath, @@ -547,7 +563,7 @@ export default class RepoService extends GitHubService { ) } - return this.githubCommitService.moveFiles( + return super.moveFiles( sessionData, githubSessionData, oldPath, @@ -604,18 +620,12 @@ export default class RepoService extends GitHubService { async updateTree( sessionData: any, githubSessionData: any, - { gitTree, message }: any, - isStaging: boolean + { gitTree, message }: any ): Promise { - return super.updateTree( - sessionData, - githubSessionData, - { - gitTree, - message, - }, - isStaging - ) + return super.updateTree(sessionData, githubSessionData, { + gitTree, + message, + }) } async updateRepoState( @@ -646,7 +656,7 @@ export default class RepoService extends GitHubService { return } - await super.updateRepoState(sessionData, { commitSha, branchName }) + await super.updateRepoState(sessionData, { commitSha }) } async checkHasAccess(sessionData: any): Promise {