Skip to content

Commit b6a62cd

Browse files
committed
Add rule to check for high number of new dependencies
1 parent 1e61ca0 commit b6a62cd

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

data-sources/trusty.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: v1
2+
type: data-source
3+
name: insights
4+
context: {}
5+
rest:
6+
def:
7+
dependencies:
8+
endpoint: https://api.insight.stacklok.com/v2/dependencies?package_name={package}&package_type={ecosystem}
9+
parse: json
10+
input_schema:
11+
type: object
12+
properties:
13+
package:
14+
type: string
15+
ecosystem:
16+
type: string
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
version: v1
2+
release_phase: alpha
3+
type: rule-type
4+
name: pr_too_many_deps
5+
display_name: Warn on too many project dependencies
6+
short_failure_message: A PR has added too many dependencies
7+
severity:
8+
value: low
9+
context:
10+
provider: github
11+
description: |
12+
Warns if a single PR attempts to introduce too many transitive dependencies to a project.
13+
guidance: |
14+
This rule warns reviewers if a PR would introduce too many transitive dependencies
15+
into a project. The threshold is configurable, though not on a percentage basis.
16+
17+
Additional dependencies can increase the security surface area of a project,
18+
increase the size of software deployments and artifacts, and introduce additional
19+
maintenance work or security vulnerabilities in managing the dependencies.
20+
def:
21+
in_entity: pull_request
22+
rule_schema:
23+
type: object
24+
properties:
25+
max_deps:
26+
type: integer
27+
description: "The maximum number of dependencies that can be added in a single PR."
28+
default: 50
29+
ingest:
30+
type: deps
31+
pr:
32+
filter: new
33+
eval:
34+
type: rego
35+
data_sources:
36+
- name: insights
37+
rego:
38+
type: deny-by-default
39+
def: |
40+
package minder
41+
42+
import rego.v1
43+
44+
default allow := false
45+
46+
added := {node.name: ecosystem |
47+
node := input.ingested.node_list.nodes[_]
48+
ecosystem := get_ecosystem(node.properties)
49+
}
50+
51+
get_ecosystem(properties) := eco if {
52+
count(properties) >= 1
53+
prop := properties[_]
54+
55+
prop.name == "sourceFile"
56+
eco := get_ecosystem_from_file(prop.data)
57+
}
58+
59+
get_ecosystem_from_file(file) = "pypi" if {
60+
file == "requirements.txt"
61+
}
62+
63+
get_ecosystem_from_file(file) = "npm" if {
64+
file == "package.json"
65+
}
66+
67+
get_ecosystem_from_file(file) = "go" if {
68+
file == "go.mod"
69+
}
70+
71+
get_ecosystem_from_file(file) = "crates" if {
72+
file == "Cargo.toml"
73+
}
74+
75+
get_ecosystem_from_file(file) = "maven" if {
76+
file == "pom.xml"
77+
}
78+
79+
transitive contains pkg if {
80+
added[pkg]
81+
}
82+
83+
transitive contains pkg if {
84+
ecosystem := added[name]
85+
86+
lookup := minder.datasource.insights.dependencies({
87+
"package": name,
88+
"ecosystem": ecosystem
89+
})
90+
pkg := lookup.body.dependencies[_].name
91+
}
92+
93+
allow := false # (count(transitive) <= input.profile.max_deps)
94+
new_deps := concat("\n- ", transitive)
95+
message := sprintf("This PR introduces %d new transitive dependencies (limit of %d):\n\n- %s\n\n",
96+
[count(transitive), input.profile.max_deps, new_deps])
97+
alert:
98+
type: pull_request_comment
99+
pull_request_comment:
100+
review_message: |
101+
This pull request introduces too many dependencies. Please consider finding libraries with fewer dependencies.
102+
103+
{{.EvalErrorDetails}}

0 commit comments

Comments
 (0)