Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed May 4, 2024
1 parent f3f8d44 commit 3ae0ac0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
13 changes: 9 additions & 4 deletions src/probot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ export default (app: Probot, {getRouter}: ApplicationFunctionOptions) => {
owner: ctx.payload.repository.owner.login,
repo: ctx.payload.repository.name
}
const dbService = new MongoDbService();
app.log(`Inserting event into database: ${JSON.stringify(webhookEvent)}`);
await dbService.storeEvent(webhookEvent);
dbService.disconnect();
const connectionString = process.env.CREATE_ISSUE_BRANCH_MONGODB;
if (!connectionString) {
app.log('Environment variable CREATE_ISSUE_BRANCH_MONGODB not set, skipping database insert')
} else {
const dbService = new MongoDbService(connectionString);
app.log(`Inserting event into database: ${JSON.stringify(webhookEvent)}`);
await dbService.storeEvent(webhookEvent);
dbService.disconnect();
}
});
}

Expand Down
13 changes: 6 additions & 7 deletions src/services/MongoDbService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import {MongoClient, MongoClientOptions} from "mongodb";
import {WebhookEvent} from "../entities/WebhookEvent";

export class MongoDbService {
connectionString: string;
client?: MongoClient = undefined;

constructor(connectionString: string) {
this.connectionString = connectionString;
}

async getDatabase() {
if (!this.client) {
const options: MongoClientOptions = {
authSource: 'admin',
};
const connectionString = process.env.CREATE_ISSUE_BRANCH_MONGODB;
if (!connectionString) {
throw new Error('Environment variable CREATE_ISSUE_BRANCH_MONGODB not set');
} else {
this.client = await MongoClient.connect(connectionString, options);
}
this.client = await MongoClient.connect(this.connectionString, options);
}
return this.client.db('create-issue-branch');
}
Expand All @@ -28,5 +28,4 @@ export class MongoDbService {
disconnect() {
this.client?.close();
}

}

0 comments on commit 3ae0ac0

Please sign in to comment.