diff --git a/services/bots/src/github-webhook/github-webhook.module.ts b/services/bots/src/github-webhook/github-webhook.module.ts index 519668d..6300da8 100644 --- a/services/bots/src/github-webhook/github-webhook.module.ts +++ b/services/bots/src/github-webhook/github-webhook.module.ts @@ -23,6 +23,7 @@ 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 { RequiredLabels } from './handlers/required_labels'; import { ReviewDrafter } from './handlers/review_drafter'; import { SetDocumentationSection } from './handlers/set_documentation_section'; import { SetIntegration } from './handlers/set_integration'; @@ -48,6 +49,7 @@ import { ValidateCla } from './handlers/validate-cla'; NewIntegrationsHandler, PlatinumReview, QualityScaleLabeler, + RequiredLabels, ReviewDrafter, SetDocumentationSection, SetIntegration, diff --git a/services/bots/src/github-webhook/handlers/required_labels.ts b/services/bots/src/github-webhook/handlers/required_labels.ts new file mode 100644 index 0000000..0360c1a --- /dev/null +++ b/services/bots/src/github-webhook/handlers/required_labels.ts @@ -0,0 +1,43 @@ +import { PullRequestLabeledEvent, PullRequestUnlabeledEvent } from '@octokit/webhooks-types'; +import { EventType, HomeAssistantRepository, Repository } from '../github-webhook.const'; +import { WebhookContext } from '../github-webhook.model'; +import { BaseWebhookHandler } from './base'; + +export const LabelsToCheck: { + [key in Repository]?: string[]; +} = { + [HomeAssistantRepository.CORE]: [ + 'breaking-change', + 'bugfix', + 'code-quality', + 'dependency', + 'deprecation', + 'new-feature', + 'new-integration', + ], +}; + +export class RequiredLabels extends BaseWebhookHandler { + public allowedEventTypes = [ + EventType.PULL_REQUEST_LABELED, + EventType.PULL_REQUEST_UNLABELED, + EventType.PULL_REQUEST_SYNCHRONIZE, + ]; + public allowedRepositories = Object.keys(LabelsToCheck) as Repository[]; + + async handle(context: WebhookContext) { + const currentLabels = new Set(context.payload.pull_request.labels.map((label) => label.name)); + const requiredLabels = LabelsToCheck[context.repository]; + + const hasRequiredLabels = requiredLabels.some((label) => currentLabels.has(label)); + + await context.github.repos.createCommitStatus( + context.repo({ + sha: context.payload.pull_request.head.sha, + context: 'required-labels', + state: hasRequiredLabels ? 'success' : 'failure', + description: `Has at least one of the required labels (${requiredLabels.join(', ')})`, + }), + ); + } +}