diff --git a/functions/code.js b/functions/code.js index e6c61000..02eee160 100644 --- a/functions/code.js +++ b/functions/code.js @@ -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 diff --git a/functions/colab.js b/functions/colab.js index c7e2431e..193d5b3f 100644 --- a/functions/colab.js +++ b/functions/colab.js @@ -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 @@ -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-', '') @@ -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}` diff --git a/functions/utils.js b/functions/utils.js index a1a72703..20c11a05 100644 --- a/functions/utils.js +++ b/functions/utils.js @@ -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 }) @@ -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 + } +} diff --git a/src/components/NavCode.vue b/src/components/NavCode.vue index 7494f154..c73aeefb 100644 --- a/src/components/NavCode.vue +++ b/src/components/NavCode.vue @@ -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) { diff --git a/src/components/NavColab.vue b/src/components/NavColab.vue index a9e21fc7..c9bc8398 100644 --- a/src/components/NavColab.vue +++ b/src/components/NavColab.vue @@ -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