forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregateGitHubDataFromUpwork.ts
175 lines (160 loc) · 5.59 KB
/
aggregateGitHubDataFromUpwork.ts
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* This script is used for categorizing upwork costs into cost buckets for accounting purposes.
*
* To run this script from the root of E/App:
*
* ts-node ./scripts/aggregateGitHubDataFromUpwork.js <path_to_csv> <github_pat> <output_path>
*
* The input file must be a CSV with a single column containing just the GitHub issue number. The CSV must have a single header row.
*/
import {getOctokitOptions, GitHub} from '@actions/github/lib/utils';
import {paginateRest} from '@octokit/plugin-paginate-rest';
import {throttling} from '@octokit/plugin-throttling';
import {createObjectCsvWriter} from 'csv-writer';
import fs from 'fs';
type OctokitOptions = {method: string; url: string; request: {retryCount: number}};
type IssueType = 'bug' | 'feature' | 'other';
if (process.argv.length < 3) {
throw new Error('Error: must provide filepath for CSV data');
}
if (process.argv.length < 4) {
throw new Error('Error: must provide GitHub token');
}
if (process.argv.length < 5) {
throw new Error('Error: must provide output file path');
}
// Get filepath for csv
const inputFilepath = process.argv.at(2);
if (!inputFilepath) {
throw new Error('Error: must provide filepath for CSV data');
}
// Get GitHub token
const token = (process.argv.at(3) ?? '').trim();
if (!token) {
throw new Error('Error: must provide GitHub token');
}
const Octokit = GitHub.plugin(throttling, paginateRest);
const octokit = new Octokit(
getOctokitOptions(token, {
throttle: {
onRateLimit: (retryAfter: number, options: OctokitOptions) => {
console.warn(`Request quota exhausted for request ${options.method} ${options.url}`);
// Retry once after hitting a rate limit error, then give up
if (options.request.retryCount <= 1) {
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onAbuseLimit: (retryAfter: number, options: OctokitOptions) => {
// does not retry, only logs a warning
console.warn(`Abuse detected for request ${options.method} ${options.url}`);
},
},
}),
);
// Get output filepath
const outputFilepath = process.argv.at(4);
if (!outputFilepath) {
throw new Error('Error: must provide output file path');
}
// Get data from csv
const issues = fs
.readFileSync(inputFilepath)
.toString()
.split('\n')
.reduce((acc, issue) => {
if (!issue) {
return acc;
}
const issueNum = Number(issue.trim());
if (!issueNum) {
return acc;
}
acc.push(issueNum);
return acc;
}, [] as number[]);
const csvWriter = createObjectCsvWriter({
path: outputFilepath,
header: [
{id: 'number', title: 'number'},
{id: 'title', title: 'title'},
{id: 'labels', title: 'labels'},
{id: 'type', title: 'type'},
{id: 'capSWProjects', title: 'capSWProjects'},
],
});
function getIssueTypeFromLabels(labels: string[]): IssueType {
if (labels.includes('NewFeature')) {
return 'feature';
}
if (labels.includes('Bug')) {
return 'bug';
}
return 'other';
}
/**
* Returns a comma-delimited string with all projects associated with the given issue.
*/
async function getProjectsForIssue(issueNumber: number): Promise<string> {
const response = await octokit.graphql(
`
{
repository(owner: "Expensify", name: "App") {
issue(number: ${issueNumber}) {
projectsV2(last: 30) {
nodes {
title
}
}
}
}
}
`,
);
return (response as {repository: {issue: {projectsV2: {nodes: Array<{title: string}>}}}}).repository.issue.projectsV2.nodes.map((node) => node.title).join(',');
}
async function getGitHubData() {
const gitHubData = [];
// Note: we fetch issues in a loop rather than in parallel to help address rate limiting issues with a PAT
for (const issueNumber of issues) {
console.info(`Fetching ${issueNumber}`);
const result = await octokit.rest.issues
.get({
owner: 'Expensify',
repo: 'App',
// eslint-disable-next-line @typescript-eslint/naming-convention
issue_number: issueNumber,
})
.catch(() => {
console.warn(`Error getting issue ${issueNumber}`);
});
if (result) {
const issue = result.data;
const labels = issue.labels.reduce((acc, label) => {
if (typeof label === 'string') {
acc.push(label);
} else if (label.name) {
acc.push(label.name);
}
return acc;
}, [] as string[]);
const type = getIssueTypeFromLabels(labels);
let capSWProjects = '';
if (type === 'feature') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
capSWProjects = await getProjectsForIssue(issueNumber);
}
gitHubData.push({
number: issue.number,
title: issue.title,
labels,
type,
capSWProjects,
});
}
}
return gitHubData;
}
getGitHubData()
.then((gitHubData) => csvWriter.writeRecords(gitHubData))
.then(() => console.info(`Done ✅ Wrote file to ${outputFilepath}`));