@@ -21,17 +21,37 @@ export async function pushToGitHub(content, filename, nbUid) {
21
21
auth : process . env . VUE_APP_GH_TOKEN
22
22
} )
23
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
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 )
32
53
}
33
- )
34
- return res . data . content . download_url
54
+ }
35
55
} catch ( e ) {
36
56
console . error ( e )
37
57
}
0 commit comments