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

fix: reposervice fix cwd #1004

Merged
merged 2 commits into from
Nov 1, 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
64 changes: 45 additions & 19 deletions src/services/db/GitFileSystemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default class GitFileSystemService {
? `${EFS_VOL_PATH_STAGING}/${repoName}`
: `${EFS_VOL_PATH_STAGING_LITE}/${repoName}`
return ResultAsync.fromPromise(
this.git.cwd(`${repoPath}`).checkIsRepo(),
this.git.cwd({ path: `${repoPath}`, root: false }).checkIsRepo(),
(error) => {
logger.error(
`Error when checking if ${repoName} is a Git repo: ${error}`
Expand All @@ -137,7 +137,9 @@ export default class GitFileSystemService {
? `${EFS_VOL_PATH_STAGING}/${repoName}`
: `${EFS_VOL_PATH_STAGING_LITE}/${repoName}`
return ResultAsync.fromPromise(
this.git.cwd(repoPath).remote(["get-url", "origin"]),
this.git
.cwd({ path: repoPath, root: false })
.remote(["get-url", "origin"]),
(error) => {
logger.error(`Error when checking origin remote URL: ${error}`)

Expand Down Expand Up @@ -204,7 +206,7 @@ export default class GitFileSystemService {
const efsVolPath = this.getEfsVolPathFromBranch(branchName)
return ResultAsync.fromPromise(
this.git
.cwd(`${efsVolPath}/${repoName}`)
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.revparse(["--abbrev-ref", "HEAD"]),
(error) => {
logger.error(`Error when getting current branch: ${error}`)
Expand All @@ -218,7 +220,9 @@ export default class GitFileSystemService {
).andThen((currentBranch) => {
if (currentBranch !== branchName) {
return ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).checkout(branchName),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.checkout(branchName),
(error) => {
logger.error(`Error when checking out ${branchName}: ${error}`)

Expand All @@ -245,7 +249,9 @@ export default class GitFileSystemService {
? EFS_VOL_PATH_STAGING
: EFS_VOL_PATH_STAGING_LITE
return ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).revparse([`HEAD:${filePath}`]),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.revparse([`HEAD:${filePath}`]),
(error) => {
logger.error(
`Error when getting Git blob hash: ${error} when trying to access ${efsVolPath}/${repoName}`
Expand Down Expand Up @@ -302,7 +308,9 @@ export default class GitFileSystemService {
): ResultAsync<LogResult<DefaultLogFields>, GitFileSystemError> {
const efsVolPath = this.getEfsVolPathFromBranch(branchName)
return ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).log([branchName]),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.log([branchName]),
(error) => {
logger.error(
`Error when getting latest commit of "${branchName}" branch: ${error}, when trying to access ${efsVolPath}/${repoName} for ${branchName}`
Expand Down Expand Up @@ -331,7 +339,7 @@ export default class GitFileSystemService {
const efsVolPath = this.getEfsVolPathFromBranch(branchName)
return ResultAsync.fromPromise(
this.git
.cwd(`${efsVolPath}/${repoName}`)
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.reset(["--hard", commitSha])
.clean(CleanOptions.FORCE + CleanOptions.RECURSIVE),
(error) => {
Expand Down Expand Up @@ -378,15 +386,15 @@ export default class GitFileSystemService {
const clonePromise = isStaging
? this.git
.clone(originUrl, `${efsVolPath}/${repoName}`)
.cwd(`${efsVolPath}/${repoName}`)
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.checkout(branch)
: this.git
.clone(originUrl, `${efsVolPath}/${repoName}`, [
"--branch",
branch,
"--single-branch",
])
.cwd(`${efsVolPath}/${repoName}`)
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })

return ResultAsync.fromPromise(clonePromise, (error) => {
logger.error(`Error when cloning ${repoName}: ${error}`)
Expand Down Expand Up @@ -448,7 +456,9 @@ export default class GitFileSystemService {

return this.ensureCorrectBranch(repoName, branchName).andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).pull(),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.pull(),
(error) => {
// Full error message 1: Your configuration specifies to merge
// with the ref 'refs/heads/staging' from the remote, but no
Expand Down Expand Up @@ -514,9 +524,11 @@ export default class GitFileSystemService {
ResultAsync.fromPromise(
isForce
? this.git
.cwd(`${efsVolPath}/${repoName}`)
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.push([...gitOptions, "--force"])
: this.git.cwd(`${efsVolPath}/${repoName}`).push(gitOptions),
: this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.push(gitOptions),
(error) => {
logger.error(`Error when pushing ${repoName}: ${error}`)

Expand All @@ -534,8 +546,12 @@ export default class GitFileSystemService {
// Retry push once
ResultAsync.fromPromise(
isForce
? this.git.cwd(`${efsVolPath}/${repoName}`).push(["--force"])
: this.git.cwd(`${efsVolPath}/${repoName}`).push(),
? this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.push(["--force"])
: this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.push(),
(error) => {
logger.error(`Error when pushing ${repoName}: ${error}`)

Expand Down Expand Up @@ -600,7 +616,9 @@ export default class GitFileSystemService {
}

return ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).add(pathSpec),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.add(pathSpec),
(error) => {
logger.error(
`Error when Git adding files to ${repoName}: ${error}`
Expand All @@ -620,7 +638,9 @@ export default class GitFileSystemService {
})
.andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).commit(commitMessage),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.commit(commitMessage),
(error) => {
logger.error(`Error when committing ${repoName}: ${error}`)

Expand Down Expand Up @@ -1163,7 +1183,9 @@ export default class GitFileSystemService {
)
.andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).mv(oldPath, newPath),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.mv(oldPath, newPath),
(error) => {
logger.error(`Error when moving ${oldPath} to ${newPath}: ${error}`)

Expand Down Expand Up @@ -1366,7 +1388,9 @@ export default class GitFileSystemService {
return this.ensureCorrectBranch(repoName, branchName)
.andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).catFile(["-t", sha]),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.catFile(["-t", sha]),
(error) => {
// An error is thrown if the SHA does not exist in the branch
if (error instanceof GitError) {
Expand All @@ -1379,7 +1403,9 @@ export default class GitFileSystemService {
)
.andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).reset(["--hard", sha]),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.reset(["--hard", sha]),
(error) => {
logger.error(`Error when updating repo state: ${error}`)

Expand Down
35 changes: 22 additions & 13 deletions src/services/identity/ReposService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,31 @@ export default class ReposService {

// 2. Commit changes in local repo
await this.simpleGit
.cwd(dir)
.cwd({ path: dir, root: false })
.checkout("staging") // ensure on staging branch
.add(".")
.addConfig("user.name", ISOMER_GITHUB_ORGANIZATION_NAME)
.addConfig("user.email", ISOMER_GITHUB_EMAIL)
.commit("Set URLs")

// 3. Push changes to staging branch
await this.simpleGit.cwd(dir).push("origin", "staging")
await this.simpleGit
.cwd({ path: dir, root: false })
.push("origin", "staging")

// 4. Merge these changes into master branch
await this.simpleGit.cwd(dir).checkout("master").merge(["staging"])
await this.simpleGit
.cwd({ path: dir, root: false })
.checkout("master")
.merge(["staging"])

// 5. Push changes to master branch
await this.simpleGit.cwd(dir).push("origin", "master")
await this.simpleGit
.cwd({ path: dir, root: false })
.push("origin", "master")

// 6. Checkout back to staging branch
await this.simpleGit.cwd(dir).checkout("staging")
await this.simpleGit.cwd({ path: dir, root: false }).checkout("staging")
}

private setUrlsInLocalConfig(
Expand Down Expand Up @@ -225,15 +232,15 @@ export default class ReposService {
// Clone base repo locally
fs.mkdirSync(stgDir)
await this.simpleGit
.cwd(stgDir)
.cwd({ path: stgDir, root: false })
.clone(SITE_CREATION_BASE_REPO_URL, stgDir, ["-b", "staging"])

// Clear git
fs.rmSync(`${stgDir}/.git`, { recursive: true, force: true })

// Prepare git repo
await this.simpleGit
.cwd(stgDir)
.cwd({ path: stgDir, root: false })
.init(["--initial-branch=staging"])
.checkoutLocalBranch("staging")

Expand All @@ -242,14 +249,14 @@ export default class ReposService {

// Commit
await this.simpleGit
.cwd(stgDir)
.cwd({ path: stgDir, root: false })
.addConfig("user.name", "isomeradmin")
.addConfig("user.email", ISOMER_GITHUB_EMAIL)
.commit("Initial commit")

// Push to origin
await this.simpleGit
.cwd(stgDir)
.cwd({ path: stgDir, root: false })
.addRemote("origin", repoUrl)
.checkout("staging")
.push(["-u", "origin", "staging"]) // push to staging first to make it the default branch on GitHub
Expand Down Expand Up @@ -376,10 +383,12 @@ export const createRecords = (zoneId: string): Record[] => {
// note: for some reason, combining below commands led to race conditions
// so we have to do it separately
// Create staging lite branch in other repo path
await this.simpleGit.cwd(stgLiteDir).clone(repoUrl, stgLiteDir)
await this.simpleGit.cwd(stgLiteDir).pull() // some repos are large, clone takes time
await this.simpleGit
.cwd(stgLiteDir)
.cwd({ path: stgLiteDir, root: false })
.clone(repoUrl, stgLiteDir)
await this.simpleGit.cwd({ path: stgLiteDir, root: false }).pull() // some repos are large, clone takes time
await this.simpleGit
.cwd({ path: stgLiteDir, root: false })
.checkout("staging")
.rm(["-r", "images"])
.rm(["-r", "files"])
Expand All @@ -389,7 +398,7 @@ export const createRecords = (zoneId: string): Record[] => {

// Prepare git repo
await this.simpleGit
.cwd(stgLiteDir)
.cwd({ path: stgLiteDir, root: false })
.init()
.checkoutLocalBranch("staging-lite")
.add(".")
Expand Down
Loading