github-stats/index.js

101 lines
2.2 KiB
JavaScript

'use strict'
require('dotenv').config()
const { graphql } = require('@octokit/graphql')
if (process.argv.length !== 4) {
console.error('Bad command line (node command owner name)')
process.exit(0)
}
const owner = process.argv[2]
const name = process.argv[3]
const graphqlWithAuth = graphql.defaults({
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`
},
});
async function run () {
const closed = await countIssuesAndPRs('CLOSED')
const opened = await countIssuesAndPRs('OPEN')
const repoStats = await repositoryStats()
const result = {
closedIssues: closed.repository.issues.totalCount,
closedPullRequests: closed.repository.pullRequests.totalCount,
openedIssues: opened.repository.issues.totalCount,
openedPullRequests: opened.repository.pullRequests.totalCount,
collaborators: repoStats.repository.collaborators.totalCount,
commitComments: repoStats.repository.commitComments.totalCount,
stargazers: repoStats.repository.stargazerCount,
watchers: repoStats.repository.watchers.totalCount,
releases: repoStats.repository.releases.totalCount,
collaborators: repoStats.repository.collaborators.totalCount,
commits: repoStats.repository.object.history.totalCount,
forkCount: repoStats.repository.forkCount
}
console.log(JSON.stringify(result))
}
run()
.catch(err => {
console.error(err)
})
function repositoryStats () {
return graphqlWithAuth(`
{
repository(owner: "${owner}", name: "${name}") {
forkCount
stargazerCount
watchers {
totalCount
}
releases {
totalCount
}
commitComments {
totalCount
}
collaborators {
totalCount
}
object(expression:"develop") {
... on Commit {
history {
totalCount
}
}
}
}
}
`)
}
function countIssuesAndPRs (state) {
return graphqlWithAuth(`
{
repository(owner: "${owner}", name: "${name}") {
issues (states: [ ${state} ]) {
totalCount
}
pullRequests (states: [ ${state} ]) {
totalCount
}
}
}
`)
}