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

Feat/update navigation file on creation of new collection and resource room #80

14 changes: 14 additions & 0 deletions classes/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const _ = require('lodash')

const { Config } = require('./Config.js')
const { File, CollectionPageType } = require('./File.js')
const { Navigation } = require('./Navigation.js')
const { deslugifyCollectionName } = require('../utils/utils.js')

class Collection {
constructor(accessToken, siteName) {
Expand Down Expand Up @@ -40,6 +42,18 @@ class Collection {

await config.update(newContent, sha)

const nav = new Navigation(this.accessToken, this.siteName)
const { content:navContent, sha:navSha } = await nav.read()
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(newNavContent, navSha)

} catch (err) {
throw err
}
Expand Down
66 changes: 66 additions & 0 deletions classes/Navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const axios = require('axios');
const validateStatus = require('../utils/axios-utils')

// Import logger
const logger = require('../logger/logger');

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

const GITHUB_ORG_NAME = process.env.GITHUB_ORG_NAME
const BRANCH_REF = process.env.BRANCH_REF

class Navigation {
constructor(accessToken, siteName) {
this.accessToken = accessToken
this.siteName = siteName
}

async read() {
try {
const endpoint = `https://api.github.com/repos/${GITHUB_ORG_NAME}/${this.siteName}/contents/_data/navigation.yml`

const params = {
"ref": BRANCH_REF,
}

const resp = await axios.get(endpoint, {
validateStatus,
params,
headers: {
Authorization: `token ${this.accessToken}`,
"Content-Type": "application/json"
}
})

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

const { content, sha } = resp.data

return { content, sha }

} catch (err) {
throw err
}
}

async update(newContent, sha) {
const endpoint = `https://api.github.com/repos/${GITHUB_ORG_NAME}/${this.siteName}/contents/_data/navigation.yml`

const params = {
"message": 'Edit navigation',
"content": newContent,
"branch": BRANCH_REF,
"sha": sha
}

await axios.put(endpoint, params, {
headers: {
Authorization: `token ${this.accessToken}`,
"Content-Type": "application/json"
}
})
}
}

module.exports = { Navigation }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually there's no need to create a Navigation class, to prevent duplication of code, we can create a new File and apply the existing DataType type to retrieve the navigation.yml file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, resolved in e9c5760, 4a0b24c, a1c294d

14 changes: 14 additions & 0 deletions classes/ResourceRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const _ = require('lodash')
const { Config } = require('./Config.js')
const { Resource } = require('../classes/Resource.js')
const { File, ResourceType } = require('../classes/File.js')
const { Navigation } = require('./Navigation.js')
const { deslugifyCollectionName } = require('../utils/utils.js')

// Constants
const RESOURCE_ROOM_INDEX_PATH = 'index.html'
Expand Down Expand Up @@ -48,6 +50,18 @@ class ResourceRoom {

await config.update(newContent, sha)

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

navContentObject.links.push({
title: deslugifyCollectionName(resourceRoom),
resource_room: true
})
const newNavContent = base64.encode(yaml.safeDump(navContentObject))

await nav.update(newNavContent, navSha)

return resourceRoom
} catch (err) {
throw err
Expand Down