Skip to content

Commit

Permalink
Make hacktoberfest handler dynamic (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Oct 3, 2022
1 parent f0d4b3f commit a5f6216
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
9 changes: 9 additions & 0 deletions services/bots/src/github-webhook/github-webhook.const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion services/bots/src/github-webhook/handlers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions services/bots/src/github-webhook/handlers/hacktoberfest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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';

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<any>) {
if (isHacktoberfestLive() && context.eventType === EventType.PULL_REQUEST_OPENED) {
Expand All @@ -17,7 +16,10 @@ export class Hacktoberfest extends BaseWebhookHandler {
}
}

async handlePullRequestOpened(context: WebhookContext<any>) {
async handlePullRequestOpened(context: WebhookContext<PullRequestOpenedEvent>) {
if (!context.payload.repository?.topics?.includes('hacktoberfest')) {
return;
}
context.scheduleIssueLabel('Hacktoberfest');
}
async handlePullRequestClosed(context: WebhookContext<PullRequestClosedEvent>) {
Expand Down
1 change: 0 additions & 1 deletion services/bots/src/github-webhook/handlers/month_of_wth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PullRequestOpenedEvent>) {
for (const link of extractForumLinks((context.payload.pull_request as PullRequest).body)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit a5f6216

Please sign in to comment.