diff --git a/.env.example b/.env.example index 32fe5065a..f9972f060 100644 --- a/.env.example +++ b/.env.example @@ -16,3 +16,4 @@ GUILD_ID = "123" LOCAL_CMS_PROVIDER = true CACHE_EXPIRE_S = 10 ADMINS = "Random,example@gmail.com" +DISCORD_BOUNTY_WEBHOOK=https://discord.com/api/webhooks/1218161863615840327/-ZztocKsF0JrVp04EVtsfAm4n3V1sjdv_nQ4x9IkGQJnwS4qUXa4-paIDxPxrlD9NRGK \ No newline at end of file diff --git a/src/app/api/discord/notification/route.ts b/src/app/api/discord/notification/route.ts new file mode 100644 index 000000000..dcc5e2725 --- /dev/null +++ b/src/app/api/discord/notification/route.ts @@ -0,0 +1,45 @@ +import { extractBounty } from '@/lib/utils'; +import { NextRequest, NextResponse } from 'next/server'; + +export async function POST(req: NextRequest) { + const data = await req.json(); + const githubActionType = data.action; + const isPR = data.issue.pull_request ? true : false; + const PRTitle = data.issue.title; + const PRURL = data.issue.html_url; + const benefactor = data.comment.user.login; // hkirat + const benefactorComment = data.comment.body; + const bounty = extractBounty(benefactorComment); + const bountyWinner = data.issue.user.login; + const bountyWinnerProfile = data.issue.user.url; + + if ( + githubActionType === 'created' && + isPR && + bounty && + benefactor === 'hkirat' + ) { + const message = `:moneybag: Congratulations, [${bountyWinner}](<${bountyWinnerProfile}>)! You've won a bounty of $${bounty} for working on [${PRTitle}](<${PRURL}>).`; + await pushNotification(message); + } + + return NextResponse.json({}); +} + +async function pushNotification(content: any) { + const DISCORD_WEBHOOK = process.env.DISCORD_BOUNTY_WEBHOOK; + if (!DISCORD_WEBHOOK) { + return; + } + const post = { + content, + }; + const config = { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify(post), + }; + await fetch(DISCORD_WEBHOOK, config); +} diff --git a/src/db/Cache.ts b/src/db/Cache.ts index 141ee455c..da447951c 100644 --- a/src/db/Cache.ts +++ b/src/db/Cache.ts @@ -26,7 +26,12 @@ export class Cache { return this.instance; } - set(type: string, args: string[], value: any, expirySeconds: number = parseInt(process.env.CACHE_EXPIRE_S || '100', 10)) { + set( + type: string, + args: string[], + value: any, + expirySeconds: number = parseInt(process.env.CACHE_EXPIRE_S || '100', 10), + ) { this.inMemoryDb.set(`${type} ${JSON.stringify(args)}`, { value, expiry: new Date().getTime() + expirySeconds * 1000, @@ -46,7 +51,5 @@ export class Cache { return entry.value; } - evict() { - - } + evict() {} } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index d465ded9a..fd14efa73 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -299,3 +299,13 @@ export const constructCommentPrismaQuery = ( return query; }; + +export const extractBounty = (message: string) => { + const regex = /\/bounty\s*\$\s*(\d+)/i; + const result = message.match(regex); + + if (result) { + return Number(result[1]); + } + return null; +}; diff --git a/yarn.lock b/yarn.lock index 552adebbc..370414795 100644 --- a/yarn.lock +++ b/yarn.lock @@ -351,15 +351,10 @@ resolved "https://registry.npmjs.org/@next/env/-/env-14.0.2.tgz" integrity sha512-HAW1sljizEaduEOes/m84oUqeIDAUYBR1CDwu2tobNlNDFP3cSm9d6QsOsGeNlIppU1p/p1+bWbYCbvwjFiceA== -"@next/swc-linux-x64-gnu@14.0.2": +"@next/swc-darwin-arm64@14.0.2": version "14.0.2" - resolved "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.0.2.tgz" - integrity sha512-WC9KAPSowj6as76P3vf1J3mf2QTm3Wv3FBzQi7UJ+dxWjK3MhHVWsWUo24AnmHx9qDcEtHM58okgZkXVqeLB+Q== - -"@next/swc-linux-x64-musl@14.0.2": - version "14.0.2" - resolved "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.0.2.tgz" - integrity sha512-KSSAwvUcjtdZY4zJFa2f5VNJIwuEVnOSlqYqbQIawREJA+gUI6egeiRu290pXioQXnQHYYdXmnVNZ4M+VMB7KQ== + resolved "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.0.2.tgz" + integrity sha512-i+jQY0fOb8L5gvGvojWyZMfQoQtDVB2kYe7fufOEiST6sicvzI2W5/EXo4lX5bLUjapHKe+nFxuVv7BA+Pd7LQ== "@nodelib/fs.scandir@2.1.5": version "2.1.5" @@ -2101,6 +2096,11 @@ fs.realpath@^1.0.0: resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz" integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== +fsevents@~2.3.2: + version "2.3.3" + resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz" + integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw== + function-bind@^1.1.2: version "1.1.2" resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz"