From 8f2e685ec0d2d02defaacea23db750f1e5967fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Tue, 20 Dec 2022 15:39:02 +0100 Subject: [PATCH] Add SetIntentsLanguage handler (#138) --- .../github-webhook/github-webhook.const.ts | 1 + .../github-webhook/github-webhook.module.ts | 4 +- .../handlers/intents_language.ts | 25 +++++++++++ .../handlers/intents_language.spec.ts | 44 +++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 services/bots/src/github-webhook/handlers/intents_language.ts create mode 100644 tests/services/bots/github-webhook/handlers/intents_language.spec.ts diff --git a/services/bots/src/github-webhook/github-webhook.const.ts b/services/bots/src/github-webhook/github-webhook.const.ts index b633833..005e5bd 100644 --- a/services/bots/src/github-webhook/github-webhook.const.ts +++ b/services/bots/src/github-webhook/github-webhook.const.ts @@ -32,6 +32,7 @@ export enum HomeAssistantRepository { DEVELOPERS_HOME_ASSISTANT = 'home-assistant/developers.home-assistant', FRONTEND = 'home-assistant/frontend', HOME_ASSISTANT_IO = 'home-assistant/home-assistant.io', + INTENTS = 'home-assistant/intents', IOS = 'home-assistant/iOS', OPERATING_SYSTEM = 'home-assistant/operating-system', SERVICE_HUB = 'home-assistant/service-hub', diff --git a/services/bots/src/github-webhook/github-webhook.module.ts b/services/bots/src/github-webhook/github-webhook.module.ts index bfce377..94fee26 100644 --- a/services/bots/src/github-webhook/github-webhook.module.ts +++ b/services/bots/src/github-webhook/github-webhook.module.ts @@ -12,6 +12,8 @@ import { DocsMissing } from './handlers/docs_missing'; import { DocsParenting } from './handlers/docs_parenting'; import { DocsTargetBranch } from './handlers/docs_target_branch'; import { Hacktoberfest } from './handlers/hacktoberfest'; +import { SetIntentsLanguage } from './handlers/intents_language'; +import { IssueCommentCommands } from './handlers/issue_comment_commands/handler'; import { IssueLinks } from './handlers/issue_links'; import { LabelBot } from './handlers/label_bot/handler'; import { LabelCleaner } from './handlers/label_cleaner'; @@ -21,7 +23,6 @@ import { QualityScaleLabeler } from './handlers/quality_scale'; import { SetDocumentationSection } from './handlers/set_documentation_section'; import { SetIntegration } from './handlers/set_integration'; import { ValidateCla } from './handlers/validate-cla'; -import { IssueCommentCommands } from './handlers/issue_comment_commands/handler'; @Module({ providers: [ @@ -42,6 +43,7 @@ import { IssueCommentCommands } from './handlers/issue_comment_commands/handler' QualityScaleLabeler, SetDocumentationSection, SetIntegration, + SetIntentsLanguage, ValidateCla, ], imports: [ diff --git a/services/bots/src/github-webhook/handlers/intents_language.ts b/services/bots/src/github-webhook/handlers/intents_language.ts new file mode 100644 index 0000000..97be880 --- /dev/null +++ b/services/bots/src/github-webhook/handlers/intents_language.ts @@ -0,0 +1,25 @@ +import { PullRequestOpenedEvent, PullRequestSynchronizeEvent } from '@octokit/webhooks-types'; +import { EventType, HomeAssistantRepository } from '../github-webhook.const'; +import { WebhookContext } from '../github-webhook.model'; +import { fetchPullRequestFilesFromContext } from '../utils/pull_request'; +import { BaseWebhookHandler } from './base'; + +const LanguageFileRegex = + /(?
sentences|responses|tests)\/(?[a-z]{2})\/(?.+)\.yaml/; + +export class SetIntentsLanguage extends BaseWebhookHandler { + public allowBots = false; + public allowedRepositories = [HomeAssistantRepository.INTENTS]; + public allowedEventTypes = [EventType.PULL_REQUEST_OPENED, EventType.PULL_REQUEST_SYNCHRONIZE]; + + async handle(context: WebhookContext) { + const files = await fetchPullRequestFilesFromContext(context); + + for (const file of files) { + const parsed = LanguageFileRegex.exec(file.filename); + if (parsed?.groups?.language_code) { + context.scheduleIssueLabel(`lang: ${parsed.groups.language_code}`); + } + } + } +} diff --git a/tests/services/bots/github-webhook/handlers/intents_language.spec.ts b/tests/services/bots/github-webhook/handlers/intents_language.spec.ts new file mode 100644 index 0000000..a8f881a --- /dev/null +++ b/tests/services/bots/github-webhook/handlers/intents_language.spec.ts @@ -0,0 +1,44 @@ +import * as assert from 'assert'; +// @ts-ignore +import { WebhookContext } from '../../../../../services/bots/src/github-webhook/github-webhook.model.ts'; +import { SetIntentsLanguage } from '../../../../../services/bots/src/github-webhook/handlers/intents_language'; +import { mockWebhookContext } from '../../../../utils/test_context'; +import { EventType } from '../../../../../services/bots/src/github-webhook/github-webhook.const'; + +describe('SetIntentsLanguage', () => { + let handler: SetIntentsLanguage; + let mockContext: WebhookContext; + let getLabelResponse: any; + + beforeEach(function () { + handler = new SetIntentsLanguage(); + getLabelResponse = {}; + mockContext = mockWebhookContext({ + eventType: EventType.PULL_REQUEST_OPENED, + github: { + // @ts-expect-error not a full mock + issues: {}, + }, + }); + }); + + it('works', async () => { + mockContext._prFilesCache = [ + { + filename: 'sentences/nb/AwesomeIntent.yaml', + }, + { + filename: 'responses/nl/AwesomeIntent.yaml', + }, + { + filename: 'tests/fr/AwesomeIntent.yaml', + }, + ]; + await handler.handle(mockContext); + assert.deepStrictEqual(mockContext.scheduledlabels, [ + 'lang: nb', + 'lang: nl', + 'lang: fr', + ]); + }); +});