diff --git a/dist/473.index.js b/dist/473.index.js index cc4a5c8a8..f2ee87a57 100644 --- a/dist/473.index.js +++ b/dist/473.index.js @@ -1,5 +1,5 @@ exports.id = 473; -exports.ids = [473,209]; +exports.ids = [473,61,209]; exports.modules = { /***/ 9042: @@ -77,6 +77,8 @@ var github = __webpack_require__(5438); var bluebird = __webpack_require__(8710); // EXTERNAL MODULE: ./src/octokit.ts var octokit = __webpack_require__(6161); +// EXTERNAL MODULE: ./src/helpers/remove-label.ts +var remove_label = __webpack_require__(61); // EXTERNAL MODULE: ./src/helpers/set-commit-status.ts var set_commit_status = __webpack_require__(2209); ;// CONCATENATED MODULE: ./src/utils/update-merge-queue.ts @@ -84,6 +86,7 @@ var set_commit_status = __webpack_require__(2209); + const updateMergeQueue = (queuedPrs) => { const prsSortedByQueuePosition = queuedPrs .map(pr => { @@ -105,7 +108,7 @@ const updateMergeQueue = (queuedPrs) => { } return Promise.all([ octokit/* octokit.issues.addLabels */.K.issues.addLabels(Object.assign({ labels: [`${constants/* QUEUED_FOR_MERGE_PREFIX */.Ee} #${newQueuePosition}`], issue_number: pull_number }, github.context.repo)), - octokit/* octokit.issues.removeLabel */.K.issues.removeLabel(Object.assign({ name: label, issue_number: pull_number }, github.context.repo)) + (0,remove_label.removeLabelIfExists)(label, pull_number) ]); }); }; @@ -139,14 +142,15 @@ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _argume + const manageMergeQueue = () => __awaiter(void 0, void 0, void 0, function* () { - const { data: { items, total_count: queuePosition } } = yield getQueuedPrData(); const issue_number = github.context.issue.number; const { data: pullRequest } = yield octokit/* octokit.pulls.get */.K.pulls.get(Object.assign({ pull_number: issue_number }, github.context.repo)); if (pullRequest.merged || !pullRequest.labels.find(label => label.name === constants/* READY_FOR_MERGE_PR_LABEL */.Ak)) { core.info('This PR is not in the merge queue.'); - return removePRFromQueue(pullRequest, items); + return removePRFromQueue(pullRequest); } + const { data: { total_count: queuePosition } } = yield getQueuedPrData(); const isFirstQueuePosition = queuePosition === 1 || pullRequest.labels.find(label => label.name === constants/* FIRST_QUEUED_PR_LABEL */.IH); return Promise.all([ octokit/* octokit.issues.addLabels */.K.issues.addLabels(Object.assign({ labels: [`${constants/* QUEUED_FOR_MERGE_PREFIX */.Ee} #${queuePosition}`], issue_number }, github.context.repo)), @@ -158,12 +162,13 @@ const manageMergeQueue = () => __awaiter(void 0, void 0, void 0, function* () { }) ]); }); -const removePRFromQueue = (pullRequest, queuedPrs) => __awaiter(void 0, void 0, void 0, function* () { +const removePRFromQueue = (pullRequest) => __awaiter(void 0, void 0, void 0, function* () { var _a; const queueLabel = (_a = pullRequest.labels.find(label => { var _a; return (_a = label.name) === null || _a === void 0 ? void 0 : _a.startsWith(constants/* QUEUED_FOR_MERGE_PREFIX */.Ee); })) === null || _a === void 0 ? void 0 : _a.name; if (queueLabel) { - yield (0,bluebird.map)([constants/* READY_FOR_MERGE_PR_LABEL */.Ak, queueLabel], label => octokit/* octokit.issues.removeLabel */.K.issues.removeLabel(Object.assign({ name: label, issue_number: pullRequest.number }, github.context.repo))); - yield updateMergeQueue(queuedPrs); + yield (0,bluebird.map)([constants/* READY_FOR_MERGE_PR_LABEL */.Ak, queueLabel], label => (0,remove_label.removeLabelIfExists)(label, pullRequest.number)); + const { data: { items } } = yield getQueuedPrData(); + yield updateMergeQueue(items); } }); const getQueuedPrData = () => { @@ -174,6 +179,46 @@ const getQueuedPrData = () => { }; +/***/ }), + +/***/ 61: +/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { + +"use strict"; +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ "removeLabel": () => (/* binding */ removeLabel), +/* harmony export */ "removeLabelIfExists": () => (/* binding */ removeLabelIfExists) +/* 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 _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 _octokit__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6161); +/* +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. +*/ + + + +const removeLabel = ({ label }) => removeLabelIfExists(label, _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.issue.number); +const removeLabelIfExists = (labelName, issue_number) => _octokit__WEBPACK_IMPORTED_MODULE_2__/* .octokit.issues.removeLabel */ .K.issues.removeLabel(Object.assign({ name: labelName, issue_number }, _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.repo)) + .catch(error => { + if (error.status === 404) { + _actions_core__WEBPACK_IMPORTED_MODULE_0__.info('Label is not present on PR.'); + } +}); + + /***/ }), /***/ 2209: diff --git a/dist/473.index.js.map b/dist/473.index.js.map index d626b21cf..495cd0786 100644 --- a/dist/473.index.js.map +++ b/dist/473.index.js.map @@ -1 +1 @@ -{"version":3,"file":"473.index.js","sources":["webpack://github-helpers/./src/constants.ts","webpack://github-helpers/./src/utils/update-merge-queue.ts","webpack://github-helpers/./src/helpers/manage-merge-queue.ts","webpack://github-helpers/./src/helpers/set-commit-status.ts","webpack://github-helpers/./src/octokit.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 operations. Newer versions of octokit may not require this\nexport const GITHUB_OPTIONS = {\n headers: {\n accept: 'application/vnd.github.ant-man-preview+json,application/vnd.github.flash-preview+json,application/vnd.github.v3+json'\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 DEFAULT_BRANCH = 'main';\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 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)((.*))?: .+$';\n","import { PullRequestSearchResults } from '../types';\nimport { QUEUED_FOR_MERGE_PREFIX } from '../constants';\nimport { context } from '@actions/github';\nimport { map } from 'bluebird';\nimport { octokit } from '../octokit';\n\nexport const updateMergeQueue = (queuedPrs: PullRequestSearchResults) => {\n const prsSortedByQueuePosition = queuedPrs\n .map(pr => {\n const label = pr.labels.find(label => label.name?.startsWith(QUEUED_FOR_MERGE_PREFIX))?.name;\n const queuePosition = Number(label?.split('#')?.[1]);\n return {\n pull_number: pr.number,\n label,\n queuePosition\n };\n })\n .sort((pr1, pr2) => pr1.queuePosition - pr2.queuePosition);\n return map(prsSortedByQueuePosition, (pr, index) => {\n const { pull_number, label, queuePosition } = pr;\n const newQueuePosition = index + 1;\n if (!label || queuePosition === newQueuePosition) {\n return;\n }\n return Promise.all([\n octokit.issues.addLabels({\n labels: [`${QUEUED_FOR_MERGE_PREFIX} #${newQueuePosition}`],\n issue_number: pull_number,\n ...context.repo\n }),\n octokit.issues.removeLabel({\n name: label,\n issue_number: pull_number,\n ...context.repo\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 { FIRST_QUEUED_PR_LABEL, QUEUED_FOR_MERGE_PREFIX, READY_FOR_MERGE_PR_LABEL } from '../constants';\nimport { PullRequest, PullRequestSearchResults } from '../types';\nimport { context } from '@actions/github';\nimport { map } from 'bluebird';\nimport { octokit } from '../octokit';\nimport { setCommitStatus } from './set-commit-status';\nimport { updateMergeQueue } from '../utils/update-merge-queue';\n\nexport const manageMergeQueue = async () => {\n const {\n data: { items, total_count: queuePosition }\n } = await getQueuedPrData();\n const issue_number = context.issue.number;\n const { data: pullRequest } = await octokit.pulls.get({ pull_number: issue_number, ...context.repo });\n if (pullRequest.merged || !pullRequest.labels.find(label => label.name === READY_FOR_MERGE_PR_LABEL)) {\n core.info('This PR is not in the merge queue.');\n return removePRFromQueue(pullRequest, items);\n }\n\n const isFirstQueuePosition = queuePosition === 1 || pullRequest.labels.find(label => label.name === FIRST_QUEUED_PR_LABEL);\n return Promise.all([\n octokit.issues.addLabels({\n labels: [`${QUEUED_FOR_MERGE_PREFIX} #${queuePosition}`],\n issue_number,\n ...context.repo\n }),\n setCommitStatus({\n sha: pullRequest.head.sha,\n context: 'QUEUE CHECKER',\n state: isFirstQueuePosition ? 'success' : 'pending',\n description: isFirstQueuePosition ? 'This PR is next to merge.' : `This PR is #${queuePosition} in line to merge.`\n })\n ]);\n};\n\nconst removePRFromQueue = async (pullRequest: PullRequest, queuedPrs: PullRequestSearchResults) => {\n const queueLabel = pullRequest.labels.find(label => label.name?.startsWith(QUEUED_FOR_MERGE_PREFIX))?.name;\n if (queueLabel) {\n await map([READY_FOR_MERGE_PR_LABEL, queueLabel], label =>\n octokit.issues.removeLabel({\n name: label,\n issue_number: pullRequest.number,\n ...context.repo\n })\n );\n await updateMergeQueue(queuedPrs);\n }\n};\n\nconst getQueuedPrData = () => {\n const { repo, owner } = context.repo;\n return octokit.search.issuesAndPullRequests({\n q: `org:${owner}+repo:${repo}+is:pr+is:open+label:\"${READY_FOR_MERGE_PR_LABEL}\"`\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 { PipelineState } from '../types';\nimport { context as githubContext } from '@actions/github';\nimport { map } from 'bluebird';\nimport { octokit } from '../octokit';\n\ninterface SetCommitStatus {\n sha: string;\n context: string;\n state: PipelineState;\n description?: string;\n target_url?: string;\n}\n\nexport const setCommitStatus = ({ sha, context, state, description, target_url }: SetCommitStatus) =>\n map(context.split('\\n'), context =>\n octokit.repos.createCommitStatus({\n sha,\n context,\n state,\n description,\n target_url,\n ...githubContext.repo\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 { Octokit } from '@octokit/rest';\nimport { getOctokit } from '@actions/github';\n\nexport const octokit = getOctokit(core.getInput('github_token', { required: true })).rest as unknown as Octokit;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;A;;;;;;;;;;;;;;;;;;;;;;;;;;;AC9BA;AACA;AACA;AACA;AAEA;AACA;AACA;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAKA;AAKA;AACA;AACA;;;ACrCA;;;;;;;;;;;AAWA;;;;;;;;;;AAEA;AACA;AAEA;AACA;AACA;AACA;AACA;AAEA;AACA;AAGA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;;AACA;AACA;AACA;AAOA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;;;A;;;;;;;;;;;;;;;ACpEA;;;;;;;;;;;AAWA;AAGA;AACA;AACA;AAUA;AAIA;AACA;AACA;AACA;;;A;;;;;;;;;;;;;ACjCA;;;;;;;;;;;AAWA;AAEA;AAEA;AAEA;;;A;;;A","sourceRoot":""} \ No newline at end of file +{"version":3,"file":"473.index.js","sources":["webpack://github-helpers/./src/constants.ts","webpack://github-helpers/./src/utils/update-merge-queue.ts","webpack://github-helpers/./src/helpers/manage-merge-queue.ts","webpack://github-helpers/./src/helpers/remove-label.ts","webpack://github-helpers/./src/helpers/set-commit-status.ts","webpack://github-helpers/./src/octokit.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 operations. Newer versions of octokit may not require this\nexport const GITHUB_OPTIONS = {\n headers: {\n accept: 'application/vnd.github.ant-man-preview+json,application/vnd.github.flash-preview+json,application/vnd.github.v3+json'\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 DEFAULT_BRANCH = 'main';\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 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)((.*))?: .+$';\n","import { PullRequestSearchResults } from '../types';\nimport { QUEUED_FOR_MERGE_PREFIX } from '../constants';\nimport { context } from '@actions/github';\nimport { map } from 'bluebird';\nimport { octokit } from '../octokit';\nimport { removeLabelIfExists } from '../helpers/remove-label';\n\nexport const updateMergeQueue = (queuedPrs: PullRequestSearchResults) => {\n const prsSortedByQueuePosition = queuedPrs\n .map(pr => {\n const label = pr.labels.find(label => label.name?.startsWith(QUEUED_FOR_MERGE_PREFIX))?.name;\n const queuePosition = Number(label?.split('#')?.[1]);\n return {\n pull_number: pr.number,\n label,\n queuePosition\n };\n })\n .sort((pr1, pr2) => pr1.queuePosition - pr2.queuePosition);\n return map(prsSortedByQueuePosition, (pr, index) => {\n const { pull_number, label, queuePosition } = pr;\n const newQueuePosition = index + 1;\n if (!label || queuePosition === newQueuePosition) {\n return;\n }\n return Promise.all([\n octokit.issues.addLabels({\n labels: [`${QUEUED_FOR_MERGE_PREFIX} #${newQueuePosition}`],\n issue_number: pull_number,\n ...context.repo\n }),\n removeLabelIfExists(label, pull_number)\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 { FIRST_QUEUED_PR_LABEL, QUEUED_FOR_MERGE_PREFIX, READY_FOR_MERGE_PR_LABEL } from '../constants';\nimport { PullRequest } from '../types';\nimport { context } from '@actions/github';\nimport { map } from 'bluebird';\nimport { octokit } from '../octokit';\nimport { removeLabelIfExists } from './remove-label';\nimport { setCommitStatus } from './set-commit-status';\nimport { updateMergeQueue } from '../utils/update-merge-queue';\n\nexport const manageMergeQueue = async () => {\n const issue_number = context.issue.number;\n const { data: pullRequest } = await octokit.pulls.get({ pull_number: issue_number, ...context.repo });\n if (pullRequest.merged || !pullRequest.labels.find(label => label.name === READY_FOR_MERGE_PR_LABEL)) {\n core.info('This PR is not in the merge queue.');\n return removePRFromQueue(pullRequest);\n }\n\n const {\n data: { total_count: queuePosition }\n } = await getQueuedPrData();\n const isFirstQueuePosition = queuePosition === 1 || pullRequest.labels.find(label => label.name === FIRST_QUEUED_PR_LABEL);\n return Promise.all([\n octokit.issues.addLabels({\n labels: [`${QUEUED_FOR_MERGE_PREFIX} #${queuePosition}`],\n issue_number,\n ...context.repo\n }),\n setCommitStatus({\n sha: pullRequest.head.sha,\n context: 'QUEUE CHECKER',\n state: isFirstQueuePosition ? 'success' : 'pending',\n description: isFirstQueuePosition ? 'This PR is next to merge.' : `This PR is #${queuePosition} in line to merge.`\n })\n ]);\n};\n\nconst removePRFromQueue = async (pullRequest: PullRequest) => {\n const queueLabel = pullRequest.labels.find(label => label.name?.startsWith(QUEUED_FOR_MERGE_PREFIX))?.name;\n if (queueLabel) {\n await map([READY_FOR_MERGE_PR_LABEL, queueLabel], label => removeLabelIfExists(label, pullRequest.number));\n const {\n data: { items }\n } = await getQueuedPrData();\n await updateMergeQueue(items);\n }\n};\n\nconst getQueuedPrData = () => {\n const { repo, owner } = context.repo;\n return octokit.search.issuesAndPullRequests({\n q: `org:${owner}+repo:${repo}+is:pr+is:open+label:\"${READY_FOR_MERGE_PR_LABEL}\"`\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 { context } from '@actions/github';\nimport { octokit } from '../octokit';\n\ninterface RemoveLabel {\n label: string;\n}\n\nexport const removeLabel = ({ label }: RemoveLabel) => removeLabelIfExists(label, context.issue.number);\n\nexport const removeLabelIfExists = (labelName: string, issue_number: number) =>\n octokit.issues\n .removeLabel({\n name: labelName,\n issue_number,\n ...context.repo\n })\n .catch(error => {\n if (error.status === 404) {\n core.info('Label is not present on PR.');\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 { PipelineState } from '../types';\nimport { context as githubContext } from '@actions/github';\nimport { map } from 'bluebird';\nimport { octokit } from '../octokit';\n\ninterface SetCommitStatus {\n sha: string;\n context: string;\n state: PipelineState;\n description?: string;\n target_url?: string;\n}\n\nexport const setCommitStatus = ({ sha, context, state, description, target_url }: SetCommitStatus) =>\n map(context.split('\\n'), context =>\n octokit.repos.createCommitStatus({\n sha,\n context,\n state,\n description,\n target_url,\n ...githubContext.repo\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 { Octokit } from '@octokit/rest';\nimport { getOctokit } from '@actions/github';\n\nexport const octokit = getOctokit(core.getInput('github_token', { required: true })).rest as unknown as Octokit;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;A;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC9BA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAKA;AACA;AACA;AACA;;;AClCA;;;;;;;;;;;AAWA;;;;;;;;;;AAEA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAGA;AACA;AACA;AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;;AACA;AACA;AACA;AACA;AAGA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;;;A;;;;;;;;;;;;;;;;AClEA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AAMA;AAEA,qJAEA;AAKA;AACA;AACA;AACA;AACA;;;A;;;;;;;;;;;;;;;AClCA;;;;;;;;;;;AAWA;AAGA;AACA;AACA;AAUA;AAIA;AACA;AACA;AACA;;;A;;;;;;;;;;;;;ACjCA;;;;;;;;;;;AAWA;AAEA;AAEA;AAEA;;;A;;;A","sourceRoot":""} \ No newline at end of file diff --git a/dist/61.index.js b/dist/61.index.js index b7d1a5544..ff5374f2a 100644 --- a/dist/61.index.js +++ b/dist/61.index.js @@ -8,7 +8,8 @@ exports.modules = { "use strict"; __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { -/* harmony export */ "removeLabel": () => (/* binding */ removeLabel) +/* harmony export */ "removeLabel": () => (/* binding */ removeLabel), +/* harmony export */ "removeLabelIfExists": () => (/* binding */ removeLabelIfExists) /* 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__); @@ -30,7 +31,8 @@ limitations under the License. -const removeLabel = ({ label }) => _octokit__WEBPACK_IMPORTED_MODULE_2__/* .octokit.issues.removeLabel */ .K.issues.removeLabel(Object.assign({ name: label, issue_number: _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.issue.number }, _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.repo)) +const removeLabel = ({ label }) => removeLabelIfExists(label, _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.issue.number); +const removeLabelIfExists = (labelName, issue_number) => _octokit__WEBPACK_IMPORTED_MODULE_2__/* .octokit.issues.removeLabel */ .K.issues.removeLabel(Object.assign({ name: labelName, issue_number }, _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.repo)) .catch(error => { if (error.status === 404) { _actions_core__WEBPACK_IMPORTED_MODULE_0__.info('Label is not present on PR.'); diff --git a/dist/61.index.js.map b/dist/61.index.js.map index 5f15c6a12..2c0e213d1 100644 --- a/dist/61.index.js.map +++ b/dist/61.index.js.map @@ -1 +1 @@ -{"version":3,"file":"61.index.js","sources":["webpack://github-helpers/./src/helpers/remove-label.ts","webpack://github-helpers/./src/octokit.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 { context } from '@actions/github';\nimport { octokit } from '../octokit';\n\ninterface RemoveLabel {\n label: string;\n}\n\nexport const removeLabel = ({ label }: RemoveLabel) =>\n octokit.issues\n .removeLabel({\n name: label,\n issue_number: context.issue.number,\n ...context.repo\n })\n .catch(error => {\n if (error.status === 404) {\n core.info('Label is not present on PR.');\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 { Octokit } from '@octokit/rest';\nimport { getOctokit } from '@actions/github';\n\nexport const octokit = getOctokit(core.getInput('github_token', { required: true })).rest as unknown as Octokit;\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AAMA,+HAEA;AAKA;AACA;AACA;AACA;AACA;;;A;;;;;;;;;;;;;AChCA;;;;;;;;;;;AAWA;AAEA;AAEA;AAEA;;;A;;;A","sourceRoot":""} \ No newline at end of file +{"version":3,"file":"61.index.js","sources":["webpack://github-helpers/./src/helpers/remove-label.ts","webpack://github-helpers/./src/octokit.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 { context } from '@actions/github';\nimport { octokit } from '../octokit';\n\ninterface RemoveLabel {\n label: string;\n}\n\nexport const removeLabel = ({ label }: RemoveLabel) => removeLabelIfExists(label, context.issue.number);\n\nexport const removeLabelIfExists = (labelName: string, issue_number: number) =>\n octokit.issues\n .removeLabel({\n name: labelName,\n issue_number,\n ...context.repo\n })\n .catch(error => {\n if (error.status === 404) {\n core.info('Label is not present on PR.');\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 { Octokit } from '@octokit/rest';\nimport { getOctokit } from '@actions/github';\n\nexport const octokit = getOctokit(core.getInput('github_token', { required: true })).rest as unknown as Octokit;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AAMA;AAEA,qJAEA;AAKA;AACA;AACA;AACA;AACA;;;A;;;;;;;;;;;;;AClCA;;;;;;;;;;;AAWA;AAEA;AAEA;AAEA;;;A;;;A","sourceRoot":""} \ No newline at end of file diff --git a/src/helpers/manage-merge-queue.ts b/src/helpers/manage-merge-queue.ts index f5ab46eef..0eb5dfaac 100644 --- a/src/helpers/manage-merge-queue.ts +++ b/src/helpers/manage-merge-queue.ts @@ -13,24 +13,25 @@ limitations under the License. import * as core from '@actions/core'; import { FIRST_QUEUED_PR_LABEL, QUEUED_FOR_MERGE_PREFIX, READY_FOR_MERGE_PR_LABEL } from '../constants'; -import { PullRequest, PullRequestSearchResults } from '../types'; +import { PullRequest } from '../types'; import { context } from '@actions/github'; import { map } from 'bluebird'; import { octokit } from '../octokit'; +import { removeLabelIfExists } from './remove-label'; import { setCommitStatus } from './set-commit-status'; import { updateMergeQueue } from '../utils/update-merge-queue'; export const manageMergeQueue = async () => { - const { - data: { items, total_count: queuePosition } - } = await getQueuedPrData(); const issue_number = context.issue.number; const { data: pullRequest } = await octokit.pulls.get({ pull_number: issue_number, ...context.repo }); if (pullRequest.merged || !pullRequest.labels.find(label => label.name === READY_FOR_MERGE_PR_LABEL)) { core.info('This PR is not in the merge queue.'); - return removePRFromQueue(pullRequest, items); + return removePRFromQueue(pullRequest); } + const { + data: { total_count: queuePosition } + } = await getQueuedPrData(); const isFirstQueuePosition = queuePosition === 1 || pullRequest.labels.find(label => label.name === FIRST_QUEUED_PR_LABEL); return Promise.all([ octokit.issues.addLabels({ @@ -47,17 +48,14 @@ export const manageMergeQueue = async () => { ]); }; -const removePRFromQueue = async (pullRequest: PullRequest, queuedPrs: PullRequestSearchResults) => { +const removePRFromQueue = async (pullRequest: PullRequest) => { const queueLabel = pullRequest.labels.find(label => label.name?.startsWith(QUEUED_FOR_MERGE_PREFIX))?.name; if (queueLabel) { - await map([READY_FOR_MERGE_PR_LABEL, queueLabel], label => - octokit.issues.removeLabel({ - name: label, - issue_number: pullRequest.number, - ...context.repo - }) - ); - await updateMergeQueue(queuedPrs); + await map([READY_FOR_MERGE_PR_LABEL, queueLabel], label => removeLabelIfExists(label, pullRequest.number)); + const { + data: { items } + } = await getQueuedPrData(); + await updateMergeQueue(items); } }; diff --git a/src/helpers/remove-label.ts b/src/helpers/remove-label.ts index 3e4b76653..556bdf868 100644 --- a/src/helpers/remove-label.ts +++ b/src/helpers/remove-label.ts @@ -19,11 +19,13 @@ interface RemoveLabel { label: string; } -export const removeLabel = ({ label }: RemoveLabel) => +export const removeLabel = ({ label }: RemoveLabel) => removeLabelIfExists(label, context.issue.number); + +export const removeLabelIfExists = (labelName: string, issue_number: number) => octokit.issues .removeLabel({ - name: label, - issue_number: context.issue.number, + name: labelName, + issue_number, ...context.repo }) .catch(error => { diff --git a/src/utils/update-merge-queue.ts b/src/utils/update-merge-queue.ts index a78587c44..d6d655d76 100644 --- a/src/utils/update-merge-queue.ts +++ b/src/utils/update-merge-queue.ts @@ -3,6 +3,7 @@ import { QUEUED_FOR_MERGE_PREFIX } from '../constants'; import { context } from '@actions/github'; import { map } from 'bluebird'; import { octokit } from '../octokit'; +import { removeLabelIfExists } from '../helpers/remove-label'; export const updateMergeQueue = (queuedPrs: PullRequestSearchResults) => { const prsSortedByQueuePosition = queuedPrs @@ -28,11 +29,7 @@ export const updateMergeQueue = (queuedPrs: PullRequestSearchResults) => { issue_number: pull_number, ...context.repo }), - octokit.issues.removeLabel({ - name: label, - issue_number: pull_number, - ...context.repo - }) + removeLabelIfExists(label, pull_number) ]); }); }; diff --git a/test/helpers/manage-merge-queue.test.ts b/test/helpers/manage-merge-queue.test.ts index c657fea77..5587483ea 100644 --- a/test/helpers/manage-merge-queue.test.ts +++ b/test/helpers/manage-merge-queue.test.ts @@ -16,9 +16,11 @@ import { READY_FOR_MERGE_PR_LABEL } from '../../src/constants'; import { context } from '@actions/github'; import { manageMergeQueue } from '../../src/helpers/manage-merge-queue'; import { octokit } from '../../src/octokit'; +import { removeLabelIfExists } from '../../src/helpers/remove-label'; import { setCommitStatus } from '../../src/helpers/set-commit-status'; import { updateMergeQueue } from '../../src/utils/update-merge-queue'; +jest.mock('../../src/helpers/remove-label'); jest.mock('../../src/helpers/set-commit-status'); jest.mock('../../src/utils/update-merge-queue'); jest.mock('@actions/core'); @@ -28,8 +30,7 @@ jest.mock('@actions/github', () => ({ rest: { pulls: { get: jest.fn() }, issues: { - addLabels: jest.fn(), - removeLabel: jest.fn() + addLabels: jest.fn() }, search: { issuesAndPullRequests: jest.fn() } } @@ -63,16 +64,8 @@ describe('manageMergeQueue', () => { }); it('should call remove label with correct params', () => { - expect(octokit.issues.removeLabel).toHaveBeenCalledWith({ - name: READY_FOR_MERGE_PR_LABEL, - issue_number: 123, - ...context.repo - }); - expect(octokit.issues.removeLabel).toHaveBeenCalledWith({ - name: 'QUEUED FOR MERGE #1', - issue_number: 123, - ...context.repo - }); + expect(removeLabelIfExists).toHaveBeenCalledWith(READY_FOR_MERGE_PR_LABEL, 123); + expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #1', 123); }); it('should call updateMergeQueue with correct params', () => { @@ -105,16 +98,8 @@ describe('manageMergeQueue', () => { }); it('should call remove label with correct params', () => { - expect(octokit.issues.removeLabel).toHaveBeenCalledWith({ - name: READY_FOR_MERGE_PR_LABEL, - issue_number: 123, - ...context.repo - }); - expect(octokit.issues.removeLabel).toHaveBeenCalledWith({ - name: 'QUEUED FOR MERGE #2', - issue_number: 123, - ...context.repo - }); + expect(removeLabelIfExists).toHaveBeenCalledWith(READY_FOR_MERGE_PR_LABEL, 123); + expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #2', 123); }); it('should call updateMergeQueue with correct params', () => { diff --git a/test/utils/update-merge-queue.test.ts b/test/utils/update-merge-queue.test.ts index 54b1c008b..194196b2f 100644 --- a/test/utils/update-merge-queue.test.ts +++ b/test/utils/update-merge-queue.test.ts @@ -14,8 +14,10 @@ limitations under the License. import { PullRequestSearchResults } from '../../src/types'; import { context } from '@actions/github'; import { octokit } from '../../src/octokit'; +import { removeLabelIfExists } from '../../src/helpers/remove-label'; import { updateMergeQueue } from '../../src/utils/update-merge-queue'; +jest.mock('../../src/helpers/remove-label'); jest.mock('@actions/core'); jest.mock('@actions/github', () => ({ context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } }, @@ -24,7 +26,6 @@ jest.mock('@actions/github', () => ({ pulls: { get: jest.fn() }, issues: { addLabels: jest.fn(), - removeLabel: jest.fn(), listLabelsOnIssue: jest.fn() }, search: { issuesAndPullRequests: jest.fn() } @@ -62,16 +63,8 @@ describe('updateMergeQueue', () => { }); it('should call remove label with correct params', () => { - expect(octokit.issues.removeLabel).toHaveBeenCalledWith({ - name: 'QUEUED FOR MERGE #2', - issue_number: 123, - ...context.repo - }); - expect(octokit.issues.removeLabel).toHaveBeenCalledWith({ - name: 'QUEUED FOR MERGE #3', - issue_number: 456, - ...context.repo - }); + expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #2', 123); + expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #3', 456); }); }); @@ -104,16 +97,8 @@ describe('updateMergeQueue', () => { }); it('should call remove label with correct params', () => { - expect(octokit.issues.removeLabel).not.toHaveBeenCalledWith({ - issue_number: 123, - name: 'QUEUED FOR MERGE #1', - ...context.repo - }); - expect(octokit.issues.removeLabel).toHaveBeenCalledWith({ - issue_number: 456, - name: 'QUEUED FOR MERGE #3', - ...context.repo - }); + expect(removeLabelIfExists).not.toHaveBeenCalledWith('QUEUED FOR MERGE #1', 123); + expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #3', 456); }); });