Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add prettier for code formatting #35

Merged
merged 5 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bunx lint-staged
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore artifacts:
build
coverage
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
44 changes: 29 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ A Site Reliability Engineering (SRE) bot that integrates with Slack, GitHub, and
- Docker and Docker Compose
- PostgreSQL (via Docker or local installation)
- Accounts and API keys for:
- OpenAI
- Slack
- GitHub
- Checkly
- OpenAI
- Slack
- GitHub
- Checkly

## Setup

Expand All @@ -32,21 +32,25 @@ Duplicate the `env.example` file in the root directory and add your keys as per
The project uses PostgreSQL as its database. To set it up using Docker:

1. Start the PostgreSQL container:

```bash
docker compose up -d
```

2. Run database migrations:

```bash
npm run db:migrate
```

3. (Optional) To explore the database using Prisma Studio:

```bash
npm run db:studio
```

To reset the database if needed:

```bash
docker compose down -v # Remove containers and volumes
docker compose up -d # Start fresh
Expand All @@ -56,41 +60,48 @@ npm run db:migrate # Run migrations again
### 3. Installation

1. Install dependencies:

```bash
npm install
```

2. Initialize the OpenAI assistant:

- First, get your OpenAI API key from https://platform.openai.com/api-keys
- Add the API key to your .env file
- Create the assistant by running:

```bash
npx ts-node scripts/init-assistant.ts
```
Go to https://platform.openai.com/assistants to find your assistant ID

Go to https://platform.openai.com/assistants to find your assistant ID

## Running the Application

There are several ways to run the application depending on your needs:

### Development Mode

```bash
npm run dev
```

### Slack Bot Only

```bash
npm run bot:start
```

### Production Mode

```bash
npm run build
npm start
```

### Running Tests

```bash
npm test
```
Expand Down Expand Up @@ -121,27 +132,30 @@ npm test
## External Service Setup

### Slack Setup

1. Create a new Slack app in your workspace: https://api.slack.com/apps
2. Configure Bot Token Scopes:
- chat:write
- app_mentions:read
- commands
- chat:write
- app_mentions:read
- commands
3. Install the app to your workspace
4. Copy the signing secret, bot token, and app token to your .env file

### GitHub Setup

1. Create a Personal Access Token with repo permissions
2. Configure webhook in your organization/repository:
- Payload URL: your-server/github-webhook
- Content type: application/json
- Secret: Same as GH_WEBHOOK_SECRET in .env
- Events: Release events
- Payload URL: your-server/github-webhook
- Content type: application/json
- Secret: Same as GH_WEBHOOK_SECRET in .env
- Events: Release events

### Checkly Setup
1. Get your API key and Account ID from Checkly: https://app.checklyhq.com/

1. Get your API key and Account ID from Checkly: https://app.checklyhq.com/
2. Configure webhook in Checkly:
- URL: your-server/checkly-webhook
- Select relevant alert types
- URL: your-server/checkly-webhook
- Select relevant alert types

## License

Expand Down
238 changes: 225 additions & 13 deletions bun.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/*.spec.ts'],
};
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["**/*.spec.ts"],
};
8 changes: 4 additions & 4 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"watch": ["src"],
"ext": "ts",
"exec": "concurrently \"npx tsc --watch\" \"ts-node src/index.ts\""
}
"watch": ["src"],
"ext": "ts",
"exec": "concurrently \"npx tsc --watch\" \"ts-node src/index.ts\""
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"db:generate": "prisma generate",
"postinstall": "prisma generate",
"vercel-build": "prisma generate && prisma migrate deploy && next build",
"smee": "npx smee-client"
"smee": "npx smee-client",
"prepare": "husky"
},
"author": "",
"license": "ISC",
Expand Down Expand Up @@ -56,12 +57,18 @@
"@types/jest": "29.5.14",
"@types/react": "18.3.12",
"concurrently": "9.1.0",
"husky": "^9.1.7",
"jest": "29.7.0",
"lint-staged": "^15.4.3",
"nodemon": "3.1.7",
"prettier": "3.4.2",
"prisma": "5.22.0",
"ts-jest": "29.2.5",
"ts-node": "10.9.2"
},
"lint-staged": {
"**/*": "prettier --write --ignore-unknown"
},
"name": "srebot",
"version": "1.0.0",
"description": "bajo construcción",
Expand Down
4 changes: 2 additions & 2 deletions scripts/init-assistant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getOpenaiClient } from "../src/ai/openai";
import { initConfig } from "../src/lib/init-config";

initConfig()
initConfig();

const openai = getOpenaiClient();

Expand All @@ -20,7 +20,7 @@ async function main() {
await createAssistant("sre-assistant");
} else {
const sreAssistant = assistants.data.find(
(assistant) => assistant.name === "sre-assistant"
(assistant) => assistant.name === "sre-assistant",
);

if (!sreAssistant) {
Expand Down
11 changes: 8 additions & 3 deletions src/aggregator/ContextAggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export interface CheckContext {

export class CheckContextAggregator {
alert: WebhookAlertDto;
plugins = [checklyAggregator, githubAggregator, slackChannelAggregator, knowledgeAggregator];
plugins = [
checklyAggregator,
githubAggregator,
slackChannelAggregator,
knowledgeAggregator,
];

constructor(alert: WebhookAlertDto) {
this.alert = alert;
Expand All @@ -42,11 +47,11 @@ export class CheckContextAggregator {
return plugin.fetchContext(this.alert).catch((error) => {
console.error(
`Error fetching context from ${plugin.name ?? "unknown plugin"}:`,
error
error,
);
return [];
});
})
}),
).then((results) => results.flat());
}
}
4 changes: 2 additions & 2 deletions src/aggregator/checkly-aggregator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ describe("ChecklyService", () => {
$UUID: "380b94c2-2c56-4f1d-904a-a6122d96722a",
moment: "January 15, 2025",
},
{ enableImplicitConversion: true }
)
{ enableImplicitConversion: true },
),
);

expect(context).toBeDefined();
Expand Down
7 changes: 5 additions & 2 deletions src/aggregator/checkly-aggregator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { CheckContext, ContextKey } from "./ContextAggregator";
import { checkly } from "../checkly/client";
import { WebhookAlertDto } from "../checkly/alertDTO";
import { mapCheckResultToContextValue, mapCheckToContextValue, } from "../checkly/utils";
import {
mapCheckResultToContextValue,
mapCheckToContextValue,
} from "../checkly/utils";

export const checklyAggregator = {
name: "Checkly",
Expand Down Expand Up @@ -29,7 +32,7 @@ export const checklyAggregator = {
makeCheckContext(ContextKey.ChecklyCheck, mapCheckToContextValue(check)),
makeCheckContext(
ContextKey.ChecklyResults,
mapCheckResultToContextValue(results)
mapCheckResultToContextValue(results),
),
makeCheckContext(ContextKey.ChecklyLogs, logs),
] as CheckContext[];
Expand Down
4 changes: 2 additions & 2 deletions src/aggregator/github-aggregator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ describe.skip("GithubAggregator", () => {
$UUID: "test-uuid",
moment: "March 15, 2024",
},
{ enableImplicitConversion: true }
)
{ enableImplicitConversion: true },
),
)
.catch((error) => {
console.error("Error fetching context:", error);
Expand Down
25 changes: 14 additions & 11 deletions src/aggregator/knowledge-aggregator.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import { WebhookAlertDto } from "../checkly/alertDTO";
import { CheckContext, ContextKey } from "./ContextAggregator";
import { getAllDocuments, KnowledgeDocument, } from "../knowledge-base/knowledgeBase";
import {
getAllDocuments,
KnowledgeDocument,
} from "../knowledge-base/knowledgeBase";

const transformDocument = (document: KnowledgeDocument, checkId: string): CheckContext => {
const transformDocument = (
document: KnowledgeDocument,
checkId: string,
): CheckContext => {
return {
checkId,
value: document.content,
source: 'knowledge',
key: ContextKey.Knowledge.replace(
"$documentSlug",
document.slug
),
source: "knowledge",
key: ContextKey.Knowledge.replace("$documentSlug", document.slug),
analysis: document.summary,
} as CheckContext;
}
};

export const knowledgeAggregator = {
name: "Knowledge",
fetchContext: async (alert: WebhookAlertDto): Promise<CheckContext[]> => {
console.log('Aggregating Knowledge Context...');
const documents = await getAllDocuments()
console.log("Aggregating Knowledge Context...");
const documents = await getAllDocuments();

return documents.map((doc) => transformDocument(doc, alert.CHECK_ID));
},
}
};
Loading
Loading