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