From a5fd0f1a9377e62742442ab83a7da8a5084b65b0 Mon Sep 17 00:00:00 2001 From: ludeeus Date: Thu, 29 Sep 2022 17:28:28 +0000 Subject: [PATCH] Add MonthOfWTH handler --- .../github-webhook/github-webhook.module.ts | 2 ++ .../github-webhook/handlers/month_of_wth.ts | 28 +++++++++++++++++++ .../src/github-webhook/utils/text_parser.ts | 10 +++++++ .../github-webhook/utils/text_parser.spec.ts | 28 +++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 services/bots/src/github-webhook/handlers/month_of_wth.ts create mode 100644 tests/services/bots/github-webhook/utils/text_parser.spec.ts diff --git a/services/bots/src/github-webhook/github-webhook.module.ts b/services/bots/src/github-webhook/github-webhook.module.ts index e299f9e..c09f9d6 100644 --- a/services/bots/src/github-webhook/github-webhook.module.ts +++ b/services/bots/src/github-webhook/github-webhook.module.ts @@ -18,6 +18,7 @@ import { LabelCleaner } from './handlers/label_cleaner'; import { SetDocumentationSection } from './handlers/set_documentation_section'; import { SetIntegration } from './handlers/set_integration'; import { ValidateCla } from './handlers/validate-cla'; +import { MonthOfWTH } from './handlers/month_of_wth'; @Module({ providers: [ @@ -32,6 +33,7 @@ import { ValidateCla } from './handlers/validate-cla'; IssueLinks, LabelBot, LabelCleaner, + MonthOfWTH, SetDocumentationSection, SetIntegration, ValidateCla, diff --git a/services/bots/src/github-webhook/handlers/month_of_wth.ts b/services/bots/src/github-webhook/handlers/month_of_wth.ts new file mode 100644 index 0000000..695017d --- /dev/null +++ b/services/bots/src/github-webhook/handlers/month_of_wth.ts @@ -0,0 +1,28 @@ +import fetch from 'node-fetch'; + +import { PullRequest, PullRequestOpenedEvent } from '@octokit/webhooks-types'; +import { EventType } from '../github-webhook.const'; +import { WebhookContext } from '../github-webhook.model'; +import { extractForumLinks } from '../utils/text_parser'; +import { BaseWebhookHandler } from './base'; + +const WTH_CATEGORY_ID = 56; + +export class MonthOfWTH extends BaseWebhookHandler { + public allowedEventTypes = [EventType.PULL_REQUEST_OPENED]; + public allowedRepositories = []; + + async handle(context: WebhookContext) { + for (const link of extractForumLinks((context.payload.pull_request as PullRequest).body)) { + try { + const linkData = await (await fetch(`${link}.json`)).json(); + if (linkData.category_id === WTH_CATEGORY_ID) { + context.scheduleIssueLabel('WTH'); + return; + } + } catch (_) { + // Assume bad link + } + } + } +} diff --git a/services/bots/src/github-webhook/utils/text_parser.ts b/services/bots/src/github-webhook/utils/text_parser.ts index 3d042c2..991289c 100644 --- a/services/bots/src/github-webhook/utils/text_parser.ts +++ b/services/bots/src/github-webhook/utils/text_parser.ts @@ -31,6 +31,16 @@ export const extractIntegrationDocumentationLinks = ( return results; }; +export const extractForumLinks = (body: string): string[] => + body + .split('\n') + .map( + (line) => + /.*(?https:\/\/community.home-assistant.io\/t\/.*\S)(\s|\n|)/.exec(line)?.groups, + ) + .filter((groups) => groups !== undefined) + .map((groups) => groups.link); + export const extractTasks = (body: string): Task[] => body .split('\n') diff --git a/tests/services/bots/github-webhook/utils/text_parser.spec.ts b/tests/services/bots/github-webhook/utils/text_parser.spec.ts new file mode 100644 index 0000000..4463f11 --- /dev/null +++ b/tests/services/bots/github-webhook/utils/text_parser.spec.ts @@ -0,0 +1,28 @@ +// @ts-nocheck +import * as assert from 'assert'; + +import { extractForumLinks } from '../../../../../services/bots/src/github-webhook/utils/text_parser'; + +describe('text_parser', () => { + it('extractForumLinks', async () => { + assert.deepStrictEqual( + extractForumLinks(`https://community.home-assistant.io/t/example-topic/some_id`), + ['https://community.home-assistant.io/t/example-topic/some_id'], + ); + + assert.deepStrictEqual( + extractForumLinks(`\nhttps://community.home-assistant.io/t/example-topic/some_id `), + ['https://community.home-assistant.io/t/example-topic/some_id'], + ); + + assert.deepStrictEqual( + extractForumLinks(`HI https://community.home-assistant.io/t/example-topic/some_id\n `), + ['https://community.home-assistant.io/t/example-topic/some_id'], + ); + + assert.deepStrictEqual( + extractForumLinks(`HI http://community.home-assistant.io/t/example-topic/some_id\n `), + [], + ); + }); +});