|
| 1 | +import { readFile } from 'fs/promises'; |
1 | 2 | import { getGitHubMetrics } from "./get-github-metrics.js"; |
2 | 3 | import { addMetricsToAtlas } from "./write-to-db.js"; |
| 4 | +import { RepoDetails } from './RepoDetails.js'; // Import the RepoDetails class |
3 | 5 |
|
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. |
6 | 8 | 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 | +} |
9 | 14 | 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. */ |
10 | 15 |
|
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'); |
17 | 21 |
|
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); |
20 | 24 |
|
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 | + ); |
22 | 29 |
|
23 | | -const metricsDocs = []; |
| 30 | + const metricsDocs = []; |
24 | 31 |
|
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 | + } |
28 | 42 | } |
29 | 43 |
|
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