Skip to content

Commit

Permalink
Add a command line utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamstah committed Oct 18, 2023
1 parent dd3ad66 commit 7ab3198
Show file tree
Hide file tree
Showing 3 changed files with 642 additions and 340 deletions.
92 changes: 92 additions & 0 deletions bin/github-inactive-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// const github = require('@actions/github')
// , core = require('@actions/core')
const fs = require('fs')
, json2csv = require('json2csv')
, OrganizationActivity = require('../src/OrganizationUserActivity')
, githubClient = require('../src/github/githubClient')
, dateUtil = require('../src/dateUtil')
, yargs = require('yargs/yargs')
, { hideBin } = require('yargs/helpers');
;

async function run(organization, days, maxRetries, output, format, token) {
fromDate = dateUtil.convertDaysToDate(days);

const octokit = githubClient.create(token, maxRetries)
, orgActivity = new OrganizationActivity(octokit)
;

console.error(`Attempting to generate organization user activity data, this could take some time...`);
const userActivity = await orgActivity.getUserActivity(organization, fromDate);

if (format == 'json') {
writeOutput(JSON.stringify(userActivity.map(activity => activity.jsonPayload)), output)
return
}

// Convert the JavaScript objects into a JSON payload so it can be output
console.error(`User activity data captured, generating report...`);
const data = userActivity.map(activity => activity.jsonPayload)
, csv = json2csv.parse(data, {})
;

writeOutput(csv, output)
}

function writeOutput(data, output) {
if (output) {
fs.writeFileSync(output, data);
console.error(`User Activity Report Generated: ${output}`);
} else {
console.log(data);
console.error(`User Activity Report Generated`);
}
}

async function execute() {
const argv = yargs(hideBin(process.argv))
.command('$0 <organization>', 'Analyze user activity for a github organization', (yargs) => {
yargs.positional('organization', {
describe: 'Github organization to analyze',
type: 'string'
})
})
.option('days', {
alias: 'd',
description: 'How many days of recent activity to check',
type: 'number',
default: 180
})
.option('maxRetries', {
alias: 'r',
description: 'Maximum retries for the github API calls',
type: 'number',
default: 10
})
.option('token', {
alias: 't',
description: 'Github token. Will use the environment GH_TOKEN if not set.',
type: 'string',
demandOption : true
})
.option('output', {
alias: 'o',
description: 'Location to store the report. By default it will be printed to stdout.',
type: 'string'
})
.option('format', {
alias: 'f',
description: 'Format of the report',
type: 'string',
choices: ['json', 'csv'],
default: 'csv'
})
.help()
.alias('help', 'h')
.parse();

const token = argv.token ? argv.token : process.env.GH_TOKEN;

await run(argv.organization, argv.days, argv.maxRetries, argv.output, argv.format, token);
}
execute();
Loading

0 comments on commit 7ab3198

Please sign in to comment.