Skip to content

Commit

Permalink
Add RequiredLabels handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Sep 11, 2024
1 parent a2fb976 commit d59a7ee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
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 @@ -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';
Expand All @@ -48,6 +49,7 @@ import { ValidateCla } from './handlers/validate-cla';
NewIntegrationsHandler,
PlatinumReview,
QualityScaleLabeler,
RequiredLabels,
ReviewDrafter,
SetDocumentationSection,
SetIntegration,
Expand Down
43 changes: 43 additions & 0 deletions services/bots/src/github-webhook/handlers/required_labels.ts
Original file line number Diff line number Diff line change
@@ -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<PullRequestLabeledEvent | PullRequestUnlabeledEvent>) {
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(', ')})`,
}),
);
}
}

0 comments on commit d59a7ee

Please sign in to comment.