diff --git a/package.json b/package.json index 0dd731a6..7e391767 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "test": "jest", "test:docker": "docker-compose up", "coverage": "jest --collect-coverage", - "probot:start": "yarn build && NODE_OPTIONS='--max_old_space_size=490' probot run ./build/probot.js", + "probot:start": "yarn action:build && NODE_OPTIONS='--max_old_space_size=490' probot run ./build/probot.js", "action:build": "tsc --noemit false", "action:dist": "yarn action:build && ncc build build/action.js" }, @@ -55,6 +55,7 @@ "@types/dlv": "^1.1.4", "analytics": "^0.8.11", "js-tokens": "^9.0.0", + "mongodb": "^6.6.0", "next": "^14.1.4", "node-fetch": "^2.6.1", "probot": "^12.3.3", diff --git a/src/entities/WebhookEvent.ts b/src/entities/WebhookEvent.ts new file mode 100644 index 00000000..7ac83436 --- /dev/null +++ b/src/entities/WebhookEvent.ts @@ -0,0 +1,7 @@ +export interface WebhookEvent { + timestamp: Date; + name: string; + action: string; + owner: string; + repo: string; +} \ No newline at end of file diff --git a/src/probot.ts b/src/probot.ts index 88689811..0b9041a6 100644 --- a/src/probot.ts +++ b/src/probot.ts @@ -12,6 +12,8 @@ import {pullRequest} from "./webhooks/pull-request"; import {pullRequestClosed} from "./webhooks/pull-request-closed"; import {gitDate, gitSha, version} from "./version"; import {isRunningInGitHubActions, logMemoryUsage} from "./utils"; +import {MongoDbService} from "./services/MongoDbService"; +import {WebhookEvent} from "./entities/WebhookEvent"; export default (app: Probot, {getRouter}: ApplicationFunctionOptions) => { const buildDate = gitDate.toISOString().substring(0, 10); @@ -51,7 +53,18 @@ export default (app: Probot, {getRouter}: ApplicationFunctionOptions) => { }) app.onAny(async (ctx: any) => { app.log(`Received webhook event: ${ctx.name}.${ctx.payload.action}`) - }) + const webhookEvent: WebhookEvent = { + timestamp: new Date(), + name: ctx.name, + action: ctx.payload.action, + 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(); + }); } function addStatsRoute(getRouter: (path?: string) => express.Router) { diff --git a/src/services/MongoDbService.ts b/src/services/MongoDbService.ts new file mode 100644 index 00000000..6e5db283 --- /dev/null +++ b/src/services/MongoDbService.ts @@ -0,0 +1,32 @@ +import {MongoClient, MongoClientOptions} from "mongodb"; +import {WebhookEvent} from "../entities/WebhookEvent"; + +export class MongoDbService { + client?: MongoClient = undefined; + + 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); + } + } + return this.client.db('create-issue-branch'); + } + + async storeEvent(event: WebhookEvent) { + const db = await this.getDatabase(); + const collection = db.collection('action'); + await collection.insertOne(event); + } + + disconnect() { + this.client?.close(); + } + +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 2680cb72..a1673f5e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -679,6 +679,13 @@ semver "^7.3.5" tar "^6.1.11" +"@mongodb-js/saslprep@^1.1.5": + version "1.1.6" + resolved "https://registry.yarnpkg.com/@mongodb-js/saslprep/-/saslprep-1.1.6.tgz#54da3f794c71a17445740fe2b74882e0c76a3058" + integrity sha512-jqTTXQ46H8cAxmXBu8wm1HTSIMBMrIcoVrsjdQkKdMBj3il/fSCgWyya4P2I1xjPBl69mw+nRphrPlcIqBd20Q== + dependencies: + sparse-bitfield "^3.0.3" + "@next/env@14.1.4": version "14.1.4" resolved "https://registry.yarnpkg.com/@next/env/-/env-14.1.4.tgz#432e80651733fbd67230bf262aee28be65252674" @@ -1409,6 +1416,18 @@ resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.3.tgz#6209321eb2c1712a7e7466422b8cb1fc0d9dd5d8" integrity sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw== +"@types/webidl-conversions@*": + version "7.0.3" + resolved "https://registry.yarnpkg.com/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz#1306dbfa53768bcbcfc95a1c8cde367975581859" + integrity sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA== + +"@types/whatwg-url@^11.0.2": + version "11.0.4" + resolved "https://registry.yarnpkg.com/@types/whatwg-url/-/whatwg-url-11.0.4.tgz#ffed0dc8d89d91f62e3f368fcbda222a487c4f63" + integrity sha512-lXCmTWSHJvf0TRSO58nm978b8HJ/EdsSsEKLd3ODHFjo+3VGAyyTp4v50nWvwtzBxSMQrVOK7tcuN0zGPLICMw== + dependencies: + "@types/webidl-conversions" "*" + "@types/yargs-parser@*": version "21.0.3" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.3.tgz#815e30b786d2e8f0dcd85fd5bcf5e1a04d008f15" @@ -1830,6 +1849,11 @@ bser@2.1.1: dependencies: node-int64 "^0.4.0" +bson@^6.7.0: + version "6.7.0" + resolved "https://registry.yarnpkg.com/bson/-/bson-6.7.0.tgz#51973b132cdc424c8372fda3cb43e3e3e2ae2227" + integrity sha512-w2IquM5mYzYZv6rs3uN2DZTOBe2a0zXLj53TGDqwF4l6Sz/XsISrisXOJihArF9+BZ6Cq/GjVht7Sjfmri7ytQ== + btoa-lite@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/btoa-lite/-/btoa-lite-1.0.0.tgz#337766da15801210fdd956c22e9c6891ab9d0337" @@ -3707,6 +3731,11 @@ media-typer@0.3.0: resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" integrity sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ== +memory-pager@^1.0.2: + version "1.5.0" + resolved "https://registry.yarnpkg.com/memory-pager/-/memory-pager-1.5.0.tgz#d8751655d22d384682741c972f2c3d6dfa3e66b5" + integrity sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg== + merge-descriptors@1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61" @@ -3806,6 +3835,23 @@ mkver@^2.1.0: resolved "https://registry.yarnpkg.com/mkver/-/mkver-2.1.0.tgz#3f47b08810ba78ed5f301f6dd9d05e7677c506b6" integrity sha512-a5tfefhIVPqHgy98IAyVU2Tn45knQ5Um44j68EdtfXKikfbRoxOsO+0wjJCsl4k0VgSEnqWx3VuItcBynb78Pw== +mongodb-connection-string-url@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.0.tgz#b4f87f92fd8593f3b9365f592515a06d304a1e9c" + integrity sha512-t1Vf+m1I5hC2M5RJx/7AtxgABy1cZmIPQRMXw+gEIPn/cZNF3Oiy+l0UIypUwVB5trcWHq3crg2g3uAR9aAwsQ== + dependencies: + "@types/whatwg-url" "^11.0.2" + whatwg-url "^13.0.0" + +mongodb@^6.6.0: + version "6.6.0" + resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-6.6.0.tgz#5ff05a3f8fbf5ac961c4d87ac3d7771547fc6d08" + integrity sha512-LoR0hel6y1i7pubPOzbflccdc89RDOapIGqwce4aenOfhhLKOqiQ2F4yQHP+Hqa7TuPhpVqDVjixbvUSx7R0rQ== + dependencies: + "@mongodb-js/saslprep" "^1.1.5" + bson "^6.7.0" + mongodb-connection-string-url "^3.0.0" + mri@1.1.4: version "1.1.4" resolved "https://registry.yarnpkg.com/mri/-/mri-1.1.4.tgz#7cb1dd1b9b40905f1fac053abe25b6720f44744a" @@ -4369,7 +4415,7 @@ punycode@^1.3.2: resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e" integrity sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ== -punycode@^2.1.0: +punycode@^2.1.0, punycode@^2.3.0: version "2.3.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.1.tgz#027422e2faec0b25e1549c3e1bd8309b9133b6e5" integrity sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg== @@ -4735,6 +4781,13 @@ source-map@^0.6.0, source-map@^0.6.1: resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== +sparse-bitfield@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz#ff4ae6e68656056ba4b3e792ab3334d38273ca11" + integrity sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ== + dependencies: + memory-pager "^1.0.2" + split2@^3.1.1: version "3.2.2" resolved "https://registry.yarnpkg.com/split2/-/split2-3.2.2.tgz#bf2cf2a37d838312c249c89206fd7a17dd12365f" @@ -4929,6 +4982,13 @@ touch@^3.1.0: dependencies: nopt "~1.0.10" +tr46@^4.1.1: + version "4.1.1" + resolved "https://registry.yarnpkg.com/tr46/-/tr46-4.1.1.tgz#281a758dcc82aeb4fe38c7dfe4d11a395aac8469" + integrity sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw== + dependencies: + punycode "^2.3.0" + tr46@~0.0.3: version "0.0.3" resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" @@ -5158,6 +5218,19 @@ webidl-conversions@^3.0.0: resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ== +webidl-conversions@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz#256b4e1882be7debbf01d05f0aa2039778ea080a" + integrity sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g== + +whatwg-url@^13.0.0: + version "13.0.0" + resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-13.0.0.tgz#b7b536aca48306394a34e44bda8e99f332410f8f" + integrity sha512-9WWbymnqj57+XEuqADHrCJ2eSXzn8WXIW/YSGaZtb2WKAInQ6CHfaUUcTyyver0p8BDg5StLQq8h1vtZuwmOig== + dependencies: + tr46 "^4.1.1" + webidl-conversions "^7.0.0" + whatwg-url@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"