Skip to content

Commit

Permalink
feat(quickie): only gitfile should have quickie
Browse files Browse the repository at this point in the history
  • Loading branch information
kishore03109 committed Dec 4, 2023
1 parent 05329f0 commit b9701ed
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 45 deletions.
172 changes: 165 additions & 7 deletions src/services/db/GitHubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<void> {
// 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<GitCommitResult> {
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<GitCommitResult> {
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}`,
}
Expand Down
19 changes: 6 additions & 13 deletions src/services/db/GithubCommitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -280,8 +275,7 @@ export default class GitHubCommitService extends GitHubService {
{
gitTree: newGitTree,
message,
},
!!isStaging
}
)
await super.updateRepoState(sessionData, {
commitSha: newCommitSha,
Expand Down Expand Up @@ -362,8 +356,7 @@ export default class GitHubCommitService extends GitHubService {
{
gitTree: newGitTree,
message,
},
!!isStaging
}
)

await super.updateRepoState(sessionData, {
Expand Down
60 changes: 35 additions & 25 deletions src/services/db/RepoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,21 @@ interface RepoServiceParams {
isomerRepoAxiosInstance: AxiosCacheInstance
gitFileSystemService: GitFileSystemService
gitFileCommitService: GitFileCommitService
gitHubCommitService: GitHubCommitService
}

export default class RepoService extends GitHubService {
private readonly gitFileSystemService: GitFileSystemService

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) {
Expand Down Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -486,7 +502,7 @@ export default class RepoService extends GitHubService {
}

// GitHub flow
await this.githubCommitService.delete(sessionData, {
await super.delete(sessionData, {
sha,
fileName,
directoryName,
Expand Down Expand Up @@ -514,7 +530,7 @@ export default class RepoService extends GitHubService {
message
)
}
return this.githubCommitService.renameSinglePath(
return super.renameSinglePath(
sessionData,
githubSessionData,
oldPath,
Expand Down Expand Up @@ -547,7 +563,7 @@ export default class RepoService extends GitHubService {
)
}

return this.githubCommitService.moveFiles(
return super.moveFiles(
sessionData,
githubSessionData,
oldPath,
Expand Down Expand Up @@ -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<any> {
return super.updateTree(
sessionData,
githubSessionData,
{
gitTree,
message,
},
isStaging
)
return super.updateTree(sessionData, githubSessionData, {
gitTree,
message,
})
}

async updateRepoState(
Expand Down Expand Up @@ -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<any> {
Expand Down

0 comments on commit b9701ed

Please sign in to comment.