Skip to content

Commit

Permalink
Use OctokitApp to display repository list
Browse files Browse the repository at this point in the history
This setup iterates over all installations of the GitHub app and
displays their repositories in the previously made table.

Iterating over multiple installations rather than specifying it
explicitly will allow us to support repositories we manage in other
organisations by getting them to install Towtruck as a GitHub app in
their organisation settings and granting access to the repositories that
we maintain for them.
  • Loading branch information
danlivings-dxw committed Sep 9, 2024
1 parent 569602b commit 0b3309b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
32 changes: 19 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -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,
}));
})
Expand Down
6 changes: 4 additions & 2 deletions index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
<body>
<h1>Towtruck</h1>

{% for installation in installations %}
<table>
<caption>
dxw's repos
{{ installation.name }}'s repos
</caption>
<thead>
<tr>
<th scope="col">Name</th>
</tr>
</thead>
<tbody>
{% for repo in repos %}
{% for repo in installation.repos %}
<tr>
<td>{{ repo.name }}</td>
</tr>
Expand All @@ -32,5 +33,6 @@
{% endfor %}
</tbody>
</table>
{% endfor %}
</body>
</html>

0 comments on commit 0b3309b

Please sign in to comment.