Skip to content

Commit

Permalink
Extract data from each GitHub API call into separate DB keyed value
Browse files Browse the repository at this point in the history
This will make it easier to update individual pieces of data in the
future, such as via a webhook.
  • Loading branch information
danlivings-dxw committed Oct 1, 2024
1 parent f2d7d38 commit 975f99f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
Binary file modified e2es/testData/towtruck.db
Binary file not shown.
11 changes: 8 additions & 3 deletions utils/githubApi/fetchAllRepos.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const fetchAllRepos = async () => {
}),
]).then(([dependencies, prInfo, issueInfo , alerts]) => {
repo.dependencies = dependencies;
repo = { ...repo, ...prInfo, ...issueInfo, ...alerts };
repo = { repo, prInfo, issueInfo, alerts };
});

repos.push(repo);
Expand All @@ -64,8 +64,13 @@ const saveAllRepos = async () => {
console.info("Saving all repos...");
const saveAllRepos = db.transaction((repos) => {
repos.forEach((repo) => {
db.saveToRepository(repo.name, "main", repo);
db.saveToRepository(repo.name, "owner", repo.owner);
const name = repo.repo.name;
const owner = repo.repo.owner;
db.saveToRepository(name, "main", repo.repo);
db.saveToRepository(name, "owner", owner);
db.saveToRepository(name, "pullRequests", repo.prInfo);
db.saveToRepository(name, "issues", repo.issueInfo);
db.saveToRepository(name, "dependabotAlerts", repo.alerts);
});
});

Expand Down
19 changes: 11 additions & 8 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,29 @@ export const mapRepoFromStorageToUi = (persistedData, persistedLifetimes) => {
const newDate = new Date(repo.main.updatedAt).toLocaleDateString();
const dependencies = repo.main.dependencies.map((dependency) => mapDependencyFromStorageToUi(dependency, persistedLifetimes));

const mostRecentPrOpenedAt = repo.main.mostRecentPrOpenedAt && formatDistanceToNow(repo.main.mostRecentPrOpenedAt, { addSuffix: true });
const oldestOpenPrOpenedAt = repo.main.oldestOpenPrOpenedAt && formatDistanceToNow(repo.main.oldestOpenPrOpenedAt, { addSuffix: true });
const mostRecentIssueOpenedAt = repo.main.mostRecentIssueOpenedAt && formatDistanceToNow(repo.main.mostRecentIssueOpenedAt, { addSuffix: true });
const oldestOpenIssueOpenedAt = repo.main.oldestOpenIssueOpenedAt && formatDistanceToNow(repo.main.oldestOpenIssueOpenedAt, { addSuffix: true });
const mostRecentPrOpenedAt = repo.pullRequests.mostRecentPrOpenedAt && formatDistanceToNow(repo.pullRequests.mostRecentPrOpenedAt, { addSuffix: true });
const oldestOpenPrOpenedAt = repo.pullRequests.oldestOpenPrOpenedAt && formatDistanceToNow(repo.pullRequests.oldestOpenPrOpenedAt, { addSuffix: true });
const mostRecentIssueOpenedAt = repo.issues.mostRecentIssueOpenedAt && formatDistanceToNow(repo.issues.mostRecentIssueOpenedAt, { addSuffix: true });
const oldestOpenIssueOpenedAt = repo.issues.oldestOpenIssueOpenedAt && formatDistanceToNow(repo.issues.oldestOpenIssueOpenedAt, { addSuffix: true });

const languageColor = hashToTailwindColor(repo.main.language);

return {
...repo.main,
...repo.dependabotAlerts,
...repo.pullRequests,
...repo.issues,
updatedAt: newDate,
updatedAtISO8601: repo.main.updatedAt,
dependencies,
mostRecentPrOpenedAt,
mostRecentPrOpenedAtISO8601: repo.main.mostRecentPrOpenedAt,
mostRecentPrOpenedAtISO8601: repo.pullRequests.mostRecentPrOpenedAt,
oldestOpenPrOpenedAt,
oldestOpenPrOpenedAtISO8601: repo.main.oldestOpenPrOpenedAt,
oldestOpenPrOpenedAtISO8601: repo.pullRequests.oldestOpenPrOpenedAt,
mostRecentIssueOpenedAt,
mostRecentIssueOpenedAtISO8601: repo.main.mostRecentIssueOpenedAt,
mostRecentIssueOpenedAtISO8601: repo.issues.mostRecentIssueOpenedAt,
oldestOpenIssueOpenedAt,
oldestOpenIssueOpenedAtISO8601: repo.main.oldestOpenIssueOpenedAt,
oldestOpenIssueOpenedAtISO8601: repo.issues.oldestOpenIssueOpenedAt,
languageColor,
};
});
Expand Down
48 changes: 42 additions & 6 deletions utils/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ describe("mapRepoFromStorageToUi", () => {
issuesUrl: "http://api.com/repo1/issues",
language: null,
topics: [],
openIssues: 0,
dependencies: [],
},
pullRequests: {
mostRecentPrOpenedAt: "2021-01-01T00:00:00Z",
oldestOpenPrOpenedAt: "2022-02-02T00:00:00Z",
},
issues: {
openIssues: 0,
mostRecentIssueOpenedAt: "2023-03-03T00:00:00Z",
oldestOpenIssueOpenedAt: "2024-04-04T00:00:00Z",
},
Expand Down Expand Up @@ -71,10 +75,14 @@ describe("mapRepoFromStorageToUi", () => {
issuesUrl: "http://api.com/repo1/issues",
language: "Ruby",
topics: [],
openIssues: 0,
dependencies: [],
},
pullRequests: {
mostRecentPrOpenedAt: "2021-01-01T00:00:00Z",
oldestOpenPrOpenedAt: "2022-02-02T00:00:00Z",
},
issues: {
openIssues: 0,
mostRecentIssueOpenedAt: "2023-03-03T00:00:00Z",
oldestOpenIssueOpenedAt: "2024-04-04T00:00:00Z",
},
Expand All @@ -91,10 +99,14 @@ describe("mapRepoFromStorageToUi", () => {
issuesUrl: "http://api.com/repo2/issues",
language: "TypeScript",
topics: [],
openIssues: 0,
dependencies: [],
},
pullRequests: {
mostRecentPrOpenedAt: "2021-01-01T00:00:00Z",
oldestOpenPrOpenedAt: "2022-02-02T00:00:00Z",
},
issues: {
openIssues: 0,
mostRecentIssueOpenedAt: "2023-03-03T00:00:00Z",
oldestOpenIssueOpenedAt: "2024-04-04T00:00:00Z",
},
Expand Down Expand Up @@ -167,9 +179,17 @@ describe("mapRepoFromStorageToUi", () => {
issuesUrl: "http://api.com/repo1/issues",
language: null,
topics: [],
openIssues: 0,
dependencies: [],
},
pullRequests: {
mostRecentPrOpenedAt: "2021-01-01T00:00:00Z",
oldestOpenPrOpenedAt: "2022-02-02T00:00:00Z",
},
issues: {
openIssues: 0,
mostRecentIssueOpenedAt: "2023-03-03T00:00:00Z",
oldestOpenIssueOpenedAt: "2024-04-04T00:00:00Z",
},
},
repo2: {
owner: "dxw",
Expand All @@ -183,9 +203,17 @@ describe("mapRepoFromStorageToUi", () => {
issuesUrl: "http://api.com/repo2/issues",
language: null,
topics: [],
openIssues: 0,
dependencies: [],
},
pullRequests: {
mostRecentPrOpenedAt: "2021-01-01T00:00:00Z",
oldestOpenPrOpenedAt: "2022-02-02T00:00:00Z",
},
issues: {
openIssues: 0,
mostRecentIssueOpenedAt: "2023-03-03T00:00:00Z",
oldestOpenIssueOpenedAt: "2024-04-04T00:00:00Z",
},
},
repo3: {
owner: "dxw",
Expand All @@ -199,9 +227,17 @@ describe("mapRepoFromStorageToUi", () => {
issuesUrl: "http://api.com/repo3/issues",
language: null,
topics: [],
openIssues: 0,
dependencies: [],
},
pullRequests: {
mostRecentPrOpenedAt: "2021-01-01T00:00:00Z",
oldestOpenPrOpenedAt: "2022-02-02T00:00:00Z",
},
issues: {
openIssues: 0,
mostRecentIssueOpenedAt: "2023-03-03T00:00:00Z",
oldestOpenIssueOpenedAt: "2024-04-04T00:00:00Z",
},
},
};

Expand Down

0 comments on commit 975f99f

Please sign in to comment.