Skip to content

Commit

Permalink
feat: use pem key as an env var & readme to contribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamPalriwala committed Feb 13, 2024
1 parent b58a70e commit ee113ab
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ SMTP_USER=smtpUser
SMTP_PASSWORD=smtpPassword

# GitHub Webhook Secret
GITHUB_WEBHOOK_SECRET=
GITHUB_WEBHOOK_SECRET=

# GitHub App Private Key [.pem] (enclose in double quotes to preserve newlines)
GITHUB_APP_PRIVATE_KEY=
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
oss.gg 🕹️
# oss.gg 🕹️

## Run Locally

1. Clone the project

```bash
git clone https://github.com/formbricks/oss.gg.git
cd oss.gg
```

2. Install dependencies

```bash
pnpm install
```

3. Copy `.env.example` to `.env` and fill in the required environment variables

```bash
cp .env.example .env
```

> The Github Env Vars (incl the webhook secret) can be accessed from your GitHub App Settings
4. Start the server

```bash
pnpm dev
```

5. Run the Webhook Proxy

```bash
smee --url https://smee.io/<your-smee-path> --path /api/github-webhook --port 3000
```

## GitHub App Configuration

- Callback URL: `http://localhost:3000/api/auth/callback`
- Setup URL: `http://localhost:3000/select-repo` (Check the **Redirect on Update** option)
- Webhook set to Active and get a URL from `https://smee.io/` by clicking on **Start a new channel** and then paste the URL in the Webhook URL field.
- Disable SSL verification for now
- Generate a Client Secret and keep it in the `.env` file
- Generate a Private Key and keep it in the `.env` file
- Copy the Client ID and keep it in the `.env` file
- Now Hit the **Save Changes** button
2 changes: 2 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ export enum EVENT_TRIGGERS {
export const ON_NEW_ISSUE = "Thanks for opening an issue! It's live on oss.gg!";
export const ON_REPO_NOT_REGISTERED = `This repository is not registered with oss.gg. Please register it at https://oss.gg.`;
export const GITHUB_APP_APP_ID = env.GITHUB_APP_APP_ID as string;
export const GITHUB_APP_PRIVATE_KEY = env.GITHUB_APP_PRIVATE_KEY as string;
export const GITHUB_WEBHOOK_SECRET = env.GITHUB_WEBHOOK_SECRET as string;
4 changes: 2 additions & 2 deletions lib/github/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { env } from "@/env.mjs";
import { Webhooks, createNodeMiddleware } from "@octokit/webhooks";

import { onInstallationCreated } from "./hooks/installation";
import { onAssignCommented, onIssueOpened, onUnassignCommented } from "./hooks/issue";
import { GITHUB_WEBHOOK_SECRET } from "../constants";

const webhooks = new Webhooks({
secret: env.GITHUB_WEBHOOK_SECRET as string,
secret: GITHUB_WEBHOOK_SECRET,
});

export const webhookMiddleware = createNodeMiddleware(webhooks, {
Expand Down
12 changes: 3 additions & 9 deletions lib/github/services/user.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { env } from "@/env.mjs";
import { GITHUB_APP_PRIVATE_KEY, GITHUB_WEBHOOK_SECRET } from "@/lib/constants";
import { db } from "@/lib/db";
import { readFileSync } from "fs";
import { App } from "octokit";
import path from "path";

const privateKeyPath = "@/key.pem";
const resolvedPath = path.resolve(__dirname, privateKeyPath);
const privateKey = readFileSync(resolvedPath, "utf8");

export const sendInstallationDetails = async (
installationId: number,
Expand All @@ -25,9 +19,9 @@ export const sendInstallationDetails = async (
try {
const app = new App({
appId,
privateKey,
privateKey: GITHUB_APP_PRIVATE_KEY,
webhooks: {
secret: env.GITHUB_WEBHOOK_SECRET!,
secret: GITHUB_WEBHOOK_SECRET!,
},
});
const octokit = await app.getInstallationOctokit(installationId);
Expand Down
9 changes: 2 additions & 7 deletions lib/github/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { createAppAuth } from "@octokit/auth-app";
import { Octokit } from "@octokit/rest";
import { readFileSync } from "fs";
import path from "path";

import { GITHUB_APP_APP_ID } from "../constants";
import { GITHUB_APP_APP_ID, GITHUB_APP_PRIVATE_KEY } from "../constants";

const privateKeyPath = "@/key.pem";
const resolvedPath = path.resolve(__dirname, privateKeyPath);
const privateKey = readFileSync(resolvedPath, "utf8");

export const getOctokitInstance = (installationId: number) => {
if (!installationId) {
Expand All @@ -18,7 +13,7 @@ export const getOctokitInstance = (installationId: number) => {
authStrategy: createAppAuth,
auth: {
appId: GITHUB_APP_APP_ID!,
privateKey,
privateKey:GITHUB_APP_PRIVATE_KEY!,
installationId,
},
});
Expand Down

0 comments on commit ee113ab

Please sign in to comment.