diff --git a/dist/153.index.js b/dist/153.index.js index 22656bd4e..8e1eab31b 100644 --- a/dist/153.index.js +++ b/dist/153.index.js @@ -3,6 +3,73 @@ exports.id = 153; exports.ids = [153]; exports.modules = { +/***/ 9042: +/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { + +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ "Cc": () => (/* binding */ GITHUB_OPTIONS), +/* harmony export */ "$9": () => (/* binding */ DEFAULT_PIPELINE_STATUS), +/* harmony export */ "Km": () => (/* binding */ DEFAULT_PIPELINE_DESCRIPTION), +/* harmony export */ "Hc": () => (/* binding */ PRODUCTION_ENVIRONMENT), +/* harmony export */ "fy": () => (/* binding */ LATE_REVIEW), +/* harmony export */ "_d": () => (/* binding */ CORE_APPROVED_PR_LABEL), +/* harmony export */ "Xt": () => (/* binding */ PEER_APPROVED_PR_LABEL), +/* harmony export */ "Ak": () => (/* binding */ READY_FOR_MERGE_PR_LABEL), +/* harmony export */ "Cb": () => (/* binding */ MERGE_QUEUE_STATUS), +/* harmony export */ "Ee": () => (/* binding */ QUEUED_FOR_MERGE_PREFIX), +/* harmony export */ "IH": () => (/* binding */ FIRST_QUEUED_PR_LABEL), +/* harmony export */ "nJ": () => (/* binding */ JUMP_THE_QUEUE_PR_LABEL), +/* harmony export */ "HW": () => (/* binding */ DEFAULT_PR_TITLE_REGEX) +/* harmony export */ }); +/* unused harmony exports DEFAULT_EXEMPT_DESCRIPTION, COPYRIGHT_HEADER */ +/* +Copyright 2021 Expedia, Inc. +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 + https://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. +*/ +// These extra headers are for experimental API features on Github Enterprise. See https://docs.github.com/en/enterprise-server@3.0/rest/overview/api-previews for details. +const PREVIEWS = ['ant-man', 'flash', 'groot', 'inertia', 'starfox']; +const GITHUB_OPTIONS = { + headers: { + accept: PREVIEWS.map(preview => `application/vnd.github.${preview}-preview+json`).join() + } +}; +const DEFAULT_EXEMPT_DESCRIPTION = 'Passed in case the check is exempt.'; +const DEFAULT_PIPELINE_STATUS = 'Pipeline Status'; +const DEFAULT_PIPELINE_DESCRIPTION = 'Pipeline clear.'; +const PRODUCTION_ENVIRONMENT = 'production'; +const LATE_REVIEW = 'Late Review'; +const CORE_APPROVED_PR_LABEL = 'CORE APPROVED'; +const PEER_APPROVED_PR_LABEL = 'PEER APPROVED'; +const READY_FOR_MERGE_PR_LABEL = 'READY FOR MERGE'; +const MERGE_QUEUE_STATUS = 'QUEUE CHECKER'; +const QUEUED_FOR_MERGE_PREFIX = 'QUEUED FOR MERGE'; +const FIRST_QUEUED_PR_LABEL = `${QUEUED_FOR_MERGE_PREFIX} #1`; +const JUMP_THE_QUEUE_PR_LABEL = 'JUMP THE QUEUE'; +const DEFAULT_PR_TITLE_REGEX = '^(build|ci|chore|docs|feat|fix|perf|refactor|style|test|revert|Revert|BREAKING CHANGE)((.*))?: .+$'; +const COPYRIGHT_HEADER = (/* unused pure expression or super */ null && (`/* +Copyright 2021 Expedia, Inc. +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 + https://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. +*/`)); + + +/***/ }), + /***/ 2153: /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { @@ -13,7 +80,7 @@ __webpack_require__.r(__webpack_exports__); /* harmony export */ }); /* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2186); /* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__); -/* harmony import */ var _types_generated__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(3476); +/* harmony import */ var _types_generated__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(3476); /* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5438); /* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_actions_github__WEBPACK_IMPORTED_MODULE_1__); /* harmony import */ var _utils_get_core_member_logins__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(7290); @@ -23,6 +90,7 @@ __webpack_require__.r(__webpack_exports__); /* harmony import */ var _octokit__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(6161); /* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(250); /* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_6___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_6__); +/* harmony import */ var _constants__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(9042); /* Copyright 2021 Expedia, Inc. Licensed under the Apache License, Version 2.0 (the "License"); @@ -52,15 +120,20 @@ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _argume -class AssignPrReviewer extends _types_generated__WEBPACK_IMPORTED_MODULE_7__/* .HelperInputs */ .s { + +class AssignPrReviewer extends _types_generated__WEBPACK_IMPORTED_MODULE_8__/* .HelperInputs */ .s { } const assignPrReviewers = ({ teams, login, number_of_assignees = '1', slack_webhook_url, pull_number = String(_actions_github__WEBPACK_IMPORTED_MODULE_1__.context.issue.number) }) => __awaiter(void 0, void 0, void 0, function* () { const coreMemberLogins = yield (0,_utils_get_core_member_logins__WEBPACK_IMPORTED_MODULE_2__/* .getCoreMemberLogins */ .c)(_actions_github__WEBPACK_IMPORTED_MODULE_1__.context.issue.number, teams === null || teams === void 0 ? void 0 : teams.split('\n')); - const { data: { user } } = yield _octokit__WEBPACK_IMPORTED_MODULE_5__/* .octokit.pulls.get */ .K.pulls.get(Object.assign({ pull_number: _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.issue.number }, _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.repo)); + const { data: { user, labels } } = yield _octokit__WEBPACK_IMPORTED_MODULE_5__/* .octokit.pulls.get */ .K.pulls.get(Object.assign({ pull_number: _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.issue.number }, _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.repo)); if (login && coreMemberLogins.includes(login)) { _actions_core__WEBPACK_IMPORTED_MODULE_0__.info('Already a core member, no need to assign.'); return; } + if (labels === null || labels === void 0 ? void 0 : labels.find(label => label.name === _constants__WEBPACK_IMPORTED_MODULE_7__/* .CORE_APPROVED_PR_LABEL */ ._d)) { + _actions_core__WEBPACK_IMPORTED_MODULE_0__.info('Already approved by a core member, no need to assign.'); + return; + } const prAuthorUsername = user === null || user === void 0 ? void 0 : user.login; const filteredCoreMemberLogins = coreMemberLogins.filter(userName => userName !== prAuthorUsername); const assignees = (0,lodash__WEBPACK_IMPORTED_MODULE_6__.sampleSize)(filteredCoreMemberLogins, Number(number_of_assignees)); diff --git a/dist/153.index.js.map b/dist/153.index.js.map index d0df36f90..5864a06a6 100644 --- a/dist/153.index.js.map +++ b/dist/153.index.js.map @@ -1 +1 @@ -{"version":3,"file":"153.index.js","mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;AAWA;;;;;;;;;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAMA;AAEA;AAOA;AACA;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AAEA;AACA;;;;;;;;;;;;;;;;;;ACjEA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AAEA;AACA;;;;;;;;;;;AClBA;;;;;;;;;;;AAWA;AAEA;AAmCA;;;;;;;;;;;;;;AChDA;;;;;;;;;;;AAWA;;;;;;;;;;AAGA;AACA;AAEA;AACA;AACA;AACA;AAEA;AACA;AAMA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;;;;;;ACjCA;;;;;;;;;;;AAWA;;;;;;;;;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AAEA;AACA;AACA;AACA;AAEA;AACA;AAEA;AACA;AACA;AACA;AACA;AAAA;AAEA;AACA;AAEA;;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AAEA;;;;;;;;;;;;;;;;;;ACpDA;;;;;;;;;;;AAWA;;;;;;;;;;AAEA;AACA;AACA;AACA;AAQA;AACA;AAGA;AACA;AACA;AACA;AACA;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AACA;AACA;AACA;AACA","sources":["webpack://github-helpers/./src/helpers/assign-pr-reviewers.ts","webpack://github-helpers/./src/octokit.ts","webpack://github-helpers/./src/types/generated.ts","webpack://github-helpers/./src/utils/get-changed-filepaths.ts","webpack://github-helpers/./src/utils/get-core-member-logins.ts","webpack://github-helpers/./src/utils/notify-user.ts"],"sourcesContent":["/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport { HelperInputs } from '../types/generated';\nimport { context } from '@actions/github';\nimport { getCoreMemberLogins } from '../utils/get-core-member-logins';\nimport { map } from 'bluebird';\nimport { notifyUser } from '../utils/notify-user';\nimport { octokit } from '../octokit';\nimport { sampleSize } from 'lodash';\n\nexport class AssignPrReviewer extends HelperInputs {\n teams?: string;\n login?: string;\n number_of_assignees?: string;\n slack_webhook_url?: string;\n pull_number?: string;\n}\n\nexport const assignPrReviewers = async ({\n teams,\n login,\n number_of_assignees = '1',\n slack_webhook_url,\n pull_number = String(context.issue.number)\n}: AssignPrReviewer) => {\n const coreMemberLogins = await getCoreMemberLogins(context.issue.number, teams?.split('\\n'));\n const {\n data: { user }\n } = await octokit.pulls.get({ pull_number: context.issue.number, ...context.repo });\n\n if (login && coreMemberLogins.includes(login)) {\n core.info('Already a core member, no need to assign.');\n return;\n }\n const prAuthorUsername = user?.login;\n const filteredCoreMemberLogins = coreMemberLogins.filter(userName => userName !== prAuthorUsername);\n const assignees = sampleSize(filteredCoreMemberLogins, Number(number_of_assignees));\n\n await octokit.issues.addAssignees({\n assignees,\n issue_number: Number(pull_number),\n ...context.repo\n });\n\n if (slack_webhook_url) {\n return map(assignees, async assignee =>\n notifyUser({\n login: assignee,\n pull_number: Number(pull_number),\n slack_webhook_url\n })\n );\n }\n};\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport * as fetch from '@adobe/node-fetch-retry';\nimport { getOctokit } from '@actions/github';\n\nconst githubToken = core.getInput('github_token', { required: true });\nexport const { rest: octokit, graphql: octokitGraphql } = getOctokit(githubToken, { request: { fetch } });\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nexport class HelperInputs {\n helper?: string;\n github_token?: string;\n body?: string;\n project_name?: string;\n project_destination_column_name?: string;\n note?: string;\n project_origin_column_name?: string;\n sha?: string;\n context?: string;\n state?: string;\n description?: string;\n target_url?: string;\n environment?: string;\n environment_url?: string;\n label?: string;\n labels?: string;\n paths?: string;\n extensions?: string;\n override_filter_paths?: string;\n batches?: string;\n pattern?: string;\n teams?: string;\n login?: string;\n paths_no_filter?: string;\n slack_webhook_url?: string;\n number_of_assignees?: string;\n globs?: string;\n override_filter_globs?: string;\n title?: string;\n seconds?: string;\n pull_number?: string;\n base?: string;\n head?: string;\n days?: string;\n}\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport { ChangedFilesList } from '../types/github';\nimport { context } from '@actions/github';\nimport { octokit } from '../octokit';\n\nexport const getChangedFilepaths = async (pull_number: number) => {\n const changedFiles = await paginateAllChangedFilepaths(pull_number);\n return changedFiles.map(file => file.filename);\n};\n\nconst paginateAllChangedFilepaths = async (pull_number: number, page = 1): Promise => {\n const response = await octokit.pulls.listFiles({\n pull_number,\n per_page: 100,\n page,\n ...context.repo\n });\n if (!response.data.length) {\n return [];\n }\n return response.data.concat(await paginateAllChangedFilepaths(pull_number, page + 1));\n};\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport { CodeOwnersEntry, loadOwners, matchFile } from 'codeowners-utils';\nimport { union, uniq } from 'lodash';\nimport { context } from '@actions/github';\nimport { getChangedFilepaths } from './get-changed-filepaths';\nimport { map } from 'bluebird';\nimport { octokit } from '../octokit';\n\nexport const getCoreMemberLogins = async (pull_number: number, teams?: string[]) => {\n const codeOwners = teams ?? (await getCodeOwners(pull_number));\n\n if (!codeOwners?.length) {\n core.setFailed('No code owners found.');\n throw new Error();\n }\n\n const adminLogins = await map(codeOwners, async team =>\n octokit.teams\n .listMembersInOrg({\n org: context.repo.owner,\n team_slug: team,\n per_page: 100\n })\n .then(listMembersResponse => listMembersResponse.data.map(member => member.login))\n );\n return union(...adminLogins);\n};\n\nconst getCodeOwners = async (pull_number: number) => {\n const codeOwners = (await loadOwners(process.cwd())) ?? [];\n const changedFilePaths = await getChangedFilepaths(pull_number);\n const matchingCodeOwners = changedFilePaths.map(filePath => matchFile(filePath, codeOwners) ?? ({} as CodeOwnersEntry));\n return uniq(\n matchingCodeOwners\n .map(owner => owner.owners)\n .flat()\n .filter(Boolean)\n .map(owner => owner.substring(owner.indexOf('/') + 1))\n );\n};\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport axios from 'axios';\nimport { context } from '@actions/github';\nimport { octokit } from '../octokit';\n\ninterface NotifyUser {\n login: string;\n pull_number: number;\n slack_webhook_url: string;\n}\n\nexport const notifyUser = async ({ login, pull_number, slack_webhook_url }: NotifyUser) => {\n const {\n data: { email }\n } = await octokit.users.getByUsername({ username: login });\n if (!email) {\n core.info(`No github email found for user ${login}. Ensure you have set your email to be publicly visible on your Github profile.`);\n return;\n }\n const {\n data: { title, html_url }\n } = await octokit.pulls.get({ pull_number, ...context.repo });\n\n try {\n return axios.post(slack_webhook_url, {\n assignee: email,\n title,\n html_url,\n repo: context.repo.repo\n });\n } catch (error) {\n core.warning('User notification failed');\n core.warning(error as Error);\n }\n};\n"],"names":[],"sourceRoot":""} \ No newline at end of file +{"version":3,"file":"153.index.js","mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;AAWA;;;;;;;;;;;;;;;;;;;;;;;;;;AC7CA;;;;;;;;;;;AAWA;;;;;;;;;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAMA;AAEA;AAOA;AACA;AAIA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AAEA;AACA;;;;;;;;;;;;;;;;;;ACvEA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AAEA;AACA;;;;;;;;;;;AClBA;;;;;;;;;;;AAWA;AAEA;AAmCA;;;;;;;;;;;;;;AChDA;;;;;;;;;;;AAWA;;;;;;;;;;AAGA;AACA;AAEA;AACA;AACA;AACA;AAEA;AACA;AAMA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;;;;;;ACjCA;;;;;;;;;;;AAWA;;;;;;;;;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AAEA;AACA;AACA;AACA;AAEA;AACA;AAEA;AACA;AACA;AACA;AACA;AAAA;AAEA;AACA;AAEA;;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AAEA;;;;;;;;;;;;;;;;;;ACpDA;;;;;;;;;;;AAWA;;;;;;;;;;AAEA;AACA;AACA;AACA;AAQA;AACA;AAGA;AACA;AACA;AACA;AACA;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AACA;AACA;AACA;AACA","sources":["webpack://github-helpers/./src/constants.ts","webpack://github-helpers/./src/helpers/assign-pr-reviewers.ts","webpack://github-helpers/./src/octokit.ts","webpack://github-helpers/./src/types/generated.ts","webpack://github-helpers/./src/utils/get-changed-filepaths.ts","webpack://github-helpers/./src/utils/get-core-member-logins.ts","webpack://github-helpers/./src/utils/notify-user.ts"],"sourcesContent":["/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\n// These extra headers are for experimental API features on Github Enterprise. See https://docs.github.com/en/enterprise-server@3.0/rest/overview/api-previews for details.\nconst PREVIEWS = ['ant-man', 'flash', 'groot', 'inertia', 'starfox'];\nexport const GITHUB_OPTIONS = {\n headers: {\n accept: PREVIEWS.map(preview => `application/vnd.github.${preview}-preview+json`).join()\n }\n};\n\nexport const DEFAULT_EXEMPT_DESCRIPTION = 'Passed in case the check is exempt.';\nexport const DEFAULT_PIPELINE_STATUS = 'Pipeline Status';\nexport const DEFAULT_PIPELINE_DESCRIPTION = 'Pipeline clear.';\nexport const PRODUCTION_ENVIRONMENT = 'production';\nexport const LATE_REVIEW = 'Late Review';\nexport const CORE_APPROVED_PR_LABEL = 'CORE APPROVED';\nexport const PEER_APPROVED_PR_LABEL = 'PEER APPROVED';\nexport const READY_FOR_MERGE_PR_LABEL = 'READY FOR MERGE';\nexport const MERGE_QUEUE_STATUS = 'QUEUE CHECKER';\nexport const QUEUED_FOR_MERGE_PREFIX = 'QUEUED FOR MERGE';\nexport const FIRST_QUEUED_PR_LABEL = `${QUEUED_FOR_MERGE_PREFIX} #1`;\nexport const JUMP_THE_QUEUE_PR_LABEL = 'JUMP THE QUEUE';\nexport const DEFAULT_PR_TITLE_REGEX = '^(build|ci|chore|docs|feat|fix|perf|refactor|style|test|revert|Revert|BREAKING CHANGE)((.*))?: .+$';\nexport const COPYRIGHT_HEADER = `/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/`;\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport { HelperInputs } from '../types/generated';\nimport { context } from '@actions/github';\nimport { getCoreMemberLogins } from '../utils/get-core-member-logins';\nimport { map } from 'bluebird';\nimport { notifyUser } from '../utils/notify-user';\nimport { octokit } from '../octokit';\nimport { sampleSize } from 'lodash';\nimport { CORE_APPROVED_PR_LABEL } from '../constants';\n\nexport class AssignPrReviewer extends HelperInputs {\n teams?: string;\n login?: string;\n number_of_assignees?: string;\n slack_webhook_url?: string;\n pull_number?: string;\n}\n\nexport const assignPrReviewers = async ({\n teams,\n login,\n number_of_assignees = '1',\n slack_webhook_url,\n pull_number = String(context.issue.number)\n}: AssignPrReviewer) => {\n const coreMemberLogins = await getCoreMemberLogins(context.issue.number, teams?.split('\\n'));\n const {\n data: { user, labels }\n } = await octokit.pulls.get({ pull_number: context.issue.number, ...context.repo });\n\n if (login && coreMemberLogins.includes(login)) {\n core.info('Already a core member, no need to assign.');\n return;\n }\n\n if (labels?.find(label => label.name === CORE_APPROVED_PR_LABEL)) {\n core.info('Already approved by a core member, no need to assign.');\n return;\n }\n const prAuthorUsername = user?.login;\n const filteredCoreMemberLogins = coreMemberLogins.filter(userName => userName !== prAuthorUsername);\n const assignees = sampleSize(filteredCoreMemberLogins, Number(number_of_assignees));\n\n await octokit.issues.addAssignees({\n assignees,\n issue_number: Number(pull_number),\n ...context.repo\n });\n\n if (slack_webhook_url) {\n return map(assignees, async assignee =>\n notifyUser({\n login: assignee,\n pull_number: Number(pull_number),\n slack_webhook_url\n })\n );\n }\n};\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport * as fetch from '@adobe/node-fetch-retry';\nimport { getOctokit } from '@actions/github';\n\nconst githubToken = core.getInput('github_token', { required: true });\nexport const { rest: octokit, graphql: octokitGraphql } = getOctokit(githubToken, { request: { fetch } });\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nexport class HelperInputs {\n helper?: string;\n github_token?: string;\n body?: string;\n project_name?: string;\n project_destination_column_name?: string;\n note?: string;\n project_origin_column_name?: string;\n sha?: string;\n context?: string;\n state?: string;\n description?: string;\n target_url?: string;\n environment?: string;\n environment_url?: string;\n label?: string;\n labels?: string;\n paths?: string;\n extensions?: string;\n override_filter_paths?: string;\n batches?: string;\n pattern?: string;\n teams?: string;\n login?: string;\n paths_no_filter?: string;\n slack_webhook_url?: string;\n number_of_assignees?: string;\n globs?: string;\n override_filter_globs?: string;\n title?: string;\n seconds?: string;\n pull_number?: string;\n base?: string;\n head?: string;\n days?: string;\n}\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport { ChangedFilesList } from '../types/github';\nimport { context } from '@actions/github';\nimport { octokit } from '../octokit';\n\nexport const getChangedFilepaths = async (pull_number: number) => {\n const changedFiles = await paginateAllChangedFilepaths(pull_number);\n return changedFiles.map(file => file.filename);\n};\n\nconst paginateAllChangedFilepaths = async (pull_number: number, page = 1): Promise => {\n const response = await octokit.pulls.listFiles({\n pull_number,\n per_page: 100,\n page,\n ...context.repo\n });\n if (!response.data.length) {\n return [];\n }\n return response.data.concat(await paginateAllChangedFilepaths(pull_number, page + 1));\n};\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport { CodeOwnersEntry, loadOwners, matchFile } from 'codeowners-utils';\nimport { union, uniq } from 'lodash';\nimport { context } from '@actions/github';\nimport { getChangedFilepaths } from './get-changed-filepaths';\nimport { map } from 'bluebird';\nimport { octokit } from '../octokit';\n\nexport const getCoreMemberLogins = async (pull_number: number, teams?: string[]) => {\n const codeOwners = teams ?? (await getCodeOwners(pull_number));\n\n if (!codeOwners?.length) {\n core.setFailed('No code owners found.');\n throw new Error();\n }\n\n const adminLogins = await map(codeOwners, async team =>\n octokit.teams\n .listMembersInOrg({\n org: context.repo.owner,\n team_slug: team,\n per_page: 100\n })\n .then(listMembersResponse => listMembersResponse.data.map(member => member.login))\n );\n return union(...adminLogins);\n};\n\nconst getCodeOwners = async (pull_number: number) => {\n const codeOwners = (await loadOwners(process.cwd())) ?? [];\n const changedFilePaths = await getChangedFilepaths(pull_number);\n const matchingCodeOwners = changedFilePaths.map(filePath => matchFile(filePath, codeOwners) ?? ({} as CodeOwnersEntry));\n return uniq(\n matchingCodeOwners\n .map(owner => owner.owners)\n .flat()\n .filter(Boolean)\n .map(owner => owner.substring(owner.indexOf('/') + 1))\n );\n};\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport axios from 'axios';\nimport { context } from '@actions/github';\nimport { octokit } from '../octokit';\n\ninterface NotifyUser {\n login: string;\n pull_number: number;\n slack_webhook_url: string;\n}\n\nexport const notifyUser = async ({ login, pull_number, slack_webhook_url }: NotifyUser) => {\n const {\n data: { email }\n } = await octokit.users.getByUsername({ username: login });\n if (!email) {\n core.info(`No github email found for user ${login}. Ensure you have set your email to be publicly visible on your Github profile.`);\n return;\n }\n const {\n data: { title, html_url }\n } = await octokit.pulls.get({ pull_number, ...context.repo });\n\n try {\n return axios.post(slack_webhook_url, {\n assignee: email,\n title,\n html_url,\n repo: context.repo.repo\n });\n } catch (error) {\n core.warning('User notification failed');\n core.warning(error as Error);\n }\n};\n"],"names":[],"sourceRoot":""} \ No newline at end of file diff --git a/src/helpers/assign-pr-reviewers.ts b/src/helpers/assign-pr-reviewers.ts index 3dbedf3b5..465a89b1b 100644 --- a/src/helpers/assign-pr-reviewers.ts +++ b/src/helpers/assign-pr-reviewers.ts @@ -19,6 +19,7 @@ import { map } from 'bluebird'; import { notifyUser } from '../utils/notify-user'; import { octokit } from '../octokit'; import { sampleSize } from 'lodash'; +import { CORE_APPROVED_PR_LABEL } from '../constants'; export class AssignPrReviewer extends HelperInputs { teams?: string; @@ -37,13 +38,18 @@ export const assignPrReviewers = async ({ }: AssignPrReviewer) => { const coreMemberLogins = await getCoreMemberLogins(context.issue.number, teams?.split('\n')); const { - data: { user } + data: { user, labels } } = await octokit.pulls.get({ pull_number: context.issue.number, ...context.repo }); if (login && coreMemberLogins.includes(login)) { core.info('Already a core member, no need to assign.'); return; } + + if (labels?.find(label => label.name === CORE_APPROVED_PR_LABEL)) { + core.info('Already approved by a core member, no need to assign.'); + return; + } const prAuthorUsername = user?.login; const filteredCoreMemberLogins = coreMemberLogins.filter(userName => userName !== prAuthorUsername); const assignees = sampleSize(filteredCoreMemberLogins, Number(number_of_assignees)); diff --git a/test/helpers/assign-pr-reviewers.test.ts b/test/helpers/assign-pr-reviewers.test.ts index e3be970c7..92b67e9dd 100644 --- a/test/helpers/assign-pr-reviewers.test.ts +++ b/test/helpers/assign-pr-reviewers.test.ts @@ -18,6 +18,7 @@ import { notifyUser } from '../../src/utils/notify-user'; import { octokit } from '../../src/octokit'; import { sampleSize } from 'lodash'; import { Mocktokit } from '../types'; +import { CORE_APPROVED_PR_LABEL, PEER_APPROVED_PR_LABEL } from '../../src/constants'; jest.mock('../../src/utils/get-core-member-logins'); jest.mock('../../src/utils/notify-user'); @@ -108,6 +109,60 @@ describe('assignPrReviewer', () => { }); }); + describe('already core approved', () => { + const login = 'user6'; + beforeEach(() => { + (octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({ + data: { + id: 1, + number: 123, + state: 'open', + title: 'feat: added feature to project', + user: { + login: 'user1' + }, + labels: [ + { + name: CORE_APPROVED_PR_LABEL + } + ] + } + })); + assignPrReviewers({ login, teams }); + }); + + it('should not call addAssignees', () => { + expect(octokit.issues.addAssignees).not.toHaveBeenCalled(); + }); + }); + + describe('not core approved', () => { + const login = 'user6'; + beforeEach(() => { + (octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({ + data: { + id: 1, + number: 123, + state: 'open', + title: 'feat: added feature to project', + user: { + login: 'user1' + }, + labels: [ + { + name: PEER_APPROVED_PR_LABEL + } + ] + } + })); + assignPrReviewers({ login, teams }); + }); + + it('should call addAssignee', () => { + expect(sampleSize).toHaveBeenCalledWith(['user2', 'user3'], 1); + }); + }); + describe('override pull_number', () => { const login = 'user4'; const pull_number_2 = '456';