Skip to content

Commit b506c4c

Browse files
committed
Update metrics utility to use JSON pattern for repos
1 parent 4df6358 commit b506c4c

File tree

7 files changed

+291
-239
lines changed

7 files changed

+291
-239
lines changed

github-metrics/README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,30 @@ This code is in the `get-github-metrics.js` file.
3939
4040
### Change repos to track
4141

42-
This project uses `RepoDetails` declared in the `index.js` file to track the owner and repo name for a given project
43-
whose metrics we want to track.
42+
This project pulls the configuration data from [repo-details.json](repo-details.json) to track the owner and repo name for repositories whose metrics we want to track.
4443

45-
Declare as many `RepoDetails` as you need, and add them to the `repos` array in ln 19 of the `index.js` file. The code
46-
handles tracking and writing to Atlas for multiple repos.
44+
#### Add a new repository
4745

48-
Inserting a new repo automatically creates a new corresponding collection in Atlas. You will need to manually create
49-
Charts to correspond to the new collection.
46+
To add a new repository, create a new entry in the `repo-details.json` file in the following format:
47+
48+
```json
49+
{
50+
"owner": "<repo-owner>",
51+
"repo": "<repo-name>"
52+
}
53+
```
54+
55+
You can get the owner and name from the repo URL: `https://github.com/<owner>/<repo>`
56+
For example, to add the MongoDB docs-notebooks repository, you'd add the following to the `repo-details.json`:
57+
58+
```json
59+
{
60+
"owner": "mongodb",
61+
"repo": "docs-notebooks"
62+
}
63+
```
64+
65+
The code handles tracking and writing to Atlas for multiple repos. Inserting a new repo automatically creates a new corresponding collection in Atlas. You will need to manually create Charts to correspond to the new collection.
5066

5167
### Write metrics to Atlas
5268

github-metrics/RepoDetails.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This class is used to map the deserialized owner and repo name for a given repository to the corresponding `repo-details.json` config data.
2+
3+
export class RepoDetails {
4+
constructor(owner, repo) {
5+
this.owner = owner; // the GitHub organization or member who owns the repo
6+
this.repo = repo; // the name of the repo within the organization or member
7+
}
8+
}

github-metrics/index.js

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,48 @@
1+
import { readFile } from 'fs/promises';
12
import { getGitHubMetrics } from "./get-github-metrics.js";
23
import { addMetricsToAtlas } from "./write-to-db.js";
4+
import { RepoDetails } from './RepoDetails.js'; // Import the RepoDetails class
35

4-
/* To change which repos to track metrics for, update the `repos` array before running the utility.
5-
To track metrics for a new repo, set the owner and name first.
6+
/* To change which repos to track metrics for, update the `repo-details.json` file.
7+
To track metrics for a new repo, add a new entry with the owner and repo name.
68
You can get the owner and name from the repo URL: `https://github.com/<owner>/<repo>`
7-
For example, to add `https://github.com/mongodb/docs-notebooks`, set `mongodb` as the
8-
owner and `docs-notebooks` as the repo name.
9+
For example, to add `https://github.com/mongodb/docs-notebooks`, add:
10+
{
11+
"owner": "mongodb",
12+
"repo": "docs-notebooks"
13+
}
914
NOTE: The GitHub token used to retrieve the info from a repo MUST have repo admin permissions to access all the endpoints in this code. */
1015

11-
class RepoDetails {
12-
constructor(owner, repo) {
13-
this.owner = owner; // the GitHub organization or member who owns the repo
14-
this.repo = repo; // the name of the repo within the organization or member
15-
}
16-
}
16+
// processRepos reads the JSON config file and iterates through the repos specified, converting each to an instance of the RepoDetails class.
17+
async function processRepos() {
18+
try {
19+
// Read the JSON file
20+
const data = await readFile('repo-details.json', 'utf8');
1721

18-
const docsNotebooksRepo = new RepoDetails("mongodb", "docs-notebooks");
19-
const atlasArchitectureGoSdkRepo = new RepoDetails("mongodb", "atlas-architecture-go-sdk");
22+
// Parse the JSON data into an array
23+
const reposArray = JSON.parse(data);
2024

21-
const repos = [docsNotebooksRepo, atlasArchitectureGoSdkRepo];
25+
// Convert each repo object into an instance of RepoDetails
26+
const repos = reposArray.map(
27+
(repo) => new RepoDetails(repo.owner, repo.repo)
28+
);
2229

23-
const metricsDocs = [];
30+
const metricsDocs = [];
2431

25-
for (const repo of repos) {
26-
const metricsDoc = await getGitHubMetrics(repo.owner, repo.repo);
27-
metricsDocs.push(metricsDoc);
32+
// Iterate through the repos array
33+
for (const repo of repos) {
34+
const metricsDoc = await getGitHubMetrics(repo.owner, repo.repo);
35+
metricsDocs.push(metricsDoc);
36+
}
37+
38+
await addMetricsToAtlas(metricsDocs);
39+
} catch (error) {
40+
console.error('Error processing repos:', error);
41+
}
2842
}
2943

30-
await addMetricsToAtlas(metricsDocs);
44+
// Call the function
45+
processRepos().catch(error => {
46+
console.error('Fatal error:', error);
47+
process.exit(1);
48+
});

0 commit comments

Comments
 (0)