Skip to content

Commit

Permalink
Merge pull request #93 from isomerpages/staging
Browse files Browse the repository at this point in the history
Merge to Prod
  • Loading branch information
kwajiehao authored Dec 22, 2020
2 parents 9e86e8f + 99e7864 commit fe44b25
Show file tree
Hide file tree
Showing 24 changed files with 681 additions and 223 deletions.
11 changes: 11 additions & 0 deletions .ebextensions/nginx-max-body-size.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
files:
"/etc/nginx/conf.d/proxy.conf" :
mode: "000755"
owner: root
group: root
content: |
client_max_body_size 10M;

commands:
00_reload_nginx:
command: "service nginx reload"
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
- name: Install NPM
run: npm ci
- name: Zip application
run: zip -r "deploy.zip" * -x .env-example .gitignore package-lock.json
run: zip -r "deploy.zip" * .ebextensions -x .env-example .gitignore package-lock.json
- name: Get timestamp
shell: bash
run: echo "##[set-output name=timestamp;]$(env TZ=Asia/Singapore date '+%Y%m%d%H%M%S')"
Expand Down
102 changes: 75 additions & 27 deletions classes/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const Bluebird = require('bluebird')
const _ = require('lodash')

const { Config } = require('./Config.js')
const { File, CollectionPageType } = require('./File.js')
const { File, CollectionPageType, DataType } = require('./File.js')
const { getCommitAndTreeSha, getTree, sendTree, deslugifyCollectionName } = require('../utils/utils.js')

const NAV_FILE_NAME = 'navigation.yml'

class Collection {
constructor(accessToken, siteName) {
Expand Down Expand Up @@ -32,14 +35,27 @@ class Collection {

// TO-DO: Verify that collection doesn't already exist

contentObject.collections[`${collectionName}`] = {
permalink: '/:collection/:path/:title',
contentObject.collections[`${collectionName}`] = {
output: true
}
const newContent = base64.encode(yaml.safeDump(contentObject))

await config.update(newContent, sha)

const nav = new File(this.accessToken, this.siteName)
const dataType = new DataType()
nav.setFileType(dataType)
const { content:navContent, sha:navSha } = await nav.read(NAV_FILE_NAME)
const navContentObject = yaml.safeLoad(base64.decode(navContent))

navContentObject.links.push({
title: deslugifyCollectionName(collectionName),
collection: collectionName
})
const newNavContent = base64.encode(yaml.safeDump(navContentObject))

await nav.update(NAV_FILE_NAME, newNavContent, navSha)

} catch (err) {
throw err
}
Expand All @@ -57,6 +73,21 @@ class Collection {

await config.update(newContent, sha)

// Delete collection in nav if it exists
const nav = new File(this.accessToken, this.siteName)
const dataType = new DataType()
nav.setFileType(dataType)
const { content:navContent, sha:navSha } = await nav.read(NAV_FILE_NAME)
const navContentObject = yaml.safeLoad(base64.decode(navContent))

const newNavLinks = navContentObject.links.filter(link => link.collection !== collectionName)
const newNavContentObject = {
...navContentObject,
links: newNavLinks,
}
const newNavContent = base64.encode(yaml.safeDump(newNavContentObject))
await nav.update(NAV_FILE_NAME, newNavContent, navSha)

// Get all collectionPages
const IsomerFile = new File(this.accessToken, this.siteName)
const collectionPageType = new CollectionPageType(collectionName)
Expand All @@ -66,7 +97,7 @@ class Collection {
if (!_.isEmpty(collectionPages)) {
// Delete all collectionPages
await Bluebird.map(collectionPages, async(collectionPage) => {
let pageName = collectionPage.pageName
let pageName = collectionPage.fileName
const { sha } = await IsomerFile.read(pageName)
return IsomerFile.delete(pageName, sha)
})
Expand All @@ -78,42 +109,59 @@ class Collection {

async rename(oldCollectionName, newCollectionName) {
try {
const commitMessage = `Rename collection from ${oldCollectionName} to ${newCollectionName}`
// Rename collection in config
const config = new Config(this.accessToken, this.siteName)
const { content, sha } = await config.read()
const contentObject = yaml.safeLoad(base64.decode(content))

contentObject.collections[`${newCollectionName}`] = {
permalink: '/:collection/:path/:title',
contentObject.collections[`${newCollectionName}`] = {
output: true
}
delete contentObject.collections[`${oldCollectionName}`]
const newContent = base64.encode(yaml.safeDump(contentObject))

await config.update(newContent, sha)

// Get all collectionPages
const OldIsomerFile = new File(this.accessToken, this.siteName)
const oldCollectionPageType = new CollectionPageType(oldCollectionName)
OldIsomerFile.setFileType(oldCollectionPageType)
const collectionPages = await OldIsomerFile.list()

// If the object is empty (there are no pages in the collection), do nothing
if (_.isEmpty(collectionPages)) return

// Set up new collection File instance
const NewIsomerFile = new File(this.accessToken, this.siteName)
const newCollectionPageType = new CollectionPageType(newCollectionName)
NewIsomerFile.setFileType(newCollectionPageType)

// Rename all collectionPages
await Bluebird.map(collectionPages, async(collectionPage) => {
let pageName = collectionPage.fileName
const { content, sha } = await OldIsomerFile.read(pageName)
await OldIsomerFile.delete(pageName, sha)
return NewIsomerFile.create(pageName, content)
// Rename collection in nav if it exists
const nav = new File(this.accessToken, this.siteName)
const dataType = new DataType()
nav.setFileType(dataType)
const { content:navContent, sha:navSha } = await nav.read(NAV_FILE_NAME)
const navContentObject = yaml.safeLoad(base64.decode(navContent))

const newNavLinks = navContentObject.links.map(link => {
if (link.collection === oldCollectionName) {
return {
title: deslugifyCollectionName(newCollectionName),
collection: newCollectionName
}
} else {
return link
}
})
const newNavContentObject = {
...navContentObject,
links: newNavLinks,
}
const newNavContent = base64.encode(yaml.safeDump(newNavContentObject))
await nav.update(NAV_FILE_NAME, newNavContent, navSha)

const { currentCommitSha, treeSha } = await getCommitAndTreeSha(this.siteName, this.accessToken)
const gitTree = await getTree(this.siteName, this.accessToken, treeSha);
const oldCollectionDirectoryName = `_${oldCollectionName}`
const newCollectionDirectoryName = `_${newCollectionName}`
const newGitTree = gitTree.map(item => {
if (item.path === oldCollectionDirectoryName) {
return {
...item,
path: newCollectionDirectoryName
}
} else {
return item
}
})

await sendTree(newGitTree, currentCommitSha, this.siteName, this.accessToken, commitMessage);
} catch (err) {
throw err
}
Expand Down
17 changes: 12 additions & 5 deletions classes/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const _ = require('lodash')
const validateStatus = require('../utils/axios-utils')

// Import error
const { NotFoundError } = require('../errors/NotFoundError')
const { NotFoundError } = require('../errors/NotFoundError')
const { ConflictError, inputNameConflictErrorMsg } = require('../errors/ConflictError')

const GITHUB_ORG_NAME = process.env.GITHUB_ORG_NAME
const BRANCH_REF = process.env.BRANCH_REF
Expand Down Expand Up @@ -78,14 +79,18 @@ class File {

return { sha: resp.data.content.sha }
} catch (err) {
throw err
const status = err.response.status
if (status === 422 || status === 409) throw new ConflictError(inputNameConflictErrorMsg(fileName))
throw err.response
}
}

async read(fileName) {
try {
const files = await this.list()
if (_.isEmpty(files)) throw new NotFoundError ('File does not exist')
const fileToRead = files.filter((file) => file.fileName === fileName)[0]
if (fileToRead === undefined) throw new NotFoundError ('File does not exist')
const endpoint = `${this.baseBlobEndpoint}/${fileToRead.sha}`

const params = {
Expand All @@ -100,9 +105,7 @@ class File {
"Content-Type": "application/json"
}
})

if (resp.status === 404) throw new NotFoundError ('File does not exist')


const { content, sha } = resp.data

return { content, sha }
Expand Down Expand Up @@ -131,6 +134,8 @@ class File {

return { newSha: resp.data.commit.sha }
} catch (err) {
const status = err.response.status
if (status === 404) throw new NotFoundError ('File does not exist')
throw err
}
}
Expand All @@ -153,6 +158,8 @@ class File {
}
})
} catch (err) {
const status = err.response.status
if (status === 404) throw new NotFoundError ('File does not exist')
throw err
}
}
Expand Down
64 changes: 30 additions & 34 deletions classes/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash')
// Import classes
const { File, ResourceCategoryType, ResourcePageType } = require('../classes/File.js')
const { Directory, ResourceRoomType } = require('../classes/Directory.js')
const { getCommitAndTreeSha, getTree, sendTree } = require('../utils/utils.js')

// Constants
const RESOURCE_INDEX_PATH = 'index.html'
Expand Down Expand Up @@ -38,42 +39,37 @@ class Resource {
}
}

async rename(resourceRoomName, resourceName, newResourceRoomName, newResourceName) {
async rename(resourceRoomName, resourceName, newResourceName) {
try {
// Delete old index file in old resource
const OldIsomerIndexFile = new File(this.accessToken, this.siteName)
const resourceType = new ResourceCategoryType(resourceRoomName, resourceName)
OldIsomerIndexFile.setFileType(resourceType)
const { sha: oldSha } = await OldIsomerIndexFile.read(`${RESOURCE_INDEX_PATH}`)
await OldIsomerIndexFile.delete(`${RESOURCE_INDEX_PATH}`, oldSha)

// Create new index file in new resource
const NewIsomerIndexFile = new File(this.accessToken, this.siteName)
const newResourceType = new ResourceCategoryType(newResourceRoomName, newResourceName)
NewIsomerIndexFile.setFileType(newResourceType)
await NewIsomerIndexFile.create(`${RESOURCE_INDEX_PATH}`, RESOURCE_INDEX_CONTENT)

// Rename resourcePages
const OldIsomerFile = new File(this.accessToken, this.siteName)
const resourcePageType = new ResourcePageType(resourceRoomName, resourceName)
OldIsomerFile.setFileType(resourcePageType)

const NewIsomerFile = new File(this.accessToken, this.siteName)
const newResourcePageType = new ResourcePageType(newResourceRoomName, newResourceName)
NewIsomerFile.setFileType(newResourcePageType)

// 1. List all resourcePages in old resource
const resourcePages = await OldIsomerFile.list()

if (_.isEmpty(resourcePages)) return

await Bluebird.each(resourcePages, async(resourcePage) => {
// 2. Create new resourcePages in newResource
const { content, sha } = await OldIsomerFile.read(resourcePage.fileName)
await NewIsomerFile.create(resourcePage.fileName, content)
// 3. Delete all resourcePages in resource
return OldIsomerFile.delete(resourcePage.fileName, sha)
const commitMessage = `Rename resource category from ${resourceName} to ${newResourceName}`
const { currentCommitSha, treeSha } = await getCommitAndTreeSha(this.siteName, this.accessToken)
const gitTree = await getTree(this.siteName, this.accessToken, treeSha);
let 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(this.siteName, this.accessToken, resourceRoomTreeSha)
resourceRoomTree.forEach(item => {
// We need to append resource room to the file path because the path is relative to the subtree
if (item.path === resourceName) {
newGitTree.push({
...item,
path: `${resourceRoomName}/${newResourceName}`
})
} else {
newGitTree.push({
...item,
path: `${resourceRoomName}/${item.path}`
})
}
})
await sendTree(newGitTree, currentCommitSha, this.siteName, this.accessToken, commitMessage);
} catch (err) {
throw err
}
Expand Down
Loading

0 comments on commit fe44b25

Please sign in to comment.