Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(quickie): only gitfile should have quickie #1042

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is functionally identical to the one in repoService - we could probably delete either this or replace the reposervice implementation with a super.deleteDirectory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!
link to diff checker as a sanity check: https://www.diffchecker.com/4HWiYNIH/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not entirely sure wym here - the diff checker shows that there's 1 with the flagging functionality (which i assume is in repoService?) so we should keep the base and hte one that does the flagging functionality can just call super.

idk if this is done here or downstream (cos i see that this is still unchanged) but if it's resolved downstream, feel free to resolve this

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(
kishore03109 marked this conversation as resolved.
Show resolved Hide resolved
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(
kishore03109 marked this conversation as resolved.
Show resolved Hide resolved
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: string }
) {
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
38 changes: 14 additions & 24 deletions src/services/db/RepoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { getMediaFileInfo } from "@root/utils/media-utils"

import GitFileCommitService from "./GitFileCommitService"
import GitFileSystemService from "./GitFileSystemService"
import GitHubCommitService from "./GithubCommitService"
import GitHubService from "./GitHubService"
import * as ReviewApi from "./review"

Expand Down Expand Up @@ -79,26 +78,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 +203,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 +414,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,7 +447,7 @@ export default class RepoService extends GitHubService {
return
}

await this.githubCommitService.deleteDirectory(sessionData, {
super.deleteDirectory(sessionData, {
directoryName,
message,
githubSessionData,
Expand Down Expand Up @@ -486,7 +482,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 +510,7 @@ export default class RepoService extends GitHubService {
message
)
}
return this.githubCommitService.renameSinglePath(
return super.renameSinglePath(
sessionData,
githubSessionData,
oldPath,
Expand Down Expand Up @@ -547,7 +543,7 @@ export default class RepoService extends GitHubService {
)
}

return this.githubCommitService.moveFiles(
return super.moveFiles(
sessionData,
githubSessionData,
oldPath,
Expand Down Expand Up @@ -604,18 +600,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 +636,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
Loading