Skip to content

Commit

Permalink
Merge branch 'feature/github-app-permission'
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Nov 24, 2024
2 parents 73446b4 + ea9106c commit bf45702
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 68 deletions.
7 changes: 4 additions & 3 deletions .example.vars
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
REPO_ORG = "attakei"
REPO_NAME = "zenn-contents"
REPO_PAT = "PASTE_YOUR_PAT"
GITHUB_APP_ID = "0"
GITHUB_CLIENT_ID = "YOUR_CLIENT_IDIv23lis7gjHfXnwgv7d7"
GITHUB_CLIENT_SECRET = "YOUR_CLIENT_SECRET"
GITHUB_PRIVATE_KEY = "YOUR_ONELINER_PKCS8_PRIVATE_KEY"
SENTRY_DSN = "PASE_YOUR_SENTRY_PROJECT_DSN"
SENTRY_ENVIRONMENT = "development"
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"dependencies": {
"@hono/sentry": "^1.2.0",
"@hono/zod-validator": "^0.4.1",
"@octokit/rest": "^21.0.2",
"@octokit/app": "^15.1.1",
"@octokit/core": "^6.1.2",
"front-matter": "^4.0.2",
"hono": "^4.6.9",
"node-html-parser": "^6.1.13",
Expand Down
179 changes: 146 additions & 33 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 40 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,53 @@
* GitHub api client
*
*/
import { Octokit } from '@octokit/rest';
import { App } from '@octokit/app';
import type { Octokit } from '@octokit/core';
import type { Context } from 'hono';
import type { ContentAddress } from './models';

/**
* Create client object from context of Hono.
*/
export const initClient = (c: Context): Octokit => {
return new Octokit({
auth: c.env.REPO_PAT,
});
};
export class Client {
protected app: App;
protected owners: Map<string, any>;

public constructor(c: Context) {
this.app = new App({
appId: c.env.GITHUB_APP_ID,
privateKey: c.env.GITHUB_PRIVATE_KEY,
oauth: {
clientId: c.env.GITHUB_CLIENT_ID,
clientSecret: c.env.GITHUB_CLIENT_SECRET,
},
});
this.owners = new Map();
//
}
public async getApp(owner: string): Promise<Octokit> {
if (this.owners.size === 0) {
await this.app.eachInstallation((opt) => {
this.owners.set(opt.installation.account?.login, opt.installation);
});
}
if (!this.owners.has(owner)) {
throw new Error(`Application is not installed into '${owner}'`);
}
return await this.app.getInstallationOctokit(this.owners.get(owner).id);
}
public async getInstallationUrl(): Promise<string> {
return await this.app.getInstallationUrl();
}
}

export const fetchContent = async (
octokit: Octokit,
params: ContentAddress,
): Promise<string> => {
const resp = await octokit.rest.repos.getContent(params);
const resp = await octokit.request(
'GET /repos/{owner}/{repo}/contents/{path}',
params,
);
if (resp.status !== 200) {
throw new Error('Content is not found.');
}
return Buffer.from(resp.data.content, 'base64').toString();
};
5 changes: 3 additions & 2 deletions src/routes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Hono } from 'hono';
import { zValidator } from '@hono/zod-validator';
import { ContentAddress, makeSlug } from '../models';
import { fetchContent, initClient } from '../client';
import { fetchContent, Client } from '../client';

const api = new Hono();

Expand All @@ -20,7 +20,8 @@ api.post('/content-url', zValidator('json', ContentAddress), async (c) => {
}
try {
// Try to fetch content as address validation.
const octokit = initClient(c);
const client = new Client(c);
const octokit = await client.getApp(addr.owner);
await fetchContent(octokit, addr);
return c.json({ slug: makeSlug(addr) });
} catch (error) {
Expand Down
Loading

0 comments on commit bf45702

Please sign in to comment.