Skip to content

Instantly share code, notes, and snippets.

@alestrunda
Last active March 19, 2021 19:49
Show Gist options
  • Save alestrunda/49b506118ff52f759412b9a54a12d7fc to your computer and use it in GitHub Desktop.
Save alestrunda/49b506118ff52f759412b9a54a12d7fc to your computer and use it in GitHub Desktop.
Get commits from all your GitHub repos since particular date
/*
Description: gets commits from all your GitHub repos since particular date - public and private repos (you will need access token for that)
Requirements:
- set your GitHub username
- get your GitHub access token
- make sure axios package is installed
Example:
//commits that are not older than 1 month (github api returns this activity from up to 90 days ago)
const date_since = new Date();
date_since.setMonth(date_since.getMonth() - 1);
await getCommitsSince(date_since)
*/
const axios = require("axios");
const USERNAME = "username";
const TOKEN = "your_github_access_token";
const MAX_PAGE = 10;
const getCommits = (page) =>
axios
.get(
`https://api.github.com/users/${USERNAME}/events?page=${page}`,
{
headers: {
Authorization: `token ${TOKEN}`,
},
}
)
.then((res) => res.data);
const filterCommitsSince = (commits, date) =>
commits.filter((commit) => date <= new Date(commit.created_at));
const getCommitsSince = async (date) => {
let commits = [];
for (let page = 1; page <= MAX_PAGE; page++) {
const current_commits = await getCommits(page);
if (!current_commits.length) break;
const commits_since = filterCommitsSince(current_commits, date);
if (!commits_since.length) break;
commits = commits.concat(commits_since);
}
return commits;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment