Skip to content

Rename query and add unit tests. #1

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

Merged
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
4 changes: 2 additions & 2 deletions lib/pr_checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,14 @@ export default class PRChecker {
async checkCommitsAfterReviewOrLabel() {
if (this.checkCommitsAfterReview()) return true;

await Promise.all([this.data.getLabels(), this.data.getCollaborators()]);
await Promise.all([this.data.getLabeledEvents(), this.data.getCollaborators()]);

const {
cli, data, pr
} = this;

const { updatedAt } = pr.timelineItems;
const requestCiLabels = data.labels.findLast(
const requestCiLabels = data.labeledEvents.findLast(
({ createdAt, label: { name } }) => name === 'request-ci' && createdAt > updatedAt
);
if (requestCiLabels == null) return false;
Expand Down
7 changes: 4 additions & 3 deletions lib/pr_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from './user_status.js';

// lib/queries/*.gql file names
const LABELS_QUERY = 'PRLabels';
const LABELED_EVENTS_QUERY = 'PRLabeledEvents';
const PR_QUERY = 'PR';
const REVIEWS_QUERY = 'Reviews';
const COMMENTS_QUERY = 'PRComments';
Expand Down Expand Up @@ -34,6 +34,7 @@ export default class PRData {
this.comments = [];
this.commits = [];
this.reviewers = [];
this.labeledEvents = [];
}

getThread() {
Expand Down Expand Up @@ -91,11 +92,11 @@ export default class PRData {
]);
}

async getLabels() {
async getLabeledEvents() {
const { prid, owner, repo, cli, request, prStr } = this;
const vars = { prid, owner, repo };
cli.updateSpinner(`Getting labels from ${prStr}`);
this.labels = (await request.gql(LABELS_QUERY, vars))
this.labeledEvents = (await request.gql(LABELED_EVENTS_QUERY, vars))
.repository.pullRequest.timelineItems.nodes;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
query PRLabels($prid: Int!, $owner: String!, $repo: String!, $after: String) {
query PRLabeledEvents($prid: Int!, $owner: String!, $repo: String!, $after: String) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $prid) {
timelineItems(itemTypes: LABELED_EVENT, after: $after, last: 100) {
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,12 @@ for (const subdir of readdirSync(path('./jenkins'))) {
readJSON(`./jenkins/${subdir}/${item}`);
}
};

export const labeledEvents = {};

for (const item of readdirSync(path('./labeled_events'))) {
if (!item.endsWith('.json')) {
continue;
}
labeledEvents[basename(item, '.json')] = readJSON(`./labeled_events/${item}`);
}
1 change: 1 addition & 0 deletions test/fixtures/first_timer_pr.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
}
]
},
"timelineItems": { "updatedAt": "2017-10-24T11:13:43Z" },
"title": "test: awesome changes",
"baseRefName": "main",
"headRefName": "awesome-changes"
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/labeled_events/no-request-ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"actor": { "login": "nodejs-github-bot" },
"label": { "name": "doc" },
"createdAt": "2024-05-13T15:57:10Z"
},
{
"actor": { "login": "nodejs-github-bot" },
"label": { "name": "test_runner" },
"createdAt": "2024-05-13T15:57:10Z"
}
]
12 changes: 12 additions & 0 deletions test/fixtures/labeled_events/old-request-ci-collaborator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"actor": { "login": "nodejs-github-bot" },
"label": { "name": "doc" },
"createdAt": "2024-05-13T15:57:10Z"
},
{
"actor": { "login": "foo" },
"label": { "name": "request-ci" },
"createdAt": "1999-10-24T11:13:43Z"
}
]
12 changes: 12 additions & 0 deletions test/fixtures/labeled_events/recent-request-ci-collaborator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"actor": { "login": "nodejs-github-bot" },
"label": { "name": "doc" },
"createdAt": "2024-05-13T15:57:10Z"
},
{
"actor": { "login": "foo" },
"label": { "name": "request-ci" },
"createdAt": "2024-05-13T15:57:10Z"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"actor": { "login": "nodejs-github-bot" },
"label": { "name": "doc" },
"createdAt": "2024-05-13T15:57:10Z"
},
{
"actor": { "login": "random-person" },
"label": { "name": "request-ci" },
"createdAt": "2024-05-13T15:57:10Z"
}
]
95 changes: 95 additions & 0 deletions test/unit/pr_checker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
semverMajorPR,
conflictingPR,
closedPR,
labeledEvents,
mergedPR,
pullRequests
} from '../fixtures/data.js';
Expand Down Expand Up @@ -2048,6 +2049,100 @@ describe('PRChecker', () => {
});
});

describe('checkCommitsAfterReviewOrLabel', () => {
it('should return true if PR passes post review checks', async() => {
const cli = new TestCLI();
const checker = new PRChecker(cli, {
pr: semverMajorPR,
reviewers: allGreenReviewers,
comments: commentsWithLGTM,
reviews: approvingReviews,
commits: simpleCommits,
collaborators,
authorIsNew: () => true,
getThread: PRData.prototype.getThread
}, {}, argv);

const status = await checker.checkCommitsAfterReviewOrLabel();
assert.strictEqual(status, true);
});

describe('without approvals', () => {
const data = {
pr: firstTimerPR,
reviewers: noReviewers,
comments: [],
reviews: [],
commits: [],
collaborators: [],
labeledEvents: [],
authorIsNew: () => true,
getThread: PRData.prototype.getThread,
getLabeledEvents: async() => {
data.labeledEvents = [];
},
getCollaborators: async() => {
data.collaborators = collaborators;
}
};

it('should return false if PR has no labels', async() => {
const cli = new TestCLI();
data.getLabeledEvents = async() => {
data.labeledEvents = [];
};
const checker = new PRChecker(cli, data, {}, argv);

const status = await checker.checkCommitsAfterReviewOrLabel();
assert.strictEqual(status, false);
});

it('should return false if PR has no request-ci label', async() => {
const cli = new TestCLI();
data.getLabeledEvents = async() => {
data.labeledEvents = labeledEvents['no-request-ci'];
};
const checker = new PRChecker(cli, data, {}, argv);

const status = await checker.checkCommitsAfterReviewOrLabel();
assert.strictEqual(status, false);
});

it('should return false if PR has request-ci from non-collaborator', async() => {
const cli = new TestCLI();
data.getLabeledEvents = async() => {
data.labeledEvents = labeledEvents['recent-request-ci-non-collaborator'];
};
const checker = new PRChecker(cli, data, {}, argv);

const status = await checker.checkCommitsAfterReviewOrLabel();
assert.strictEqual(status, false);
});

it('should return false if PR has outdated request-ci from a collaborator', async() => {
const cli = new TestCLI();
data.getLabeledEvents = async() => {
data.labeledEvents = labeledEvents['old-request-ci-collaborator'];
};
const checker = new PRChecker(cli, data, {}, argv);

const status = await checker.checkCommitsAfterReviewOrLabel();
assert.strictEqual(status, false);
});

it('should return true if PR has recent request-ci from a collaborator', async() => {
const cli = new TestCLI();
data.getLabeledEvents = async() => {
data.labeledEvents = labeledEvents['recent-request-ci-collaborator'];
};
const checker = new PRChecker(cli, data, {}, argv);

const status = await checker.checkCommitsAfterReviewOrLabel();
assert.strictEqual(status, true);
});
});
});

describe('checkCommitsAfterReview', () => {
const cli = new TestCLI();

Expand Down
Loading