-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,12 @@ | ||
const core = require("@actions/core"); | ||
|
||
const fetchPullRequests = require("./src/fetch-pull-requests"); | ||
const getTimeToFirstReview = require("./src/get-time-to-first-review"); | ||
const main = require("./src"); | ||
|
||
try { | ||
// GitHub repository information | ||
const repo = core.getInput("repo"); | ||
const token = core.getInput("github_token"); | ||
const maxPRs = core.getInput("num_prs") || 200; | ||
|
||
// Main function | ||
const main = async () => { | ||
const prs = await fetchPullRequests(repo, token, maxPRs); | ||
|
||
let timesToFirstReview = await Promise.all( | ||
prs.map((pr) => getTimeToFirstReview(pr, repo, token)) | ||
); | ||
|
||
timesToFirstReview = timesToFirstReview.filter( | ||
(time) => time !== undefined | ||
); | ||
|
||
if (timesToFirstReview.length > 0) { | ||
const total = timesToFirstReview.reduce((sum, value) => sum + value, 0); | ||
const average = total / timesToFirstReview.length; | ||
console.log(`Average time to first review: ${average.toFixed(2)} hours`); | ||
} else { | ||
console.log(`No reviews found for the last ${maxPRs} PRs.`); | ||
} | ||
}; | ||
|
||
main(); | ||
const maxPRs = core.getInput("num_prs") || 100; | ||
main(repo, token, maxPRs); | ||
} catch (error) { | ||
core.setFailed(error.message); | ||
} |
67 changes: 67 additions & 0 deletions
67
.github/actions/time-to-first-review/src/__tests__/get-time-to-first-review.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Mock the fetchReviews function | ||
jest.mock("../fetch-reviews", () => jest.fn()); | ||
|
||
const getTimeToFirstReview = require("../get-time-to-first-review"); | ||
const fetchReviews = require("../fetch-reviews"); | ||
|
||
const repo = "owner/repo"; | ||
const gitHubToken = "my-token"; | ||
|
||
describe.only("getTimeToFirstReview", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("should return the time difference between the PR creation and the first review", async () => { | ||
const pr = { number: 1, created_at: new Date("2024-01-01T00:00:00Z") }; | ||
|
||
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation(); | ||
|
||
fetchReviews.mockResolvedValue([ | ||
{ submitted_at: new Date("2024-01-01T01:00:00Z") }, | ||
{ submitted_at: new Date("2024-01-01T02:00:00Z") }, | ||
]); | ||
|
||
const result = await getTimeToFirstReview(pr, repo, gitHubToken); | ||
|
||
expect(result).toBe(1); | ||
expect(consoleLogSpy).toHaveBeenCalledTimes(1); | ||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
"PR #1 - Time to first review: 1 hours" | ||
); | ||
}); | ||
|
||
it("should return 0 if there are no reviews", async () => { | ||
const pr = { number: 1, created_at: new Date("2024-01-01T00:00:00Z") }; | ||
|
||
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation(); | ||
|
||
fetchReviews.mockResolvedValue([]); | ||
|
||
const result = await getTimeToFirstReview(pr, repo, gitHubToken); | ||
|
||
expect(result).toBe(undefined); | ||
expect(consoleLogSpy).toHaveBeenCalledTimes(1); | ||
expect(consoleLogSpy).toHaveBeenCalledWith("PR #1 - No reviews found"); | ||
}); | ||
|
||
it("should return 0 if the first review is created before the PR", async () => { | ||
const pr = { number: 1, created_at: new Date("2024-01-01T00:00:00Z") }; | ||
|
||
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation(); | ||
|
||
fetchReviews.mockResolvedValue([ | ||
{ submitted_at: new Date("2023-12-31T23:00:00Z") }, | ||
{ submitted_at: new Date("2024-01-01T01:00:00Z") }, | ||
]); | ||
|
||
const result = await getTimeToFirstReview(pr, repo, gitHubToken); | ||
|
||
expect(result).toBe(0); | ||
expect(consoleLogSpy).toHaveBeenCalledTimes(1); | ||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
"PR #1 - Time to first review: 0 hours" | ||
); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
.github/actions/time-to-first-review/src/__tests__/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
jest.mock("../get-time-to-first-review"); | ||
jest.mock("../fetch-pull-requests"); | ||
|
||
const getTimeToFirstReview = require("../get-time-to-first-review"); | ||
const fetchPullRequests = require("../fetch-pull-requests"); | ||
|
||
const main = require(".."); | ||
|
||
describe("main", () => { | ||
const repo = "owner/repo"; | ||
const token = "token"; | ||
const maxPRs = 10; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("should call getTimeToFirstReview and fetchPullRequests with the correct arguments", async () => { | ||
fetchPullRequests.mockResolvedValue([ | ||
{ | ||
id: 1, | ||
}, | ||
{ | ||
id: 2, | ||
}, | ||
]); | ||
|
||
getTimeToFirstReview.mockResolvedValue(5); | ||
|
||
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation(); | ||
|
||
await main(repo, token, maxPRs); | ||
|
||
expect(fetchPullRequests).toHaveBeenCalledWith(repo, token, maxPRs); | ||
expect(getTimeToFirstReview).toHaveBeenCalledTimes(2); | ||
expect(consoleLogSpy).toHaveBeenCalledTimes(1); | ||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
"Average time to first review: 5.00 hours" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const getTimeToFirstReview = require("./get-time-to-first-review"); | ||
const fetchPullRequests = require("./fetch-pull-requests"); | ||
|
||
module.exports = async (repo, token, maxPRs) => { | ||
const prs = await fetchPullRequests(repo, token, maxPRs); | ||
|
||
let timesToFirstReview = await Promise.all( | ||
prs.map((pr) => getTimeToFirstReview(pr, repo, token)) | ||
); | ||
|
||
timesToFirstReview = timesToFirstReview.filter((time) => time !== undefined); | ||
|
||
if (timesToFirstReview.length > 0) { | ||
const total = timesToFirstReview.reduce((sum, value) => sum + value, 0); | ||
const average = total / timesToFirstReview.length; | ||
console.log(`Average time to first review: ${average.toFixed(2)} hours`); | ||
} else { | ||
console.log(`No reviews found for the last ${maxPRs} PRs.`); | ||
} | ||
}; |