Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions github-metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,30 @@ This code is in the `get-github-metrics.js` file.

### Change repos to track

This project uses `RepoDetails` declared in the `index.js` file to track the owner and repo name for a given project
whose metrics we want to track.
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.

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

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.
To add a new repository, create a new entry in the `repo-details.json` file in the following format:

```json
{
"owner": "<repo-owner>",
"repo": "<repo-name>"
}
```

You can get the owner and name from the repo URL: `https://github.com/<owner>/<repo>`
For example, to add the MongoDB docs-notebooks repository, you'd add the following to the `repo-details.json`:

```json
{
"owner": "mongodb",
"repo": "docs-notebooks"
}
```

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.

### Write metrics to Atlas

Expand Down
8 changes: 8 additions & 0 deletions github-metrics/RepoDetails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This class is used to map the deserialized owner and repo name for a given repository to the corresponding `repo-details.json` config data.

export class RepoDetails {
constructor(owner, repo) {
this.owner = owner; // the GitHub organization or member who owns the repo
this.repo = repo; // the name of the repo within the organization or member
}
}
54 changes: 36 additions & 18 deletions github-metrics/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
import { readFile } from 'fs/promises';
import { getGitHubMetrics } from "./get-github-metrics.js";
import { addMetricsToAtlas } from "./write-to-db.js";
import { RepoDetails } from './RepoDetails.js'; // Import the RepoDetails class

/* To change which repos to track metrics for, update the `repos` array before running the utility.
To track metrics for a new repo, set the owner and name first.
/* To change which repos to track metrics for, update the `repo-details.json` file.
To track metrics for a new repo, add a new entry with the owner and repo name.
You can get the owner and name from the repo URL: `https://github.com/<owner>/<repo>`
For example, to add `https://github.com/mongodb/docs-notebooks`, set `mongodb` as the
owner and `docs-notebooks` as the repo name.
For example, to add `https://github.com/mongodb/docs-notebooks`, add:
{
"owner": "mongodb",
"repo": "docs-notebooks"
}
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. */

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

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

const repos = [docsNotebooksRepo, atlasArchitectureGoSdkRepo];
// Convert each repo object into an instance of RepoDetails
const repos = reposArray.map(
(repo) => new RepoDetails(repo.owner, repo.repo)
);

const metricsDocs = [];
const metricsDocs = [];

for (const repo of repos) {
const metricsDoc = await getGitHubMetrics(repo.owner, repo.repo);
metricsDocs.push(metricsDoc);
// Iterate through the repos array
for (const repo of repos) {
const metricsDoc = await getGitHubMetrics(repo.owner, repo.repo);
metricsDocs.push(metricsDoc);
}

await addMetricsToAtlas(metricsDocs);
} catch (error) {
console.error('Error processing repos:', error);
}
}

await addMetricsToAtlas(metricsDocs);
// Call the function
processRepos().catch(error => {
console.error('Fatal error:', error);
process.exit(1);
});
Loading