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

Feature | Bounty Hunter Discord notification #225

Closed
Closed
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 .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ GUILD_ID = "123"
LOCAL_CMS_PROVIDER = true
CACHE_EXPIRE_S = 10
ADMINS = "Random,[email protected]"
DISCORD_BOUNTY_WEBHOOK=https://discord.com/api/webhooks/1218161863615840327/-ZztocKsF0JrVp04EVtsfAm4n3V1sjdv_nQ4x9IkGQJnwS4qUXa4-paIDxPxrlD9NRGK
45 changes: 45 additions & 0 deletions src/app/api/discord/notification/route.ts
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 7 additions & 4 deletions src/db/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -46,7 +51,5 @@ export class Cache {
return entry.value;
}

evict() {

}
evict() {}
}
10 changes: 10 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]":
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/[email protected]":
version "2.1.5"
Expand Down Expand Up @@ -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"
Expand Down
Loading