-
Notifications
You must be signed in to change notification settings - Fork 24
Refactor the code for the netlify functions #296
Changes from 1 commit
c4e612a
c68f16f
3336f27
cb0b9ad
0319b43
012997d
311a1de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,11 @@ | |
| // 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. | ||
|
|
@@ -13,7 +18,7 @@ import { Octokit } from '@octokit/core' | |
| * @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 +38,30 @@ 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 | ||
| let hash = '' | ||
| 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. | ||
| // we generate a hash for unique code identification and | ||
| // zip generation | ||
| 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' }) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also produce the hash from the content instead of concatenated files with code. To see if this makes more interesting in terms of perfs. I suspect that content length could be smaller than the length of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is indeed smaller than concatenating the whole code. But I guess one thing I didn't understand while testing was that the So it's also generating a new Uid for each open in colab button press, which it should not for no code changes... This leads me to believe that zip being generated is slightly different every time. This creates a new uid each time, Which we wouldn't want. Ideally, we would like to keep the uid same for the template without any changes... @guptaaryan16 since you've worked on this, do you have any thoughts?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @theory-in-progress @vfdev-5 Actually when we click on 'Open in Colab' or 'Download Zip', it makes an async request to the netlify functions to get the uuid for the code and the code link. I think we should create a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But these netlify functions are triggered only when we click on 'Open in Colab' or 'Download Zip'. And each time they are being for the changed code, we would like it to contain a new uuid. Also the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably we can update the functions to take uuid parameter from |
||
| const zipRes = await pushToGitHub(content, `${template}.zip`, nbUid) | ||
| return { | ||
| zipRes: zipRes, | ||
| nbUid: nbUid | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.