Skip to content

Commit 5e52f96

Browse files
committed
Added http instead curl for requests
1 parent d043ad1 commit 5e52f96

File tree

7 files changed

+136
-62
lines changed

7 files changed

+136
-62
lines changed

install/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const AUTH_PATH = settings.authPath;
2323
const authDetails = {
2424
github: [
2525
{
26-
user: authUser.user,
27-
token: authToken.token,
26+
user: authUser.github.user,
27+
token: authToken.github.token,
2828
},
2929
],
3030
};

src/githubHandler/index.js

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
6265
async 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)

src/project/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class Project {
5454
url: '',
5555
},
5656
useGithub = false,
57+
github = {
58+
user: '',
59+
token: '',
60+
},
5761
hasRemote = false,
5862
git = {
5963
name: '',
@@ -78,6 +82,10 @@ class Project {
7882
url: author.url,
7983
};
8084
this.useGithub = useGithub;
85+
this.github = {
86+
user: github.user,
87+
token: github.token,
88+
};
8189
this.hasRemote = hasRemote;
8290
this.git = {
8391
name: git.name,

src/questionnaire/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ async function run(name) {
1111
updateToken: false,
1212
};
1313

14+
const github = {};
1415
let currentAuthUser;
1516
let currentToken;
1617

@@ -26,25 +27,28 @@ async function run(name) {
2627

2728
userAnswers = await questions.getGithubUser(currentAuthUser.user);
2829

29-
currentToken = await auth.getToken(userAnswers.user, authFileAnswers.authPath);
30+
currentToken = await auth.getToken(userAnswers.github.user, authFileAnswers.authPath);
3031

31-
tokenAnswers = await questions.getAuthToken(userAnswers.user, currentToken);
32+
tokenAnswers = await questions.getAuthToken(userAnswers.github.user, currentToken);
3233

34+
github.user = userAnswers.github.user;
35+
github.token = tokenAnswers.github.token;
3336

34-
if (userAnswers.user !== currentAuthUser.user || tokenAnswers.token !== currentToken) {
37+
if (github.user !== currentAuthUser.user
38+
|| github.token !== currentToken) {
3539
updateAnswers = await questions.confirmUpdateToken();
3640
}
3741

3842
if (updateAnswers.updateToken) {
39-
auth.updateAuthFile(userAnswers.user, tokenAnswers.token, settings.authPath);
43+
auth.updateAuthFile(github.user, github.token, settings.authPath);
4044
}
4145

4246
if (!currentToken) {
4347
resp.useGithub = false;
4448
}
4549
}
4650

47-
Object.assign(resp, remoteAnswers, authFileAnswers, userAnswers, tokenAnswers);
51+
Object.assign(resp, remoteAnswers, authFileAnswers, { github });
4852

4953
return resp;
5054
}

src/questionnaire/questions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ async function getGithubUser(user) {
150150
return inquirer.prompt([
151151
{
152152
type: 'input',
153-
name: 'user',
153+
name: 'github.user',
154154
message: 'What is your Github user?',
155155
default: user,
156156
},
@@ -167,7 +167,7 @@ async function getAuthToken(user, token) {
167167
return inquirer.prompt([
168168
{
169169
type: 'input',
170-
name: 'token',
170+
name: 'github.token',
171171
message: `What is your GitHub token for user ${user}?`,
172172
default: token,
173173
},

src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
exports.files = require('./files');
22
exports.string = require('./string');
33
exports.process = require('./process');
4+
exports.requests = require('./requests');

src/utils/requests.js

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ const https = require('https');
88
* @param {Boolean} jsonResponse If the response will be a json
99
* @return {Promise}
1010
*/
11-
function post(data, url, headers, jsonResponse = true) {
11+
function postReq(data, url, headers, jsonResponse = true) {
1212
const apiUrl = new URL(url);
1313

14+
if (apiUrl.protocol === 'https') {
15+
throw new Error('Only https requests allowed');
16+
}
17+
1418
const options = {
1519
hostname: apiUrl.hostname,
1620
path: apiUrl.pathname,
@@ -34,8 +38,71 @@ function post(data, url, headers, jsonResponse = true) {
3438
}
3539

3640
// FIXME probably also other status?
37-
if (res.statusCode !== 200 || res.statusCode !== 201) {
38-
reject(response);
41+
if (res.statusCode !== 200 && res.statusCode !== 201) {
42+
const e = {
43+
statusCode: res.statusCode,
44+
data: response,
45+
};
46+
reject(e);
47+
}
48+
49+
resolve(response);
50+
});
51+
});
52+
53+
req.on('error', (e) => {
54+
reject(e);
55+
});
56+
57+
req.write(json);
58+
req.end();
59+
});
60+
}
61+
62+
/**
63+
* HTTP delete method with json body and json response
64+
* @param {Object} data The object with all the data to send
65+
* @param {String} url The http options
66+
* @param {Object} headers The headers for the request
67+
* @param {Boolean} jsonResponse If the response will be a json
68+
* @return {Promise}
69+
*/
70+
function deleteReq(data, url, headers, jsonResponse = true) {
71+
const apiUrl = new URL(url);
72+
73+
if (apiUrl.protocol === 'https') {
74+
throw new Error('Only https requests allowed');
75+
}
76+
77+
const options = {
78+
hostname: apiUrl.hostname,
79+
path: apiUrl.pathname,
80+
method: 'DELETE',
81+
headers,
82+
};
83+
84+
const json = JSON.stringify(data);
85+
86+
return new Promise((resolve, reject) => {
87+
const req = https.request(options, (res) => {
88+
let response = '';
89+
90+
res.on('data', (chunk) => {
91+
response += chunk;
92+
});
93+
94+
res.on('end', () => {
95+
if (jsonResponse) {
96+
response = JSON.parse(response);
97+
}
98+
99+
// FIXME probably also other status?
100+
if (res.statusCode !== 204) {
101+
const e = {
102+
statusCode: res.statusCode,
103+
data: response,
104+
};
105+
reject(e);
39106
}
40107

41108
resolve(response);
@@ -52,5 +119,6 @@ function post(data, url, headers, jsonResponse = true) {
52119
}
53120

54121
module.exports = {
55-
post,
122+
postReq,
123+
deleteReq,
56124
};

0 commit comments

Comments
 (0)