Skip to content

Commit

Permalink
Refactor code: Requests and file structure (#25)
Browse files Browse the repository at this point in the history
* Added headers with version and changed structure

* Added headers for rate limit and changed octokit imports

* Changed import for getdisplayuserlist

* version update
  • Loading branch information
AlexSim93 committed Jan 14, 2024
1 parent 2753f9e commit 95b3d72
Show file tree
Hide file tree
Showing 23 changed files with 310 additions and 201 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Pull request analytics action

![Version](https://img.shields.io/badge/version-1.12.6-blue) ![License](https://img.shields.io/badge/license-MIT-green)
![Version](https://img.shields.io/badge/version-1.12.7-blue) ![License](https://img.shields.io/badge/license-MIT-green)

**pull-request-analytics-action**: A powerful tool for analyzing the effectiveness of both teams and individual developers. This action generates reports based on data from pull requests, code reviews, and comments, enabling you to identify your team's strengths as well as areas needing improvement. The statistics collected by this GitHub Action can be displayed in the form of tables and graphs or passed on for further operations as markdown or a data collection. No information is transferred to external services; all operations are conducted exclusively within the GitHub environment. The tool offers numerous configuration parameters and can be customized to suit specific project needs.

Expand Down
386 changes: 226 additions & 160 deletions build/index.js

Large diffs are not rendered by default.

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-analytics-action",
"version": "1.12.6",
"version": "1.12.7",
"description": "Generates detailed PR analytics reports within GitHub, focusing on review efficiency and team performance.",
"main": "build/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/converters/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { Collection } from './types';
export { collectData } from "./collectData";
29 changes: 11 additions & 18 deletions src/view/createOutput.ts → src/createOutput.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as core from "@actions/core";
import { getMultipleValuesInput, getValueAsIs } from "../common/utils";
import { Collection } from "../converters/types";
import { createMarkdown } from "./createMarkdown";
import { clearComments, createIssue } from "../requests";
import { getMultipleValuesInput, getValueAsIs } from "./common/utils";
import { Collection } from "./converters";
import { createMarkdown } from "./view/";
import { clearComments, createComment, createIssue } from "./requests";
import {
createTimelineMonthComparisonChart,
getDisplayUserList,
sortCollectionsByDate,
} from "./utils";
import { octokit } from "../octokit/octokit";
} from "./view/utils";
import { octokit } from "./octokit";

export const createOutput = async (
data: Record<string, Record<string, Collection>>
Expand Down Expand Up @@ -71,19 +71,11 @@ export const createOutput = async (
]
);
if (commentMarkdown === "") continue;
const comment = await octokit.rest.issues.createComment({
repo: getValueAsIs("GITHUB_REPO_FOR_ISSUE"),
owner: getValueAsIs("GITHUB_OWNER_FOR_ISSUE"),
issue_number: issue.data.number,
body: commentMarkdown,
});
const comment = await createComment(issue.data.number, commentMarkdown);
comments.push({ comment, title: date });
}
await octokit.rest.issues.update({
repo: getValueAsIs("GITHUB_REPO_FOR_ISSUE"),
owner: getValueAsIs("GITHUB_OWNER_FOR_ISSUE"),
issue_number: issue.data.number,
body: createMarkdown(
await createIssue(
createMarkdown(
data,
users,
["total"],
Expand All @@ -93,7 +85,8 @@ export const createOutput = async (
link: comment.comment.data.html_url,
}))
),
});
issue.data.number
);
}

if (outcome === "markdown" || outcome === "output") {
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import "dotenv/config";
import * as core from "@actions/core";

import { createOutput } from "./view";
import { createOutput } from "./createOutput";
import {
getOrganizationsRepositories,
getOwnersRepositories,
makeComplexRequest,
} from "./requests";
import { collectData } from "./converters";
import { octokit } from "./octokit/octokit";
import {
checkCommentSkip,
getValueAsIs,
setTimezone,
validate,
} from "./common/utils";
import { getRateLimit } from "./requests/getRateLimit";

async function main() {
setTimezone();
Expand All @@ -25,7 +25,7 @@ async function main() {
);
return;
}
const rateLimitAtBeginning = await octokit.rest.rateLimit.get();
const rateLimitAtBeginning = await getRateLimit();
console.log(
"RATE LIMIT REMAINING BEFORE REQUESTS: ",
rateLimitAtBeginning.data.rate.remaining
Expand Down Expand Up @@ -81,7 +81,7 @@ async function main() {
console.log("Calculation complete. Generating markdown.");
await createOutput(preparedData);

const rateLimitAtEnd = await octokit.rest.rateLimit.get();
const rateLimitAtEnd = await getRateLimit();
console.log(
"RATE LIMIT REMAINING AFTER REQUESTS: ",
rateLimitAtEnd.data.rate.remaining
Expand Down
1 change: 1 addition & 0 deletions src/octokit/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { octokit } from "./octokit";
4 changes: 3 additions & 1 deletion src/requests/clearComments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getValueAsIs } from "../common/utils";
import { octokit } from "../octokit/octokit";
import { octokit } from "../octokit";
import { commonHeaders } from "./constants";

export const clearComments = async (issueNumber?: string) => {
if (!issueNumber) return;
Expand All @@ -13,6 +14,7 @@ export const clearComments = async (issueNumber?: string) => {
repo: getValueAsIs("GITHUB_REPO_FOR_ISSUE"),
owner: getValueAsIs("GITHUB_OWNER_FOR_ISSUE"),
comment_id: comment.id,
headers: commonHeaders,
});
}
};
3 changes: 3 additions & 0 deletions src/requests/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const concurrentLimit = 25;
export const commonHeaders = {
"X-GitHub-Api-Version": "2022-11-28",
};
15 changes: 15 additions & 0 deletions src/requests/createComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getValueAsIs } from "../common/utils";
import { octokit } from "../octokit";
import { commonHeaders } from "./constants";

export const createComment = async (
issueNumber: number,
commentMarkdown: string
) =>
octokit.rest.issues.createComment({
repo: getValueAsIs("GITHUB_REPO_FOR_ISSUE"),
owner: getValueAsIs("GITHUB_OWNER_FOR_ISSUE"),
issue_number: issueNumber,
body: commentMarkdown,
headers: commonHeaders,
});
13 changes: 10 additions & 3 deletions src/requests/createIssue.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { octokit } from "../octokit/octokit";
import { commonHeaders } from "./constants";
import { octokit } from "../octokit";
import { format } from "date-fns";
import { getMultipleValuesInput, getValueAsIs } from "../common/utils";

export const createIssue = async (markdown: string, issueNumber?: string) => {
export const createIssue = async (
markdown: string,
issueNumber?: string | number
) => {
const issueTitle =
getValueAsIs("ISSUE_TITLE") ||
`Pull requests report(${format(new Date(), "d/MM/yyyy HH:mm")})`;
Expand All @@ -24,7 +28,9 @@ export const createIssue = async (markdown: string, issueNumber?: string) => {
repo: getValueAsIs("GITHUB_REPO_FOR_ISSUE"),
owner: getValueAsIs("GITHUB_OWNER_FOR_ISSUE"),
body: markdown,
issue_number: parseInt(issueNumber),
issue_number:
typeof issueNumber === "number" ? issueNumber : parseInt(issueNumber),
headers: commonHeaders,
});
} else {
result = await octokit.rest.issues.create({
Expand All @@ -34,6 +40,7 @@ export const createIssue = async (markdown: string, issueNumber?: string) => {
body: markdown,
labels,
assignees,
headers: commonHeaders,
});
}

Expand Down
11 changes: 9 additions & 2 deletions src/requests/getOrganizationsRepositories.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { getMultipleValuesInput } from "../common/utils";
import { octokit } from "../octokit/octokit";
import { octokit } from "../octokit";
import { commonHeaders } from "./constants";

export const getOrganizationsRepositories = async () => {
const organizations = getMultipleValuesInput("ORGANIZATIONS");
const ownersRepos = [];
for (let i = 0; i < organizations.length; i++) {
const organizationRepositories = await octokit.paginate(
octokit.rest.repos.listForOrg,
{ org: organizations[i], type: "all", sort: "pushed", direction: "desc" }
{
org: organizations[i],
type: "all",
sort: "pushed",
direction: "desc",
headers: commonHeaders,
}
);

const repos = organizationRepositories.map((el) => [
Expand Down
5 changes: 3 additions & 2 deletions src/requests/getPullRequestComments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { octokit } from "../octokit/octokit";
import { commonHeaders } from "./constants";
import { octokit } from "../octokit";
import { Repository } from "./types";

export const getPullRequestComments = async (
Expand All @@ -11,7 +12,7 @@ export const getPullRequestComments = async (
? pullRequestNumbers.map(async (number) => {
const comments = await octokit.paginate(
octokit.rest.pulls.listReviewComments,
{ owner, repo, pull_number: number }
{ owner, repo, pull_number: number, headers: commonHeaders }
);

return { data: comments };
Expand Down
4 changes: 3 additions & 1 deletion src/requests/getPullRequestData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Repository } from "./types";

import { octokit } from "../octokit/octokit";
import { octokit } from "../octokit";
import { commonHeaders } from "./constants";

export const getPullRequestDatas = async (
pullRequestNumbers: number[],
Expand All @@ -12,6 +13,7 @@ export const getPullRequestDatas = async (
owner,
repo,
pull_number: number,
headers: commonHeaders,
})
);
};
4 changes: 3 additions & 1 deletion src/requests/getPullRequestReviews.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { octokit } from "../octokit/octokit";
import { commonHeaders } from './constants';
import { octokit } from "../octokit";
import { Repository } from "./types";

export const getPullRequestReviews = async (
Expand All @@ -13,6 +14,7 @@ export const getPullRequestReviews = async (
owner,
repo,
pull_number: number,
headers: commonHeaders,
});
return { data: reviews };
})
Expand Down
4 changes: 3 additions & 1 deletion src/requests/getPullRequests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { commonHeaders } from "./constants";
import { isAfter, isBefore, parseISO } from "date-fns";

import { octokit } from "../octokit/octokit";
import { octokit } from "../octokit";
import { getReportDates } from "./utils";
import { Repository } from "./types";

Expand All @@ -25,6 +26,7 @@ export const getPullRequests = async (
state: "closed",
sort: "updated",
direction: "desc",
headers: commonHeaders,
});
if (startDate || endDate) {
const filteredPulls = pulls.data.filter((pr) => {
Expand Down
5 changes: 5 additions & 0 deletions src/requests/getRateLimit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { octokit } from "../octokit";
import { commonHeaders } from "./constants";

export const getRateLimit = async () =>
octokit.rest.rateLimit.get({ headers: commonHeaders });
3 changes: 2 additions & 1 deletion src/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { clearComments } from './clearComments';
export { createComment } from "./createComment";
export { clearComments } from "./clearComments";
export { getOwnersRepositories } from "./utils/getOwnersRepositories";
export { getOrganizationsRepositories } from "./getOrganizationsRepositories";
export { makeComplexRequest } from "./makeComplexRequest";
Expand Down
2 changes: 1 addition & 1 deletion src/requests/makeComplexRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { octokit } from "../octokit/octokit";
import { octokit } from "../octokit";
import { getMultipleValuesInput } from "../common/utils";
import { getDataWithThrottle } from "./getDataWithThrottle";
import { getPullRequests } from "./getPullRequests";
Expand Down
2 changes: 1 addition & 1 deletion src/view/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { createOutput } from "./createOutput";
export { createMarkdown } from "./createMarkdown";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getMultipleValuesInput } from "../../common/utils";
import { Collection } from "../../converters/types";
import { getMultipleValuesInput } from "../../../common/utils";
import { Collection } from "../../../converters/types";

export const getDisplayUserList = (
data: Record<string, Record<string, Collection>>
Expand Down
1 change: 1 addition & 0 deletions src/view/utils/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { getDisplayUserList } from "./getDisplayUserList";
export { createTable } from "./createTable";
export { createGanttBar } from "./createGanttBar";
export { createPieChart } from "./createPieChart";
Expand Down
2 changes: 1 addition & 1 deletion src/view/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { sortCollectionsByDate } from "./sortCollectionsByDate";
export { createTimelineGanttBar } from "./createTimelineGanttBar";
export { createTimelineTable } from "./createTimelineTable";
export { createPullRequestQualityTable } from "./createPullRequestQualityTable";
export { getDisplayUserList } from "./getDisplayUserList";
export { getDisplayUserList } from "./common";
export { createTimelineContent } from "./createTimelineContent";
export { createTimelineMonthsGanttBar } from "./createTimelineMonthsGanttBar";
export { createReferences } from "./createReferences";
Expand Down

0 comments on commit 95b3d72

Please sign in to comment.