Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add limit feature #13

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
32 changes: 25 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
});
Expand Down Expand Up @@ -1334,6 +1334,7 @@ module.exports = ({
displayCharts,
disableLinks,
currentRepo,
limit,
}) => {
const [owner, repo] = currentRepo.split('/');
const reposCount = (repos || []).length;
Expand All @@ -1349,6 +1350,7 @@ module.exports = ({
periodLength,
displayCharts,
disableLinks,
limit,
});
};

Expand Down Expand Up @@ -1380,19 +1382,24 @@ module.exports = (reviewers, options = {}) => {
periodLength,
disableLinks,
displayCharts,
limit,
} = options;

const execute = () => {
const allStats = reviewers.map((r) => r.stats);
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,
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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'));
Expand All @@ -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),
};
};

Expand Down Expand Up @@ -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(' ');
Expand All @@ -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;
Expand Down Expand Up @@ -9193,6 +9209,7 @@ const run = async (params) => {
displayCharts,
disableLinks,
pullRequestId,
limit,
} = params;
core.debug(`Params: ${JSON.stringify(params, null, 2)}`);

Expand All @@ -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');
Expand Down Expand Up @@ -9331,6 +9348,7 @@ const getParams = () => {
displayCharts: parseBoolean(core.getInput('charts')),
disableLinks: parseBoolean(core.getInput('disable-links')),
pullRequestId: getPrId(),
limit: core.getInput('limit'),
};
};

Expand Down Expand Up @@ -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"}};

/***/ }),

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion src/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const run = async (params) => {
displayCharts,
disableLinks,
pullRequestId,
limit,
} = params;
core.debug(`Params: ${JSON.stringify(params, null, 2)}`);

Expand All @@ -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');
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const getParams = () => {
displayCharts: parseBoolean(core.getInput('charts')),
disableLinks: parseBoolean(core.getInput('disable-links')),
pullRequestId: getPrId(),
limit: core.getInput('limit'),
};
};

Expand Down
9 changes: 9 additions & 0 deletions src/interactors/buildTable/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const LINKS_RESPONSE = `|
| <a href="https://github.com/user1"><img src="https://avatars.githubusercontent.com/u/1234" width="20"></a> | 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 |
| <a href="https://github.com/user2"><img src="https://avatars.githubusercontent.com/u/5678" width="20"></a> | 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 |
| ---------------------------------------------------------------------------------------------------------- | ----- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------- |
| <a href="https://github.com/user1"><img src="https://avatars.githubusercontent.com/u/1234" width="20"></a> | 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 |
| ---------------------------------------------------------------------------------------------------------- | ------------ | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
| <a href="https://github.com/user1"><img src="https://avatars.githubusercontent.com/u/1234" width="32"></a> | user1<br/>🥇 | **4**<br/>▀▀▀▀▀▀▀▀ | [**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))))<br/>▀▀ | 1<br/>▀▀ |
Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/interactors/buildTable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ module.exports = (reviewers, options = {}) => {
periodLength,
disableLinks,
displayCharts,
limit,
} = options;

const execute = () => {
const allStats = reviewers.map((r) => r.stats);
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,
Expand Down
2 changes: 1 addition & 1 deletion src/interactors/getReviewers/index.js
Original file line number Diff line number Diff line change
@@ -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 };
});
2 changes: 2 additions & 0 deletions src/interactors/trackRun.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = ({
displayCharts,
disableLinks,
currentRepo,
limit,
}) => {
const [owner, repo] = currentRepo.split('/');
const reposCount = (repos || []).length;
Expand All @@ -23,5 +24,6 @@ module.exports = ({
periodLength,
displayCharts,
disableLinks,
limit,
});
};