-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampleDownloader.js
More file actions
268 lines (206 loc) · 6.89 KB
/
sampleDownloader.js
File metadata and controls
268 lines (206 loc) · 6.89 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//created for 27.4.17
"use strict"
//var Preferences = require('preferences');
//var _ = require('lodash');
//var GitHubApi = require('github');
//var exec = require('exec')
//var touch = require('touch');
//var CLI = require('clui');
//var git = require('simple-git')();
var fs = require('fs');
var Git = require('nodegit');
var inquirer = require('inquirer');
var figlet = require('figlet');
var clear = require('clear');
var chalk = require('chalk');
var GitHub = require('github-api');
var ghdownload = require('github-download')
var maxnumber = 10;
var configUsed = ".eslintrc.js";
var inputDate = "test-";
var fullsearchString = "";
var GITHUB_TOKEN = "ThisWillBeYourGitHubToken";
var dbPW = "ThisWillBeYourDBPW";
clear();
console.log(
chalk.yellow(
figlet.textSync('sampler', { horizontalLayout: 'full' })
)
);
getInput(function(){
console.log("Parameters: ")
});
function getInput(callback) {
var argv = require('minimist')(process.argv.slice(2));
var questions = [
{
type: 'input',
name: 'sdate',
default: "2015-01-01",
message: 'Enter a sampling starting date YYYY-MM-DD'
},
{
type: 'input',
name: 'edate',
default: "2015-02-01",
message: 'Enter a sampling ending date YYYY-MM-DD'
},
{
type: 'input',
name: 'number',
default: "10",
message: 'How many to download beginning at starting date'
},
{
type: 'input',
name: 'watchers',
message: 'Enter an interval of stars / watchers',
default: "0..500"
},
{
type: 'input',
name: 'size',
message: 'Enter a maximum size',
default: "5000"
},
{
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=language:javascript+pushed:>='+answers.sdate+'+stars:'+answers.watchers+'+size:<'+answers.size+'&sort=updated&order=asc', true);
// Fire away!
fullsearchString = 'https://api.github.com/search/repositories?q=language:javascript+pushed:>='+answers.sdate+'+stars:'+answers.watchers+'+size:<'+answers.size+'&sort=updated&order=asc'
console.log("fired Request");
request.send();
//Set Answers globally for later use
maxnumber = answers.number;
configUsed = answers.configFile;
inputDate = answers.edate;
});
}
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);
console.log("Date: "+ responseObj.items[i].created_at);
//Programmatic Filtering Date
console.log("targetDate: " + inputDate + " responseValue: " + responseObj.items[i].pushed_at);
//Prepare the Date Values
var responseValue = responseObj.items[i].pushed_at + '';
var reponseCut = responseValue.substring(0,10);
var repoValueSplit = reponseCut.split('-');
var targetValue = inputDate.split('-');
console.log("targetValue: " + targetValue + " repoValueSplit: " + repoValueSplit);
var targetDate=new Date();
targetDate.setFullYear(targetValue[0],(targetValue[1]),targetValue[2]);
var repoDate=new Date();
console.log("RepoDateValues: " + repoValueSplit[0])
console.log("RepoDateValues: " + repoValueSplit[1])
console.log("RepoDateValues: " + repoValueSplit[2])
repoDate.setFullYear(repoValueSplit[0],(repoValueSplit[1]),repoValueSplit[2]);
console.log("Desired: " + targetDate + " Actual Value: " + repoDate);
if (repoDate > targetDate)
{
console.log("Repo isnt from the same Month");
}
else
{
console.log("Repo fits Month, Downloading repo nr" + i);
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",
connectTimeout: 30000
});
con.connect(function(err){
if(err){
console.log('Error connecting to Db // Connection Failed');
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 fullpath = inResponse.items[i].full_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: fullpath,
count: lintReport.warningCount,
configUsed: configUsed,
timeOfLint: Math.round(+new Date()/1000),
fullString: fullsearchString,
};
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
});
}