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

Move wip checks to github actions #276

Merged
merged 7 commits into from
Aug 1, 2021
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
7 changes: 7 additions & 0 deletions actions/src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const issueLabelsModule = require('./issues/checkIssueLabels');
const claCheckGithubActionModule = require('./pull_requests/claCheck');
const constants = require('../../constants');
const PRLabelsModule = require('./pull_requests/labelCheck');
const wipDraftModule = require('./pull_requests/checkWipDraftPR');

module.exports = {
async dispatch(event, action) {
Expand All @@ -44,11 +45,17 @@ module.exports = {
await claCheckGithubActionModule.claCheckGithubAction();
break;
case constants.prLabelCheck:
core.info('PR label check triggered');
await PRLabelsModule.checkLabels();
break;
case constants.dontMergeLabelCheck:
core.info('Don\'t label check triggered');
await PRLabelsModule.checkUnLabeled();
break;
case constants.wipCheck:
core.info('WIP check triggered');
await wipDraftModule.checkWIP();
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Oppia Authors. All Rights Reserved.
// Copyright 2021 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,18 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const { pingAndAssignUsers } = require('./utils');
/**
* @fileoverview File to handle checks when a draft or work in progress
* pull request is opened or reopened.
*/

const core = require('@actions/core');
const { context, GitHub } = require('@actions/github');
const { commentAndAssignUsers } = require('../utils');

/**
* @param {import('probot').Octokit.PullsGetResponse} pullRequest
* @param {import('@octokit/rest').Octokit.PullsGetResponse} pullRequest
* @returns {Boolean}
*/
const isDraftPr = (pullRequest) => {
return pullRequest.draft;
};

/**
* @param {import('probot').Octokit.PullsGetResponse} pullRequest
* @param {import('@octokit/rest').Octokit.PullsGetResponse} pullRequest
* @returns {Boolean}
*/
const isWIPPr = ({ title, body }) => {
Expand All @@ -33,50 +40,48 @@ const isWIPPr = ({ title, body }) => {
};

/**
* @param {import('probot').Context} context
*
* @param {import('@octokit/rest').Octokit} octokit
* @returns {Promise<Boolean>}
*/
const isSkipCICommit = async (context) => {
/**
* @type {import('probot').Octokit.PullsGetResponse} pullRequest
*/
const isSkipCICommit = async (octokit) => {
const pullRequest = context.payload.pull_request;

const commitParams = context.repo({
commit_sha: pullRequest.head.sha
});
const commitResponse = await context.github.git.getCommit(commitParams);
const commitParams = {
commit_sha: pullRequest.head.sha,
...context.repo
};
const commitResponse = await octokit.git.getCommit(commitParams);

return (
commitResponse.data.message.startsWith('[ci skip]') ||
commitResponse.data.message.startsWith('[skip ci]'));
commitResponse.data.message.startsWith('[skip ci]')
);
};

/**
* @param {import('probot').Context} context
*/
module.exports.checkWIP = async (context) => {
/**
* @type {import('probot').Octokit.PullsGetResponse} pullRequest
*/
module.exports.checkWIP = async () => {
const token = core.getInput('repo-token');
const octokit = new GitHub(token);
const pullRequest = context.payload.pull_request;
const prAuthor = pullRequest.user.login;

if (isDraftPr(pullRequest) || isWIPPr(pullRequest)) {
const hasSkipCIMessage = await isSkipCICommit(context);
const hasSkipCIMessage = await isSkipCICommit(octokit);
if (!hasSkipCIMessage) {
// Ping and assign PR author.
const link = 'here'.link(
'https://github.com/oppia/oppia/wiki/Contributing-code-to-Oppia' +
'#wip--draft-pull-requests');
'#wip--draft-pull-requests'
);

const commentBody = (
const commentBody =
'Hi @' + prAuthor + ', when creating WIP/Draft PRs, ensure that ' +
'your commit messages are prefixed with **[ci skip]** or ' +
'**[skip ci]** to prevent CI checks from running. ' +
'You can learn more about it ' + link + '.');
'You can learn more about it ' + link + '.';

await pingAndAssignUsers(context, pullRequest, [prAuthor], commentBody);
await commentAndAssignUsers(
octokit, pullRequest, [prAuthor], commentBody
);
}
}
};
28 changes: 28 additions & 0 deletions actions/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { context } = require('@actions/github');

/**
* This function creates a comment and assigns users.
* @param {import('@octokit/rest').Octokit} octokit
* @param {import('@octokit/rest').Octokit.PullsGetResponse} pullRequest
* @param {string[]} assignees
* @param {string} comment
*/
const commentAndAssignUsers = async (
octokit, pullRequest, assignees, comment
) => {
await octokit.issues.createComment({
issue_number: pullRequest.number,
body: comment,
...context.repo,
});

await octokit.issues.addAssignees({
issue_number: pullRequest.number,
assignees: assignees,
...context.repo,
});
};

module.exports = {
commentAndAssignUsers,
};
146 changes: 139 additions & 7 deletions actions_build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55282,12 +55282,13 @@ const openEvent = 'opened';
// Github action sends a different type of event.
const openEventGithubActions = 'pull_request_target_opened';
const reopenEventGithubActions = 'pull_request_target_reopened';
const editEventGithubActions = 'pull_request_target_edited';
const reopenEvent = 'pull_request_reopened';
const PRUnlabelEvent = 'pull_request_unlabeled';
const PRLabelEvent = 'pull_request_labeled';
const synchronizeEvent = 'synchronize';
const closeEvent = 'closed';
const editEvent = 'edited';
const editEvent = 'pull_request_edited';
const issuesLabelEvent = 'issues_labeled';
const issuesAssignedEvent = 'issues_assigned';
const pushEvent = 'push';
Expand Down Expand Up @@ -55347,19 +55348,18 @@ const checksWhitelist = {
changelogCheck,
codeOwnerCheck,
branchCheck,
wipCheck,
jobCheck,
cronJobCheck,
modelCheck,
prTemplateCheck
],
[openEventGithubActions]: [claCheckGithubAction],
[reopenEventGithubActions]: [claCheckGithubAction],
[openEventGithubActions]: [claCheckGithubAction, wipCheck],
[reopenEventGithubActions]: [claCheckGithubAction, wipCheck],
[editEventGithubActions]: [wipCheck],
[reopenEvent]: [
changelogCheck,
claCheckGithubAction,
branchCheck,
wipCheck,
jobCheck,
cronJobCheck,
modelCheck,
Expand Down Expand Up @@ -88807,7 +88807,40 @@ exports.default = promisify;

/***/ }),
/* 255 */,
/* 256 */,
/* 256 */
/***/ (function(module, __unusedexports, __webpack_require__) {

const { context } = __webpack_require__(469);

/**
* This function creates a comment and assigns users.
* @param {import('@octokit/rest').Octokit} octokit
* @param {import('@octokit/rest').Octokit.PullsGetResponse} pullRequest
* @param {string[]} assignees
* @param {string} comment
*/
const commentAndAssignUsers = async (
octokit, pullRequest, assignees, comment
) => {
await octokit.issues.createComment({
issue_number: pullRequest.number,
body: comment,
...context.repo,
});

await octokit.issues.addAssignees({
issue_number: pullRequest.number,
assignees: assignees,
...context.repo,
});
};

module.exports = {
commentAndAssignUsers,
};


/***/ }),
/* 257 */
/***/ (function(__unusedmodule, exports, __webpack_require__) {

Expand Down Expand Up @@ -240590,6 +240623,7 @@ const issueLabelsModule = __webpack_require__(293);
const claCheckGithubActionModule = __webpack_require__(373);
const constants = __webpack_require__(208);
const PRLabelsModule = __webpack_require__(626);
const wipDraftModule = __webpack_require__(911);

module.exports = {
async dispatch(event, action) {
Expand All @@ -240612,11 +240646,17 @@ module.exports = {
await claCheckGithubActionModule.claCheckGithubAction();
break;
case constants.prLabelCheck:
core.info('PR label check triggered');
await PRLabelsModule.checkLabels();
break;
case constants.dontMergeLabelCheck:
core.info('Don\'t label check triggered');
await PRLabelsModule.checkUnLabeled();
break;
case constants.wipCheck:
core.info('WIP check triggered');
await wipDraftModule.checkWIP();
break;
}
}
}
Expand Down Expand Up @@ -289184,7 +289224,99 @@ var servicenetworking_v1beta;
/***/ }),
/* 909 */,
/* 910 */,
/* 911 */,
/* 911 */
/***/ (function(module, __unusedexports, __webpack_require__) {

// Copyright 2021 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview File to handle checks when a draft or work in progress
* pull request is opened or reopened.
*/

const core = __webpack_require__(470);
const { context, GitHub } = __webpack_require__(469);
const { commentAndAssignUsers } = __webpack_require__(256);

/**
* @param {import('@octokit/rest').Octokit.PullsGetResponse} pullRequest
* @returns {Boolean}
*/
const isDraftPr = (pullRequest) => {
return pullRequest.draft;
};

/**
* @param {import('@octokit/rest').Octokit.PullsGetResponse} pullRequest
* @returns {Boolean}
*/
const isWIPPr = ({ title, body }) => {
return (
title.toLowerCase().includes('wip') || body.toLowerCase().includes('wip')
);
};

/**
* @param {import('@octokit/rest').Octokit} octokit
* @returns {Promise<Boolean>}
*/
const isSkipCICommit = async (octokit) => {
const pullRequest = context.payload.pull_request;

const commitParams = {
commit_sha: pullRequest.head.sha,
...context.repo
};
const commitResponse = await octokit.git.getCommit(commitParams);

return (
commitResponse.data.message.startsWith('[ci skip]') ||
commitResponse.data.message.startsWith('[skip ci]')
);
};

module.exports.checkWIP = async () => {
const token = core.getInput('repo-token');
const octokit = new GitHub(token);
const pullRequest = context.payload.pull_request;
const prAuthor = pullRequest.user.login;

if (isDraftPr(pullRequest) || isWIPPr(pullRequest)) {
const hasSkipCIMessage = await isSkipCICommit(octokit);
if (!hasSkipCIMessage) {
// Ping and assign PR author.
const link = 'here'.link(
'https://github.com/oppia/oppia/wiki/Contributing-code-to-Oppia' +
'#wip--draft-pull-requests'
);

const commentBody =
'Hi @' + prAuthor + ', when creating WIP/Draft PRs, ensure that ' +
'your commit messages are prefixed with **[ci skip]** or ' +
'**[skip ci]** to prevent CI checks from running. ' +
'You can learn more about it ' + link + '.';

await commentAndAssignUsers(
octokit, pullRequest, [prAuthor], commentBody
);
}
}
};


/***/ }),
/* 912 */,
/* 913 */
/***/ (function(__unusedmodule, exports, __webpack_require__) {
Expand Down
Loading