Skip to content

Commit

Permalink
feat: add ability to update repo state for GGS (#949)
Browse files Browse the repository at this point in the history
* chore: update admin repo list and add e2e repos

* chore: reorder unit tests to match order on main file

* feat: add ability to update repo state for GGS

* feat: add unit tests for new functionality
  • Loading branch information
dcshzj authored Sep 28, 2023
1 parent 824d669 commit 75d6982
Show file tree
Hide file tree
Showing 7 changed files with 923 additions and 518 deletions.
6 changes: 6 additions & 0 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const ISOMER_ADMIN_REPOS = [
"isomercms-backend",
"isomercms-frontend",
"isomer-redirection",
"isomer-indirection",
"isomerpages-template",
"isomer-conversion-scripts",
"isomer-wysiwyg",
Expand All @@ -53,6 +54,11 @@ export const ISOMER_ADMIN_REPOS = [
"infra",
"markdown-helper",
]
export const ISOMER_E2E_TEST_REPOS = [
"e2e-test-repo",
"e2e-email-test-repo",
"e2e-notggs-test-repo",
]

export const INACTIVE_USER_THRESHOLD_DAYS = 60
export const GITHUB_ORG_REPOS_ENDPOINT = `https://api.github.com/orgs/${ISOMER_GITHUB_ORG_NAME}/repos`
Expand Down
68 changes: 60 additions & 8 deletions src/services/db/GitFileSystemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { config } from "@config/config"

import logger from "@logger/logger"

import { BadRequestError } from "@errors/BadRequestError"
import { ConflictError } from "@errors/ConflictError"
import GitFileSystemError from "@errors/GitFileSystemError"
import GitFileSystemNeedsRollbackError from "@errors/GitFileSystemNeedsRollbackError"
Expand Down Expand Up @@ -155,8 +156,11 @@ export default class GitFileSystemService {
})
}

// Ensure that the repository is in the BRANCH_REF branch
ensureCorrectBranch(repoName: string): ResultAsync<true, GitFileSystemError> {
// Ensure that the repository is in the specified branch
ensureCorrectBranch(
repoName: string,
branchName: string
): ResultAsync<true, GitFileSystemError> {
return ResultAsync.fromPromise(
this.git
.cwd(`${EFS_VOL_PATH}/${repoName}`)
Expand All @@ -171,11 +175,11 @@ export default class GitFileSystemService {
return new GitFileSystemError("An unknown error occurred")
}
).andThen((currentBranch) => {
if (currentBranch !== BRANCH_REF) {
if (currentBranch !== branchName) {
return ResultAsync.fromPromise(
this.git.cwd(`${EFS_VOL_PATH}/${repoName}`).checkout(BRANCH_REF),
this.git.cwd(`${EFS_VOL_PATH}/${repoName}`).checkout(branchName),
(error) => {
logger.error(`Error when checking out ${BRANCH_REF}: ${error}`)
logger.error(`Error when checking out ${branchName}: ${error}`)

if (error instanceof GitError) {
return new GitFileSystemError("Unable to checkout branch")
Expand Down Expand Up @@ -346,7 +350,7 @@ export default class GitFileSystemService {
)
}

return this.ensureCorrectBranch(repoName).andThen(() =>
return this.ensureCorrectBranch(repoName, BRANCH_REF).andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${EFS_VOL_PATH}/${repoName}`).pull(),
(error) => {
Expand Down Expand Up @@ -398,6 +402,7 @@ export default class GitFileSystemService {
// Push the latest changes to upstream Git hosting provider
push(
repoName: string,
branchName: string,
isForce = false
): ResultAsync<string, GitFileSystemError> {
return this.isValidGitRepo(repoName).andThen((isValid) => {
Expand All @@ -407,7 +412,7 @@ export default class GitFileSystemService {
)
}

return this.ensureCorrectBranch(repoName)
return this.ensureCorrectBranch(repoName, branchName)
.andThen(() =>
ResultAsync.fromPromise(
isForce
Expand Down Expand Up @@ -486,7 +491,7 @@ export default class GitFileSystemService {

const commitMessage = JSON.stringify(commitMessageObj)

return this.ensureCorrectBranch(repoName)
return this.ensureCorrectBranch(repoName, BRANCH_REF)
.andThen(() => {
if (skipGitAdd) {
// This is necessary when we have performed a git mv
Expand Down Expand Up @@ -1173,4 +1178,51 @@ export default class GitFileSystemService {
)
})
}

updateRepoState(
repoName: string,
branchName: string,
sha: string
): ResultAsync<void, GitFileSystemError> {
return this.isValidGitRepo(repoName).andThen((isValid) => {
if (!isValid) {
return errAsync(
new GitFileSystemError(`Folder "${repoName}" is not a valid Git repo`)
)
}

return this.ensureCorrectBranch(repoName, branchName)
.andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${EFS_VOL_PATH}/${repoName}`).catFile(["-t", sha]),
(error) => {
// An error is thrown if the SHA does not exist in the branch
if (error instanceof GitError) {
return new BadRequestError("The provided SHA is invalid")
}

return new GitFileSystemError("An unknown error occurred")
}
)
)
.andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${EFS_VOL_PATH}/${repoName}`).reset(["--hard", sha]),
(error) => {
logger.error(`Error when updating repo state: ${error}`)

if (error instanceof GitError) {
return new GitFileSystemError(
`Unable to update repo state to commit SHA ${sha}`
)
}

return new GitFileSystemError("An unknown error occurred")
}
)
)
.andThen(() => this.push(repoName, branchName, true))
.map(() => undefined)
})
}
}
4 changes: 2 additions & 2 deletions src/services/db/GitHubService.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,10 @@ class GitHubService {
return newCommitSha
}

async updateRepoState(sessionData, { commitSha }) {
async updateRepoState(sessionData, { commitSha, branchName = BRANCH_REF }) {
const { accessToken } = sessionData
const { siteName } = sessionData
const refEndpoint = `${siteName}/git/refs/heads/${BRANCH_REF}`
const refEndpoint = `${siteName}/git/refs/heads/${branchName}`
const headers = {
Authorization: `token ${accessToken}`,
}
Expand Down
45 changes: 37 additions & 8 deletions src/services/db/RepoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { GitHubService } from "./GitHubService"
import * as ReviewApi from "./review"

const PLACEHOLDER_FILE_NAME = ".keep"
const BRANCH_REF = config.get("github.branchRef")

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

Expand Down Expand Up @@ -172,7 +174,7 @@ export default class RepoService extends GitHubService {
throw result.error
}

this.gitFileSystemService.push(sessionData.siteName)
this.gitFileSystemService.push(sessionData.siteName, BRANCH_REF)
return { sha: result.value.newSha }
}
return await super.create(sessionData, {
Expand Down Expand Up @@ -385,7 +387,7 @@ export default class RepoService extends GitHubService {
throw result.error
}

this.gitFileSystemService.push(sessionData.siteName)
this.gitFileSystemService.push(sessionData.siteName, BRANCH_REF)
return { newSha: result.value }
}

Expand Down Expand Up @@ -430,7 +432,7 @@ export default class RepoService extends GitHubService {
throw result.error
}

this.gitFileSystemService.push(sessionData.siteName)
this.gitFileSystemService.push(sessionData.siteName, BRANCH_REF)
return
}

Expand Down Expand Up @@ -497,7 +499,7 @@ export default class RepoService extends GitHubService {
throw result.error
}

this.gitFileSystemService.push(sessionData.siteName)
this.gitFileSystemService.push(sessionData.siteName, BRANCH_REF)
return
}

Expand Down Expand Up @@ -535,7 +537,7 @@ export default class RepoService extends GitHubService {
throw result.error
}

this.gitFileSystemService.push(sessionData.siteName)
this.gitFileSystemService.push(sessionData.siteName, BRANCH_REF)
return { newSha: result.value }
}

Expand Down Expand Up @@ -626,7 +628,7 @@ export default class RepoService extends GitHubService {
throw result.error
}

this.gitFileSystemService.push(sessionData.siteName)
this.gitFileSystemService.push(sessionData.siteName, BRANCH_REF)
return { newSha: result.value }
}

Expand Down Expand Up @@ -737,8 +739,35 @@ export default class RepoService extends GitHubService {
})
}

async updateRepoState(sessionData: any, { commitSha }: any): Promise<any> {
return await super.updateRepoState(sessionData, { commitSha })
async updateRepoState(
sessionData: UserWithSiteSessionData,
{
commitSha,
branchName = BRANCH_REF,
}: { commitSha: string; branchName?: string }
): Promise<void> {
const { siteName } = sessionData
if (
this.isRepoWhitelisted(
siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
)
) {
logger.info(
`Updating repo state for site ${siteName} to ${commitSha} on local Git file system`
)
const result = await this.gitFileSystemService.updateRepoState(
siteName,
branchName,
commitSha
)
if (result.isErr()) {
throw result.error
}
return
}

return await super.updateRepoState(sessionData, { commitSha, branchName })
}

async checkHasAccess(sessionData: any): Promise<any> {
Expand Down
Loading

0 comments on commit 75d6982

Please sign in to comment.