-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add findEmails utility function
- Loading branch information
1 parent
941d684
commit 85b7172
Showing
11 changed files
with
1,092 additions
and
68 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
/dist |
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 |
---|---|---|
|
@@ -12,3 +12,6 @@ node_modules | |
|
||
# ESLint | ||
.eslintcache | ||
|
||
# Build output | ||
/dist |
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,2 +1,3 @@ | ||
/dist | ||
.eslintcache | ||
pnpm-lock.yaml |
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,22 @@ | ||
#!/usr/bin/env node | ||
import { program } from "@commander-js/extra-typings"; | ||
import { findEmails } from "../lib/index.ts"; | ||
|
||
program | ||
.name("find-gh-commit-emails") | ||
.description( | ||
"Search for emails used by a given GitHub user. " + | ||
"Setting the GITHUB_TOKEN environment variable to a valid GitHub personal " + | ||
"access token will enable searching in non-public repositories.", | ||
) | ||
.argument("username", "GitHub username to find emails for") | ||
.option("--include-committer", "include committer email in results", false) | ||
.configureHelp({ helpWidth: 80 }) | ||
.showHelpAfterError() | ||
.action(async (username, opts) => { | ||
const token = process.env["GITHUB_TOKEN"]; | ||
const emailsMap = await findEmails(username, token, opts); | ||
console.log(JSON.stringify(emailsMap, null, 2)); | ||
}); | ||
|
||
program.parse(process.argv); |
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,51 @@ | ||
import { Octokit } from "octokit"; | ||
|
||
interface Options { | ||
readonly includeCommitter?: boolean; | ||
} | ||
|
||
/** | ||
* Find emails for a GitHub user | ||
* @param username a GitHub username | ||
* @param token a GitHub token | ||
* @param opts additional options | ||
*/ | ||
export async function findEmails( | ||
username: string, | ||
token?: string, | ||
opts?: Options, | ||
) { | ||
const github = new Octokit({ auth: token }); | ||
|
||
const commitData = await github.paginate("GET /search/commits", { | ||
q: `author:${username}`, | ||
}); | ||
|
||
const emailMap = new Map<string, Set<string>>(); | ||
|
||
for (const entry of commitData) { | ||
const repo = entry.repository.full_name; | ||
const emails = [entry.commit.author.email]; | ||
|
||
if (opts?.includeCommitter && entry.commit.committer?.email) { | ||
emails.push(entry.commit.committer.email); | ||
} | ||
|
||
for (const email of emails) { | ||
let repos = emailMap.get(email); | ||
if (!repos) { | ||
repos = new Set(); | ||
emailMap.set(email, repos); | ||
} | ||
repos.add(repo); | ||
} | ||
} | ||
|
||
const object: { [key: string]: string[] } = {}; | ||
for (const [email, repos] of emailMap) { | ||
Object.assign(object, { | ||
[email]: Array.from(repos), | ||
}); | ||
} | ||
return object; | ||
} |
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
Oops, something went wrong.