-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
211 lines (178 loc) · 5.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
var request = require('request');
const chalk = require('chalk');
var urlRoot = "https://api.github.ncsu.edu";
// Github Standard Endpoint
//var urlRoot = "https://api.github.com";
// NCSU Enterprise endpoint:
//var urlRoot = "https://api.github.ncsu.edu";
var config = {};
// Retrieve our api token from the environment variables.
config.token = process.env.GITHUBTOKEN;
if( !config.token )
{
console.log(chalk`{red.bold GITHUBTOKEN is not defined!}`);
console.log(`Please set your environment variables with appropriate token.`);
console.log(chalk`{italic You may need to refresh your shell in order for your changes to take place.}`);
process.exit(1);
}
console.log(chalk.green(`Your token is: ${config.token.substring(0,4)}...`));
if (process.env.NODE_ENV != 'test')
{
(async () => {
await listAuthenicatedUserRepos();
await listBranches("pppandi2", "HW1-510");
await createRepo("pppandi2","Test-Repo");
await createIssue("pppandi2", "HW1-510", "Issue created using REST Api", "This issue was created using REST Api for the purpose of learning.");
await enableWikiSupport("pppandi2", "Test-Repo");
})()
}
function getDefaultOptions(endpoint, method)
{
var options = {
url: urlRoot + endpoint,
method: method,
headers: {
"User-Agent": "CSC510-REST-WORKSHOP",
"content-type": "application/json",
"Authorization": `token ${config.token}`
}
};
return options;
}
async function getUser()
{
let options = getDefaultOptions("/user", "GET");
// Send a http request to url and specify a callback that will be called upon its return.
return new Promise(function(resolve, reject)
{
request(options, function (error, response, body) {
resolve( JSON.parse(body).login );
});
});
}
function listAuthenicatedUserRepos()
{
let options = getDefaultOptions("/user/repos?visibility=all", "GET");
// Send a http request to url and specify a callback that will be called upon its return.
return new Promise(function(resolve, reject)
{
request(options, function (error, response, body)
{
if( error )
{
console.log( chalk.red( error ));
reject(error);
return; // Terminate execution.
}
var obj = JSON.parse(body);
// console.log(JSON.stringify(body));
console.log('Authenticated user repositories:');
for( var i = 0; i < obj.length; i++ )
{
var name = obj[i].name;
console.log( name );
}
console.log();
// Return object for people calling our method.
resolve(obj);
});
});
}
// 1. Write code for listBranches in a given repo under an owner. See list branches
async function listBranches(owner,repo)
{
let options = getDefaultOptions(`/repos/`+owner+`/`+repo+`/branches`, "GET");
// console.log(options);
// Send a http request to url and specify a callback that will be called upon its return.
return new Promise(function(resolve, reject)
{
request(options, function (error, response, body) {
if (error) {
console.log(chalk.red(error));
reject(error);
return; // Terminate execution.
}
var obj = JSON.parse(body);
console.log('Branches belonging to the '+repo+' repository:');
for (var i = 0; i < obj.length; i++) {
var name = obj[i].name;
console.log(name);
}
console.log();
resolve(obj);
});
});
}
// 2. Write code to create a new repo
async function createRepo(owner,repo)
{
let options = getDefaultOptions("/user/repos", "POST");
options.body = JSON.stringify({
name: repo,
description:'This repo was created using REST Api'
})
// Send a http request to url and specify a callback that will be called upon its return.
return new Promise(function(resolve, reject)
{
request(options, function (error, response, body) {
if (error) {
console.log(chalk.red(error));
reject(error);
return; // Terminate execution.
}
resolve( response.statusCode );
});
});
}
// 3. Write code for creating an issue for an existing repo.
async function createIssue(owner, repo, issueName, issueBody)
{
let options = getDefaultOptions("/repos/"+owner+"/"+repo+"/issues", "POST");
var details = {
title:issueName,
body:issueBody
}
options.body = JSON.stringify(details);
// console.log(options)
// Send a http request to url and specify a callback that will be called upon its return.
return new Promise(function(resolve, reject)
{
request(options, function (error, response, body) {
if (error) {
console.log(chalk.red(error));
reject(error);
return; // Terminate execution.
}
// if(response.statusCode==201){console.log('successful!')}
resolve( response.statusCode );
});
});
}
// 4. Write code for editing a repo to enable wiki support.
async function enableWikiSupport(owner, repo)
{
let options = getDefaultOptions("/repos/"+owner+"/"+repo, "PATCH");
options.body = JSON.stringify({
has_wiki:true
})
// console.log(options)
// Send a http request to url and specify a callback that will be called upon its return.
return new Promise(function(resolve, reject)
{
request(options, function (error, response, body) {
if (error) {
console.log(chalk.red(error));
reject(error);
return; // Terminate execution.
}
console.log(JSON.parse(body));
resolve( JSON.parse(body) );
});
});
}
module.exports.getUser = getUser;
module.exports.listAuthenicatedUserRepos = listAuthenicatedUserRepos;
module.exports.listBranches = listBranches;
module.exports.createRepo = createRepo;
module.exports.createIssue = createIssue;
module.exports.enableWikiSupport = enableWikiSupport;