-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcategory.go
More file actions
118 lines (99 loc) · 4.64 KB
/
category.go
File metadata and controls
118 lines (99 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package github_primary_ratelimit
import (
"net/http"
"strings"
)
// General references (note there are some inconsistencies between them):
// https://docs.github.com/en/rest/rate-limit/rate-limit#about-rate-limits
// https://docs.github.com/en/rest/rate-limit/rate-limit#get-rate-limit-status-for-the-authenticated-user
type ResourceCategory string
const (
// The default category
// used for all HTTP method/url with no other match.
ResourceCategoryCore ResourceCategory = "core"
// https://docs.github.com/en/rest/search/search#about-search
// * /search (except for /search/code)
ResourceCategorySearch ResourceCategory = "search"
// https://docs.github.com/en/rest/search/search#search-code
// * /search/code
ResourceCategoryCodeSearch ResourceCategory = "code_search"
// https://docs.github.com/en/graphql
// * /graphql
ResourceCategoryGraphQL ResourceCategory = "graphql"
// https://docs.github.com/en/rest/migrations/source-imports#start-an-import
// deprecated endpoint; still applicable
// * /repos/{OWNER}/{REPO}/import
ResourceCategorySourceImport ResourceCategory = "source_import"
// https://docs.github.com/en/enterprise-cloud@latest/rest/enterprise-admin/audit-log#get-the-audit-log-for-an-enterprise
// * /enterprises/{ENTERPRISE}/audit-log
ResourceCategoryAuditLog ResourceCategory = "audit_log"
// https://docs.github.com/en/rest/dependency-graph/dependency-submission
// POST /app/manfiests/{code}/conversions
ResourceCategoryIntegrationManifest ResourceCategory = "integration_manifest"
// https://docs.github.com/en/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
// POST /repos/{OWNER}/{REPO}/dependency-graph/snapshots
ResourceCategoryDependencySnapshots ResourceCategory = "dependency_snapshots"
// https://docs.github.com/en/rest/code-scanning/code-scanning#upload-an-analysis-as-sarif-data
// POST /repos/{OWNER}/{REPO}/code-scanning/sarifs
ResourceCategoryCodeScanningUpload ResourceCategory = "code_scanning_upload"
// https://docs.github.com/en/rest/actions/self-hosted-runners#about-self-hosted-runners-in-github-actions
// "... for registring self-hosted runners"; assuming only POST requests are counted
// POST /orgs/{ORG}/actions/runners
ResourceCategoryActionsRunnerRegistration ResourceCategory = "actions_runner_registration"
// https://docs.github.com/en/enterprise-cloud@latest/rest/scim/scim
// no explicit documentation; assuming only POST requests are counted
// POST /scim
ResourceCategoryScim ResourceCategory = "scim"
// https://docs.github.com/en/enterprise-cloud@latest/admin/monitoring-activity-in-your-enterprise/reviewing-audit-logs-for-your-enterprise/streaming-the-audit-log-for-your-enterprise
// no API endpoints
ResourceCategoryAuditLogStreaming ResourceCategory = "audit_log_streaming"
)
func GetAllCategories() []ResourceCategory {
return []ResourceCategory{
ResourceCategoryCore,
ResourceCategorySearch,
ResourceCategoryCodeSearch,
ResourceCategoryGraphQL,
ResourceCategorySourceImport,
ResourceCategoryAuditLog,
ResourceCategoryIntegrationManifest,
ResourceCategoryDependencySnapshots,
ResourceCategoryCodeScanningUpload,
ResourceCategoryActionsRunnerRegistration,
ResourceCategoryScim,
}
}
func parseRequestCategory(request *http.Request) ResourceCategory {
// RawPath is only populated when there is encoding in the path
return parseCategory(request.Method, request.URL.Path)
}
func parseCategory(method string, path string) ResourceCategory {
switch { // method-agnostic checks:
case strings.HasPrefix(path, "/search/code"):
return ResourceCategoryCodeSearch
case strings.HasPrefix(path, "/search"):
return ResourceCategorySearch
case strings.HasPrefix(path, "/graphql"):
return ResourceCategoryGraphQL
case strings.HasPrefix(path, "/repos/") && strings.HasSuffix(path, "/import"):
return ResourceCategorySourceImport
case strings.HasSuffix(path, "/audit_log"):
return ResourceCategoryAuditLog
}
if method == http.MethodPost {
switch {
case strings.HasPrefix(path, "/app/manfiests/") && strings.HasSuffix(path, "/conversions"):
return ResourceCategoryIntegrationManifest
case strings.HasPrefix(path, "/repos/") && strings.HasSuffix(path, "/dependency-graph/snapshots"):
return ResourceCategoryDependencySnapshots
case strings.HasPrefix(path, "/repos/") && strings.HasSuffix(path, "/code-scanning/sarifs"):
return ResourceCategoryCodeScanningUpload
case strings.HasPrefix(path, "/orgs/") && strings.HasSuffix(path, "/actions/runners"):
return ResourceCategoryActionsRunnerRegistration
case strings.HasPrefix(path, "/scim"):
return ResourceCategoryScim
}
}
// default to core
return ResourceCategoryCore
}