-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (57 loc) · 1.83 KB
/
index.js
File metadata and controls
69 lines (57 loc) · 1.83 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
const url = require('url');
const Table = require('cli-table');
const _ = require('lodash');
const axios = require('axios');
const argv = require('yargs').argv;
const endpoint = 'https://api.bitbucket.org/';
const codeSearch = `2.0/teams/${argv.team}/search/code`;
const CSV = 'csv';
const TABLE = 'table';
const JSON = 'json';
const head = ['Project', 'No. of occurrences'];
async function getData(query, page = 0, existingData = []) {
try {
const { data } = await axios.get(
url.resolve(endpoint, codeSearch),
{
auth: {
username: argv.user,
password: argv.pass,
},
params: {
search_query: (query),
...((() => { return page > 0 ? { page } : {};})()),
},
});
const updatedData = Array.prototype.concat(data.values, existingData);
if(data.next) {
return getData(query, page + 1, updatedData);
} else {
const updates = _.groupBy(updatedData.map((el) => el
.file
.links
.self
.href
.match(/^.*repositories\/[^\/]*\/([^\/]*)/)[1])
);
const data = _.map(updates, (v,k) => [k, v.length]);
if(argv.output === CSV) {
console.log(_.map([head, ...data], (el) => el.join(',')).join('\n'));
} else if (argv.output === TABLE){
const occurenceTable = new Table({head , colWidths: [50, 25]});
occurenceTable.push(...data)
console.log(occurenceTable.toString());
} else {
console.log(JSON.stringify(_.map(data, ([proj, num]) => ({
[head[0].toLowerCase()]: proj,
[head[1].toLowerCase().replace(/[^a-z]+/g, '_')]: num,
}))));
}
return data;
}
} catch (err) {
console.log(err.response);
return null;
}
}
getData(argv.query);