From 439deae354379508859410dd8d3c65bb28a6f71a Mon Sep 17 00:00:00 2001 From: Cristian Escudero Date: Mon, 23 Aug 2021 14:22:14 +0200 Subject: [PATCH 1/3] Add limit feature for the rows --- README.md | 1 + action.yml | 4 ++++ src/execute.js | 3 ++- src/index.js | 1 + src/interactors/buildTable/__tests__/index.test.js | 9 +++++++++ src/interactors/buildTable/index.js | 7 ++++++- src/interactors/getReviewers/index.js | 2 +- src/interactors/trackRun.js | 2 ++ 8 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8068175..326437d 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ The possible inputs for this action are: | `charts` | Whether to add a chart to the start or not. Possible values: `true` or `false`. | `false` | | `disable-links` | If `true`, removes the links to the detailed charts. Possible values: `true` or `false`. | `false` | | `sort-by` | The column used to sort the data. Possible values: `REVIEWS`, `TIME`, `COMMENTS`. | `REVIEWS` | +| `limit` | The maximum number of rows for the table. 0 means unlimited. | `0` ## Examples diff --git a/action.yml b/action.yml index 8b107b1..6ede0da 100644 --- a/action.yml +++ b/action.yml @@ -28,6 +28,10 @@ inputs: description: 'Prevents from adding any external links in the stats' required: false default: false + limit: + description: 'The maximum number of rows for the table. 0 means unlimited.' + required: false + default: 0 runs: using: 'node12' main: 'dist/index.js' diff --git a/src/execute.js b/src/execute.js index f9eb461..2e755b9 100644 --- a/src/execute.js +++ b/src/execute.js @@ -24,6 +24,7 @@ const run = async (params) => { displayCharts, disableLinks, pullRequestId, + limit, } = params; core.debug(`Params: ${JSON.stringify(params, null, 2)}`); @@ -45,7 +46,7 @@ const run = async (params) => { core.info(`Analyzed stats for ${reviewers.length} pull request reviewers`); const tableOptions = { - displayCharts, disableLinks, sortBy, periodLength, + displayCharts, disableLinks, sortBy, periodLength, limit, }; const table = buildTable(reviewers, tableOptions); core.debug('Stats table built successfully'); diff --git a/src/index.js b/src/index.js index fec42cf..8d44d9a 100644 --- a/src/index.js +++ b/src/index.js @@ -36,6 +36,7 @@ const getParams = () => { displayCharts: parseBoolean(core.getInput('charts')), disableLinks: parseBoolean(core.getInput('disable-links')), pullRequestId: getPrId(), + limit: core.getInput('limit'), }; }; diff --git a/src/interactors/buildTable/__tests__/index.test.js b/src/interactors/buildTable/__tests__/index.test.js index 250b520..92bfb6e 100644 --- a/src/interactors/buildTable/__tests__/index.test.js +++ b/src/interactors/buildTable/__tests__/index.test.js @@ -21,6 +21,10 @@ const LINKS_RESPONSE = `| | | user1 | **4** | [**34m**](https://app.flowwer.dev/charts/review-time/~(u~(i~'1234~n~'user1)~r~(~(d~'qpvagu~t~'a3)~(d~'qpvn25~t~'3lu)~(d~'qprzn9~t~'84)~(d~'qqqtu5~t~'2vy)))) | 1 | | | user2 | 1 | [2h 21m](https://app.flowwer.dev/charts/review-time/~(u~(i~'5678~n~'user2)~r~(~(d~'qq0fbc~t~'6j5)))) | **5** |`; +const LIMIT_ONE_RESPONSE = `| | User | Total reviews | Median time to review | Total comments | +| ---------------------------------------------------------------------------------------------------------- | ----- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------- | +| | user1 | **4** | [**34m**](https://app.flowwer.dev/charts/review-time/~(u~(i~'1234~n~'user1)~r~(~(d~'qpvagu~t~'a3)~(d~'qpvn25~t~'3lu)~(d~'qprzn9~t~'84)~(d~'qqqtu5~t~'2vy)))) | 1 |`; + const LINKS_AND_CHARTS_RESPONSE = `| | User | Total reviews | Median time to review | Total comments | | ---------------------------------------------------------------------------------------------------------- | ------------ | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ | | | user1
🥇 | **4**
▀▀▀▀▀▀▀▀ | [**34m**](https://app.flowwer.dev/charts/review-time/~(u~(i~'1234~n~'user1)~r~(~(d~'qpvagu~t~'a3)~(d~'qpvn25~t~'3lu)~(d~'qprzn9~t~'84)~(d~'qqqtu5~t~'2vy))))
▀▀ | 1
▀▀ | @@ -47,6 +51,11 @@ describe('Interactors | .buildTable', () => { expect(response).toEqual(LINKS_RESPONSE); }); + it('with limit 1', () => { + const response = buildTable(reviewers, { limit: 1 }); + expect(response).toEqual(LIMIT_ONE_RESPONSE); + }); + it('with links and charts', () => { const response = buildTable(reviewers, { displayCharts: true }); expect(response).toEqual(LINKS_AND_CHARTS_RESPONSE); diff --git a/src/interactors/buildTable/index.js b/src/interactors/buildTable/index.js index 4d931d6..a86f3dc 100644 --- a/src/interactors/buildTable/index.js +++ b/src/interactors/buildTable/index.js @@ -13,6 +13,7 @@ module.exports = (reviewers, options = {}) => { periodLength, disableLinks, displayCharts, + limit, } = options; const execute = () => { @@ -20,12 +21,16 @@ module.exports = (reviewers, options = {}) => { const totals = calculateTotals(allStats); const bests = calculateBests(allStats); - const populatedReviewers = sortByStats(reviewers, sortBy).map((reviewer) => ({ + let populatedReviewers = sortByStats(reviewers, sortBy).map((reviewer) => ({ ...reviewer, contributions: getContributions(reviewer, totals), urls: { timeToReview: buildReviewTimeLink(reviewer, periodLength) }, })); + if (limit > 0) { + populatedReviewers = populatedReviewers.slice(0, limit); + } + const tableData = getTableData({ bests, disableLinks, diff --git a/src/interactors/getReviewers/index.js b/src/interactors/getReviewers/index.js index 2112927..8e752b3 100644 --- a/src/interactors/getReviewers/index.js +++ b/src/interactors/getReviewers/index.js @@ -1,7 +1,7 @@ const calculateReviewsStats = require('./calculateReviewsStats'); const groupReviews = require('./groupReviews'); -module.exports = (pulls) => groupReviews(pulls).map(({ author, reviews }) => { +module.exports = (pulls, limit) => groupReviews(pulls).map(({ author, reviews }) => { const stats = calculateReviewsStats(reviews); return { author, reviews, stats }; }); diff --git a/src/interactors/trackRun.js b/src/interactors/trackRun.js index d8cdf2b..ef6f113 100644 --- a/src/interactors/trackRun.js +++ b/src/interactors/trackRun.js @@ -8,6 +8,7 @@ module.exports = ({ displayCharts, disableLinks, currentRepo, + limit, }) => { const [owner, repo] = currentRepo.split('/'); const reposCount = (repos || []).length; @@ -23,5 +24,6 @@ module.exports = ({ periodLength, displayCharts, disableLinks, + limit, }); }; From 9bed83dfcbf8c8cab7b6a9f25d25ab0d0fe46911 Mon Sep 17 00:00:00 2001 From: Cristian Escudero Date: Mon, 23 Aug 2021 14:22:24 +0200 Subject: [PATCH 2/3] Include new dist file --- dist/index.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/dist/index.js b/dist/index.js index cc35e3e..96e2c78 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1289,7 +1289,7 @@ module.exports = { const calculateReviewsStats = __webpack_require__(57); const groupReviews = __webpack_require__(755); -module.exports = (pulls) => groupReviews(pulls).map(({ author, reviews }) => { +module.exports = (pulls, limit) => groupReviews(pulls).map(({ author, reviews }) => { const stats = calculateReviewsStats(reviews); return { author, reviews, stats }; }); @@ -1334,6 +1334,7 @@ module.exports = ({ displayCharts, disableLinks, currentRepo, + limit, }) => { const [owner, repo] = currentRepo.split('/'); const reposCount = (repos || []).length; @@ -1349,6 +1350,7 @@ module.exports = ({ periodLength, displayCharts, disableLinks, + limit, }); }; @@ -1380,6 +1382,7 @@ module.exports = (reviewers, options = {}) => { periodLength, disableLinks, displayCharts, + limit, } = options; const execute = () => { @@ -1387,12 +1390,16 @@ module.exports = (reviewers, options = {}) => { const totals = calculateTotals(allStats); const bests = calculateBests(allStats); - const populatedReviewers = sortByStats(reviewers, sortBy).map((reviewer) => ({ + let populatedReviewers = sortByStats(reviewers, sortBy).map((reviewer) => ({ ...reviewer, contributions: getContributions(reviewer, totals), urls: { timeToReview: buildReviewTimeLink(reviewer, periodLength) }, })); + if (limit > 0) { + populatedReviewers = populatedReviewers.slice(0, limit); + } + const tableData = getTableData({ bests, disableLinks, @@ -1424,7 +1431,7 @@ const { TABLE_TITLE } = __webpack_require__(648); module.exports = (pullRequest) => { const { body } = pullRequest || {}; - const regexp = new RegExp(`\\n(${TABLE_TITLE})\\n`); + const regexp = new RegExp(`(^|\\n)(${TABLE_TITLE})\\n`); return regexp.test(body); }; @@ -3292,6 +3299,10 @@ const get = __webpack_require__(854); const parseUser = __webpack_require__(359); const parseReview = __webpack_require__(158); +const filterNullAuthor = ({ author }) => !!author; + +const getFilteredReviews = (data) => get(data, 'node.reviews.nodes', []).filter(filterNullAuthor); + module.exports = (data = {}) => { const author = parseUser(get(data, 'node.author')); const publishedAt = new Date(get(data, 'node.publishedAt')); @@ -3302,7 +3313,7 @@ module.exports = (data = {}) => { publishedAt, cursor: data.cursor, id: get(data, 'node.id'), - reviews: get(data, 'node.reviews.nodes', []).map(handleReviews), + reviews: getFilteredReviews(data).map(handleReviews), }; }; @@ -8924,6 +8935,8 @@ module.exports = ({ const { fetchPullRequests } = __webpack_require__(162); const { parsePullRequest } = __webpack_require__(120); +const filterNullAuthor = ({ node }) => !!node.author; + const ownerFilter = ({ org, repos }) => { if (org) return `org:${org}`; return (repos || []).map((r) => `repo:${r}`).join(' '); @@ -8937,7 +8950,10 @@ const buildQuery = ({ org, repos, startDate }) => { const getPullRequests = async (params) => { const { limit } = params; const data = await fetchPullRequests(params); - const results = data.search.edges.map(parsePullRequest); + const results = data.search.edges + .filter(filterNullAuthor) + .map(parsePullRequest); + if (results.length < limit) return results; const last = results[results.length - 1].cursor; @@ -9193,6 +9209,7 @@ const run = async (params) => { displayCharts, disableLinks, pullRequestId, + limit, } = params; core.debug(`Params: ${JSON.stringify(params, null, 2)}`); @@ -9214,7 +9231,7 @@ const run = async (params) => { core.info(`Analyzed stats for ${reviewers.length} pull request reviewers`); const tableOptions = { - displayCharts, disableLinks, sortBy, periodLength, + displayCharts, disableLinks, sortBy, periodLength, limit, }; const table = buildTable(reviewers, tableOptions); core.debug('Stats table built successfully'); @@ -9331,6 +9348,7 @@ const getParams = () => { displayCharts: parseBoolean(core.getInput('charts')), disableLinks: parseBoolean(core.getInput('disable-links')), pullRequestId: getPrId(), + limit: core.getInput('limit'), }; }; @@ -9489,7 +9507,7 @@ module.exports = (value) => parser(value, { /***/ 731: /***/ (function(module) { -module.exports = {"name":"pull-request-stats","version":"2.0.0","description":"Github action to print relevant stats about Pull Request reviewers","main":"dist/index.js","scripts":{"build":"ncc build src/index.js","test":"jest"},"keywords":[],"author":"Manuel de la Torre","license":"agpl-3.0","jest":{"testEnvironment":"node","testMatch":["**/?(*.)+(spec|test).[jt]s?(x)"]},"dependencies":{"@actions/core":"^1.2.6","@actions/github":"^4.0.0","humanize-duration":"^3.25.1","jsurl":"^0.1.5","lodash.get":"^4.4.2","markdown-table":"^2.0.0","mixpanel":"^0.13.0"},"devDependencies":{"@zeit/ncc":"^0.22.3","eslint":"^7.2.0","eslint-config-airbnb-base":"14.2.1","eslint-plugin-import":"^2.22.1","eslint-plugin-jest":"^24.3.3","jest":"^26.6.3"}}; +module.exports = {"name":"pull-request-stats","version":"2.0.3","description":"Github action to print relevant stats about Pull Request reviewers","main":"dist/index.js","scripts":{"build":"ncc build src/index.js","test":"jest"},"keywords":[],"author":"Manuel de la Torre","license":"agpl-3.0","jest":{"testEnvironment":"node","testMatch":["**/?(*.)+(spec|test).[jt]s?(x)"]},"dependencies":{"@actions/core":"^1.2.6","@actions/github":"^4.0.0","humanize-duration":"^3.25.1","jsurl":"^0.1.5","lodash.get":"^4.4.2","markdown-table":"^2.0.0","mixpanel":"^0.13.0"},"devDependencies":{"@zeit/ncc":"^0.22.3","eslint":"^7.2.0","eslint-config-airbnb-base":"14.2.1","eslint-plugin-import":"^2.22.1","eslint-plugin-jest":"^24.3.3","jest":"^26.6.3"}}; /***/ }), From d81acf25a5101989ba8fd222301dce790605bc03 Mon Sep 17 00:00:00 2001 From: Cristian Escudero Date: Mon, 23 Aug 2021 14:23:39 +0200 Subject: [PATCH 3/3] Pump minor version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b44aae6..04f742c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pull-request-stats", - "version": "2.0.3", + "version": "2.1.3", "description": "Github action to print relevant stats about Pull Request reviewers", "main": "dist/index.js", "scripts": {