Skip to content

Commit

Permalink
Fix: change git tree object format (#305)
Browse files Browse the repository at this point in the history
* Fix: change git tree object format

* Nit: add comments
  • Loading branch information
alexanderleegs authored Sep 23, 2021
1 parent d870f6e commit 89b5810
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 80 deletions.
46 changes: 38 additions & 8 deletions classes/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,24 @@ class Collection {

async delete(collectionName, currentCommitSha, treeSha) {
const commitMessage = `Delete collection ${collectionName}`
const gitTree = await getTree(this.siteName, this.accessToken, treeSha)
const newGitTree = gitTree.filter((item) => item.path !== `_${collectionName}`)
const gitTree = await getTree(
this.siteName,
this.accessToken,
treeSha,
true
)
const newGitTree = gitTree
.filter(
(item) =>
item.type !== "tree" && item.path.startsWith(`_${collectionName}/`)
)
.map((item) => ({
...item,
sha: null,
}))
await sendTree(
newGitTree,
treeSha,
currentCommitSha,
this.siteName,
this.accessToken,
Expand Down Expand Up @@ -125,20 +139,36 @@ class Collection {
) {
const commitMessage = `Rename collection from ${oldCollectionName} to ${newCollectionName}`

const gitTree = await getTree(this.siteName, this.accessToken, treeSha)
const gitTree = await getTree(
this.siteName,
this.accessToken,
treeSha,
true
)
const oldCollectionDirectoryName = `_${oldCollectionName}`
const newCollectionDirectoryName = `_${newCollectionName}`
const newGitTree = gitTree.map((item) => {
if (item.path === oldCollectionDirectoryName) {
return {
const newGitTree = []
gitTree.forEach((item) => {
if (item.path === oldCollectionDirectoryName && item.type === "tree") {
// Rename old subdirectory to new name
newGitTree.push({
...item,
path: newCollectionDirectoryName,
}
})
} else if (
item.path.startsWith(`${oldCollectionDirectoryName}/`) &&
item.type !== "tree"
) {
// Delete old subdirectory items
newGitTree.push({
...item,
sha: null,
})
}
return item
})
await sendTree(
newGitTree,
treeSha,
currentCommitSha,
this.siteName,
this.accessToken,
Expand Down
36 changes: 19 additions & 17 deletions classes/MediaSubfolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ class MediaSubfolder {
)
const directoryName = `${this.mediaFolderName}/${subfolderPath}`
const newGitTree = gitTree
.filter((item) => {
return !item.path.includes(directoryName)
})
.filter((item) => {
return !(
item.type === "tree" && item.path.includes(this.mediaFolderName)
)
})

.filter(
(item) =>
item.type !== "tree" && item.path.startsWith(`${directoryName}/`)
)
.map((item) => ({
...item,
sha: null,
}))
await sendTree(
newGitTree,
treeSha,
currentCommitSha,
this.siteName,
this.accessToken,
Expand All @@ -67,24 +67,26 @@ class MediaSubfolder {
const newDirectoryName = `${this.mediaFolderName}/${newSubfolderPath}`
const newGitTree = []
gitTree.forEach((item) => {
if (item.path === oldDirectoryName) {
if (item.path === oldDirectoryName && item.type === "tree") {
// Rename old subdirectory to new name
newGitTree.push({
...item,
path: newDirectoryName,
})
} else if (item.path.includes(oldDirectoryName)) {
// We don't want to include these because they use the old path, they are included with the renamed tree
} else if (
item.type === "tree" &&
item.path.includes(this.mediaFolderName)
item.path.startsWith(`${oldDirectoryName}/`) &&
item.type !== "tree"
) {
// We don't include any other trees - we reconstruct them by adding all their individual files instead
} else {
newGitTree.push(item)
// Delete old subdirectory items
newGitTree.push({
...item,
sha: null,
})
}
})
await sendTree(
newGitTree,
treeSha,
currentCommitSha,
this.siteName,
this.accessToken,
Expand Down
34 changes: 17 additions & 17 deletions classes/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,38 @@ class Resource {
this.siteName,
this.accessToken
)
const gitTree = await getTree(this.siteName, this.accessToken, treeSha)
const newGitTree = []
let resourceRoomTreeSha
// Retrieve all git trees of other items
gitTree.forEach((item) => {
if (item.path === resourceRoomName) {
resourceRoomTreeSha = item.sha
} else {
newGitTree.push(item)
}
})
const resourceRoomTree = await getTree(
const gitTree = await getTree(
this.siteName,
this.accessToken,
resourceRoomTreeSha
treeSha,
true
)
resourceRoomTree.forEach((item) => {
const newGitTree = []
gitTree.forEach((item) => {
// We need to append resource room to the file path because the path is relative to the subtree
if (item.path === resourceName) {
if (
item.path === `${resourceRoomName}/${resourceName}` &&
item.type === "tree"
) {
// Rename old subdirectory to new name
newGitTree.push({
...item,
path: `${resourceRoomName}/${newResourceName}`,
})
} else {
} else if (
item.path.startsWith(`${resourceRoomName}/${resourceName}/`) &&
item.type !== "tree"
) {
// Delete old subdirectory items
newGitTree.push({
...item,
path: `${resourceRoomName}/${item.path}`,
sha: null,
})
}
})
await sendTree(
newGitTree,
treeSha,
currentCommitSha,
this.siteName,
this.accessToken,
Expand Down
34 changes: 25 additions & 9 deletions classes/ResourceRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,34 @@ class ResourceRoom {
this.siteName,
this.accessToken
)
const gitTree = await getTree(this.siteName, this.accessToken, treeSha)
const newGitTree = gitTree.map((item) => {
if (item.path === resourceRoomName) {
return {
const gitTree = await getTree(
this.siteName,
this.accessToken,
treeSha,
true
)
const newGitTree = []
gitTree.forEach((item) => {
if (item.path === resourceRoomName && item.type === "tree") {
// Rename old subdirectory to new name
newGitTree.push({
...item,
path: newResourceRoom,
}
})
} else if (
item.path.startsWith(`${resourceRoomName}/`) &&
item.type !== "tree"
) {
// Delete old subdirectory items
newGitTree.push({
...item,
sha: null,
})
}
return item
})
await sendTree(
newGitTree,
treeSha,
currentCommitSha,
this.siteName,
this.accessToken,
Expand Down Expand Up @@ -197,9 +213,9 @@ class ResourceRoom {
const resources = await IsomerResource.list(resourceRoomName)

if (!_.isEmpty(resources)) {
await Bluebird.map(resources, async (resource) => {
return IsomerResource.delete(resourceRoomName, resource.dirName)
})
await Bluebird.map(resources, async (resource) =>
IsomerResource.delete(resourceRoomName, resource.dirName)
)
}

// Delete index file in resourceRoom
Expand Down
52 changes: 23 additions & 29 deletions routes/folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ const { Collection } = require("@classes/Collection")
const { CollectionConfig } = require("@classes/Config")
const { File, CollectionPageType } = require("@classes/File")

const { getTree, sendTree, deslugifyCollectionName } = require("@utils/utils.js")

const {
getTree,
sendTree,
deslugifyCollectionName,
} = require("@utils/utils.js")

const router = express.Router()

Expand Down Expand Up @@ -45,31 +48,19 @@ async function deleteSubfolder(req, res) {
const commitMessage = `Delete subfolder ${folderName}/${subfolderName}`
const isRecursive = true
const gitTree = await getTree(siteName, accessToken, treeSha, isRecursive)
const baseTreeWithoutFolder = gitTree.filter(
(item) =>
// keep all root-level items except for tree object of folder whose subfolder is to be deleted
!item.path.includes("/") && item.path !== `_${folderName}`
)
const folderTreeWithoutSubfolder = gitTree
.filter((item) =>
// get all folder items
item.path.includes(`_${folderName}`)
)
const newGitTree = gitTree
.filter(
(item) =>
// remove tree objects of folder and subfolder to be renamed
item.path !== `_${folderName}` &&
item.path !== `_${folderName}/${subfolderName}`
item.type !== "tree" &&
item.path.startsWith(`_${folderName}/${subfolderName}/`)
)
.filter(
(item) =>
// exclude all subfolder items
!item.path.includes(`_${folderName}/${subfolderName}`)
)

const newGitTree = [...baseTreeWithoutFolder, ...folderTreeWithoutSubfolder]
.map((item) => ({
...item,
sha: null,
}))
await sendTree(
newGitTree,
treeSha,
currentCommitSha,
siteName,
accessToken,
Expand Down Expand Up @@ -108,14 +99,14 @@ async function renameSubfolder(req, res) {

const filesToBeModified = await CurrentIsomerFile.list()

await Bluebird.mapSeries(filesToBeModified, async(fileInfo) => {
await Bluebird.mapSeries(filesToBeModified, async (fileInfo) => {
const { fileName } = fileInfo

// Read existing file content
const { content, sha } = await CurrentIsomerFile.read(fileName)

// Handle keep file differently
if (fileName === '.keep') {
if (fileName === ".keep") {
await NewIsomerFile.create(fileName, content)
return CurrentIsomerFile.delete(fileName, sha)
}
Expand All @@ -128,21 +119,24 @@ async function renameSubfolder(req, res) {
// Modify `third_nav_title` and save as new file in newSubfolderName
const newFrontMatter = {
...frontMatter,
third_nav_title: deslugifyCollectionName(newSubfolderName)
third_nav_title: deslugifyCollectionName(newSubfolderName),
}

const newContent = ["---\n", yaml.stringify(newFrontMatter), "---\n", mdBody].join("")
const newContent = [
"---\n",
yaml.stringify(newFrontMatter),
"---\n",
mdBody,
].join("")

const encodedNewContent = Base64.encode(newContent)

await NewIsomerFile.create(fileName, encodedNewContent)

// Delete existing file in subfolderName
return CurrentIsomerFile.delete(fileName, sha)

})


// // Update collection config
const collectionConfig = new CollectionConfig(
accessToken,
Expand Down
2 changes: 2 additions & 0 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async function getTree(
// send the new tree object back to Github and point the latest commit on the staging branch to it
async function sendTree(
gitTree,
baseTreeSha,
currentCommitSha,
repo,
accessToken,
Expand All @@ -77,6 +78,7 @@ async function sendTree(
`https://api.github.com/repos/${GITHUB_ORG_NAME}/${repo}/git/trees`,
{
tree: gitTree,
base_tree: baseTreeSha,
},
{
headers,
Expand Down

0 comments on commit 89b5810

Please sign in to comment.