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

refactor(ff): make site launch flag a bool flag #958

Merged
merged 6 commits into from
Oct 24, 2023
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
2 changes: 1 addition & 1 deletion src/constants/featureFlags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const FEATURE_FLAGS = {
seaerchin marked this conversation as resolved.
Show resolved Hide resolved
GGS_WHITELISTED_REPOS: "ggs_whitelisted_repos",
IS_BUILD_TIMES_REDUCTION_ENABLED: "is_build_times_reduction_enabled",
IS_GGS_ENABLED: "is_ggs_enabled",
} as const
43 changes: 8 additions & 35 deletions src/middleware/routeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ const gitFileSystemService = new GitFileSystemService(
new SimpleGit({ maxConcurrentProcesses: MAX_CONCURRENT_GIT_PROCESSES })
)

const isRepoWhitelisted = (siteName, ggsWhitelistedRepos) => {
// TODO: adding log to simplify debugging, to be removed after stabilising
logger.info(
`Checking if ${siteName} is GGS whitelisted: ${ggsWhitelistedRepos.includes(
siteName
)}`
)
ggsWhitelistedRepos.includes(siteName)
}

const handleGitFileLock = async (repoName, next) => {
const result = await gitFileSystemService.hasGitFileLock(repoName)
if (result.isErr()) {
Expand Down Expand Up @@ -74,23 +64,15 @@ const attachWriteRouteHandlerWrapper = (routeHandler) => async (
const { siteName } = req.params
const { growthbook } = req

let ggsWhitelistedRepos = { repos: [] }
if (growthbook) {
ggsWhitelistedRepos = growthbook.getFeatureValue(
FEATURE_FLAGS.GGS_WHITELISTED_REPOS,
{
repos: [],
}
)
}

let isGitAvailable = true
// only check git file lock if the repo is whitelisted
if (isRepoWhitelisted(siteName, ggsWhitelistedRepos.repos)) {

// only check git file lock if the repo is ggs enabled
if (growthbook?.getFeatureValue(FEATURE_FLAGS.IS_GGS_ENABLED, false)) {
isGitAvailable = await handleGitFileLock(siteName, next)
}

if (!isGitAvailable) return

try {
await lock(siteName)
} catch (err) {
Expand All @@ -102,6 +84,7 @@ const attachWriteRouteHandlerWrapper = (routeHandler) => async (
await unlock(siteName)
next(err)
})

try {
await unlock(siteName)
} catch (err) {
Expand All @@ -120,19 +103,9 @@ const attachRollbackRouteHandlerWrapper = (routeHandler) => async (
const { accessToken } = userSessionData
const { growthbook } = req

let ggsWhitelistedRepos = { repos: [] }
if (growthbook) {
ggsWhitelistedRepos = growthbook.getFeatureValue(
FEATURE_FLAGS.GGS_WHITELISTED_REPOS,
{
repos: [],
}
)
}

const shouldUseGitFileSystem = isRepoWhitelisted(
siteName,
ggsWhitelistedRepos.repos
const shouldUseGitFileSystem = !!growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)

const isGitAvailable = await handleGitFileLock(siteName, next)
Expand Down
98 changes: 36 additions & 62 deletions src/services/db/RepoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,6 @@ export default class RepoService extends GitHubService {
this.githubCommitService = gitHubCommitService
}

getGgsWhitelistedRepos(
growthbook: GrowthBook<FeatureFlags> | undefined
): string[] {
if (!growthbook) return []

const whitelistedGgsRepos = growthbook.getFeatureValue(
FEATURE_FLAGS.GGS_WHITELISTED_REPOS,
{ repos: [] }
)
return whitelistedGgsRepos.repos
}

isRepoGgsWhitelisted(
repoName: string,
ggsWhitelistedRepos: string[]
): boolean {
// TODO: Adding for initial debugging if required. Remove once stabilised
logger.info(
`Evaluating if ${repoName} is GGS whitelisted: ${ggsWhitelistedRepos.includes(
repoName
)}`
)

return ggsWhitelistedRepos.includes(repoName)
}

getCommitDiff(siteName: string, base?: string, head?: string) {
return ReviewApi.getCommitDiff(siteName, base, head)
}
Expand Down Expand Up @@ -202,9 +176,9 @@ export default class RepoService extends GitHubService {
}
): Promise<{ sha: string }> {
if (
this.isRepoGgsWhitelisted(
sessionData.siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
logger.info(
Expand All @@ -231,9 +205,9 @@ export default class RepoService extends GitHubService {
{ fileName, directoryName }: { fileName: string; directoryName?: string }
): Promise<GitFile> {
if (
this.isRepoGgsWhitelisted(
sessionData.siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
logger.info("Reading file from local Git file system")
Expand Down Expand Up @@ -266,9 +240,9 @@ export default class RepoService extends GitHubService {

// fetch from local disk
if (
this.isRepoGgsWhitelisted(
siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
logger.info(
Expand Down Expand Up @@ -312,9 +286,9 @@ export default class RepoService extends GitHubService {
): Promise<GitDirectoryItem[]> {
const defaultBranch = STAGING_BRANCH
if (
this.isRepoGgsWhitelisted(
sessionData.siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
logger.info("Reading directory from local Git file system")
Expand Down Expand Up @@ -355,9 +329,9 @@ export default class RepoService extends GitHubService {
let dirContent: GitDirectoryItem[] = []

if (
this.isRepoGgsWhitelisted(
siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
const result = await this.gitFileSystemService.listDirectoryContents(
Expand Down Expand Up @@ -408,9 +382,9 @@ export default class RepoService extends GitHubService {
}
): Promise<GitCommitResult> {
if (
this.isRepoGgsWhitelisted(
sessionData.siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
return this.gitFileCommitService.update(sessionData, {
Expand Down Expand Up @@ -442,9 +416,9 @@ export default class RepoService extends GitHubService {
}
): Promise<void> {
if (
this.isRepoGgsWhitelisted(
sessionData.siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
await this.gitFileCommitService.deleteDirectory(sessionData, {
Expand Down Expand Up @@ -474,9 +448,9 @@ export default class RepoService extends GitHubService {
}
): Promise<void> {
if (
this.isRepoGgsWhitelisted(
sessionData.siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
await this.gitFileCommitService.delete(sessionData, {
Expand All @@ -503,9 +477,9 @@ export default class RepoService extends GitHubService {
message?: string
): Promise<GitCommitResult> {
if (
this.isRepoGgsWhitelisted(
sessionData.siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
return this.gitFileCommitService.renameSinglePath(
Expand Down Expand Up @@ -534,9 +508,9 @@ export default class RepoService extends GitHubService {
message?: string
): Promise<GitCommitResult> {
if (
this.isRepoGgsWhitelisted(
sessionData.siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
return this.gitFileCommitService.moveFiles(
Expand Down Expand Up @@ -573,9 +547,9 @@ export default class RepoService extends GitHubService {
): Promise<GitHubCommitData> {
const { siteName } = sessionData
if (
this.isRepoGgsWhitelisted(
siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
logger.info(
Expand Down Expand Up @@ -629,9 +603,9 @@ export default class RepoService extends GitHubService {
): Promise<void> {
const { siteName } = sessionData
if (
this.isRepoGgsWhitelisted(
siteName,
this.getGgsWhitelistedRepos(sessionData.growthbook)
sessionData.growthbook?.getFeatureValue(
FEATURE_FLAGS.IS_GGS_ENABLED,
false
)
) {
logger.info(
Expand Down
Loading
Loading