Skip to content

Commit f41ea18

Browse files
Fix bug "Open in Colab" returning "undefined" (#305)
* Test commit using GET for existing file * Adding printing * Remove extra lines * Modify log contents * Modify print statements * Test if the try catch statements work * Add print statements * Added comments * Changed the error handling * Remove log statements * Fix lint --------- Co-authored-by: vfdev <vfdev.5@gmail.com>
1 parent 492dd76 commit f41ea18

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

functions/utils.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,37 @@ export async function pushToGitHub(content, filename, nbUid) {
2121
auth: process.env.VUE_APP_GH_TOKEN
2222
})
2323
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
24+
try {
25+
// first try the GET request to check if the zip/ipynb already exists
26+
// if it already exists, then return the same url
27+
const res = await octokit.request(
28+
'GET /repos/{owner}/{repo}/contents/{path}',
29+
{
30+
owner: repoOwner,
31+
repo: repo,
32+
path: `nbs/${nbUid}/${filename}`
33+
}
34+
)
35+
return res.data.download_url
36+
} catch (err) {
37+
// if the url doesn't exist, then create a new url for that specific nbUid
38+
// push to github and return the download url
39+
if (err.status == '404') {
40+
const res = await octokit.request(
41+
'PUT /repos/{owner}/{repo}/contents/{path}',
42+
{
43+
owner: repoOwner,
44+
repo: repo,
45+
path: `nbs/${nbUid}/${filename}`,
46+
message: `nb: add ${nbUid}`,
47+
content: content
48+
}
49+
)
50+
return res.data.content.download_url
51+
} else {
52+
console.error(err)
3253
}
33-
)
34-
return res.data.content.download_url
54+
}
3555
} catch (e) {
3656
console.error(e)
3757
}

0 commit comments

Comments
 (0)