|
| 1 | +// Below imports are defined in |
| 2 | +// `external_node_modules` of [functions] in netlify.toml |
| 3 | +// They are required for this function to run |
| 4 | + |
| 5 | +import { v4 as uuidv4 } from 'uuid' |
| 6 | +import { Octokit } from '@octokit/core' |
| 7 | +import JSZip from 'jszip' |
| 8 | + |
| 9 | +const nbUid = uuidv4() |
| 10 | +const repoOwner = process.env.VUE_APP_GH_USER |
| 11 | +const repo = process.env.VUE_APP_GH_REPO |
| 12 | + |
| 13 | +/** |
| 14 | + * Create a file on GitHub with Octokit. |
| 15 | + * @param {string} content |
| 16 | + * @param {string} filename |
| 17 | + * @returns download_url |
| 18 | + */ |
| 19 | +async function pushToGitHub(content, filename) { |
| 20 | + const octokit = new Octokit({ |
| 21 | + auth: process.env.VUE_APP_GH_TOKEN |
| 22 | + }) |
| 23 | + try { |
| 24 | + const res = await octokit.request( |
| 25 | + 'PUT /repos/{owner}/{repo}/contents/{path}', |
| 26 | + { |
| 27 | + owner: repoOwner, |
| 28 | + repo: repo, |
| 29 | + path: `nbs/${nbUid}/${filename}`, |
| 30 | + message: `nb: add ${nbUid}`, |
| 31 | + content: content |
| 32 | + } |
| 33 | + ) |
| 34 | + // the download url is raw url - https://raw.githubusercontent.com/... |
| 35 | + return res.data.content.download_url |
| 36 | + } catch (e) { |
| 37 | + console.error(e) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// This function is the one Netlify function runs on |
| 42 | +// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format |
| 43 | +export async function handler(event, _) { |
| 44 | + // event is a JSON object |
| 45 | + const data = JSON.parse(event.body) |
| 46 | + const zip = new JSZip() |
| 47 | + const code = data.code |
| 48 | + const template = `ignite-${data.template}` |
| 49 | + const nbName = `${template}.ipynb` |
| 50 | + |
| 51 | + // As usual from Download component, |
| 52 | + // we will zip the files and |
| 53 | + // generate a base64 format for pushing to GitHub |
| 54 | + // with Octokit. |
| 55 | + for (const filename in code) { |
| 56 | + zip.file(filename, code[filename]) |
| 57 | + } |
| 58 | + const content = await zip.generateAsync({ type: 'base64' }) |
| 59 | + const zipRes = await pushToGitHub(content, `${template}.zip`) |
| 60 | + |
| 61 | + const title = template |
| 62 | + .replace('ignite-', '') |
| 63 | + .split('-') |
| 64 | + .map((v) => v[0].toUpperCase() + v.slice(1)) |
| 65 | + .join(' ') |
| 66 | + // notebook cell structure |
| 67 | + const nb = { |
| 68 | + nbformat: 4, |
| 69 | + nbformat_minor: 0, |
| 70 | + metadata: { |
| 71 | + kernelspec: { |
| 72 | + display_name: 'Python 3', |
| 73 | + name: 'python3' |
| 74 | + }, |
| 75 | + accelerator: 'GPU' |
| 76 | + }, |
| 77 | + cells: [ |
| 78 | + { |
| 79 | + cell_type: 'markdown', |
| 80 | + metadata: {}, |
| 81 | + execution_count: null, |
| 82 | + outputs: [], |
| 83 | + source: [ |
| 84 | + `# ${title} by PyTorch-Ignite Code-Generator\n\n`, |
| 85 | + 'Please, run the cell below to execute your code.' |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + cell_type: 'code', |
| 90 | + metadata: {}, |
| 91 | + execution_count: null, |
| 92 | + outputs: [], |
| 93 | + source: [ |
| 94 | + `!wget ${zipRes}\n`, |
| 95 | + `!unzip ${template}.zip\n`, |
| 96 | + '!pip install -r requirements.txt\n', |
| 97 | + '!python main.py\n' |
| 98 | + ] |
| 99 | + } |
| 100 | + ] |
| 101 | + } |
| 102 | + // Create the notebook on GitHub |
| 103 | + await pushToGitHub(Buffer.from(JSON.stringify(nb)).toString('base64'), nbName) |
| 104 | + |
| 105 | + const colabLink = `https://colab.research.google.com/github/${repoOwner}/${repo}/blob/main/nbs/${nbUid}/${nbName}` |
| 106 | + return { |
| 107 | + statusCode: 200, |
| 108 | + body: colabLink |
| 109 | + } |
| 110 | +} |
0 commit comments