-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepoDownloader3.js
221 lines (169 loc) · 5.22 KB
/
repoDownloader3.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
212
213
214
215
216
217
218
219
"use strict"
var chalk = require('chalk');
var clear = require('clear');
var CLI = require('clui');
var figlet = require('figlet');
var inquirer = require('inquirer');
var Preferences = require('preferences');
var Spinner = CLI.Spinner;
var GitHubApi = require('github');
var _ = require('lodash');
//var git = require('simple-git')();
var touch = require('touch');
var fs = require('fs');
var files = require('./lib/files');
var GitHub = require('github-api');
var ghdownload = require('github-download')
var exec = require('exec')
var Git = require('nodegit');
var maxnumber = 10;
var configUsed = ".eslintrc.js";
var GITHUB_TOKEN = "ThisWillBeYourGitHubToken";
var dbPW = "ThisWillBeYourDBPW";
clear();
console.log(
chalk.yellow(
figlet.textSync('RepoDownloader', { horizontalLayout: 'full' })
)
);
getInput(function(){
});
function getInput(callback) {
var argv = require('minimist')(process.argv.slice(2));
var questions = [
{
type: 'input',
name: 'SearchString',
message: 'Enter a SearchString:',
default: 'Objectoriented'
},
{
type: 'input',
name: 'language',
default: "javascript",
message: 'Enter a preferred Language'
},
{
type: 'input',
name: 'date',
default: "2015-01-01",
message: 'Enter creation or update minimum'
},
{
type: 'input',
name: 'number',
default: "10",
message: 'How many do you want to download'
},
{
type: 'input',
name: 'configFile',
default: ".eslintrc.js",
message: 'which config file to use'
},
];
inquirer.prompt(questions).then(function(answers) {
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var request = new XMLHttpRequest();
// Set the event handler
request.onload = printRepoNames;
// Initialize the request
request.open('get', 'https://api.github.com/search/repositories?q='+answers.SearchString+'+language:'+answers.language+'+created:>'+answers.date+'&sort=updated&order=desc', true);
// Fire away!
console.log("fired Request");
request.send();
//Set Answers globally for later use
maxnumber = answers.number;
configUsed = answers.configFile;
});
}
function printRepoNames() {
console.log("Printing the Repo Names");
var responseObj = JSON.parse(this.responseText);
//console.log(responseObj.items[1].full_name);
console.log("Nr Of Repos in search: " + responseObj.total_count);
console.log("Downloading first " + maxnumber + " Repos");
var fs = require('fs');
//Get the gitHub Token from the Textfile
try {
var data = fs.readFileSync('gitHubToken.txt', 'utf8');
console.log(data);
GITHUB_TOKEN = data;
} catch(e) {
console.log('Error:', e.stack);
}
//Start Cloning all the Repos from the Search
for( var i = 1; i <= maxnumber; i++) {
var RepoName = responseObj.items[i].name;
var UserName = responseObj.items[i].owner.login;
console.log("name Repo: " + responseObj.items[i].name);
console.log("name User: " + responseObj.items[i].owner.login);
//DownloadRepos(UserName, RepoName);
CloneRepos(UserName, RepoName);
}
console.log("Completed Cloning");
analyzeRepos(responseObj);
}
function CloneRepos(inUser, inName) {
Git.Clone("https://github.com/"+inUser+"/"+inName, "./clonedrepos/"+inName, GITHUB_TOKEN).then(function(repository) {
// Work with the repository object here.
});
}
function analyzeRepos(inResponse){
var CLIEngine = require("eslint").CLIEngine,
linter = require("eslint").linter;
var cli = new CLIEngine({
envs: ["node"],
configFile: configUsed,
});
console.log("creating connection");
var mysql = require("mysql");
//Get the PW Credentials from the Textfile
try {
var data = fs.readFileSync('dbAccess.txt', 'utf8');
console.log(data);
dbPW = data;
} catch(e) {
console.log('Error:', e.stack);
}
// First create a connection to the db
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: dbPW,
database: "projektarbeit"
});
con.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
var formatter = cli.getFormatter("json");
console.log("Linting Files with config: " + configUsed);
for( var i = 1; i <= maxnumber; i++) {
var targetDir = inResponse.items[i].name;
var lintReport = cli.executeOnFiles(["./clonedrepos/"+targetDir+"/."]);
console.log("Now Linting " + targetDir)
//console.log(lintReport);
console.log("Detections in this Lint run: " + lintReport.warningCount);
//Insert the report into the DB
var insertReport = {
report: lintReport,
location: targetDir,
count: lintReport.warningCount,
configUsed: configUsed,
timeOfLint: Math.round(+new Date()/1000)
};
con.query('INSERT INTO reports SET ?', insertReport, function(err,res){
if(err) throw err;
console.log('Last insert ID:', res.insertId);
});
}
//Close Connection
con.end(function(err) {
console.log("conn closed");
// The connection is terminated gracefully
});
}