This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
111 lines (92 loc) · 3.42 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
const {
setInventoryStats,
addToGithubList,
getLogger,
getGithubStatsFromList,
cloneTopRepos,
writeOutData,
getAppVersion,
getCodeGovReleasesFile
} = require('./libs/utils')
const { getGithubReposDataByOwner, getUniqueGithubRepoOwners } = require('./libs/github-utils')
const path = require('path')
const dotenv = require('dotenv')
const JsonFile = require('jsonfile')
const app = require('commander')
dotenv.config()
const usageText = `code-gov-stats <cmd>
Commands:
inventory-stats [releasesFile] Calculate stats for all repositories in the Code.gov inventory.
Uses the passed releases file path.
Defaults to releases.json and tries to find it in the current directory
top-stats Calculate stats for our top repositories. Uses top-repos.json file found in the apps config folder.
`
function getInventoryStats (releasesFile, logger) {
logger.info('Starting to calculate Inventory Stats')
JsonFile.readFile(releasesFile, (error, codeGovReleases) => {
if (error) {
throw error
}
const releasesKeys = Object.keys(codeGovReleases.releases)
let stats = {
totalProjects: releasesKeys.length
}
let repoUrls = []
releasesKeys.forEach(key => {
const repo = codeGovReleases.releases[key]
setInventoryStats({ usageType: repo.permissions.usageType, repositoryUrl: repo.repositoryURL, stats, logger })
addToGithubList({ usageType: repo.permissions.usageType, repositoryUrl: repo.repositoryURL, githubRepos: repoUrls })
})
logger.info('Writing out stats file.')
logger.debug(`Stats: ${stats}`)
writeOutData(stats, 'stats.json')
const uniqueRepoOwners = getUniqueGithubRepoOwners(repoUrls)
getGithubReposDataByOwner({ repoOwnerList: uniqueRepoOwners, logger })
.then(values => {
writeOutData(values, 'inventory-github-data.json')
logger.info('Finished calculating Inventory Stats')
})
})
}
function getTopRepoStats (clone, logger) {
const filePath = path.join(__dirname, '/config/top-repos.json')
JsonFile.readFile(filePath, (error, topRepos) => {
logger.info('Starting to calculate Top Repos Stats')
if (error) {
logger.error(error)
}
const repoUrls = topRepos.map(repo => repo.url)
getGithubStatsFromList(repoUrls, logger)
.then(values => {
logger.info('Writing out Top Repo Github data.')
writeOutData(values, 'github-data-top-repos.json')
logger.info('Finished calculating Top Repos Stats')
})
if (clone) {
logger.info('Cloning Top Repos')
cloneTopRepos(topRepos)
logger.info('Finished cloning Top Repos')
}
})
}
app.version(getAppVersion())
.usage(usageText)
app.command('inventory-stats [releasesFile]')
.action(releasesFile => {
const logger = getLogger()
if (!releasesFile) {
const releasesFile = path.join(__dirname, 'data/releases.json')
const releasesUrl = 'https://raw.githubusercontent.com/GSA/code-gov-data/master/releases.json'
getCodeGovReleasesFile(releasesUrl, releasesFile, logger).then(() => {
getInventoryStats(releasesFile, logger)
})
} else {
getInventoryStats(releasesFile, logger)
}
})
app.command('top-stats')
.option('-c, --clone', 'Clone repositories from remote.')
.action((command) => {
getTopRepoStats(command.clone, getLogger())
})
app.parse(process.argv)