Skip to content

Commit f8b533f

Browse files
author
taeul
committed
Formatting the repos-audit sql query
1 parent a41cbc8 commit f8b533f

File tree

2 files changed

+160
-69
lines changed

2 files changed

+160
-69
lines changed

sql/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The `audit` folder has queries that are all around auditing credentials, webhook
1515
- `hooks-repos.sql` - A report of all repository webhooks used in the past week, who owns it, and where the webhook goes. This is limited to a week based on the length of time these are kept in the `hookshot_delivery_logs` table.
1616
- `hooks-users.sql` - Same report as above, but for user-owned webhooks.
1717
- `oauth-apps.sql` - A report of all OAuth apps, who owns it, where it goes, and when it was last used.
18+
- `repos-audit.sql` - A report of all repositories including the commit count, PR count, Disk size, last push, and more.
1819
- `user-emails.sql` - A report of all emails that don't match a list of approved domains you define in the `WHERE` clause. This query should be deprecated by [this issue](https://github.com/github/roadmap/issues/204).
1920
- `user-ssh-keys.sql` - A report of all user SSH keys, when it was last used, when it was set up, and how long the key is.
2021

sql/audit/repos-audit.sql

+159-69
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
/*
22
* This pulls a list of all repositories in GHES with details on
33
* 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.
4+
* Please include the LIMIT clause at the bottom if you are concern of the number of results.
55
*/
6-
7-
SELECT
8-
repo.id as "Repo Id",
6+
SELECT repo.id as "Repo Id",
97
repo.owner_login as "Org Name",
108
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",
9+
IFNULL(repo.active, 0) as "is active",
10+
IFNULL(commits.commit_count, 0) as "Commit Count",
11+
IFNULL(pr.count, 0) as "PR Count",
12+
IFNULL(prr.count, 0) as "PR Review Count",
13+
IFNULL(issue.count, 0) as "Issue Count",
14+
IFNULL(pb.branch_count, 0) as "Protected Branch Count",
15+
IFNULL(pb.branch_names, '') as "Protected Branch Names",
1816
repo.public as "is public",
19-
IFNULL(internal.internal,0) as "is internal",
17+
IFNULL(internal.internal, 0) as "is internal",
2018
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",
19+
IFNULL(repo2.is_fork, 0) as "is Fork",
20+
IFNULL(CONCAT(repo2.owner_login, "/", repo2.name), '') as "Fork Parent",
21+
CAST(repo.disk_usage / 1024 AS DECIMAL (10, 3)) as "Disk Usage (MB)",
22+
CAST(
23+
IFNULL(lfs_repo.lfs_size, 0) / 1024 / 1024 / 1024 AS DECIMAL (10, 2)
24+
) 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(
29+
IFNULL(release_size.release_asset_disk_size, 0) / 1024 / 1024 / 1024 AS DECIMAL (10, 2)
30+
) as "Releases Usage (GB)",
31+
IFNULL(projects.count, 0) as "Projects Count",
32+
IFNULL(hooks.count, 0) as "Hooks Count",
33+
IFNULL(admins.login, '') as "Repo Admins",
34+
IFNULL(team_admin.team_admins, '') as "Team Admins",
35+
IFNULL(org_admin.org_owners, '') as "Org Admins",
3436
repo.locked as "is Locked",
3537
repo.created_at as "Created at",
3638
repo.updated_at as "Updated at",
@@ -39,49 +41,137 @@ SELECT
3941
repo.owner_id as "User/Owner Id",
4042
owner.created_at as "User/Owner Created",
4143
owner.updated_at as "User/Owner Updated",
42-
IFNULL(owner.suspended_at,'') as "User/Owner Suspended"
44+
IFNULL(owner.suspended_at, '') as "User/Owner Suspended"
4345
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
46+
LEFT JOIN users owner ON owner.id = repo.owner_id
47+
LEFT JOIN language_names language ON repo.primary_language_name_id = language.id
48+
LEFT JOIN (
49+
SELECT COUNT(id) as count,
50+
repository_id
51+
FROM pull_requests
52+
GROUP BY repository_id
53+
) pr on pr.repository_id = repo.id
54+
LEFT JOIN (
55+
SELECT COUNT(id) as count,
56+
repository_id
57+
FROM pull_request_reviews
58+
GROUP BY repository_id
59+
) prr on prr.repository_id = repo.id
60+
LEFT JOIN (
61+
SELECT COUNT(id) as count,
62+
repository_id
63+
FROM issues
64+
WHERE has_pull_request = 0
65+
GROUP BY repository_id
66+
) issue on issue.repository_id = repo.id
67+
LEFT JOIN (
68+
SELECT 1 as "internal",
69+
repository_id
70+
FROM internal_repositories
71+
) internal on internal.repository_id = repo.id
72+
LEFT JOIN (
73+
SELECT SUM(commit_count) as "commit_count",
74+
repository_id
75+
FROM commit_contributions
76+
GROUP BY repository_id
77+
) commits on commits.repository_id = repo.id
78+
LEFT JOIN (
79+
SELECT COUNT(id) as branch_count,
80+
repository_id,
81+
GROUP_CONCAT(name SEPARATOR ';') as branch_names
82+
FROM protected_branches
83+
GROUP BY repository_id
84+
) pb on pb.repository_id = repo.id
85+
LEFT JOIN (
86+
SELECT 1 as is_fork,
87+
id,
88+
name,
89+
parent_id,
90+
owner_login
91+
FROM repositories
92+
) repo2 on repo2.id = repo.parent_id
93+
LEFT JOIN (
94+
SELECT COUNT(id) as count,
95+
repository_id
96+
FROM releases
97+
GROUP BY repository_id
98+
) releases on releases.repository_id = repo.id
99+
LEFT JOIN (
100+
SELECT count(id) as count,
101+
owner_id
102+
FROM projects
103+
WHERE owner_type = "Repository"
104+
GROUP BY owner_id
105+
) projects on projects.owner_id = repo.id
106+
LEFT JOIN (
107+
SELECT count(id) as count,
108+
installation_target_id
109+
FROM hooks
110+
WHERE installation_target_type = "Repository"
111+
GROUP BY installation_target_id
112+
) hooks on hooks.installation_target_id = repo.id
113+
LEFT JOIN (
114+
SELECT a.subject_id,
115+
GROUP_CONCAT(uu.login SEPARATOR ';') as login
116+
FROM abilities a
63117
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
118+
SELECT u.id,
119+
u.login
120+
FROM users u
121+
) uu ON uu.id = a.actor_id
122+
WHERE a.subject_type = "Repository"
123+
AND a.actor_type = "User"
124+
AND a.action = 2
125+
GROUP BY a.subject_id
126+
) admins on admins.subject_id = repo.id
127+
LEFT JOIN (
128+
SELECT a.subject_id as sub_repo_id,
129+
GROUP_CONCAT(members.team_admins) as team_admins
130+
FROM abilities a
131+
LEFT JOIN (
132+
SELECT team.subject_id,
133+
GROUP_CONCAT(uu.login SEPARATOR ';') as team_admins
134+
FROM abilities team
135+
LEFT JOIN (
136+
SELECT id,
137+
login
138+
FROM users
139+
) uu ON uu.id = team.actor_id
140+
WHERE team.subject_type = "Team"
141+
AND team.actor_type = "User"
142+
GROUP BY team.subject_id
143+
) members ON members.subject_id = a.actor_id
144+
WHERE a.subject_type = "Repository"
145+
AND a.actor_type = "Team"
146+
AND a.action = 2
147+
GROUP BY a.subject_id
148+
) team_admin on team_admin.sub_repo_id = repo.id
149+
LEFT JOIN (
150+
SELECT a.subject_id,
151+
GROUP_CONCAT(uu.login SEPARATOR ';') as org_owners
152+
FROM abilities a
153+
LEFT JOIN (
154+
SELECT id,
155+
login
156+
FROM users
157+
) uu ON uu.id = a.actor_id
158+
WHERE a.subject_type = "Organization"
159+
AND a.action = 2
160+
GROUP BY a.subject_id
161+
) org_admin ON org_admin.subject_id = repo.owner_id
162+
LEFT JOIN (
163+
SELECT originating_repository_id,
164+
SUM(size) as lfs_size,
165+
MAX(created_at) as last_lfs_push
166+
FROM media_blobs
167+
GROUP BY originating_repository_id
168+
) as lfs_repo on lfs_repo.originating_repository_id = repo.id
169+
LEFT JOIN (
170+
SELECT repository_id,
171+
SUM(size) as release_asset_disk_size
172+
FROM release_assets
173+
GROUP BY repository_id
174+
) release_size on release_size.repository_id = repo.id
175+
ORDER BY repo.owner_login,
176+
repo.name
177+
-- LIMIT 100

0 commit comments

Comments
 (0)