-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
77 lines (65 loc) · 2.38 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
// const github = require('@actions/github')
// , core = require('@actions/core')
const fs = require('fs')
, path = require('path')
, core = require('@actions/core')
, io = require('@actions/io')
, json2csv = require('json2csv')
, OrganizationActivity = require('./src/OrganizationUserActivity')
, githubClient = require('./src/github/githubClient')
, dateUtil = require('./src/dateUtil')
;
async function run() {
const since = core.getInput('since')
, days = core.getInput('activity_days')
, token = getRequiredInput('token')
, outputDir = getRequiredInput('outputDir')
, organization = getRequiredInput('organization')
, maxRetries = getRequiredInput('octokit_max_retries')
;
let fromDate;
if (since) {
console.log(`Since Date has been specified, using that instead of active_days`)
fromDate = dateUtil.getFromDate(since);
} else {
fromDate = dateUtil.convertDaysToDate(days);
}
// Ensure that the output directory exists before we our limited API usage
await io.mkdirP(outputDir)
const octokit = githubClient.create(token, maxRetries)
, orgActivity = new OrganizationActivity(octokit)
;
console.log(`Attempting to generate organization user activity data, this could take some time...`);
const userActivity = await orgActivity.getUserActivity(organization, fromDate);
saveIntermediateData(outputDir, userActivity.map(activity => activity.jsonPayload));
// Convert the JavaScript objects into a JSON payload so it can be output
console.log(`User activity data captured, generating report...`);
const data = userActivity.map(activity => activity.jsonPayload)
, csv = json2csv.parse(data, {})
;
const file = path.join(outputDir, 'organization_user_activity.csv');
fs.writeFileSync(file, csv);
console.log(`User Activity Report Generated: ${file}`);
// Expose the output csv file
core.setOutput('report_csv', file);
}
async function execute() {
try {
await run();
} catch (err) {
core.setFailed(err.message);
}
}
execute();
function getRequiredInput(name) {
return core.getInput(name, {required: true});
}
function saveIntermediateData(directory, data) {
try {
const file = path.join(directory, 'organization_user_activity.json');
fs.writeFileSync(file, JSON.stringify(data));
core.setOutput('report_json', file);
} catch (err) {
console.error(`Failed to save intermediate data: ${err}`);
}
}