Skip to content

Commit

Permalink
Request changes when there is a conflict (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Oct 3, 2023
1 parent 3003115 commit 5ddfe69
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions services/bots/src/github-webhook/github-webhook.const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type GetIssueResponse = RestEndpointMethodTypes['issues']['get']['respons
export type GetIssueLabelParams = RestEndpointMethodTypes['issues']['getLabel']['parameters'];
export type GetIssueLabelResponse =
RestEndpointMethodTypes['issues']['getLabel']['response']['data'];
export type PullRequestCreateReviewParams =
RestEndpointMethodTypes['pulls']['createReview']['parameters'];

export type Repository = HomeAssistantRepository;

Expand Down
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 @@ -17,6 +17,7 @@ import { IssueCommentCommands } from './handlers/issue_comment_commands/handler'
import { IssueLinks } from './handlers/issue_links';
import { LabelBot } from './handlers/label_bot/handler';
import { LabelCleaner } from './handlers/label_cleaner';
import { MergeConflictChecker } from './handlers/merge_conflict';
import { MonthOfWTH } from './handlers/month_of_wth';
import { NewIntegrationsHandler } from './handlers/new_integrations';
import { PlatinumReview } from './handlers/platinum_review';
Expand All @@ -40,6 +41,7 @@ import { ValidateCla } from './handlers/validate-cla';
IssueLinks,
LabelBot,
LabelCleaner,
MergeConflictChecker,
MonthOfWTH,
NewIntegrationsHandler,
PlatinumReview,
Expand Down
27 changes: 27 additions & 0 deletions services/bots/src/github-webhook/handlers/merge_conflict.ts
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 tests/services/bots/github-webhook/handlers/merge_conflict.spec.ts
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',
});
});
});

0 comments on commit 5ddfe69

Please sign in to comment.