Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handler for blocking labels #236

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5,6 +5,7 @@ import { GithubWebhooksModule } from '@dev-thought/nestjs-github-webhooks';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AppConfig } from '../config';
import { GithubWebhookController } from './github-webhook.controller';
import { BlockingLabels } from './handlers/blocking_labels';
import { BranchLabels } from './handlers/branch_labels';
import { CodeOwnersMention } from './handlers/code_owners_mention';
import { DependencyBump } from './handlers/dependency_bump';
Expand All @@ -29,6 +30,7 @@ import { ValidateCla } from './handlers/validate-cla';

@Module({
providers: [
BlockingLabels,
BranchLabels,
CodeOwnersMention,
DependencyBump,
Expand Down
33 changes: 33 additions & 0 deletions services/bots/src/github-webhook/handlers/blocking_labels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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]?: Record<string, { message: string; success?: string }>;
} = {
[HomeAssistantRepository.CORE]: {
'awaiting-frontend': { message: 'This PR is awaiting changes to the frontend' },
},
};

export class BlockingLabels extends BaseWebhookHandler {
public allowedEventTypes = [EventType.PULL_REQUEST_LABELED, EventType.PULL_REQUEST_UNLABELED];
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));

for (const [label, description] of Object.entries(LabelsToCheck[context.repository] || {})) {
const hasBlockingLabel = currentLabels.has(label);
await context.github.repos.createCommitStatus(
context.repo({
sha: context.payload.pull_request.head.sha,
context: `blocking-label-${label}`,
state: hasBlockingLabel ? 'failure' : 'success',
description: hasBlockingLabel ? description['message'] : description['success'] || 'OK',
}),
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { WebhookContext } from '../../../../../services/bots/src/github-webhook/github-webhook.model';
import {
BlockingLabels,
LabelsToCheck,
} from '../../../../../services/bots/src/github-webhook/handlers/blocking_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('BlockingLabels', () => {
let handler: BlockingLabels;
let mockContext: WebhookContext<any>;
let createCommitStatusCall: any;

beforeEach(function () {
handler = new BlockingLabels();
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 label of Object.keys(lables)) {
for (const result of ['success', 'failure']) {
const description =
LabelsToCheck[repository][label][result === 'failure' ? 'message' : 'success'] || 'OK';
it(`Validate handling ${label} for ${repository} with ${result} result (${description})`, async () => {
mockContext.payload = loadJsonFixture('pull_request.opened', {
pull_request: { labels: result === 'failure' ? [{ name: label }] : [] },
});
mockContext.repository = repository as Repository;
await handler.handle(mockContext);

expect(mockContext.github.repos.createCommitStatus).toHaveBeenCalledWith(
expect.objectContaining({
context: `blocking-label-${label}`,
description,
state: result,
}),
);
});
}
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,46 @@ describe('GithubWebhookModule', () => {
},
{
eventType: EventType.PULL_REQUEST_UNLABELED,
handlers: ['DocsMissing', 'PlatinumReview'],
handlers: ['BlockingLabels', 'DocsMissing', 'PlatinumReview'],
payload: {
repository: { full_name: 'home-assistant/core', owner: { login: 'home-assistant' } },
},
},
{
eventType: EventType.PULL_REQUEST_LABELED,
handlers: [
'BlockingLabels',
'CodeOwnersMention',
'DocsMissing',
'NewIntegrationsHandler',
'PlatinumReview',
'QualityScaleLabeler',
'ValidateCla',
],
payload: {
repository: { full_name: 'home-assistant/core', owner: { login: 'home-assistant' } },
},
},
{
eventType: EventType.PULL_REQUEST_UNLABELED,
handlers: [],
payload: {
repository: {
full_name: 'home-assistant/home-assistant.io',
owner: { login: 'home-assistant' },
},
},
},
{
eventType: EventType.PULL_REQUEST_LABELED,
handlers: ['CodeOwnersMention', 'ValidateCla'],
payload: {
repository: {
full_name: 'home-assistant/home-assistant.io',
owner: { login: 'home-assistant' },
},
},
},
{
eventType: EventType.PULL_REQUEST_REVIEW_SUBMITTED,
handlers: ['ReviewDrafter'],
Expand Down Expand Up @@ -185,6 +220,13 @@ describe('GithubWebhookModule', () => {
repository: { full_name: 'esphome/esphome', owner: { login: 'esphome' } },
},
},
{
eventType: EventType.PULL_REQUEST_LABELED,
handlers: [],
payload: {
repository: { full_name: 'esphome/esphome', owner: { login: 'esphome' } },
},
},
] as {
eventType: EventType;
payload: Record<string, any>;
Expand Down