generated from scottluskcis/ts-node-project-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copilot-associations-report.ts
294 lines (258 loc) · 8.09 KB
/
copilot-associations-report.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { RequestError } from "octokit";
import { generateCopilotAssociationsData } from "../data/copilot-associations-data";
import { AppConfig } from "../shared/app-config";
import logger from "../shared/app-logger";
import {
readJsonFile,
writeToCsv,
writeToFileSync,
} from "../shared/file-utils";
export interface CopilotAssociationsData {
copilot_seats: { assignee: string; last_activity_at: string }[];
teams: {
[team_name: string]: {
team_name: string;
members: string[];
copilot_users: string[];
};
};
repositories: {
[repo_name: string]: {
repo_owner: string;
repo_name: string;
contributors: string[];
associated_copilot_users: string[];
};
};
}
export async function runCopilotAssociationsReport({
should_generate_data = true,
input_file_name = "copilot-associations.json",
output_file_name = "copilot_associations.csv",
detailed_output_file_name = "copilot_associations_detailed.csv",
}: {
should_generate_data?: boolean;
input_file_name?: string;
output_file_name?: string;
detailed_output_file_name?: string;
}): Promise<string | undefined> {
try {
if (should_generate_data) {
logger.debug("Generating copilot associations data...");
await generateData(input_file_name);
}
logger.debug("Running copilot associations report...");
const output_file = runReport(
input_file_name,
output_file_name,
detailed_output_file_name
);
logger.info(`Generated copilot associations report ${output_file}`);
return output_file;
} catch (error) {
// octokit RequestError, log details for debugging
if (error instanceof RequestError) {
logger.error("RequestError - message:", error.message);
logger.debug("RequestError - status:", error.status);
logger.debug("RequestError - request:", error.request);
logger.debug("RequestError - response:", error.response);
}
// log
logger.fatal(error);
return undefined;
}
}
async function generateData(file_name: string): Promise<string> {
logger.debug("Generating copilot associations data...");
const data: CopilotAssociationsData = await generateCopilotAssociationsData({
org: AppConfig.ORGANIZATION,
per_page: AppConfig.PER_PAGE,
time_period: AppConfig.TIME_PERIOD,
});
logger.info("Generated copilot associations data");
logger.debug("Writing copilot associations data to file...");
const file = writeToFileSync(data, file_name);
logger.info(`Wrote copilot associations data to file ${file}`);
return file;
}
function runReport(
input_file_name: string,
output_file_name: string,
detailed_output_file_name: string
): string | undefined {
const data = readJsonFile<CopilotAssociationsData>(input_file_name);
if (!data) {
console.error("Data not found, exiting...");
return undefined;
}
const associations = getCopilotAssociations(data);
const csv_file = writeToCsv(associations, output_file_name);
const detailed_associations = getDetailedCopilotAssociations(data);
writeToCsv(detailed_associations, detailed_output_file_name);
return csv_file;
}
function getCopilotAssociations(data: CopilotAssociationsData) {
const member_associations = processAssociations(data);
return Object.keys(member_associations).map((member) => {
const associations = member_associations[member];
const count_teams = associations.teams.size;
const count_repos = associations.repos.size;
const count_copilot_users = associations.copilot_users.size;
const total_sum = count_teams + count_repos + count_copilot_users;
return {
member_name: member,
count_teams,
count_repos,
count_copilot_users,
total_sum,
};
});
}
function getDetailedCopilotAssociations(data: CopilotAssociationsData) {
const member_associations = processAssociations(data);
const detailed_associations: {
member_name: string;
association_type: string;
association_name: string;
copilot_user: string;
}[] = [];
for (const member in member_associations) {
const associations = member_associations[member];
for (const team of associations.teams) {
const theTeam = data.teams[team];
if (!theTeam) {
logger.warn(`Team ${team} not found in data.teams, skipping...`);
continue;
}
if (!theTeam.copilot_users) {
logger.warn(`Copilot users not found for team ${team}, skipping...`);
continue;
}
for (const copilot_user of theTeam.copilot_users) {
detailed_associations.push({
member_name: member,
association_type: "team",
association_name: team,
copilot_user: copilot_user,
});
}
}
for (const repo of associations.repos) {
const theRepo = data.repositories[repo];
if (!theRepo) {
logger.warn(`Repo ${repo} not found in data.repositories, skipping...`);
continue;
}
if (!theRepo.associated_copilot_users) {
logger.warn(
`Associated copilot users not found for repo ${repo}, skipping...`
);
continue;
}
for (const copilot_user of theRepo.associated_copilot_users) {
detailed_associations.push({
member_name: member,
association_type: "repository",
association_name: repo,
copilot_user: copilot_user,
});
}
}
}
return detailed_associations;
}
function processAssociations(data: CopilotAssociationsData) {
const member_associations: {
[member: string]: {
teams: Set<string>;
repos: Set<string>;
copilot_users: Set<string>;
};
} = {};
processTeamAssociations(data, member_associations);
processRepositoryAssociations(data, member_associations);
return member_associations;
}
function processTeamAssociations(
data: CopilotAssociationsData,
member_associations: {
[member: string]: {
teams: Set<string>;
repos: Set<string>;
copilot_users: Set<string>;
};
}
) {
for (const team_name in data.teams) {
if (AppConfig.EXCLUDE_TEAMS.includes(team_name.toLowerCase())) {
continue;
}
const team = data.teams[team_name];
const copilot_users = new Set(team.copilot_users);
if (copilot_users.size === 0) {
logger.debug(
`No copilot users found for team ${team.team_name}, ignoring for report...`
);
continue;
}
for (const member of team.members) {
if (copilot_users.has(member)) {
logger.debug(
`Found copilot user ${member} in team ${team.team_name}, ignoring for report...`
);
continue;
}
if (!member_associations[member]) {
member_associations[member] = {
teams: new Set(),
repos: new Set(),
copilot_users: new Set(),
};
}
member_associations[member].teams.add(team.team_name);
copilot_users.forEach((user) =>
member_associations[member].copilot_users.add(user)
);
}
}
}
function processRepositoryAssociations(
data: CopilotAssociationsData,
member_associations: {
[member: string]: {
teams: Set<string>;
repos: Set<string>;
copilot_users: Set<string>;
};
}
) {
for (const repo_name in data.repositories) {
const repo = data.repositories[repo_name];
const copilot_users = new Set(repo.associated_copilot_users);
if (copilot_users.size === 0) {
logger.debug(
`No copilot users found for repo ${repo.repo_name}, ignoring for report...`
);
continue;
}
for (const contributor of repo.contributors) {
if (copilot_users.has(contributor)) {
logger.debug(
`Found copilot user ${contributor} in repo ${repo.repo_name}, ignoring for report...`
);
continue;
}
if (!member_associations[contributor]) {
member_associations[contributor] = {
teams: new Set(),
repos: new Set(),
copilot_users: new Set(),
};
}
member_associations[contributor].repos.add(repo.repo_name);
copilot_users.forEach((user) =>
member_associations[contributor].copilot_users.add(user)
);
}
}
}