Skip to content

Commit

Permalink
Add NewIntegrationsHandler (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Feb 22, 2023
1 parent 19c59d2 commit 683b187
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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 @@ -18,12 +18,13 @@ import { IssueLinks } from './handlers/issue_links';
import { LabelBot } from './handlers/label_bot/handler';
import { LabelCleaner } from './handlers/label_cleaner';
import { MonthOfWTH } from './handlers/month_of_wth';
import { NewIntegrationsHandler } from './handlers/new_integrations';
import { PlatinumReview } from './handlers/platinum_review';
import { QualityScaleLabeler } from './handlers/quality_scale';
import { ReviewDrafter } from './handlers/review_drafter';
import { SetDocumentationSection } from './handlers/set_documentation_section';
import { SetIntegration } from './handlers/set_integration';
import { ValidateCla } from './handlers/validate-cla';
import { ReviewDrafter } from './handlers/review_drafter';

@Module({
providers: [
Expand All @@ -40,6 +41,7 @@ import { ReviewDrafter } from './handlers/review_drafter';
LabelBot,
LabelCleaner,
MonthOfWTH,
NewIntegrationsHandler,
PlatinumReview,
QualityScaleLabeler,
ReviewDrafter,
Expand Down
36 changes: 36 additions & 0 deletions services/bots/src/github-webhook/handlers/new_integrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PullRequestLabeledEvent } from '@octokit/webhooks-types';
import { EventType, HomeAssistantRepository } from '../github-webhook.const';
import { WebhookContext } from '../github-webhook.model';
import { BaseWebhookHandler } from './base';

import { fetchPullRequestFilesFromContext } from '../utils/pull_request';
import { ParsedPath } from '../utils/parse_path';

export class NewIntegrationsHandler extends BaseWebhookHandler {
public allowedEventTypes = [EventType.PULL_REQUEST_LABELED];
public allowedRepositories = [HomeAssistantRepository.CORE];

/**
* When a new-integration label is added, check if the PR contains multiple platforms.
* If so, request changes. The ReviewDrafter will handle the rest.
*/
async handle(context: WebhookContext<PullRequestLabeledEvent>) {
if (context.payload.label?.name !== 'new-integration') {
return;
}

const pullRequestFiles = await fetchPullRequestFilesFromContext(context);
const parsed = pullRequestFiles.map((file) => new ParsedPath(file));

const integrationPlatforms = parsed.filter((path) => path.type === 'platform');

if (integrationPlatforms.length > 1) {
await context.github.pulls.createReview(
context.pullRequest({
body: '[When adding new integrations, limit included platforms to a single platform. Please reduce this PR to a single platform](https://developers.home-assistant.io/docs/review-process/#home-assistant-core)',
event: 'REQUEST_CHANGES',
}),
);
}
}
}

0 comments on commit 683b187

Please sign in to comment.