-
Notifications
You must be signed in to change notification settings - Fork 31
/
do.rq
executable file
·228 lines (194 loc) · 4.4 KB
/
do.rq
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env rq
# rq: query data.script.main
# rq: output /dev/null
# METADATA
# title: Regal build tasks
# description: |
# This is the `rq` build tasks used by the Regal project. Note that `rq` is possibly not
# intended as a build tool, but if we can lint Rego with Rego, surely we can build Regal
# with Rego too?
#
# To run one or more tasks, make sure to first have `rq` installed
# (https://git.sr.ht/~charles/rq), then from the project root directory, run:
#
# build/do.rq <tasks>
#
# For example, to run the `test` task, run:
#
# build/do.rq test
#
# authors:
# - https://github.com/StyraInc/regal/graphs/contributors
# related_resources:
# - description: rq
# ref: https://git.sr.ht/~charles/rq
package script
import future.keywords
main contains do[what] if some what in rq.args()
main contains job[what] if some what in rq.args()
main contains job.tasks if {
count(rq.args()) == 0
print("No task(s) provided. Available tasks:")
print()
}
main contains null if {
count(rq.args()) == 1
rq.args()[0] == "--help"
print(rego.metadata.chain()[1].annotations.description)
}
# METADATA
# title: pull_request
# description: Run all task to verify a pull request
do.pull_request {
some x in ["test", "lint", "e2e", "check_readme"]
github("::group::", x)
job[x]
github("::endgroup::", x)
}
# METADATA
# title: tasks
# description: Prints the name of all available tasks
job.tasks {
build(false)
some task in tasks
print("-", sprintf("%-20s", [task[0]]), "\t", strings.replace_n({"\n": ""}, task[1]))
}
# METADATA
# title: pr
# description: |
# Run all recommended tasks before submitting a PR
# related_resources:
# - https://github.com/daixiang0/gci
# - https://github.com/mvdan/gofumpt
# - https://github.com/golangci/golangci-lint
# - https://github.com/open-policy-agent/opa
job.pr {
run("go mod tidy")
build(true)
# format
fmt_all
write_readme
# verify
golangcilint
lint
test
e2e
}
# METADATA
# title: test
# description: Run all Regal unit tests (Go and Rego)
job.test {
test
}
# METADATA
# title: lint
# description: Run `regal lint` on the Regal bundle
job.lint {
build(true)
lint_ci
}
# METADATA
# title: e2e
# description: Run the Regal end-to-end tests
job.e2e {
build(true)
e2e
}
# METADATA
# title: check_readme
# description: Verify that the rules table in the README is up-to-date
job.check_readme {
build(true)
check_readme
}
build(true) {
run("go build")
}
build(false) {
not binary_present
run("go build")
} else := true
# any binary is good enough when calling `build(false)`, it doesn't need to be
# built freshly
binary_present {
some f in rq.tree(".", {"maxdepth": 1})
f.base == "regal"
f.is_dir == false
}
test {
run("go test ./...")
run("go run main.go test bundle")
}
e2e {
run("go test -tags e2e ./e2e")
}
lint {
run("opa check --strict --capabilities build/capabilities.json bundle")
run("./regal lint --format pretty bundle")
run("markdownlint --config docs/.markdownlint.yaml --ignore docs/CODE_OF_CONDUCT.md README.md docs/")
}
lint_ci {
run("opa check --strict --capabilities build/capabilities.json bundle")
run_quiet("./regal lint --format github bundle")
run("markdownlint --config docs/.markdownlint.yaml --ignore docs/CODE_OF_CONDUCT.md README.md docs/")
}
check_readme {
run("./regal table --compare-to-readme bundle")
}
write_readme {
run("./regal table --write-to-readme bundle")
}
fmt_all {
gci
gofumpt
opafmt
}
gci {
run(concat(" ", [
"gci write",
"-s standard",
"-s default",
"-s prefix(github.com/open-policy-agent/opa)",
"-s prefix(github.com/styrainc/regal)",
"-s blank",
"-s dot",
".",
]))
}
gofumpt {
run("gofumpt -w .")
}
opafmt {
run("opa fmt --write bundle")
}
golangcilint {
run("golangci-lint run ./...")
}
tasks := sort([[annotation.title, annotation.description] |
# Right, like you never used the AST for reflection?
some annotation in json.unmarshal(rq.run(["./regal", "parse", rq.scriptpath()], {}).stdout).annotations
annotation.scope == "rule"
])
run(cmd) {
print(cmd)
args := split(cmd, " ")
out := rq.run(args, {})
{ rq.error(sprintf("\nstdout: %s\nstderr: %s", [out.stdout, out.stderr])) | out.exitcode != 0 }
print(out.stdout)
}
run_quiet(cmd) {
print(cmd)
args := split(cmd, " ")
out := rq.run(args, {})
print(out.stdout)
{true |
out.exitcode != 0
print(out.stderr)
rq.error("")
}
}
github(what, j) {
is_github
print(what, j)
} else := true
is_github if rq.env().GITHUB_ACTION