audit-labs/audit-tools

A collection of scripts, queries, and other goodies you can use in an audit.

clone: git clone https://gitbay.org/audit-labs/audit-tools.git

main: applications/github/collectors/commits.py · raw

 1"""
 2Collect commit history for all repos in an org.
 3"""
 4
 5from .api import paginate
 6
 7
 8def commits(org, cfg, branch="main"):
 9    """
10    Return commits across all repos. Each row includes repo, branch, sha,
11    author, date, message (first line), and change counts.
12
13    Skips repos where the branch doesn't exist.
14    """
15    repos = paginate(f"https://api.github.com/orgs/{org}/repos", cfg)
16    rows = []
17
18    for repo in repos:
19        repo_name = repo["name"]
20        try:
21            repo_commits = paginate(
22                f"https://api.github.com/repos/{org}/{repo_name}/commits",
23                cfg,
24                {"sha": branch},
25            )
26        except Exception:
27            # Branch doesn't exist in this repo or other API error -- skip
28            continue
29
30        for c in repo_commits:
31            commit = c.get("commit", {})
32            author = commit.get("author", {})
33            stats = c.get("stats", {})
34            rows.append(
35                {
36                    "repo": repo_name,
37                    "branch": branch,
38                    "sha": c.get("sha", "")[:12],
39                    "author_name": author.get("name", ""),
40                    "author_email": author.get("email", ""),
41                    "date": author.get("date", ""),
42                    "message": commit.get("message", "").splitlines()[0],
43                    "additions": stats.get("additions", ""),
44                    "deletions": stats.get("deletions", ""),
45                }
46            )
47
48    return rows