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

fix: properly detect completed deprecation checklist #150

Merged
merged 1 commit into from
Aug 24, 2023
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
124 changes: 68 additions & 56 deletions spec/deprecation-review-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '../src/constants';

import { CheckRunStatus } from '../src/enums';
import { loadFixture } from './utils';

const handler = async (app: Probot) => {
setupDeprecationReviewStateManagement(app);
Expand All @@ -33,6 +34,7 @@ describe('deprecation review', () => {
let moctokit: any;

beforeEach(() => {
nock.cleanAll();
nock.disableNetConnect();
robot = new Probot({
githubToken: 'test',
Expand Down Expand Up @@ -66,6 +68,8 @@ describe('deprecation review', () => {
});

afterEach(() => {
console.log(nock.pendingMocks());
expect(nock.isDone()).toEqual(true);
nock.cleanAll();
});

Expand All @@ -82,23 +86,23 @@ describe('deprecation review', () => {

describe('isChecklistComment', () => {
it('should return true for the checklist comment', () => {
const {
comment,
} = require('./fixtures/deprecation-review-state/issue_comment.checklist_complete.json');
const { comment } = loadFixture(
'deprecation-review-state/issue_comment.checklist_complete.json',
);
expect(isChecklistComment(comment)).toEqual(true);
});

it('should false true for any other comment', () => {
const { comment } = require('./fixtures/deprecation-review-state/issue_comment.edited.json');
const { comment } = loadFixture('deprecation-review-state/issue_comment.edited.json');
expect(isChecklistComment(comment)).toEqual(false);
});
});

describe('addOrUpdateDeprecationReviewCheck', () => {
it('should reset the check when PR does not have a deprecation review label', async () => {
const {
pull_request,
} = require('./fixtures/deprecation-review-state/pull_request.no_review_label.json');
const { pull_request } = loadFixture(
'deprecation-review-state/pull_request.no_review_label.json',
);

moctokit.checks.listForRef = jest.fn().mockReturnValue({
data: {
Expand Down Expand Up @@ -132,9 +136,9 @@ describe('deprecation review', () => {
});

it(`should create the check for a PR with the ${DEPRECATION_REVIEW_LABELS.REQUESTED} label`, async () => {
const {
pull_request,
} = require('./fixtures/deprecation-review-state/pull_request.requested_review_label.json');
const { pull_request } = loadFixture(
'deprecation-review-state/pull_request.requested_review_label.json',
);

moctokit.checks.listForRef = jest.fn().mockReturnValue({
data: {
Expand All @@ -156,9 +160,9 @@ describe('deprecation review', () => {
});

it('should not use Checks API when the PR is from a fork', async () => {
const {
pull_request,
} = require('./fixtures/deprecation-review-state/pull_request.requested_review_label.json');
const { pull_request } = loadFixture(
'deprecation-review-state/pull_request.requested_review_label.json',
);

pull_request.head.repo.fork = true;

Expand All @@ -168,7 +172,9 @@ describe('deprecation review', () => {
});

it(`should correctly update deprecation review check for ${DEPRECATION_REVIEW_LABELS.COMPLETE} label`, async () => {
const payload = require('./fixtures/deprecation-review-state/pull_request.review_complete_label.json');
const payload = loadFixture(
'deprecation-review-state/pull_request.review_complete_label.json',
);

moctokit.checks.listForRef = jest.fn().mockReturnValue({
data: {
Expand Down Expand Up @@ -198,9 +204,9 @@ describe('deprecation review', () => {

describe('maybeAddChecklistComment', () => {
it('should comment on the PR when Deprecation Review is requested', async () => {
const {
pull_request,
} = require('./fixtures/deprecation-review-state/pull_request.requested_review_label.json');
const { pull_request } = loadFixture(
'deprecation-review-state/pull_request.requested_review_label.json',
);

await maybeAddChecklistComment(moctokit, pull_request);
expect(moctokit.issues.createComment).toHaveBeenCalledWith(
Expand All @@ -211,18 +217,18 @@ describe('deprecation review', () => {
});

it('should not comment on a PR when no Deprecation Review is requested', async () => {
const {
pull_request,
} = require('./fixtures/deprecation-review-state/pull_request.no_review_label.json');
const { pull_request } = loadFixture(
'deprecation-review-state/pull_request.no_review_label.json',
);

await maybeAddChecklistComment(moctokit, pull_request);
expect(moctokit.issues.createComment).not.toHaveBeenCalled();
});

it('should not comment on the PR when the comment already exists', async () => {
const {
pull_request,
} = require('./fixtures/deprecation-review-state/pull_request.requested_review_label.json');
const { pull_request } = loadFixture(
'deprecation-review-state/pull_request.requested_review_label.json',
);

moctokit.issues.listComments = jest.fn().mockReturnValue({
data: [
Expand All @@ -241,7 +247,9 @@ describe('deprecation review', () => {
});

it(`creates the deprecation review check and comments when review is requested`, async () => {
const payload = require('./fixtures/deprecation-review-state/pull_request.requested_review_label.json');
const payload = loadFixture(
'deprecation-review-state/pull_request.requested_review_label.json',
);

nock('https://api.github.com')
.get(
Expand All @@ -267,7 +275,7 @@ describe('deprecation review', () => {
};

nock('https://api.github.com')
.post(`/repos/dsanders11/deprecation-review/check-runs/`, (body) => {
.post('/repos/dsanders11/deprecation-review/check-runs', (body) => {
expect(body).toMatchObject(expected);
return true;
})
Expand All @@ -291,7 +299,7 @@ describe('deprecation review', () => {
});

it(`correctly updates deprecation review check when no review labels are found`, async () => {
const payload = require('./fixtures/deprecation-review-state/pull_request.no_review_label.json');
const payload = loadFixture('deprecation-review-state/pull_request.no_review_label.json');

nock('https://api.github.com')
.get(
Expand Down Expand Up @@ -331,7 +339,7 @@ describe('deprecation review', () => {
});

it('correctly updates deprecation review check and deprecation review label when pr is unlabeled', async () => {
const payload = require('./fixtures/deprecation-review-state/pull_request.unlabeled.json');
const payload = loadFixture('deprecation-review-state/pull_request.unlabeled.json');

nock('https://api.github.com')
.get(
Expand All @@ -354,53 +362,57 @@ describe('deprecation review', () => {
});

it('does nothing for an edited comment that is not the deprecation checklist', async () => {
const payload = require('./fixtures/deprecation-review-state/issue_comment.edited.json');
const payload = loadFixture('deprecation-review-state/issue_comment.edited.json');

await robot.receive({
id: '123-456',
name: 'issue_comment',
payload,
});
});

it('does nothing when checklist is incomplete', async () => {
const payload = loadFixture('deprecation-review-state/issue_comment.checklist_incomplete.json');

await robot.receive({
id: '123-456',
name: 'issue_comment',
payload,
});
expect(nock.isDone()).toEqual(true);
});

it('correctly updates deprecation review check and deprecation review label when checklist complete', async () => {
const payload = require('./fixtures/deprecation-review-state/issue_comment.checklist_complete.json');
const payload = loadFixture('deprecation-review-state/issue_comment.checklist_complete.json');

nock('https://api.github.com')
.get(
`/repos/dsanders11/deprecation-review/issues/${payload.number}/labels?per_page=100&page=1`,
`/repos/dsanders11/deprecation-review/issues/${payload.issue.number}/labels?per_page=100&page=1`,
)
.reply(200, [DEPRECATION_REVIEW_LABELS.REQUESTED]);
.reply(200, [{ name: DEPRECATION_REVIEW_LABELS.REQUESTED }])
.get(
`/repos/dsanders11/deprecation-review/issues/${payload.issue.number}/labels?per_page=100&page=1`,
)
.reply(200, [
{ name: DEPRECATION_REVIEW_LABELS.REQUESTED },
{ name: DEPRECATION_REVIEW_LABELS.COMPLETE },
]);

nock('https://api.github.com')
.post(`/repos/dsanders11/deprecation-review/issues/${payload.number}/labels`, (body) => {
expect(body).toEqual([DEPRECATION_REVIEW_LABELS.REQUESTED]);
return true;
})
.reply(200)
.post(`/repos/dsanders11/deprecation-review/issues/${payload.number}/labels`, (body) => {
expect(body).toEqual([DEPRECATION_REVIEW_LABELS.COMPLETE]);
return true;
})
.post(
`/repos/dsanders11/deprecation-review/issues/${payload.issue.number}/labels`,
(body) => {
expect(body).toEqual([DEPRECATION_REVIEW_LABELS.COMPLETE]);
return true;
},
)
.reply(200);

const expected = {
name: DEPRECATION_REVIEW_CHECK_NAME,
status: 'completed',
output: {
title: 'Complete',
summary: 'All review items have been checked off',
},
conclusion: CheckRunStatus.SUCCESS,
};

const encoded = encodeURIComponent(DEPRECATION_REVIEW_LABELS.REQUESTED);
nock('https://api.github.com')
.patch(`/repos/dsanders11/deprecation-review/check-runs/12345`, (body) => {
expect(body).toMatchObject(expected);
return true;
})
.reply(200);
.delete(
`/repos/dsanders11/deprecation-review/issues/${payload.issue.number}/labels/${encoded}`,
)
.reply(200, [{ name: DEPRECATION_REVIEW_LABELS.COMPLETE }]);

await robot.receive({
id: '123-456',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@
"type": "User",
"site_admin": false
},
"labels": [],
"labels": [
{
"id": 5824334761,
"node_id": "LA_kwDOKE6NNM8AAAABWyhLqQ",
"url": "https://api.github.com/repos/dsanders11/deprecation-review/labels/deprecation-review/requested%20%F0%9F%93%9D",
"name": "deprecation-review/requested 📝",
"color": "c5def5",
"default": false,
"description": ""
}
],
"state": "open",
"locked": false,
"assignee": null,
Expand Down
Loading