Skip to content

Commit

Permalink
Add SetIntentsLanguage handler (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Dec 20, 2022
1 parent bcc9f25 commit 8f2e685
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions services/bots/src/github-webhook/github-webhook.const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion services/bots/src/github-webhook/github-webhook.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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: [
Expand All @@ -42,6 +43,7 @@ import { IssueCommentCommands } from './handlers/issue_comment_commands/handler'
QualityScaleLabeler,
SetDocumentationSection,
SetIntegration,
SetIntentsLanguage,
ValidateCla,
],
imports: [
Expand Down
25 changes: 25 additions & 0 deletions services/bots/src/github-webhook/handlers/intents_language.ts
Original file line number Diff line number Diff line change
@@ -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 =
/(?<section>sentences|responses|tests)\/(?<language_code>[a-z]{2})\/(?<intent>.+)\.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<PullRequestOpenedEvent | PullRequestSynchronizeEvent>) {
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}`);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<any>;
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',
]);
});
});

0 comments on commit 8f2e685

Please sign in to comment.