Skip to content

Refactor the code for the netlify functions #296

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

Merged
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
31 changes: 2 additions & 29 deletions functions/code.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
// Below imports are defined in
// `external_node_modules` of [functions] in netlify.toml
// They are required for this function to run

import { v4 as uuidv4 } from 'uuid'
import JSZip from 'jszip'
import { pushToGitHub } from './utils'

const nbUid = uuidv4()
const repoOwner = process.env.VUE_APP_GH_USER
const repo = process.env.VUE_APP_GH_REPO
import { getZip_Uid } from './utils'

// This function is the one Netlify function runs on
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format
exports.handler = async function (event, _) {
// event is a JSON object
const data = JSON.parse(event.body)
const zip = new JSZip()
const code = data.code
const template = `ignite-${data.template}`
const { zipRes, nbUid } = await getZip_Uid(data)

// As usual from Download component,
// we will zip the files and
// generate a base64 format for pushing to GitHub
// with Octokit.
for (const filename in code) {
zip.file(filename, code[filename])
}
const content = await zip.generateAsync({ type: 'base64' })
const zipRes = await pushToGitHub(
content,
`${template}.zip`,
nbUid,
repoOwner,
repo
)
return {
statusCode: 200,
body: zipRes
Expand Down
34 changes: 3 additions & 31 deletions functions/colab.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
// Below imports are defined in
// `external_node_modules` of [functions] in netlify.toml
// They are required for this function to run

import { v5 as uuidv5 } from 'uuid'
import JSZip from 'jszip'
import { pushToGitHub } from './utils'
import { pushToGitHub, getZip_Uid } from './utils'

const repoOwner = process.env.VUE_APP_GH_USER
const repo = process.env.VUE_APP_GH_REPO
Expand All @@ -14,29 +8,9 @@ const repo = process.env.VUE_APP_GH_REPO
exports.handler = async function (event, _) {
// event is a JSON object
const data = JSON.parse(event.body)
const zip = new JSZip()
const code = data.code
let hash = ''
const template = `ignite-${data.template}`
const nbName = `${template}.ipynb`

// As usual from Download component,
// we will zip the files and
// generate a base64 format for pushing to GitHub
// with Octokit.
for (const filename in code) {
hash += code[filename]
zip.file(filename, code[filename])
}
const nbUid = uuidv5(hash, uuidv5.URL)
const content = await zip.generateAsync({ type: 'base64' })
const zipRes = await pushToGitHub(
content,
`${template}.zip`,
nbUid,
repoOwner,
repo
)
const { zipRes, nbUid } = await getZip_Uid(data)

const title = template
.replace('ignite-', '')
Expand Down Expand Up @@ -101,9 +75,7 @@ exports.handler = async function (event, _) {
await pushToGitHub(
Buffer.from(JSON.stringify(nb)).toString('base64'),
nbName,
nbUid,
repoOwner,
repo
nbUid
)

const colabLink = `https://colab.research.google.com/github/${repoOwner}/${repo}/blob/main/nbs/${nbUid}/${nbName}`
Expand Down
35 changes: 32 additions & 3 deletions functions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
// They are required for this function to run

import { Octokit } from '@octokit/core'
import JSZip from 'jszip'
import { v5 as uuidv5 } from 'uuid'

const repoOwner = process.env.VUE_APP_GH_USER
const repo = process.env.VUE_APP_GH_REPO

/**
* Create a file on GitHub with Octokit.
* @param {string} content
* @param {string} filename
* @param {string} nbUid
* @param {string} repoOwner
* @param {string} repo
* @returns download_url
*/
export async function pushToGitHub(content, filename, nbUid, repoOwner, repo) {
export async function pushToGitHub(content, filename, nbUid) {
const octokit = new Octokit({
auth: process.env.VUE_APP_GH_TOKEN
})
Expand All @@ -33,3 +36,29 @@ export async function pushToGitHub(content, filename, nbUid, repoOwner, repo) {
console.error(e)
}
}

// This function is the one Netlify function runs on
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format
export async function getZip_Uid(data) {
const zip = new JSZip()
const code = data.code
const template = `ignite-${data.template}`

// As usual from Download component,
// we will zip the files and
// generate a base64 format for pushing to GitHub
// with Octokit.
for (const filename in code) {
zip.file(filename, code[filename])
}
// since the generated zip varies every time even with the same code
// it can't be used to generate a UUID
const content = await zip.generateAsync({ type: 'base64' })
// we generate an unique id from the current config for pushing to github
const nbUid = uuidv5(JSON.stringify(data.config), uuidv5.URL)
const zipRes = await pushToGitHub(content, `${template}.zip`, nbUid)
return {
zipRes: zipRes,
nbUid: nbUid
}
}
3 changes: 2 additions & 1 deletion src/components/NavCode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ export default {
},
body: JSON.stringify({
code: store.code,
template: store.config.template
template: store.config.template,
config: store.config
})
})
if (res.ok) {
Expand Down
3 changes: 2 additions & 1 deletion src/components/NavColab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export default {
},
body: JSON.stringify({
code: store.code,
template: store.config.template
template: store.config.template,
config: store.config
})
})
// response body is plain text
Expand Down