Skip to content

Commit

Permalink
Add RequiredLabels handler (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Sep 11, 2024
1 parent a2fb976 commit f2abe67
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 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(', ')})`,
}),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { WebhookContext } from '../../../../../services/bots/src/github-webhook/github-webhook.model';
import {
RequiredLabels,
LabelsToCheck,
} from '../../../../../services/bots/src/github-webhook/handlers/required_labels';
import { loadJsonFixture } from '../../../../utils/fixture';
import { mockWebhookContext } from '../../../../utils/test_context';
import {
ESPHomeRepository,
EventType,
HomeAssistantRepository,
Repository,
} from '../../../../../services/bots/src/github-webhook/github-webhook.const';

describe('RequiredLabels', () => {
let handler: RequiredLabels;
let mockContext: WebhookContext<any>;
let createCommitStatusCall: any;

beforeEach(function () {
handler = new RequiredLabels();
createCommitStatusCall = {};
mockContext = mockWebhookContext({
payload: loadJsonFixture('pull_request.opened'),
eventType: EventType.PULL_REQUEST_LABELED,
// @ts-ignore partial mock
github: {
repos: {
// @ts-ignore partial mock
createCommitStatus: jest.fn(),
},
},
});
});

for (const [repository, lables] of Object.entries(LabelsToCheck)) {
for (const result of ['success', 'failure']) {
it(`Validate handling ${result} for ${repository} with ${lables}`, async () => {
mockContext.payload = loadJsonFixture('pull_request.opened', {
pull_request: { labels: result === 'success' ? [{ name: lables[0] }] : [] },
});
mockContext.repository = repository as Repository;
await handler.handle(mockContext);

expect(mockContext.github.repos.createCommitStatus).toHaveBeenCalledWith(
expect.objectContaining({
context: 'required-labels',
description: `Has at least one of the required labels (${lables.join(', ')})`,
state: result,
}),
);
});
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,20 @@ describe('GithubWebhookModule', () => {
},
{
eventType: EventType.PULL_REQUEST_SYNCHRONIZE,
handlers: ['DocsMissing', 'MergeConflictChecker', 'PlatinumReview', 'ValidateCla'],
handlers: [
'DocsMissing',
'MergeConflictChecker',
'PlatinumReview',
'RequiredLabels',
'ValidateCla',
],
payload: {
repository: { full_name: 'home-assistant/core', owner: { login: 'home-assistant' } },
},
},
{
eventType: EventType.PULL_REQUEST_UNLABELED,
handlers: ['BlockingLabels', 'DocsMissing', 'PlatinumReview'],
handlers: ['BlockingLabels', 'DocsMissing', 'RequiredLabels', 'PlatinumReview'],
payload: {
repository: { full_name: 'home-assistant/core', owner: { login: 'home-assistant' } },
},
Expand All @@ -124,6 +130,7 @@ describe('GithubWebhookModule', () => {
'NewIntegrationsHandler',
'PlatinumReview',
'QualityScaleLabeler',
'RequiredLabels',
'ValidateCla',
],
payload: {
Expand Down

0 comments on commit f2abe67

Please sign in to comment.