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

Add MonthOfWTH handler #57

Merged
merged 1 commit into from
Sep 30, 2022
Merged
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
2 changes: 2 additions & 0 deletions services/bots/src/github-webhook/github-webhook.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -32,6 +33,7 @@ import { ValidateCla } from './handlers/validate-cla';
IssueLinks,
LabelBot,
LabelCleaner,
MonthOfWTH,
SetDocumentationSection,
SetIntegration,
ValidateCla,
Expand Down
28 changes: 28 additions & 0 deletions services/bots/src/github-webhook/handlers/month_of_wth.ts
Original file line number Diff line number Diff line change
@@ -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<PullRequestOpenedEvent>) {
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
}
}
}
}
10 changes: 10 additions & 0 deletions services/bots/src/github-webhook/utils/text_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export const extractIntegrationDocumentationLinks = (
return results;
};

export const extractForumLinks = (body: string): string[] =>
body
.split('\n')
.map(
(line) =>
/.*(?<link>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')
Expand Down
28 changes: 28 additions & 0 deletions tests/services/bots/github-webhook/utils/text_parser.spec.ts
Original file line number Diff line number Diff line change
@@ -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 `),
[],
);
});
});