diff --git a/dist/index.js b/dist/index.js index 0689d50..6dc7b13 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33230,6 +33230,29 @@ exports.run = void 0; const core = __importStar(__nccwpck_require__(2186)); const github = __importStar(__nccwpck_require__(5438)); const axios_1 = __importDefault(__nccwpck_require__(8757)); +async function getAllReviews(owner, repo, pullNumber, GITHUB_TOKEN) { + let allReviews = []; + let page = 1; + let perPage = 100; + while (true) { + const reviewsResponse = await axios_1.default.get(`https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/reviews`, { + headers: { + Accept: 'application/vnd.github.v3+json', + Authorization: `token ${GITHUB_TOKEN}` + }, + params: { + page: page, + per_page: perPage + } + }); + allReviews = allReviews.concat(reviewsResponse.data); + if (reviewsResponse.data.length < perPage) { + break; + } + page++; + } + return allReviews; +} /** * The main function for the action. * @returns {Promise} Resolves when the action is complete. @@ -33243,16 +33266,12 @@ async function run() { const pullNumber = github.context.payload.pull_request ?.number; const GITHUB_TOKEN = core.getInput('github-token'); - const response = await axios_1.default.get(`https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/reviews`, { - headers: { - Accept: 'application/vnd.github.v3+json', - Authorization: `token ${GITHUB_TOKEN}` - } - }); - if (response.data.length === 0) { - core.setFailed('There are no reviews for this pull request yet. Or the url is incorrect.'); + const allReviews = await getAllReviews(owner, repo, pullNumber, GITHUB_TOKEN); + if (allReviews.length === 0) { + core.setFailed('There are no reviews for this pull request yet.'); + return; } - const approvedReviews = response.data.filter((review) => review.state === 'APPROVED'); + const approvedReviews = allReviews.filter((review) => review.state === 'APPROVED'); const teamApprovalStatus = await Promise.all(conditions.map(async (c) => { const res = await axios_1.default.get(`https://api.github.com/orgs/${owner}/teams/${c.team}/members`, { headers: { diff --git a/dist/index.js.map b/dist/index.js.map new file mode 100644 index 0000000..787c483 --- /dev/null +++ b/dist/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA;;GAEG;AACH,iCAA4B;AAE5B,mEAAmE;AACnE,IAAA,UAAG,GAAE,CAAA"} \ No newline at end of file diff --git a/dist/main.js b/dist/main.js new file mode 100644 index 0000000..3ccb09d --- /dev/null +++ b/dist/main.js @@ -0,0 +1,111 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.run = void 0; +const core = __importStar(require("@actions/core")); +const github = __importStar(require("@actions/github")); +const axios_1 = __importDefault(require("axios")); +async function getAllReviews(owner, repo, pullNumber, GITHUB_TOKEN) { + let allReviews = []; + let page = 1; + let perPage = 100; + while (true) { + const reviewsResponse = await axios_1.default.get(`https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/reviews`, { + headers: { + Accept: 'application/vnd.github.v3+json', + Authorization: `token ${GITHUB_TOKEN}` + }, + params: { + page: page, + per_page: perPage + } + }); + allReviews = allReviews.concat(reviewsResponse.data); + if (reviewsResponse.data.length < perPage) { + break; + } + page++; + } + return allReviews; +} +/** + * The main function for the action. + * @returns {Promise} Resolves when the action is complete. + */ +async function run() { + try { + const conditionsInput = core.getInput('conditions'); + const conditions = JSON.parse(conditionsInput); + const owner = github.context.repo.owner; + const repo = github.context.repo.repo; + const pullNumber = github.context.payload.pull_request + ?.number; + const GITHUB_TOKEN = core.getInput('github-token'); + const allReviews = await getAllReviews(owner, repo, pullNumber, GITHUB_TOKEN); + if (allReviews.length === 0) { + core.setFailed('There are no reviews for this pull request yet.'); + return; + } + const approvedReviews = allReviews.filter((review) => review.state === 'APPROVED'); + const teamApprovalStatus = await Promise.all(conditions.map(async (c) => { + const res = await axios_1.default.get(`https://api.github.com/orgs/${owner}/teams/${c.team}/members`, { + headers: { + Accept: 'application/vnd.github.v3+json', + Authorization: `token ${GITHUB_TOKEN}` + } + }); + if (!res.data) { + core.setFailed('There are no teams for this organization. Or the url is incorrect.'); + } + const members = res.data; + return { + team: c.team, + minimumCount: c.minimumCount, + members, + actuallCount: 0 + }; + })); + for (const review of approvedReviews) { + for (const conditionResult of teamApprovalStatus) { + if (conditionResult.members.some(member => member.login === review.user.login)) { + conditionResult.actuallCount++; + } + } + } + const isPassAllConditions = teamApprovalStatus.every((conditionResult) => conditionResult.actuallCount >= conditionResult.minimumCount); + if (!isPassAllConditions) { + core.setFailed('The pull request is not approved based on the specified conditions.'); + } + } + catch (error) { + if (error instanceof Error) + core.setFailed(error.message); + } +} +exports.run = run; +//# sourceMappingURL=main.js.map \ No newline at end of file diff --git a/dist/main.js.map b/dist/main.js.map new file mode 100644 index 0000000..63abaac --- /dev/null +++ b/dist/main.js.map @@ -0,0 +1 @@ +{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAqC;AACrC,wDAAyC;AACzC,kDAAyB;AAiBzB,KAAK,UAAU,aAAa,CAAC,KAAa,EAAE,IAAY,EAAE,UAAkB,EAAE,YAAoB;IAChG,IAAI,UAAU,GAAa,EAAE,CAAC;IAC9B,IAAI,IA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B,EAAE;YACxB,IAAI,CAAC,SAAS,CACZ,qEAAqE,CACtE,CAAA;SACF;KACF;IAAC,OAAO,KAAK,EAAE;QACd,IAAI,KAAK,YAAY,KAAK;YAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;KAC1D;AACH,CAAC;AAzED,kBAyEC"} \ No newline at end of file diff --git a/dist/types.js b/dist/types.js new file mode 100644 index 0000000..11e638d --- /dev/null +++ b/dist/types.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=types.js.map \ No newline at end of file diff --git a/dist/types.js.map b/dist/types.js.map new file mode 100644 index 0000000..c768b79 --- /dev/null +++ b/dist/types.js.map @@ -0,0 +1 @@ +{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 83f7244..f067b25 100644 --- a/package-lock.json +++ b/package-lock.json @@ -230,12 +230,12 @@ } }, "node_modules/@babel/generator": { - "version": "7.22.10", - "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.22.10.tgz", - "integrity": "sha512-79KIf7YiWjjdZ81JnLujDRApWtl7BxTqWD88+FFdQEIOG8LJ0etDOM7CXuIgGJa55sGOwZVwuEsaLEm0PJ5/+A==", + "version": "7.23.0", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.23.0.tgz", + "integrity": "sha512-lN85QRR+5IbYrMWM6Y4pE/noaQtg4pNiqeNGX60eqOfo6gtEj6uw/JagelB8vVztSd7R6M5n1+PQkDbHbBRU4g==", "dev": true, "dependencies": { - "@babel/types": "^7.22.10", + "@babel/types": "^7.23.0", "@jridgewell/gen-mapping": "^0.3.2", "@jridgewell/trace-mapping": "^0.3.17", "jsesc": "^2.5.1" @@ -270,22 +270,22 @@ } }, "node_modules/@babel/helper-environment-visitor": { - "version": "7.22.5", - "resolved": "https://registry.npmjs.org/@babel/helper-environment-visitor/-/helper-environment-visitor-7.22.5.tgz", - "integrity": "sha512-XGmhECfVA/5sAt+H+xpSg0mfrHq6FzNr9Oxh7PSEBBRUb/mL7Kz3NICXb194rCqAEdxkhPT1a88teizAFyvk8Q==", + "version": "7.22.20", + "resolved": "https://registry.npmjs.org/@babel/helper-environment-visitor/-/helper-environment-visitor-7.22.20.tgz", + "integrity": "sha512-zfedSIzFhat/gFhWfHtgWvlec0nqB9YEIVrpuwjruLlXfUSnA8cJB0miHKwqDnQ7d32aKo2xt88/xZptwxbfhA==", "dev": true, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-function-name": { - "version": "7.22.5", - "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.22.5.tgz", - "integrity": "sha512-wtHSq6jMRE3uF2otvfuD3DIvVhOsSNshQl0Qrd7qC9oQJzHvOL4qQXlQn2916+CXGywIjpGuIkoyZRRxHPiNQQ==", + "version": "7.23.0", + "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.23.0.tgz", + "integrity": "sha512-OErEqsrxjZTJciZ4Oo+eoZqeW9UIiOcuYKRJA4ZAgV9myA+pOXhhmpfNCKjEH/auVfEYVFJ6y1Tc4r0eIApqiw==", "dev": true, "dependencies": { - "@babel/template": "^7.22.5", - "@babel/types": "^7.22.5" + "@babel/template": "^7.22.15", + "@babel/types": "^7.23.0" }, "engines": { "node": ">=6.9.0" @@ -377,9 +377,9 @@ } }, "node_modules/@babel/helper-validator-identifier": { - "version": "7.22.5", - "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.22.5.tgz", - "integrity": "sha512-aJXu+6lErq8ltp+JhkJUfk1MTGyuA4v7f3pA+BJ5HLfNC6nAQ0Cpi9uOquUj8Hehg0aUiHzWQbOVJGao6ztBAQ==", + "version": "7.22.20", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.22.20.tgz", + "integrity": "sha512-Y4OZ+ytlatR8AI+8KZfKuL5urKp7qey08ha31L8b3BwewJAoJamTzyvxPR/5D+KkdJCGPq/+8TukHBlY10FX9A==", "dev": true, "engines": { "node": ">=6.9.0" @@ -494,9 +494,9 @@ } }, "node_modules/@babel/parser": { - "version": "7.22.13", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.22.13.tgz", - "integrity": "sha512-3l6+4YOvc9wx7VlCSw4yQfcBo01ECA8TicQfbnCPuCEpRQrf+gTUyGdxNw+pyTUyywp6JRD1w0YQs9TpBXYlkw==", + "version": "7.23.0", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.23.0.tgz", + "integrity": "sha512-vvPKKdMemU85V9WE/l5wZEmImpCtLqbnTvqDS2U1fJ96KrxoW7KrXhNsNCblQlg8Ck4b85yxdTyelsMUgFUXiw==", "dev": true, "bin": { "parser": "bin/babel-parser.js" @@ -695,33 +695,33 @@ } }, "node_modules/@babel/template": { - "version": "7.22.5", - "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.22.5.tgz", - "integrity": "sha512-X7yV7eiwAxdj9k94NEylvbVHLiVG1nvzCV2EAowhxLTwODV1jl9UzZ48leOC0sH7OnuHrIkllaBgneUykIcZaw==", + "version": "7.22.15", + "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.22.15.tgz", + "integrity": "sha512-QPErUVm4uyJa60rkI73qneDacvdvzxshT3kksGqlGWYdOTIUOwJ7RDUL8sGqslY1uXWSL6xMFKEXDS3ox2uF0w==", "dev": true, "dependencies": { - "@babel/code-frame": "^7.22.5", - "@babel/parser": "^7.22.5", - "@babel/types": "^7.22.5" + "@babel/code-frame": "^7.22.13", + "@babel/parser": "^7.22.15", + "@babel/types": "^7.22.15" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/traverse": { - "version": "7.22.11", - "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.22.11.tgz", - "integrity": "sha512-mzAenteTfomcB7mfPtyi+4oe5BZ6MXxWcn4CX+h4IRJ+OOGXBrWU6jDQavkQI9Vuc5P+donFabBfFCcmWka9lQ==", + "version": "7.23.2", + "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.23.2.tgz", + "integrity": "sha512-azpe59SQ48qG6nu2CzcMLbxUudtN+dOM9kDbUqGq3HXUJRlo7i8fvPoxQUzYgLZ4cMVmuZgm8vvBpNeRhd6XSw==", "dev": true, "dependencies": { - "@babel/code-frame": "^7.22.10", - "@babel/generator": "^7.22.10", - "@babel/helper-environment-visitor": "^7.22.5", - "@babel/helper-function-name": "^7.22.5", + "@babel/code-frame": "^7.22.13", + "@babel/generator": "^7.23.0", + "@babel/helper-environment-visitor": "^7.22.20", + "@babel/helper-function-name": "^7.23.0", "@babel/helper-hoist-variables": "^7.22.5", "@babel/helper-split-export-declaration": "^7.22.6", - "@babel/parser": "^7.22.11", - "@babel/types": "^7.22.11", + "@babel/parser": "^7.23.0", + "@babel/types": "^7.23.0", "debug": "^4.1.0", "globals": "^11.1.0" }, @@ -739,13 +739,13 @@ } }, "node_modules/@babel/types": { - "version": "7.22.11", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.22.11.tgz", - "integrity": "sha512-siazHiGuZRz9aB9NpHy9GOs9xiQPKnMzgdr493iI1M67vRXpnEq8ZOOKzezC5q7zwuQ6sDhdSp4SD9ixKSqKZg==", + "version": "7.23.0", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.23.0.tgz", + "integrity": "sha512-0oIyUfKoI3mSqMvsxBdclDwxXKXAUA8v/apZbc+iSyARYou1o8ZGDxbUYyLFoW2arqS2jDGqJuZvv1d/io1axg==", "dev": true, "dependencies": { "@babel/helper-string-parser": "^7.22.5", - "@babel/helper-validator-identifier": "^7.22.5", + "@babel/helper-validator-identifier": "^7.22.20", "to-fast-properties": "^2.0.0" }, "engines": { diff --git a/src/main.ts b/src/main.ts index 60a55d2..425da01 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,6 +17,41 @@ type TeamApprovalStatus = { actuallCount: number } +async function getAllReviews( + owner: string, + repo: string, + pullNumber: number, + GITHUB_TOKEN: string +) { + let allReviews: Review[] = [] + let page = 1 + let perPage = 100 + + while (true) { + const reviewsResponse = await axios.get( + `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/reviews`, + { + headers: { + Accept: 'application/vnd.github.v3+json', + Authorization: `token ${GITHUB_TOKEN}` + }, + params: { + page: page, + per_page: perPage + } + } + ) + + allReviews = allReviews.concat(reviewsResponse.data) + if (reviewsResponse.data.length < perPage) { + break + } + page++ + } + + return allReviews +} + /** * The main function for the action. * @returns {Promise} Resolves when the action is complete. @@ -31,23 +66,19 @@ export async function run(): Promise { ?.number as number const GITHUB_TOKEN: string = core.getInput('github-token') - const response = await axios.get( - `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/reviews`, - { - headers: { - Accept: 'application/vnd.github.v3+json', - Authorization: `token ${GITHUB_TOKEN}` - } - } + const allReviews = await getAllReviews( + owner, + repo, + pullNumber, + GITHUB_TOKEN ) - if (response.data.length === 0) { - core.setFailed( - 'There are no reviews for this pull request yet. Or the url is incorrect.' - ) + if (allReviews.length === 0) { + core.setFailed('There are no reviews for this pull request yet.') + return } - const approvedReviews = response.data.filter( + const approvedReviews = allReviews.filter( (review: Review) => review.state === 'APPROVED' )