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

Implement PR reviewed checks #116

Merged
merged 17 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const editEvent = 'edited';
const issuesLabelEvent = 'issues_labeled';
const issuesAssignedEvent = 'issues_assigned';
const pushEvent = 'push';
const pullRequestReviewEvent = 'pr-review';
const checkCompletedEvent = 'check_completed';

const claCheck = 'cla-check';
Expand All @@ -27,6 +28,7 @@ const modelCheck = 'model-check';
const issuesLabelCheck = 'issues-labeled-check';
const issuesAssignedCheck = 'issues-assigned-check';
const forcePushCheck = 'force-push-check';
const pullRequestReviewCheck = 'pr-review-check';
const ciFailureCheck = 'ci-failure-check';
const updateWithDevelopCheck = 'update-with-develop-check';

Expand Down Expand Up @@ -66,6 +68,7 @@ const checksWhitelist = {
[issuesAssignedEvent]: [issuesAssignedCheck],
[unlabelEvent]: [datastoreLabelCheck],
[pushEvent]: [forcePushCheck],
[pullRequestReviewEvent]: [pullRequestReviewCheck],
[checkCompletedEvent]: [ciFailureCheck]
},
'oppiabot': {
Expand All @@ -92,6 +95,7 @@ module.exports.editEvent = editEvent;
module.exports.issuesLabelEvent = issuesLabelEvent;
module.exports.issuesAssignedEvent = issuesAssignedEvent;
module.exports.pushEvent = pushEvent;
module.exports.pullRequestReviewEvent = pullRequestReviewEvent;
module.exports.checkCompletedEvent = checkCompletedEvent;

module.exports.claCheck = claCheck;
Expand All @@ -109,6 +113,7 @@ module.exports.datastoreLabelCheck = datastoreLabelCheck;
module.exports.prLabelCheck = prLabelCheck;
module.exports.prTemplateCheck = prTemplateCheck;
module.exports.forcePushCheck = forcePushCheck;
module.exports.pullRequestReviewCheck = pullRequestReviewCheck;
module.exports.ciFailureCheck = ciFailureCheck;
module.exports.updateWithDevelopCheck = updateWithDevelopCheck;

Expand Down
570 changes: 570 additions & 0 deletions fixtures/pullRequestReview.json

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const checkPullRequestJobModule = require('./lib/checkPullRequestJob');
const checkPullRequestTemplateModule = require('./lib/checkPullRequestTemplate');
const checkCriticalPullRequestModule = require('./lib/checkCriticalPullRequest');
const checkBranchPushModule = require('./lib/checkBranchPush');
const checkPullRequestReviewModule = require('./lib/checkPullRequestReview');
const ciCheckModule = require('./lib/ciChecks');

const constants = require('./constants');
Expand Down Expand Up @@ -83,6 +84,9 @@ const runChecks = async (context, checkEvent) => {
case constants.prTemplateCheck:
await checkPullRequestTemplateModule.checkTemplate(context);
break;
case constants.pullRequestReviewCheck:
await checkPullRequestReviewModule.handlePullRequestReview(context);
break;
case constants.ciFailureCheck:
await ciCheckModule.handleFailure(context);
break;
Expand Down Expand Up @@ -202,6 +206,13 @@ module.exports = (oppiabot) => {
}
});

oppiabot.on('pull_request_review.submitted', async (context) => {
if(checkWhitelistedAccounts(context)) {
console.log('A Pull Request got reviewed');
await runChecks(context, constants.pullRequestReviewEvent);
}
});

oppiabot.on('check_suite.completed', async (context) => {
if(checkWhitelistedAccounts(context)) {
// eslint-disable-next-line no-console
Expand Down
9 changes: 3 additions & 6 deletions lib/checkBranchPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
/**
* @fileoverview File to handle checks when a branch gets forced pushed.
*/

const { sleep } = require('./utils');

const WHITELISTED_BRANCH_PREFIXES = ['develop', 'release-'];
/**
* @param {Number} ms - Sleep time.
*/
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* @param {import('probot').Context} context
*/
Expand Down
4 changes: 1 addition & 3 deletions lib/checkMergeConflicts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const { sleep } = require('./utils');
const mergeConflictLabel = "PR: don't merge - HAS MERGE CONFLICTS";
const mergeConflictLabelArray = [mergeConflictLabel];
const mergeAllPRsWithDevelopLabel = 'PR: require post-merge sync to HEAD';
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
* @param {import('probot').Context} context
Expand Down
59 changes: 11 additions & 48 deletions lib/checkPullRequestLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
/**
* @fileoverview File to handle checks when a PR gets labeled.
*/
const { DATASTORE_LABEL } = require('./utils');
const {
DATASTORE_LABEL,
getProjectOwnerFromLabel,
canAssignProjectOwner,
getChangelogLabelFromPullRequest
} = require('./utils');
const {
releaseCoordinators: RELEASE_COORDINATORS,
SERVER_JOBS_ADMIN,
} = require('../userWhitelist.json');
const PR_LABELS = ['dependencies', 'stale'];
const DEFAULT_CHANGELOG_LABEL = 'PR CHANGELOG: Miscellaneous -- @ankita240796';
const LABELS_EXCLUDED_FROM_CODEOWNER_ASSIGNMENT = [
'PR CHANGELOG: Angular Migration'
]

/**
* This function tests a changelog label against a regex.
*
Expand All @@ -38,40 +41,6 @@ var matchChangelogLabelWithRegex = function(changelogLabel) {
return regExpForChangelogLabel.test(changelogLabel);
};

/**
* This function checks if the project owner can be assigned to a PR.
*
* @param {import('probot').Octokit.PullsGetResponse} pullRequest
* @param {String} changelogLabel
*/
const canAssignProjectOwner = (pullRequest, changelogLabel) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we moving this to utils?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was done before I decided to assign the last reviewer. I'll move it back here.

// Check if the changelog label is excluded from codeowner assignment.
const isLabelExcluded = LABELS_EXCLUDED_FROM_CODEOWNER_ASSIGNMENT.some(
(label) => changelogLabel.startsWith(label)
);
if (isLabelExcluded) {
return true;
}
const projectOwner = getProjectOwner(changelogLabel);
const reviewers = pullRequest.requested_reviewers.map(
reviewer => reviewer.login
);
const canReview = reviewers.includes(projectOwner);

return canReview;
};

/**
* This function is used to obtain the project owner from a changelog label.
*
* @param {String} changelogLabel
*/
const getProjectOwner = (changelogLabel) => {
const labelSubstrings = changelogLabel.split('@');
const projectOwner = labelSubstrings[labelSubstrings.length - 1].trim();
return projectOwner
};


/**
* This function assigns pull request to the appropriate reviewers.
Expand All @@ -81,7 +50,7 @@ const getProjectOwner = (changelogLabel) => {
const assignAllCodeowners = async (context) => {
// Github automatically requests for review from codeowners.
/**
* @type {import('probot').Octokit.PullsGetResponse} context
* @type {import('probot').Octokit.PullsGetResponse} pullRequest
*/
const pullRequest = context.payload.pull_request;
const assignees = pullRequest.requested_reviewers.map(
Expand Down Expand Up @@ -176,7 +145,7 @@ module.exports.checkAssignee = async function(context) {
return;
}

const projectOwner = getProjectOwner(changelogLabel);
const projectOwner = getProjectOwnerFromLabel(changelogLabel);

const shouldAssignProjectOwner = canAssignProjectOwner(
pullRequest, changelogLabel);
Expand Down Expand Up @@ -222,15 +191,9 @@ module.exports.checkChangelogLabel = async function(context) {
' ...'
);

var labels = pullRequest.labels;
var userName = pullRequest.user.login;
var changelogLabel = '';
var hasChangelogLabel = labels.some(function(label) {
if (label.name.trim().toUpperCase().startsWith('PR CHANGELOG')) {
changelogLabel = label.name.trim();
return true;
}
});
var changelogLabel = getChangelogLabelFromPullRequest(pullRequest);
var hasChangelogLabel = Boolean(changelogLabel);

// If the PR has a changelog label, no action is required.
if (hasChangelogLabel) {
Expand Down
Loading