Skip to content

Commit 871bcbf

Browse files
authored
Improve contributor docs refs count script (#17)
The script to count the number of references to the contributor-docs throughout comments in MetaMask repositories has never provided accurate counts. Upon reviewing this script, I noticed that while comments posted directly to pull requests were included in the count, comments posted as a part of reviews, which are separate types in the GitHub GraphQL API, were excluded. This resulted in a smaller count than expected. In addition, the cutoff for data that the script used — 5000 maximum comments per repo — hasn't been particularly useful. We need to define _some_ kind of cutoff because it would be impractical to pull all comments from all time, but since we use this script to gauge progress toward OKRs, it helps us better to define a fixed target, in this case 4/1/2023, the start of the first quarter where the Shared Libraries team began working on the contributor docs. In addition, the output of this script now breaks down the counts per quarter instead of giving a lump sum. Here is an example: ``` About to retrieve data. Please be patient, this could take a while: ✔ Retrieving data for core since 2023-04-01T00:00:00.000Z ✔ Retrieving data for design-tokens since 2023-04-01T00:00:00.000Z ✔ Retrieving data for metamask-extension since 2023-04-01T00:00:00.000Z ✔ Retrieving data for metamask-mobile since 2023-04-01T00:00:00.000Z ✔ Retrieving data for snaps since 2023-04-01T00:00:00.000Z ---------------------- Number of references to contributor-docs by repository: - core - Q2-2023: 0 - Q3-2023: 0 - Q4-2023: 2 - Q1-2024: 1 - design-tokens - Q2-2023: 0 - Q3-2023: 0 - Q4-2023: 0 - Q1-2024: 0 - metamask-extension - Q2-2023: 0 - Q3-2023: 0 - Q4-2023: 0 - Q1-2024: 2 - metamask-mobile - Q2-2023: 0 - Q3-2023: 0 - Q4-2023: 0 - Q1-2024: 4 - snaps - Q2-2023: 0 - Q3-2023: 0 - Q4-2023: 0 - Q1-2024: 0 Total number of references: 9 ```
1 parent 0332621 commit 871bcbf

File tree

3 files changed

+222
-133
lines changed

3 files changed

+222
-133
lines changed

src/github/pull-request-comments-query.ts

+42-8
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,26 @@ export type GitHubComment = {
1919
};
2020
};
2121
body: string;
22+
createdAt: string;
2223
updatedAt: string;
2324
};
2425

25-
type GitHubPullRequest = {
26+
type GitHubReview = {
27+
id: string;
28+
comments: {
29+
nodes: GitHubComment[];
30+
};
31+
};
32+
33+
export type GitHubPullRequest = {
2634
number: number;
2735
comments: {
2836
nodes: GitHubComment[];
29-
pageInfo: GitHubPageInfo;
3037
};
38+
reviews: {
39+
nodes: GitHubReview[];
40+
};
41+
createdAt: string;
3142
updatedAt: string;
3243
};
3344

@@ -63,6 +74,8 @@ const QUERY = `
6374
pullRequests(first: 100, orderBy: { field: UPDATED_AT, direction: DESC }, after: $after) {
6475
nodes {
6576
number
77+
createdAt
78+
updatedAt
6679
comments(first: 100, orderBy: { field: UPDATED_AT, direction: DESC }) {
6780
nodes {
6881
author {
@@ -75,10 +88,31 @@ const QUERY = `
7588
}
7689
}
7790
body
91+
createdAt
7892
updatedAt
7993
}
8094
}
81-
updatedAt
95+
reviews(first: 40) {
96+
nodes {
97+
id
98+
comments(first: 100) {
99+
nodes {
100+
author {
101+
__typename
102+
login
103+
...on User {
104+
metamaskOrganization: organization(login: "MetaMask") {
105+
login
106+
}
107+
}
108+
}
109+
body
110+
createdAt
111+
updatedAt
112+
}
113+
}
114+
}
115+
}
82116
}
83117
pageInfo {
84118
endCursor
@@ -96,10 +130,10 @@ const QUERY = `
96130
`;
97131

98132
/**
99-
* When requesting a collection of resources, the GitHub API returns a maximum
100-
* of 100 at a time. This function is used to retrieve a "page" of the most
101-
* recent comments made on pull requests submitted for the given MetaMask
102-
* repository.
133+
* Executes a GraphQL query to retrieve the most recent comments posted on pull
134+
* requests submitted for the given MetaMask repository, ordered from latest to
135+
* earliest. A cursor may be provided to retrieve a particular "page" of pull
136+
* requests.
103137
*
104138
* @param args - The arguments.
105139
* @param args.repositoryName - The name of the repository.
@@ -114,7 +148,7 @@ export async function makeGitHubPullRequestCommentsQuery({
114148
after: string | undefined;
115149
}): Promise<GitHubPullRequestCommentsQueryResponse> {
116150
log(
117-
`Fetching pull requests for ${repositoryName}${
151+
`Executing GraphQL query to fetch pull requests for ${repositoryName}${
118152
after ? ` (after: ${after})` : ''
119153
}`,
120154
);
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
import ora from 'ora';
22

3-
import { countCommentsWithReferencesToContributorDocs } from './utils';
3+
import { tallyCommentsWithReferencesToContributorDocsByQuarter } from './utils';
4+
import type { TalliedQuarter } from './utils';
5+
import { log } from '../../logging-utils';
46

5-
const INTERESTING_REPOSITORY_NAMES = [
7+
/**
8+
* The repositories we want to scan.
9+
*/
10+
const REPOSITORY_NAMES = [
611
'core',
712
'design-tokens',
813
'metamask-extension',
914
'metamask-mobile',
1015
'snaps',
11-
];
16+
] as const;
17+
18+
type RepositoryName = typeof REPOSITORY_NAMES[number];
1219

13-
const MAX_NUMBER_OF_COMMENTS_PER_REPO = 5000;
20+
/**
21+
* It is not necessary for us to query all of the pull requests or pull requests
22+
* comments for all time; we only need those since at least Q2 2023, which is
23+
* when the Shared Libraries decided to start working on the contributor docs.
24+
*/
25+
const START_DATE = new Date(Date.UTC(2023, 3, 1));
1426

1527
main().catch(console.error);
1628

1729
/**
1830
* This script counts the number of references to the `contributor-docs` repo
19-
* across the primary repositories used by the MetaMask Core lane.
31+
* across a selection of MetaMask repositories.
2032
*/
2133
async function main() {
2234
const spinner = ora();
@@ -25,23 +37,46 @@ async function main() {
2537
'About to retrieve data. Please be patient, this could take a while:\n',
2638
);
2739

28-
let total = 0;
29-
// Can't do this in parallel or else we get rate limits
30-
for (const repositoryName of INTERESTING_REPOSITORY_NAMES) {
31-
spinner.start(`Retrieving data for ${repositoryName}`);
40+
const quartersByRepositoryName: Partial<
41+
Record<RepositoryName, TalliedQuarter[]>
42+
> = {};
43+
// NOTE: We can't do this in parallel or else we'll get rate limits.
44+
for (const repositoryName of REPOSITORY_NAMES) {
45+
spinner.start(
46+
`Retrieving data for ${repositoryName} since ${START_DATE.toISOString()}`,
47+
);
48+
log('');
3249
try {
33-
total += await countCommentsWithReferencesToContributorDocs({
34-
repositoryName,
35-
sampleSize: MAX_NUMBER_OF_COMMENTS_PER_REPO,
36-
});
50+
const quarters =
51+
await tallyCommentsWithReferencesToContributorDocsByQuarter({
52+
repositoryName,
53+
since: START_DATE,
54+
});
55+
quartersByRepositoryName[repositoryName] = quarters;
3756
spinner.succeed();
3857
} catch (error) {
3958
spinner.fail();
4059
throw error;
4160
}
4261
}
4362

44-
console.log(
45-
`\nNumber of comments with references to contributor documentation: ${total}`,
63+
const grandTotal = Object.values(quartersByRepositoryName).reduce(
64+
(sum1, quarters) => {
65+
return sum1 + quarters.reduce((sum2, quarter) => sum2 + quarter.total, 0);
66+
},
67+
0,
4668
);
69+
70+
console.log('\n----------------------\n');
71+
console.log('Number of references to contributor-docs by repository:\n');
72+
for (const [repositoryName, quarters] of Object.entries(
73+
quartersByRepositoryName,
74+
)) {
75+
console.log(`- ${repositoryName}`);
76+
77+
for (const quarter of quarters) {
78+
console.log(` - ${quarter.name}: ${quarter.total}`);
79+
}
80+
}
81+
console.log(`\nTotal number of references: ${grandTotal}`);
4782
}

0 commit comments

Comments
 (0)