diff --git a/index.js b/index.js index 2340a0e..fff32df 100644 --- a/index.js +++ b/index.js @@ -1,35 +1,41 @@ import { createServer } from "http"; -import { Octokit } from "@octokit/rest"; import nunjucks from "nunjucks"; +import { OctokitApp } from "./octokitApp.js"; nunjucks.configure({ autoescape: true, watch: true, }); -const ACCESS_TOKEN = process.env.GITHUB_PERSONAL_ACCESS_TOKEN; +const httpServer = createServer(async (request, response) => { + if (await OctokitApp.middleware(request, response)) return; -const octokit = new Octokit({ - auth: ACCESS_TOKEN, -}); + const installations = []; + await OctokitApp.app.eachInstallation(async octokit => { + const name = octokit.installation.account.login; + + const repos = await getReposForInstallation(octokit); -const httpServer = createServer(async (_, response) => { - const repos = await getRepos({ org: "dxw" }); + installations.push({ + name, + repos, + }); + }); + + installations.sort((a, b) => a.name.localeCompare(b.name)); const template = nunjucks.render("index.njk", { - repos, + installations, }); return response.end(template); }); -const getRepos = async ({ org }) => { +const getReposForInstallation = async ({ octokit, installation }) => { return octokit - .request(`GET /orgs/${org}/repos`, { - org, - }) + .request(installation.repositories_url) .then(({ data }) => { - return data.map((repo) => ({ + return data.repositories.map((repo) => ({ name: repo.name, })); }) diff --git a/index.njk b/index.njk index 595dc8d..0a907cf 100644 --- a/index.njk +++ b/index.njk @@ -11,9 +11,10 @@

Towtruck

+ {% for installation in installations %} @@ -21,7 +22,7 @@ - {% for repo in repos %} + {% for repo in installation.repos %} @@ -32,5 +33,6 @@ {% endfor %}
- dxw's repos + {{ installation.name }}'s repos
{{ repo.name }}
+ {% endfor %}