Skip to content

Commit d37ddb1

Browse files
committed
Adding query for repository audit report
1 parent 9b67875 commit d37ddb1

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

sql/audit/repos-audit.sql

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* This pulls a list of all repositories in GHES with details on
3+
* commit count, PR count, Issue count, Disk usage, Repo admins, Org owners, LFS usage, etc.
4+
* Make sure to remove the limit for returning all results.
5+
*/
6+
7+
SELECT
8+
repo.id as "Repo Id",
9+
repo.owner_login as "Org Name",
10+
repo.name as "Repository",
11+
IFNULL(repo.active,0) as "is active",
12+
IFNULL(commits.commit_count,0) as "Commit Count",
13+
IFNULL(pr.count,0) as "PR Count",
14+
IFNULL(prr.count,0) as "PR Review Count",
15+
IFNULL(issue.count,0) as "Issue Count",
16+
IFNULL(pb.branch_count,0) as "P Branch Count",
17+
IFNULL(pb.branch_names,'') as "P Branch Names",
18+
repo.public as "is public",
19+
IFNULL(internal.internal,0) as "is internal",
20+
repo.public_fork_count as "Fork Child Count",
21+
IFNULL(repo2.is_fork,0) as "is Fork",
22+
IFNULL(CONCAT(repo2.owner_login,"/",repo2.name),'') as "Fork Parent",
23+
CAST(repo.disk_usage / 1024 AS DECIMAL (10,3)) as "Disk Usage (MB)",
24+
CAST(IFNULL(lfs_repo.lfs_size,0) / 1024 / 1024 / 1024 AS DECIMAL (10,2)) as "LFS Usage (GB)",
25+
IFNULL(lfs_repo.last_lfs_push,'') as "Last LFS Push",
26+
IFNULL(language.name,"none") as "Language",
27+
IFNULL(releases.count,0) as "Release Count",
28+
CAST(IFNULL(release_size.release_asset_disk_size,0) / 1024 / 1024 / 1024 AS DECIMAL (10,2)) as "Releases Usage (GB)",
29+
IFNULL(projects.count,0) as "Projects Count",
30+
IFNULL(hooks.count,0) as "Hooks Count",
31+
IFNULL(admins.login,'') as "Repo Admins",
32+
IFNULL(team_admin.team_admins,'') as "Team Admins",
33+
IFNULL(org_admin.org_owners,'') as "Org Admins",
34+
repo.locked as "is Locked",
35+
repo.created_at as "Created at",
36+
repo.updated_at as "Updated at",
37+
repo.pushed_at as "Last Code Push",
38+
owner.type as "Org Type",
39+
repo.owner_id as "User/Owner Id",
40+
owner.created_at as "User/Owner Created",
41+
owner.updated_at as "User/Owner Updated",
42+
IFNULL(owner.suspended_at,'') as "User/Owner Suspended"
43+
FROM repositories repo
44+
LEFT JOIN users owner
45+
ON owner.id = repo.owner_id
46+
LEFT JOIN language_names language
47+
ON repo.primary_language_name_id = language.id
48+
LEFT JOIN (SELECT COUNT(id) as count, repository_id FROM pull_requests GROUP BY repository_id) pr on pr.repository_id = repo.id
49+
LEFT JOIN (SELECT COUNT(id) as count, repository_id FROM pull_request_reviews GROUP BY repository_id) prr on prr.repository_id = repo.id
50+
LEFT JOIN (SELECT COUNT(id) as count, repository_id FROM issues WHERE has_pull_request = 0 GROUP BY repository_id) issue on issue.repository_id = repo.id
51+
LEFT JOIN (SELECT 1 as "internal", repository_id FROM internal_repositories) internal on internal.repository_id = repo.id
52+
LEFT JOIN (SELECT SUM(commit_count) as "commit_count", repository_id FROM commit_contributions GROUP BY repository_id) commits on commits.repository_id = repo.id
53+
LEFT JOIN (SELECT COUNT(id) as branch_count, repository_id, GROUP_CONCAT(name SEPARATOR ';') as branch_names FROM protected_branches GROUP BY repository_id) pb on pb.repository_id = repo.id
54+
LEFT JOIN (SELECT 1 as is_fork, id, name, parent_id, owner_login FROM repositories) repo2 on repo2.id = repo.parent_id
55+
LEFT JOIN (SELECT COUNT(id) as count, repository_id FROM releases GROUP BY repository_id) releases on releases.repository_id = repo.id
56+
LEFT JOIN (SELECT count(id) as count, owner_id FROM projects WHERE owner_type = "Repository" GROUP BY owner_id) projects on projects.owner_id = repo.id
57+
LEFT JOIN (SELECT count(id) as count, installation_target_id FROM hooks WHERE installation_target_type = "Repository" GROUP BY installation_target_id) hooks on hooks.installation_target_id = repo.id
58+
LEFT JOIN (SELECT a.subject_id, GROUP_CONCAT(uu.login SEPARATOR ';') as login FROM abilities a
59+
LEFT JOIN (SELECT u.id, u.login FROM users u) uu ON uu.id = a.actor_id
60+
WHERE a.subject_type = "Repository" AND a.actor_type = "User" AND a.action = 2
61+
GROUP BY a.subject_id) admins on admins.subject_id = repo.id
62+
LEFT JOIN (SELECT a.subject_id as sub_repo_id, GROUP_CONCAT(members.team_admins) as team_admins FROM abilities a
63+
LEFT JOIN (
64+
SELECT team.subject_id, GROUP_CONCAT(uu.login SEPARATOR ';') as team_admins
65+
FROM abilities team
66+
LEFT JOIN (
67+
SELECT id, login
68+
FROM users
69+
) uu ON uu.id = team.actor_id
70+
WHERE team.subject_type = "Team" AND team.actor_type = "User"
71+
GROUP BY team.subject_id
72+
) members ON members.subject_id = a.actor_id
73+
WHERE a.subject_type = "Repository" AND a.actor_type = "Team" AND a.action = 2
74+
GROUP BY a.subject_id
75+
) team_admin on team_admin.sub_repo_id = repo.id
76+
LEFT JOIN (SELECT a.subject_id, GROUP_CONCAT(uu.login SEPARATOR ';') as org_owners FROM abilities a
77+
LEFT JOIN (SELECT id, login FROM users) uu ON uu.id = a.actor_id
78+
WHERE a.subject_type = "Organization" AND a.action = 2
79+
GROUP BY a.subject_id
80+
) org_admin ON org_admin.subject_id = repo.owner_id
81+
LEFT JOIN (SELECT originating_repository_id, SUM(size) as lfs_size, MAX(created_at) as last_lfs_push FROM media_blobs
82+
GROUP BY originating_repository_id) as lfs_repo on lfs_repo.originating_repository_id = repo.id
83+
LEFT JOIN (SELECT repository_id, SUM(size) as release_asset_disk_size
84+
FROM release_assets
85+
GROUP BY repository_id) release_size on release_size.repository_id = repo.id
86+
ORDER BY repo.owner_login, repo.name
87+
LIMIT 100

0 commit comments

Comments
 (0)