-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handler for blocking labels (#236)
- Loading branch information
Showing
4 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
services/bots/src/github-webhook/handlers/blocking_labels.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}), | ||
); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
tests/services/bots/github-webhook/handlers/blocking_labels.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}), | ||
); | ||
}); | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters