Skip to content

Commit

Permalink
Fetch repos from JSON in the server
Browse files Browse the repository at this point in the history
Previously we were fetching all the repos on each request to the server.
Now we just read from the JSON instead.
This drastically simplifies the server code.
  • Loading branch information
rich committed Sep 11, 2024
1 parent 98a5c23 commit 108b6d0
Showing 1 changed file with 8 additions and 36 deletions.
44 changes: 8 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,25 @@
import { createServer } from "http";
import nunjucks from "nunjucks";
import { OctokitApp } from "./octokitApp.js";
import { orgRespositoriesToUiRepositories } from "./utils.js";
import { getReposFromJson, mapRepoFromStorageToUi } from "./utils.js";

nunjucks.configure({
autoescape: true,
watch: true,
});

const httpServer = createServer(async (request, response) => {
if (await OctokitApp.middleware(request, response)) return;
const httpServer = createServer(async (_, response) => {
const pathToRepos = "./data/repos.json";

// Currently we only want to support single-account installations.
// There doesn't seem to be a neat way to get the installation ID from an account name,
// so we will use `eachInstallation` to loop (hopefully once) and just take the first (hopefully only)
// element from `installations` so that we can have more meaningful template names in Nunjucks.
//
// We can enforce this one-installation approach through GitHub by configuring the app to be
// "Only on this account" when registering the app.
const persistedData = await getReposFromJson(pathToRepos);

const installations = [];
await OctokitApp.app.eachInstallation(async (octokit) => {
const name = octokit.installation.account.login;

const { repos, totalRepos } = await getReposForInstallation(octokit);

installations.push({
name,
repos,
totalRepos,
});
});

const template = nunjucks.render("index.njk", installations[0]);
const template = nunjucks.render(
"index.njk",
mapRepoFromStorageToUi(persistedData)
);

return response.end(template);
});

const getReposForInstallation = async ({ octokit, installation }) => {
return octokit
.request(installation.repositories_url)
.then(({ data }) => {
return orgRespositoriesToUiRepositories(data);
})
.catch((error) => {
console.error(error);
});
};

const PORT = 3000;
httpServer.listen(PORT, () => {
console.info(`Server is running on port ${PORT}`);
Expand Down

0 comments on commit 108b6d0

Please sign in to comment.