From f7ce64f524401f5a22dddc32ae878c3edb2b664d Mon Sep 17 00:00:00 2001 From: Kishore <42832651+kishore03109@users.noreply.github.com> Date: Mon, 4 Dec 2023 04:41:23 +0800 Subject: [PATCH 1/2] chore(quickie): delete quickie for gh --- src/integration/Notifications.spec.ts | 3 - src/integration/Privatisation.spec.ts | 4 +- src/integration/Reviews.spec.ts | 4 +- src/integration/Sites.spec.ts | 4 +- src/server.js | 3 - src/services/db/GitHubService.ts | 26 +- src/services/db/GithubCommitService.ts | 383 ------------------ src/services/db/RepoService.ts | 1 - src/services/db/__tests__/RepoService.spec.ts | 352 ++++++++-------- 9 files changed, 189 insertions(+), 591 deletions(-) delete mode 100644 src/services/db/GithubCommitService.ts diff --git a/src/integration/Notifications.spec.ts b/src/integration/Notifications.spec.ts index b25a4b660..3fc89a30a 100644 --- a/src/integration/Notifications.spec.ts +++ b/src/integration/Notifications.spec.ts @@ -34,7 +34,6 @@ import { import { getAuthorizationMiddleware } from "@root/middleware" import { NotificationsRouter as _NotificationsRouter } from "@root/routes/v2/authenticated/notifications" import GitFileCommitService from "@root/services/db/GitFileCommitService" -import GitHubCommitService from "@root/services/db/GithubCommitService" import { BaseDirectoryService } from "@root/services/directoryServices/BaseDirectoryService" import { ResourceRoomDirectoryService } from "@root/services/directoryServices/ResourceRoomDirectoryService" import { CollectionPageService } from "@root/services/fileServices/MdPageServices/CollectionPageService" @@ -71,12 +70,10 @@ const MOCK_SITE_MEMBER_ID = "1" const gitFileSystemService = new GitFileSystemService(simpleGit()) const gitFileCommitService = new GitFileCommitService(gitFileSystemService) -const gitHubCommitService = new GitHubCommitService(isomerRepoAxiosInstance) const gitHubService = new RepoService({ isomerRepoAxiosInstance, gitFileSystemService, gitFileCommitService, - gitHubCommitService, }) const identityAuthService = getIdentityAuthService(gitHubService) const usersService = getUsersService(sequelize) diff --git a/src/integration/Privatisation.spec.ts b/src/integration/Privatisation.spec.ts index d47157bc0..c129a5569 100644 --- a/src/integration/Privatisation.spec.ts +++ b/src/integration/Privatisation.spec.ts @@ -45,7 +45,6 @@ import { AuthorizationMiddleware } from "@root/middleware/authorization" import { SettingsRouter as _SettingsRouter } from "@root/routes/v2/authenticatedSites/settings" import { SettingsService } from "@root/services/configServices/SettingsService" import GitFileCommitService from "@root/services/db/GitFileCommitService" -import GitHubCommitService from "@root/services/db/GithubCommitService" import { BaseDirectoryService } from "@root/services/directoryServices/BaseDirectoryService" import { ResourceRoomDirectoryService } from "@root/services/directoryServices/ResourceRoomDirectoryService" import { CollectionPageService } from "@root/services/fileServices/MdPageServices/CollectionPageService" @@ -100,12 +99,11 @@ jest.mock("../services/identity/DeploymentClient", () => ) const gitFileSystemService = new GitFileSystemService(simpleGit()) const gitFileCommitService = new GitFileCommitService(gitFileSystemService) -const gitHubCommitService = new GitHubCommitService(isomerRepoAxiosInstance) + const gitHubService = new RepoService({ isomerRepoAxiosInstance, gitFileSystemService, gitFileCommitService, - gitHubCommitService, }) const configYmlService = new ConfigYmlService({ gitHubService }) const usersService = getUsersService(sequelize) diff --git a/src/integration/Reviews.spec.ts b/src/integration/Reviews.spec.ts index 927dc7956..a63d551a5 100644 --- a/src/integration/Reviews.spec.ts +++ b/src/integration/Reviews.spec.ts @@ -75,7 +75,6 @@ import { } from "@fixtures/users" import { ReviewRequestStatus } from "@root/constants" import GitFileCommitService from "@root/services/db/GitFileCommitService" -import GitHubCommitService from "@root/services/db/GithubCommitService" import { BaseDirectoryService } from "@root/services/directoryServices/BaseDirectoryService" import { ResourceRoomDirectoryService } from "@root/services/directoryServices/ResourceRoomDirectoryService" import { CollectionPageService } from "@root/services/fileServices/MdPageServices/CollectionPageService" @@ -105,12 +104,11 @@ import { sequelize } from "@tests/database" const gitFileSystemService = new GitFileSystemService(simpleGit()) const gitFileCommitService = new GitFileCommitService(gitFileSystemService) -const gitHubCommitService = new GitHubCommitService(isomerRepoAxiosInstance) + const gitHubService = new RepoService({ isomerRepoAxiosInstance, gitFileSystemService, gitFileCommitService, - gitHubCommitService, }) const configYmlService = new ConfigYmlService({ gitHubService }) const usersService = getUsersService(sequelize) diff --git a/src/integration/Sites.spec.ts b/src/integration/Sites.spec.ts index 0df835d79..61d3f5a1c 100644 --- a/src/integration/Sites.spec.ts +++ b/src/integration/Sites.spec.ts @@ -26,7 +26,6 @@ import { statsMiddleware } from "@root/middleware/stats" import { SitesRouter as _SitesRouter } from "@root/routes/v2/authenticated/sites" import { isomerRepoAxiosInstance } from "@root/services/api/AxiosInstance" import GitFileCommitService from "@root/services/db/GitFileCommitService" -import GitHubCommitService from "@root/services/db/GithubCommitService" import { BaseDirectoryService } from "@root/services/directoryServices/BaseDirectoryService" import { ResourceRoomDirectoryService } from "@root/services/directoryServices/ResourceRoomDirectoryService" import { CollectionPageService } from "@root/services/fileServices/MdPageServices/CollectionPageService" @@ -67,13 +66,12 @@ const mockPermissions = { push: true } const mockPrivate = true const gitFileSystemService = new GitFileSystemService(simpleGit()) -const gitHubCommitService = new GitHubCommitService(isomerRepoAxiosInstance) + const gitFileCommitService = new GitFileCommitService(gitFileSystemService) const gitHubService = new RepoService({ isomerRepoAxiosInstance, gitFileSystemService, gitFileCommitService, - gitHubCommitService, }) const configYmlService = new ConfigYmlService({ gitHubService }) const usersService = getUsersService(sequelize) diff --git a/src/server.js b/src/server.js index 73027b686..302e10ea9 100644 --- a/src/server.js +++ b/src/server.js @@ -79,7 +79,6 @@ import { SgidAuthRouter } from "./routes/v2/sgidAuth" import RepoManagementService from "./services/admin/RepoManagementService" import GitFileCommitService from "./services/db/GitFileCommitService" import GitFileSystemService from "./services/db/GitFileSystemService" -import GitHubCommitService from "./services/db/GithubCommitService" import RepoService from "./services/db/RepoService" import { PageService } from "./services/fileServices/MdPageServices/PageService" import { ConfigService } from "./services/fileServices/YmlFileServices/ConfigService" @@ -170,14 +169,12 @@ const simpleGitInstance = new simpleGit({ }) const gitFileSystemService = new GitFileSystemService(simpleGitInstance) -const gitHubCommitService = new GitHubCommitService(isomerRepoAxiosInstance) const gitFileCommitService = new GitFileCommitService(gitFileSystemService) const gitHubService = new RepoService({ isomerRepoAxiosInstance, gitFileSystemService, gitFileCommitService, - gitHubCommitService, }) const repoManagementService = new RepoManagementService({ diff --git a/src/services/db/GitHubService.ts b/src/services/db/GitHubService.ts index f4076e281..5c312e398 100644 --- a/src/services/db/GitHubService.ts +++ b/src/services/db/GitHubService.ts @@ -132,13 +132,11 @@ export default class GitHubService { fileName, directoryName, isMedia = false, - branchName = STAGING_BRANCH, }: { content: string fileName: string directoryName: string isMedia: boolean - branchName: string } ) { const { accessToken, siteName, isomerUserId: userId } = sessionData @@ -185,7 +183,7 @@ export default class GitHubService { const params = { message, content: encodedContent, - branch: branchName, + branch: STAGING_BRANCH, } const resp = await this.axiosInstance.put(endpoint, params, { @@ -209,18 +207,14 @@ export default class GitHubService { async read( sessionData: UserWithSiteSessionData, - { - fileName, - directoryName, - branchName = STAGING_BRANCH, - }: { fileName: any; directoryName: any; branchName?: string } + { fileName, directoryName }: { fileName: any; directoryName: any } ) { const { accessToken } = sessionData const { siteName } = sessionData const endpoint = this.getFilePath({ siteName, fileName, directoryName }) const params = { - ref: branchName, + ref: STAGING_BRANCH, } const resp = await this.axiosInstance.get(endpoint, { @@ -241,10 +235,7 @@ export default class GitHubService { async readMedia( sessionData: UserWithSiteSessionData, - { - fileSha, - branchName = STAGING_BRANCH, - }: { fileSha: any; branchName?: string } + { fileSha }: { fileSha: any; branchName?: string } ) { /** * Files that are bigger than 1 MB needs to be retrieved @@ -254,7 +245,7 @@ export default class GitHubService { const { accessToken } = sessionData const { siteName } = sessionData const params = { - ref: branchName, + ref: STAGING_BRANCH, } const blobEndpoint = this.getBlobPath({ siteName, fileSha }) @@ -379,12 +370,10 @@ export default class GitHubService { sha, fileName, directoryName, - branchName = STAGING_BRANCH, }: { sha: string fileName: string directoryName: string - branchName?: string } ) { const { accessToken, siteName, isomerUserId: userId } = sessionData @@ -392,11 +381,10 @@ export default class GitHubService { const endpoint = this.getFilePath({ siteName, fileName, directoryName }) let fileSha = sha - if (!sha || branchName === STAGING_LITE_BRANCH) { + if (!sha) { const { sha: retrievedSha } = await this.read(sessionData, { fileName, directoryName, - branchName, }) fileSha = retrievedSha } @@ -408,7 +396,7 @@ export default class GitHubService { }) const params = { message, - branch: branchName, + branch: STAGING_BRANCH, sha: fileSha, } diff --git a/src/services/db/GithubCommitService.ts b/src/services/db/GithubCommitService.ts deleted file mode 100644 index 3d0e386a0..000000000 --- a/src/services/db/GithubCommitService.ts +++ /dev/null @@ -1,383 +0,0 @@ -import { AxiosCacheInstance } from "axios-cache-interceptor" - -import GithubSessionData from "@root/classes/GithubSessionData" -import UserWithSiteSessionData from "@root/classes/UserWithSiteSessionData" -import { STAGING_BRANCH, STAGING_LITE_BRANCH } from "@root/constants" -import { ConflictError } from "@root/errors/ConflictError" -import { GitCommitResult } from "@root/types/gitfilesystem" -import isFileAsset from "@root/utils/commit-utils" -import { isReduceBuildTimesWhitelistedRepo } from "@root/utils/growthbook-utils" -import GitHubService from "@services/db/GitHubService" - -/** - * Responsibilities of this class - * 1. Creates all commits to staging - * 2. Creates non-asset related commits to staging-lite - */ -export default class GitHubCommitService extends GitHubService { - constructor(axiosInstance: AxiosCacheInstance) { - super({ axiosInstance }) - } - - async create( - sessionData: UserWithSiteSessionData, - { - content, - fileName, - directoryName, - isMedia = false, - }: { - content: string - fileName: string - directoryName: string - isMedia?: boolean - } - ): Promise<{ - sha: string - }> { - const createPromises = [ - super.create(sessionData, { - content, - fileName, - directoryName, - isMedia, - branchName: STAGING_BRANCH, - }), - ] - const shouldStagingLiteUpdate = - !isFileAsset({ directoryName, fileName }) && - isReduceBuildTimesWhitelistedRepo(sessionData.growthbook) - - if (shouldStagingLiteUpdate) { - createPromises.push( - super.create(sessionData, { - content, - directoryName, - fileName, - isMedia, - branchName: STAGING_LITE_BRANCH, - }) - ) - } - - // We still await and throw error if the commit to staging-lite fails, - // but the caller only gets the `resultToStaging` commit returned - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const [resultToStaging, _] = await Promise.all(createPromises) - - return resultToStaging - } - - async update( - sessionData: UserWithSiteSessionData, - { - fileContent, - sha, - fileName, - directoryName, - }: { - fileContent: string - sha: string - fileName: string - directoryName: string | undefined - } - ): Promise { - const updatePromises = [ - super.update(sessionData, { - fileContent, - sha, - fileName, - directoryName, - branchName: STAGING_BRANCH, - }), - ] - const shouldStagingLiteUpdate = - !isFileAsset({ directoryName, fileName }) && - isReduceBuildTimesWhitelistedRepo(sessionData.growthbook) - - if (shouldStagingLiteUpdate) { - updatePromises.push( - super.update(sessionData, { - fileContent, - sha, - fileName, - directoryName, - branchName: STAGING_LITE_BRANCH, - }) - ) - } - - // We still await and throw error if the commit to staging-lite fails, - // but the caller only gets the `resultToStaging` commit returned - const [resultToStaging, _] = await Promise.all(updatePromises) - - return resultToStaging - } - - async deleteDirectory( - sessionData: UserWithSiteSessionData, - { - directoryName, - message, - githubSessionData, - isStaging = true, - }: { - directoryName: string - message: string - githubSessionData: GithubSessionData - isStaging?: boolean - } - ): Promise { - // GitHub flow - const gitTree = await this.getTree( - sessionData, - githubSessionData, - { - isRecursive: true, - }, - isStaging - ) - - // 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, - }) - - const deletePromises = [ - this.updateRepoState(sessionData, { - commitSha: newCommitSha, - }), - ] - - if ( - isReduceBuildTimesWhitelistedRepo(sessionData.growthbook) && - !isFileAsset({ directoryName }) - ) { - deletePromises.push( - this.deleteDirectory(sessionData, { - directoryName, - message, - githubSessionData, - isStaging: false, - }) - ) - } - await Promise.all(deletePromises) - } - - async delete( - sessionData: UserWithSiteSessionData, - { - sha, - fileName, - directoryName, - }: { - sha: string - fileName: string - directoryName: string - } - ): Promise { - const deletePromises = [ - super.delete(sessionData, { - sha, - fileName, - directoryName, - }), - ] - const shouldStagingLiteUpdate = - !isFileAsset({ directoryName, fileName }) && - isReduceBuildTimesWhitelistedRepo(sessionData.growthbook) - if (shouldStagingLiteUpdate) { - deletePromises.push( - super.delete(sessionData, { - sha, - fileName, - directoryName, - branchName: STAGING_LITE_BRANCH, - }) - ) - } - - await Promise.all(deletePromises) - } - - async renameSinglePath( - sessionData: UserWithSiteSessionData, - githubSessionData: GithubSessionData, - oldPath: string, - newPath: string, - message?: string, - isStaging = true - ): Promise { - const gitTree = await super.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 super.updateTree( - sessionData, - githubSessionData, - { - gitTree: newGitTree, - message, - } - ) - await super.updateRepoState(sessionData, { - commitSha: newCommitSha, - }) - - const shouldStagingLiteUpdate = - isReduceBuildTimesWhitelistedRepo(sessionData.growthbook) && - !isFileAsset({ directoryName: oldPath }) - - if (shouldStagingLiteUpdate) { - // we await this call, but we do not need to return this result - await this.renameSinglePath( - sessionData, - githubSessionData, - oldPath, - newPath, - message, - false - ) - } - - return { newSha: newCommitSha } - } - - async moveFiles( - sessionData: UserWithSiteSessionData, - githubSessionData: GithubSessionData, - oldPath: string, - newPath: string, - targetFiles: string[], - message?: string, - isStaging = true - ): Promise { - const gitTree = await super.getTree( - sessionData, - githubSessionData, - { - isRecursive: true, - }, - isStaging - ) - 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 super.updateTree( - sessionData, - githubSessionData, - { - gitTree: newGitTree, - message, - } - ) - - await super.updateRepoState(sessionData, { - commitSha: newCommitSha, - }) - - const shouldUpdateStagingLite = - isReduceBuildTimesWhitelistedRepo(sessionData.growthbook) && - !isFileAsset({ directoryName: oldPath }) - if (shouldUpdateStagingLite) { - // We don't have to return the sha, just update this should be ok - await this.moveFiles( - sessionData, - githubSessionData, - oldPath, - newPath, - targetFiles, - message, - false - ) - } - return { newSha: newCommitSha } - } -} diff --git a/src/services/db/RepoService.ts b/src/services/db/RepoService.ts index 97f5646c3..1d48bbeda 100644 --- a/src/services/db/RepoService.ts +++ b/src/services/db/RepoService.ts @@ -208,7 +208,6 @@ export default class RepoService extends GitHubService { fileName, directoryName, isMedia, - branchName: STAGING_BRANCH, }) } diff --git a/src/services/db/__tests__/RepoService.spec.ts b/src/services/db/__tests__/RepoService.spec.ts index 7f482bf60..26675dd7b 100644 --- a/src/services/db/__tests__/RepoService.spec.ts +++ b/src/services/db/__tests__/RepoService.spec.ts @@ -8,7 +8,6 @@ import { mockGithubSessionData, mockGrowthBook, mockIsomerUserId, - mockSiteName, mockUserWithSiteSessionDataAndGrowthBook, } from "@fixtures/sessionData" import UserWithSiteSessionData from "@root/classes/UserWithSiteSessionData" @@ -24,7 +23,6 @@ import GitFileSystemService from "@services/db/GitFileSystemService" import _RepoService from "@services/db/RepoService" import GitFileCommitService from "../GitFileCommitService" -import GitHubCommitService from "../GithubCommitService" import GitHubService from "../GitHubService" const MockAxiosInstance = { @@ -58,7 +56,7 @@ const MockGitFileCommitService = { moveFiles: jest.fn(), } -const MockGitHubCommitService = { +const MockGitHubService = { create: jest.fn(), update: jest.fn(), delete: jest.fn(), @@ -71,7 +69,6 @@ const RepoService = new _RepoService({ isomerRepoAxiosInstance: (MockAxiosInstance as unknown) as AxiosCacheInstance, gitFileSystemService: (MockGitFileSystemService as unknown) as GitFileSystemService, gitFileCommitService: (MockGitFileCommitService as unknown) as GitFileCommitService, - gitHubCommitService: (MockGitHubCommitService as unknown) as GitHubCommitService, }) describe("RepoService", () => { @@ -179,7 +176,6 @@ describe("RepoService", () => { const expected = { sha: "test-sha", } - MockGitHubCommitService.create.mockResolvedValueOnce(expected) const actual = await RepoService.create(sessionData, { content: mockContent, @@ -189,7 +185,7 @@ describe("RepoService", () => { }) expect(actual).toEqual(expected) - expect(MockGitHubCommitService.create).toHaveBeenCalledWith(sessionData, { + expect(MockGitHubService.create).toHaveBeenCalledWith(sessionData, { content: mockContent, fileName: mockFileName, directoryName: mockDirectoryName, @@ -568,9 +564,6 @@ describe("RepoService", () => { email: mockEmail, siteName: "not-whitelisted", }) - MockGitHubCommitService.update.mockResolvedValueOnce({ - newSha: expectedSha, - }) const actual = await RepoService.update(sessionData, { fileContent: "test content", @@ -616,200 +609,213 @@ describe("RepoService", () => { siteName: "not-whitelisted", }) + const mockedGitHubService = jest + .spyOn(GitHubService.prototype, "delete") + .mockResolvedValueOnce(undefined) + await RepoService.delete(sessionData, { sha: "fake-original-sha", fileName: "test.md", directoryName: "pages", }) - expect(MockGitHubCommitService.delete).toBeCalledTimes(1) - expect(MockGitHubCommitService.delete).toBeCalledWith(sessionData, { + expect(mockedGitHubService).toBeCalledWith(sessionData, { sha: "fake-original-sha", fileName: "test.md", directoryName: "pages", }) }) - }) - - describe("renameSinglePath", () => { - it("should rename using the local Git file system if the repo is ggs enabled", async () => { - const expected: GitCommitResult = { newSha: "fake-commit-sha" } - MockGitFileCommitService.renameSinglePath.mockResolvedValueOnce(expected) - gbSpy.mockReturnValueOnce(true) - - const actual = await RepoService.renameSinglePath( - mockUserWithSiteSessionDataAndGrowthBook, - mockGithubSessionData, - "fake-old-path", - "fake-new-path", - "fake-commit-message" - ) - - expect(actual).toEqual(expected) - }) - it("should rename file using GitHub directly if the repo is not ggs enabled", async () => { - const expectedSha = "fake-commit-sha" - const fakeCommitMessage = "fake-commit-message" - const sessionData: UserWithSiteSessionData = new UserWithSiteSessionData({ - githubId: mockGithubId, - accessToken: mockAccessToken, - isomerUserId: mockIsomerUserId, - email: mockEmail, - siteName: "not-whitelisted", + describe("renameSinglePath", () => { + it("should rename using the local Git file system if the repo is ggs enabled", async () => { + const expected: GitCommitResult = { newSha: "fake-commit-sha" } + MockGitFileCommitService.renameSinglePath.mockResolvedValueOnce( + expected + ) + gbSpy.mockReturnValueOnce(true) + + const actual = await RepoService.renameSinglePath( + mockUserWithSiteSessionDataAndGrowthBook, + mockGithubSessionData, + "fake-old-path", + "fake-new-path", + "fake-commit-message" + ) + + expect(actual).toEqual(expected) }) - MockGitHubCommitService.renameSinglePath.mockResolvedValueOnce({ - newSha: expectedSha, + it("should rename file using GitHub directly if the repo is not ggs enabled", async () => { + const expectedSha = "fake-commit-sha" + const fakeCommitMessage = "fake-commit-message" + const sessionData: UserWithSiteSessionData = new UserWithSiteSessionData( + { + githubId: mockGithubId, + accessToken: mockAccessToken, + isomerUserId: mockIsomerUserId, + email: mockEmail, + siteName: "not-whitelisted", + } + ) + + MockGitHubService.renameSinglePath.mockResolvedValueOnce({ + newSha: expectedSha, + }) + + const actual = await RepoService.renameSinglePath( + sessionData, + mockGithubSessionData, + "fake-path/old-fake-file.md", + "fake-path/new-fake-file.md", + fakeCommitMessage + ) + + expect(actual).toEqual({ newSha: expectedSha }) }) - - const actual = await RepoService.renameSinglePath( - sessionData, - mockGithubSessionData, - "fake-path/old-fake-file.md", - "fake-path/new-fake-file.md", - fakeCommitMessage - ) - - expect(actual).toEqual({ newSha: expectedSha }) - }) - }) - - describe("moveFiles", () => { - it("should move files using the Git local file system if the repo is ggs enabled", async () => { - const expected = { newSha: "fake-commit-sha" } - MockGitFileCommitService.moveFiles.mockResolvedValueOnce(expected) - gbSpy.mockReturnValueOnce(true) - // MockCommitServiceGitFile.push.mockReturnValueOnce(undefined) - - const actual = await RepoService.moveFiles( - mockUserWithSiteSessionDataAndGrowthBook, - mockGithubSessionData, - "fake-old-path", - "fake-new-path", - ["fake-file1", "fake-file2"], - "fake-commit-message" - ) - - expect(actual).toEqual(expected) }) - it("should move files using GitHub directly if the repo is not ggs enabled", async () => { - const expected = { newSha: "fake-commit-sha" } - const fakeCommitMessage = "fake-commit-message" - const sessionData: UserWithSiteSessionData = new UserWithSiteSessionData({ - githubId: mockGithubId, - accessToken: mockAccessToken, - isomerUserId: mockIsomerUserId, - email: mockEmail, - siteName: "not-whitelisted", + describe("moveFiles", () => { + it("should move files using the Git local file system if the repo is ggs enabled", async () => { + const expected = { newSha: "fake-commit-sha" } + MockGitFileCommitService.moveFiles.mockResolvedValueOnce(expected) + gbSpy.mockReturnValueOnce(true) + // MockCommitServiceGitFile.push.mockReturnValueOnce(undefined) + + const actual = await RepoService.moveFiles( + mockUserWithSiteSessionDataAndGrowthBook, + mockGithubSessionData, + "fake-old-path", + "fake-new-path", + ["fake-file1", "fake-file2"], + "fake-commit-message" + ) + + expect(actual).toEqual(expected) }) - MockGitHubCommitService.moveFiles.mockResolvedValueOnce(expected) - - const actual = await RepoService.moveFiles( - sessionData, - mockGithubSessionData, - "fake-path", - "fake-new-path", - ["old-fake-file.md", "old-fake-file-two.md"], - fakeCommitMessage - ) - - expect(actual).toEqual(expected) + it("should move files using GitHub directly if the repo is not ggs enabled", async () => { + const expected = { newSha: "fake-commit-sha" } + const fakeCommitMessage = "fake-commit-message" + const sessionData: UserWithSiteSessionData = new UserWithSiteSessionData( + { + githubId: mockGithubId, + accessToken: mockAccessToken, + isomerUserId: mockIsomerUserId, + email: mockEmail, + siteName: "not-whitelisted", + } + ) + + MockGitHubService.moveFiles.mockResolvedValueOnce(expected) + + const actual = await RepoService.moveFiles( + sessionData, + mockGithubSessionData, + "fake-path", + "fake-new-path", + ["old-fake-file.md", "old-fake-file-two.md"], + fakeCommitMessage + ) + + expect(actual).toEqual(expected) + }) }) - }) - describe("getLatestCommitOfBranch", () => { - it("should read the latest commit data from the local Git file system if the repo is ggs enabled", async () => { - const expected: GitHubCommitData = { - author: { - name: "test author", - email: "test@email.com", - date: "2023-07-20T11:25:05+08:00", - }, - sha: "test-sha", - message: "test message", - } - gbSpy.mockReturnValueOnce(true) - MockGitFileSystemService.getLatestCommitOfBranch.mockResolvedValueOnce( - okAsync(expected) - ) - - const actual = await RepoService.getLatestCommitOfBranch( - mockUserWithSiteSessionDataAndGrowthBook, - "master" - ) - expect(actual).toEqual(expected) - }) + describe("getLatestCommitOfBranch", () => { + it("should read the latest commit data from the local Git file system if the repo is ggs enabled", async () => { + const expected: GitHubCommitData = { + author: { + name: "test author", + email: "test@email.com", + date: "2023-07-20T11:25:05+08:00", + }, + sha: "test-sha", + message: "test message", + } + gbSpy.mockReturnValueOnce(true) + MockGitFileSystemService.getLatestCommitOfBranch.mockResolvedValueOnce( + okAsync(expected) + ) + + const actual = await RepoService.getLatestCommitOfBranch( + mockUserWithSiteSessionDataAndGrowthBook, + "master" + ) + expect(actual).toEqual(expected) + }) - it("should read latest commit data from GitHub if the repo is not ggs enabled", async () => { - const sessionData: UserWithSiteSessionData = new UserWithSiteSessionData({ - githubId: mockGithubId, - accessToken: mockAccessToken, - isomerUserId: mockIsomerUserId, - email: mockEmail, - siteName: "not-whitelisted", + it("should read latest commit data from GitHub if the repo is not ggs enabled", async () => { + const sessionData: UserWithSiteSessionData = new UserWithSiteSessionData( + { + githubId: mockGithubId, + accessToken: mockAccessToken, + isomerUserId: mockIsomerUserId, + email: mockEmail, + siteName: "not-whitelisted", + } + ) + const expected: GitHubCommitData = { + author: { + name: "test author", + email: "test@email.com", + date: "2023-07-20T11:25:05+08:00", + }, + message: "test message", + } + const gitHubServiceReadDirectory = jest.spyOn( + GitHubService.prototype, + "getLatestCommitOfBranch" + ) + gitHubServiceReadDirectory.mockResolvedValueOnce(expected) + const actual = await RepoService.getLatestCommitOfBranch( + sessionData, + "master" + ) + expect(actual).toEqual(expected) }) - const expected: GitHubCommitData = { - author: { - name: "test author", - email: "test@email.com", - date: "2023-07-20T11:25:05+08:00", - }, - message: "test message", - } - const gitHubServiceReadDirectory = jest.spyOn( - GitHubService.prototype, - "getLatestCommitOfBranch" - ) - gitHubServiceReadDirectory.mockResolvedValueOnce(expected) - const actual = await RepoService.getLatestCommitOfBranch( - sessionData, - "master" - ) - expect(actual).toEqual(expected) }) - }) - describe("updateRepoState", () => { - it("should update the repo state on the local Git file system if the repo is ggs enabled", async () => { - MockGitFileSystemService.updateRepoState.mockResolvedValueOnce( - okAsync(undefined) - ) - gbSpy.mockReturnValueOnce(true) + describe("updateRepoState", () => { + it("should update the repo state on the local Git file system if the repo is ggs enabled", async () => { + MockGitFileSystemService.updateRepoState.mockResolvedValueOnce( + okAsync(undefined) + ) + gbSpy.mockReturnValueOnce(true) + + await RepoService.updateRepoState( + mockUserWithSiteSessionDataAndGrowthBook, + { + commitSha: "fake-sha", + branchName: "master", + } + ) + + expect(MockGitFileSystemService.updateRepoState).toBeCalledTimes(1) + }) - await RepoService.updateRepoState( - mockUserWithSiteSessionDataAndGrowthBook, - { + it("should update the repo state on GitHub if the repo is not ggs enabled", async () => { + const sessionData: UserWithSiteSessionData = new UserWithSiteSessionData( + { + githubId: mockGithubId, + accessToken: mockAccessToken, + isomerUserId: mockIsomerUserId, + email: mockEmail, + siteName: "not-whitelisted", + } + ) + const gitHubServiceUpdateRepoState = jest.spyOn( + GitHubService.prototype, + "updateRepoState" + ) + gitHubServiceUpdateRepoState.mockResolvedValueOnce(undefined) + + await RepoService.updateRepoState(sessionData, { commitSha: "fake-sha", branchName: "master", - } - ) - - expect(MockGitFileSystemService.updateRepoState).toBeCalledTimes(1) - }) + }) - it("should update the repo state on GitHub if the repo is not ggs enabled", async () => { - const sessionData: UserWithSiteSessionData = new UserWithSiteSessionData({ - githubId: mockGithubId, - accessToken: mockAccessToken, - isomerUserId: mockIsomerUserId, - email: mockEmail, - siteName: "not-whitelisted", + expect(gitHubServiceUpdateRepoState).toBeCalledTimes(1) }) - const gitHubServiceUpdateRepoState = jest.spyOn( - GitHubService.prototype, - "updateRepoState" - ) - gitHubServiceUpdateRepoState.mockResolvedValueOnce(undefined) - - await RepoService.updateRepoState(sessionData, { - commitSha: "fake-sha", - branchName: "master", - }) - - expect(gitHubServiceUpdateRepoState).toBeCalledTimes(1) }) }) }) From a875e6f8a820fd996fb379583a428dfa20e7022f Mon Sep 17 00:00:00 2001 From: Kishore <42832651+kishore03109@users.noreply.github.com> Date: Mon, 4 Dec 2023 05:10:53 +0800 Subject: [PATCH 2/2] Untitled commit --- src/services/db/GitHubService.ts | 6 +++-- .../db/__tests__/GitHubService.spec.ts | 21 +++------------ src/services/db/__tests__/RepoService.spec.ts | 26 ++++++++++++++----- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/services/db/GitHubService.ts b/src/services/db/GitHubService.ts index 5c312e398..c5710b98a 100644 --- a/src/services/db/GitHubService.ts +++ b/src/services/db/GitHubService.ts @@ -755,11 +755,13 @@ export default class GitHubService { async updateRepoState( sessionData: UserWithSiteSessionData, - { commitSha }: { commitSha: string } + { commitSha, branchName }: { commitSha: any; branchName?: string } ) { const { accessToken } = sessionData const { siteName } = sessionData - const refEndpoint = `${siteName}/git/refs/heads/${STAGING_BRANCH}` + const refEndpoint = `${siteName}/git/refs/heads/${ + branchName || STAGING_BRANCH + }` const headers = { Authorization: `token ${accessToken}`, } diff --git a/src/services/db/__tests__/GitHubService.spec.ts b/src/services/db/__tests__/GitHubService.spec.ts index be83fe6da..03a1ced24 100644 --- a/src/services/db/__tests__/GitHubService.spec.ts +++ b/src/services/db/__tests__/GitHubService.spec.ts @@ -173,7 +173,6 @@ describe("Github Service", () => { fileName, directoryName, isMedia: false, - branchName: BRANCH_REF, }) ).resolves.toMatchObject({ sha, @@ -200,7 +199,6 @@ describe("Github Service", () => { fileName: topLevelDirectoryFileName, directoryName, isMedia: false, - branchName: BRANCH_REF, }) ).resolves.toMatchObject({ sha, @@ -228,7 +226,6 @@ describe("Github Service", () => { fileName: resourceCategoryFileName, directoryName, isMedia: false, - branchName: BRANCH_REF, }) ).resolves.toMatchObject({ sha, @@ -257,7 +254,6 @@ describe("Github Service", () => { fileName, directoryName, isMedia: true, - branchName: BRANCH_REF, }) ).resolves.toMatchObject({ sha, @@ -289,7 +285,6 @@ describe("Github Service", () => { fileName, directoryName, isMedia: false, - branchName: BRANCH_REF, }) ).rejects.toThrowError(ConflictError) expect(mockAxiosInstance.put).toHaveBeenCalledWith( @@ -309,7 +304,6 @@ describe("Github Service", () => { fileName, directoryName, isMedia: false, - branchName: BRANCH_REF, }) ).rejects.toThrowError(NotFoundError) expect(mockAxiosInstance.get).toHaveBeenCalledWith(folderParentEndpoint, { @@ -332,7 +326,6 @@ describe("Github Service", () => { fileName: subDirectoryFileName, directoryName: subDirectoryName, isMedia: false, - branchName: BRANCH_REF, }) ).rejects.toThrowError() expect(mockAxiosInstance.get).toHaveBeenCalledWith(fileParentEndpoint, { @@ -354,7 +347,6 @@ describe("Github Service", () => { fileName, directoryName: `${resourceCategoryName}/_posts`, isMedia: false, - branchName: BRANCH_REF, }) ).rejects.toThrowError(NotFoundError) expect(mockAxiosInstance.get).toHaveBeenCalledWith(resourceRoomEndpoint, { @@ -892,15 +884,10 @@ describe("Github Service", () => { .mockResolvedValueOnce(firstResp) .mockResolvedValueOnce(secondResp) await expect( - service.updateTree( - sessionData, - mockGithubSessionData, - { - gitTree, - message, - }, - true - ) + service.updateTree(sessionData, mockGithubSessionData, { + gitTree, + message, + }) ).resolves.toEqual(secondSha) expect(mockAxiosInstance.post).toHaveBeenCalledWith( url, diff --git a/src/services/db/__tests__/RepoService.spec.ts b/src/services/db/__tests__/RepoService.spec.ts index 26675dd7b..19f8b9fb7 100644 --- a/src/services/db/__tests__/RepoService.spec.ts +++ b/src/services/db/__tests__/RepoService.spec.ts @@ -161,7 +161,7 @@ describe("RepoService", () => { ) }) - it("should create files on GitHub directly if the repo is not ggs enabled", async () => { + it("should create files on GitHub directly if the repo is not whitelisted", async () => { const mockContent = "content" const mockFileName = "test.md" const mockDirectoryName = "" @@ -176,16 +176,18 @@ describe("RepoService", () => { const expected = { sha: "test-sha", } + const gitHubServiceCreate = jest.spyOn(GitHubService.prototype, "create") + gitHubServiceCreate.mockResolvedValueOnce(expected) const actual = await RepoService.create(sessionData, { - content: mockContent, - fileName: mockFileName, - directoryName: mockDirectoryName, + content: "content", + fileName: "test.md", + directoryName: "", isMedia, }) expect(actual).toEqual(expected) - expect(MockGitHubService.create).toHaveBeenCalledWith(sessionData, { + expect(gitHubServiceCreate).toHaveBeenCalledWith(sessionData, { content: mockContent, fileName: mockFileName, directoryName: mockDirectoryName, @@ -564,6 +566,8 @@ describe("RepoService", () => { email: mockEmail, siteName: "not-whitelisted", }) + const gitHubServiceUpdate = jest.spyOn(GitHubService.prototype, "update") + gitHubServiceUpdate.mockResolvedValueOnce({ newSha: expectedSha }) const actual = await RepoService.update(sessionData, { fileContent: "test content", @@ -658,7 +662,11 @@ describe("RepoService", () => { } ) - MockGitHubService.renameSinglePath.mockResolvedValueOnce({ + const gitHubServiceRenameSinglePath = jest.spyOn( + GitHubService.prototype, + "renameSinglePath" + ) + gitHubServiceRenameSinglePath.mockResolvedValueOnce({ newSha: expectedSha, }) @@ -706,7 +714,11 @@ describe("RepoService", () => { } ) - MockGitHubService.moveFiles.mockResolvedValueOnce(expected) + const gitHubServiceMoveFiles = jest.spyOn( + GitHubService.prototype, + "moveFiles" + ) + gitHubServiceMoveFiles.mockResolvedValueOnce(expected) const actual = await RepoService.moveFiles( sessionData,