@@ -18,37 +18,40 @@ async function create({
1818 isPrivate = false ,
1919 description = '' ,
2020 url = '' ,
21- user = '' ,
21+ github = {
22+ user : '' ,
23+ token : '' ,
24+ } ,
2225} ) {
23- const token = await auth . getToken ( user ) ;
24- let result ;
26+ let data ;
2527
26- if ( ! token ) {
28+ if ( ! github . token ) {
2729 throw new Error ( 'Token missing' ) ;
2830 }
2931
30- try {
31- console . info ( 'Creating github repository ...' ) ;
32- // TODO consider use http instead curl?
33- const cmd = `curl -w "%{http_code}" -H "Authorization: token ${ token } " -d '{"name": "${ name } ", "private": ${ isPrivate } , "description": "${ description } ", "homepage": "${ url } "}' https://api.github.com/user/repos` ;
34-
35- result = await utils . process . execp ( cmd ) ;
36-
37- let json = result . substring ( 0 , result . lastIndexOf ( '\n' ) ) ;
38- const statusCode = Number ( result . substring ( result . lastIndexOf ( '\n' ) ) ) ;
32+ console . info ( 'Creating github repository ...' ) ;
33+ const project = {
34+ name,
35+ private : isPrivate ,
36+ description,
37+ homepage : url ,
38+ } ;
39+
40+ const headers = {
41+ 'Content-Type' : 'application/json' ,
42+ Accept : 'application/vnd.github.v3+json' ,
43+ Authorization : `token ${ github . token } ` ,
44+ 'User-Agent' : github . user ,
45+ } ;
3946
40- json = JSON . parse ( json ) ;
41-
42- if ( statusCode !== 201 ) {
43- console . error ( 'Repository not created: ' , json . errors [ 0 ] . message ) ;
44- return false ;
45- }
46-
47- console . log ( `Repository ${ json . name } created` ) ;
48- return json ;
49- } catch ( error ) {
50- throw error ;
47+ try {
48+ data = await utils . requests . postReq ( project , 'https://api.github.com/user/repos' , headers ) ;
49+ console . log ( `Repository ${ data . name } created` ) ;
50+ } catch ( e ) {
51+ console . error ( 'Repository not created: ' , e . statusCode , e . data ) ;
5152 }
53+
54+ return data ;
5255}
5356
5457/**
@@ -61,39 +64,29 @@ async function create({
6164 */
6265async function deleteRepo ( name , user ) {
6366 const token = await auth . getToken ( user ) ;
64- let result ;
67+ let data ;
68+
69+ const headers = {
70+ 'Content-Type' : 'application/json' ,
71+ Accept : 'application/vnd.github.v3+json' ,
72+ Authorization : `token ${ token } ` ,
73+ 'User-Agent' : user ,
74+ } ;
6575
6676 if ( ! token ) {
6777 throw new Error ( 'Token missing' ) ;
6878 }
6979
70- try {
71- console . info ( 'Deleting github repository ...' ) ;
72- // TODO consider use http instead curl?
73- const cmd = `curl -w "%{http_code}" -XDELETE -H "Authorization: token ${ token } " https://api.github.com/repos/${ user } /${ name } ` ;
74-
75- result = await utils . process . execp ( cmd ) ;
76-
77- const statusCode = Number ( result . substring ( result . lastIndexOf ( '\n' ) ) ) ;
78-
79- if ( statusCode !== 204 ) {
80- let json = result . substring ( 0 , result . lastIndexOf ( '\n' ) ) ;
81-
82- json = JSON . parse ( json ) ;
83-
84- console . error ( 'Repository not deleted: ' , json . message ) ;
85- return false ;
86- }
8780
81+ console . info ( 'Deleting github repository ...' ) ;
82+ try {
83+ data = await utils . requests . deleteReq ( null , `https://api.github.com/repos/${ user } /${ name } ` , headers ) ;
8884 console . log ( `Repository ${ name } deleted` ) ;
89-
90- return {
91- message : 'Repository deleted' ,
92- statusCode,
93- } ;
94- } catch ( error ) {
95- throw error ;
85+ } catch ( e ) {
86+ console . error ( 'Repository not deleted: ' , e . statusCode , e . data ) ;
9687 }
88+
89+ return data ;
9790}
9891
9992// TODO include a method to handle the topics (will require to get the github user)
0 commit comments