-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request changes when there is a conflict (#208)
- Loading branch information
Showing
4 changed files
with
88 additions
and
0 deletions.
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
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
27 changes: 27 additions & 0 deletions
27
services/bots/src/github-webhook/handlers/merge_conflict.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,27 @@ | ||
import { PullRequestOpenedEvent, PullRequestSynchronizeEvent } from '@octokit/webhooks-types'; | ||
import { EventType, HomeAssistantRepository } from '../github-webhook.const'; | ||
import { WebhookContext } from '../github-webhook.model'; | ||
import { BaseWebhookHandler } from './base'; | ||
|
||
export class MergeConflictChecker extends BaseWebhookHandler { | ||
public allowedEventTypes = [EventType.PULL_REQUEST_OPENED, EventType.PULL_REQUEST_SYNCHRONIZE]; | ||
public allowedRepositories = [HomeAssistantRepository.CORE]; | ||
|
||
async handle(context: WebhookContext<PullRequestOpenedEvent | PullRequestSynchronizeEvent>) { | ||
// The data in the event is stale, so we need to re-fetch it. | ||
const { data: pullRequest } = await context.github.pulls.get(context.pullRequest()); | ||
|
||
if (pullRequest.mergeable_state !== 'dirty') { | ||
// The Pull request is not dirty. | ||
return; | ||
} | ||
|
||
// Create a review with a comment to let the user know that there is a merge conflict. | ||
await context.github.pulls.createReview( | ||
context.pullRequest({ | ||
body: `There is a merge conflict.`, | ||
event: 'REQUEST_CHANGES', | ||
}), | ||
); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
tests/services/bots/github-webhook/handlers/merge_conflict.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,57 @@ | ||
// @ts-nocheck | ||
import * as assert from 'assert'; | ||
import { WebhookContext } from '../../../../../bots/src/github-webhook/github-webhook.model'; | ||
import { MergeConflictChecker } from '../../../../../services/bots/src/github-webhook/handlers/merge_conflict'; | ||
import { mockWebhookContext } from '../../../../utils/test_context'; | ||
import { loadJsonFixture } from '../../../../utils/fixture'; | ||
|
||
describe('MergeConflictChecker', () => { | ||
let handler: MergeConflictChecker; | ||
let mockContext: WebhookContext<any>; | ||
let getPullResponse: any; | ||
let createReviewContents: PullRequestCreateReviewParams; | ||
|
||
beforeEach(function () { | ||
handler = new MergeConflictChecker(); | ||
getPullResponse = { data: {} }; | ||
createReviewContents = {}; | ||
mockContext = mockWebhookContext({ | ||
eventType: 'pull_request.opened', | ||
payload: loadJsonFixture('pull_request.opened', {}), | ||
github: { | ||
pulls: { | ||
async get() { | ||
return getPullResponse; | ||
}, | ||
async createReview(params: PullRequestCreateReviewParams) { | ||
createReviewContents = params; | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('PR with clean state', async () => { | ||
mockContext.github.pulls.createReview = jest.fn(); | ||
getPullResponse = { data: { mergeable_state: 'clean' } }; | ||
|
||
await handler.handle(mockContext); | ||
|
||
assert.deepStrictEqual(createReviewContents, {}); | ||
expect(mockContext.github.pulls.createReview).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('PR with dirty state', async () => { | ||
getPullResponse = { data: { mergeable_state: 'dirty' } }; | ||
|
||
await handler.handle(mockContext); | ||
|
||
assert.deepStrictEqual(createReviewContents, { | ||
body: 'There is a merge conflict.', | ||
event: 'REQUEST_CHANGES', | ||
owner: 'Codertocat', | ||
pull_number: 2, | ||
repo: 'Hello-World', | ||
}); | ||
}); | ||
}); |