From a5f6216b2e9be15ba898a40b6e6244058a25b73c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Mon, 3 Oct 2022 11:38:45 +0200 Subject: [PATCH] Make hacktoberfest handler dynamic (#61) --- .../bots/src/github-webhook/github-webhook.const.ts | 9 +++++++++ services/bots/src/github-webhook/handlers/base.ts | 2 +- .../bots/src/github-webhook/handlers/hacktoberfest.ts | 10 ++++++---- .../bots/src/github-webhook/handlers/month_of_wth.ts | 1 - .../bots/github-webhook/handlers/hacktoberfest.spec.ts | 1 + 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/services/bots/src/github-webhook/github-webhook.const.ts b/services/bots/src/github-webhook/github-webhook.const.ts index d7e3018..73ae256 100644 --- a/services/bots/src/github-webhook/github-webhook.const.ts +++ b/services/bots/src/github-webhook/github-webhook.const.ts @@ -19,11 +19,20 @@ export type GetIssueLabelResponse = RestEndpointMethodTypes['issues']['getLabel']['response']['data']; export enum Repository { + ADDONS = 'addons', + ANDROID = 'android', BRANDS = 'brands', + CLI = 'cli', + COMPANION_HOME_ASSISTANT = 'companion.home-assistant', CORE = 'core', DEVELOPERS_HOME_ASSISTANT = 'developers.home-assistant', FRONTEND = 'frontend', HOME_ASSISTANT_IO = 'home-assistant.io', + IOS = 'iOS', + OPERATING_SYSTEM = 'operating-system', + SERVICE_HUB = 'service-hub', + SUPERVISED_INSTALLER = 'supervised-installer', + SUPERVISOR = 'supervisor', } export enum EventType { diff --git a/services/bots/src/github-webhook/handlers/base.ts b/services/bots/src/github-webhook/handlers/base.ts index cf30f29..99433a4 100644 --- a/services/bots/src/github-webhook/handlers/base.ts +++ b/services/bots/src/github-webhook/handlers/base.ts @@ -6,7 +6,7 @@ import { WebhookContext } from '../github-webhook.model'; export class BaseWebhookHandler { public allowBots: boolean = true; public allowedEventTypes: EventType[] = []; - public allowedRepositories: Repository[] = Object.values(Repository); + public allowedRepositories: Repository[] = []; constructor() { WEBHOOK_HANDLERS.push(this); diff --git a/services/bots/src/github-webhook/handlers/hacktoberfest.ts b/services/bots/src/github-webhook/handlers/hacktoberfest.ts index 0774b47..4f3d8f1 100644 --- a/services/bots/src/github-webhook/handlers/hacktoberfest.ts +++ b/services/bots/src/github-webhook/handlers/hacktoberfest.ts @@ -1,5 +1,5 @@ -import { PullRequestClosedEvent } from '@octokit/webhooks-types'; -import { EventType, Repository } from '../github-webhook.const'; +import { PullRequestClosedEvent, PullRequestOpenedEvent } from '@octokit/webhooks-types'; +import { EventType } from '../github-webhook.const'; import { WebhookContext } from '../github-webhook.model'; import { BaseWebhookHandler } from './base'; @@ -7,7 +7,6 @@ export const isHacktoberfestLive = () => new Date().getMonth() === 9; export class Hacktoberfest extends BaseWebhookHandler { public allowedEventTypes = [EventType.PULL_REQUEST_OPENED, EventType.PULL_REQUEST_CLOSED]; - public allowedRepositories = [Repository.CORE, Repository.HOME_ASSISTANT_IO, Repository.FRONTEND]; async handle(context: WebhookContext) { if (isHacktoberfestLive() && context.eventType === EventType.PULL_REQUEST_OPENED) { @@ -17,7 +16,10 @@ export class Hacktoberfest extends BaseWebhookHandler { } } - async handlePullRequestOpened(context: WebhookContext) { + async handlePullRequestOpened(context: WebhookContext) { + if (!context.payload.repository?.topics?.includes('hacktoberfest')) { + return; + } context.scheduleIssueLabel('Hacktoberfest'); } async handlePullRequestClosed(context: WebhookContext) { diff --git a/services/bots/src/github-webhook/handlers/month_of_wth.ts b/services/bots/src/github-webhook/handlers/month_of_wth.ts index 695017d..9e33e96 100644 --- a/services/bots/src/github-webhook/handlers/month_of_wth.ts +++ b/services/bots/src/github-webhook/handlers/month_of_wth.ts @@ -10,7 +10,6 @@ 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)) { diff --git a/tests/services/bots/github-webhook/handlers/hacktoberfest.spec.ts b/tests/services/bots/github-webhook/handlers/hacktoberfest.spec.ts index a9ca545..70d8c68 100644 --- a/tests/services/bots/github-webhook/handlers/hacktoberfest.spec.ts +++ b/tests/services/bots/github-webhook/handlers/hacktoberfest.spec.ts @@ -51,6 +51,7 @@ describe('Hacktoberfest', () => { it('Add hacktoberfest label on new PR', async () => { const clock = sinon.useFakeTimers(new Date(2020, 9, 1).getTime()); + mockContext.payload.repository = { topics: ['hacktoberfest'] }; await handler.handle(mockContext); clock.restore();