Skip to content

Instantly share code, notes, and snippets.

@ravgeetdhillon
Last active January 7, 2025 02:43
Show Gist options
  • Save ravgeetdhillon/30e75e51eaf9c1ca85f23adc6a43a1ff to your computer and use it in GitHub Desktop.
Save ravgeetdhillon/30e75e51eaf9c1ca85f23adc6a43a1ff to your computer and use it in GitHub Desktop.
Script to get all commits that a GitHub user has made after a given date in all repositories owned by the given owner
"use strict";
const AFTER_DATE = "2024-12-02";
const MY_GITHUB_ID = "ravgeetdhillon";
const REPOS = ["cloudanswers/designeradvantage-2"];
const beautifyCommitObject = ({
messageBody,
messageHeadline,
prNumber,
branch,
}) => ({
messageBody,
messageHeadline,
prNumber,
branch,
});
const execSync = require("child_process").execSync;
const prsQuery = `gh search prs --repo="${REPOS.join(
","
)}" --sort="updated" --updated=">=${AFTER_DATE}" --json="number"`;
const prQueryResponse = JSON.parse(
execSync(prsQuery, (error, stdout, stderr) => stdout).toString()
);
const prNumbers = prQueryResponse.map((pr) => pr.number);
const commitsQuery = (prNumber) =>
`gh pr view ${prNumber} --json "commits,headRefName,number"`;
const commits = prNumbers.reduce((prev, prNumber) => {
const commitsQueryResponse = JSON.parse(
execSync(
commitsQuery(prNumber),
(error, stdout, stderr) => stdout
).toString()
);
const commitsAfterDate = commitsQueryResponse.commits.filter(
(commit) =>
new Date(commit.committedDate).getTime() > new Date(AFTER_DATE).getTime()
);
const myCommits = commitsAfterDate.filter((commit) =>
commit.authors.map((author) => author.login).includes(MY_GITHUB_ID)
);
return [
...prev,
...myCommits
.map((commit) => ({
...commit,
branch: commitsQueryResponse.headRefName,
prNumber: commitsQueryResponse.number,
}))
.filter((commit) => commit.branch !== "master"),
];
}, []);
const groupByDate = commits.reduce((prev, commit) => {
if (commit.committedDate.split("T")[0] in prev) {
prev = {
...prev,
[commit.committedDate.split("T")[0]]: [
...prev[commit.committedDate.split("T")[0]],
beautifyCommitObject(commit),
],
};
} else {
prev = {
...prev,
[commit.committedDate.split("T")[0]]: [beautifyCommitObject(commit)],
};
}
return prev;
}, {});
const result = Object.keys(groupByDate)
.sort()
.reduce((obj, key) => {
obj[key] = groupByDate[key];
return obj;
}, {});
console.log(JSON.stringify(result, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment