-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflows_spec.rb
More file actions
253 lines (232 loc) 路 13.5 KB
/
Copy pathworkflows_spec.rb
File metadata and controls
253 lines (232 loc) 路 13.5 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# frozen_string_literal: true
require "yaml"
RSpec.describe "workflow commands" do
it "regenerates and commits only per-record advisories" do
path = File.expand_path("../.github/workflows/regenerate.yml", __dir__)
workflow = YAML.safe_load_file(path, aliases: true)
steps = workflow.fetch("jobs").values.flat_map { |job| job.fetch("steps", []) }
commit_step = steps.find { |step| step["name"] == "Commit advisories" }
expect({
concat_step: steps.any? { |step| step["name"] == "Concatenate advisories" },
staged_paths: commit_step.fetch("run").lines.grep(/git add/).map(&:strip),
}).to eq({
concat_step: false,
staged_paths: ["git add advisories/"],
})
end
it "authors automation commits as BrewTestBot with signing and a machine token" do
%w[regenerate ingest].each do |name|
path = File.expand_path("../.github/workflows/#{name}.yml", __dir__)
workflow = YAML.safe_load_file(path, aliases: true)
steps = workflow.fetch("jobs").values.flat_map { |job| job.fetch("steps", []) }
config = steps.find { |step| step["name"] == "Configure git" }
signing = steps.find { |step| step["name"] == "Set up commit signing" }
expect(config.fetch("with")).to include("username" => "BrewTestBot")
expect(signing.fetch("uses")).to match(
%r{\AHomebrew/actions/setup-commit-signing@[0-9a-f]{40}\z},
)
expect(signing.fetch("with")).to include(
"signing_key" => "${{ secrets.BREWTESTBOT_SSH_SIGNING_KEY }}",
)
end
machine_token = "${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN || github.token }}"
regenerate = YAML.safe_load_file(File.expand_path("../.github/workflows/regenerate.yml", __dir__),
aliases: true)
steps = regenerate.dig("jobs", "regenerate", "steps")
push = steps.find { |step| step["name"] == "Push commits" }
open_pr = steps.find { |step| step["name"] == "Open or update pull request" }
expect(push.fetch("with")).to include("token" => machine_token)
expect(open_pr.dig("env", "GITHUB_TOKEN")).to eq machine_token
end
it "summarizes regeneration changes and refreshes the standing pull request body" do
path = File.expand_path("../.github/workflows/regenerate.yml", __dir__)
workflow = YAML.safe_load_file(path, aliases: true)
steps = workflow.dig("jobs", "regenerate", "steps")
commit = steps.find { |step| step["name"] == "Commit advisories" }.fetch("run")
open_pr = steps.find { |step| step["name"] == "Open or update pull request" }
publish = open_pr.fetch("run")
expect(commit).to include("bundle exec rake advisories:summary")
expect(open_pr.dig("env", "BODY")).to eq "${{ runner.temp }}/regenerate-pr-body.md"
expect(publish).to include('gh pr edit "${open_pr_number}" --body-file "${BODY}"')
expect(publish).to include('--body-file "${BODY}"')
expect(publish).not_to include("--body ")
end
it "builds the Repology index in Ingest and reads it via --repology" do
regenerate = YAML.safe_load_file(File.expand_path("../.github/workflows/regenerate.yml", __dir__),
aliases: true)
ingest = YAML.safe_load_file(File.expand_path("../.github/workflows/ingest.yml", __dir__), aliases: true)
steps = ingest.dig("jobs", "match", "steps")
setup_homebrew = steps.find { |step| step["name"] == "Set up Homebrew" }
build_index = steps.index { |step| step["name"] == "Build Repology index" }
match_index = steps.index { |step| step["name"] == "Match advisories" }
build = steps.fetch(build_index).fetch("run")
match = steps.fetch(match_index).fetch("run")
expect(regenerate.dig("jobs", "regenerate", "steps")
.none? { |step| step["name"] == "Build Repology index" }).to be true
expect(build_index).to be < match_index
expect(build).to include("bundle exec rake repology:build")
expect(build).to include("::warning::")
expect(setup_homebrew.dig("with", "stable")).to be false
expect(match).to include("--output advisories")
expect(match).to include("--new-history")
expect(match).not_to include("--no-history")
expect(match).to include("--repology data/repology.json")
expect(match).to include("--overrides data/overrides.yml")
end
it "filters matched candidates between matching and snapshotting" do
path = File.expand_path("../.github/workflows/ingest.yml", __dir__)
workflow = YAML.safe_load_file(path, aliases: true)
steps = workflow.dig("jobs", "match", "steps")
match_index = steps.index { |step| step["name"] == "Match advisories" }
filter_index = steps.index { |step| step["name"] == "Drop uncomparable and rejected candidates" }
snapshot_index = steps.index { |step| step["name"] == "Snapshot matched advisories" }
filter = steps.fetch(filter_index).fetch("run")
expect(match_index).to be < filter_index
expect(filter_index).to be < snapshot_index
expect(filter).to include("git ls-files --others --exclude-standard -z -- advisories/")
expect(filter).to include("bundle exec rake advisories:filter")
end
it "publishes matched advisories in stable formula shards" do
path = File.expand_path("../.github/workflows/ingest.yml", __dir__)
workflow = YAML.safe_load_file(path, aliases: true)
steps = workflow.dig("jobs", "match", "steps")
snapshot = steps.find { |step| step["name"] == "Snapshot matched advisories" }.fetch("run")
partition_index = steps.index { |step| step["name"] == "Partition matched advisories" }
partition = steps.fetch(partition_index).fetch("run")
credentials_index = steps.index { |step| step["name"] == "Configure push credentials" }
publish_index = steps.index { |step| step["name"] == "Commit, push and open sharded pull requests" }
credentials = steps.fetch(credentials_index)
publish = steps.fetch(publish_index).fetch("run")
expect(snapshot).to include('git switch --detach "${GITHUB_SHA}"')
expect(snapshot).to include('echo "diff_base=${GITHUB_SHA}"')
expect(snapshot).not_to include('echo "base=${GITHUB_SHA}"')
expect(steps.fetch(partition_index).dig("env", "DIFF_BASE_SHA")).to eq(
"${{ steps.snapshot.outputs.diff_base }}",
)
expect(steps.fetch(partition_index).fetch("env")).not_to have_key("BASE_SHA")
expect(partition).to include('"${DIFF_BASE_SHA}" "${SNAPSHOT_SHA}"')
expect(partition).to include("bundle exec rake advisories:shard")
expect(partition).to include("--no-renames")
expect(partition).to include("--diff-filter=AM")
expect(partition).to include('cp "${changed}" "${SHARD_OUTPUT}/all"')
expect(partition).to include("count <= SINGLE_PR_LIMIT")
expect(steps.fetch(partition_index).dig("env", "SINGLE_PR_LIMIT")).to eq "500"
expect(credentials_index).to be < publish_index
expect(credentials.fetch("if")).to eq "steps.snapshot.outputs.created == 'true'"
expect(credentials.fetch("uses")).to match(
%r{\AHomebrew/actions/git-try-push@[0-9a-f]{40}\z},
)
expect(credentials.fetch("with")).to include(
"token" => "${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN || github.token }}",
"tries" => 0,
)
expect(publish).not_to include("gh auth setup-git")
expect(publish).to include('manifests=("${SHARD_OUTPUT}"/*)')
fetch_main = "git fetch --no-tags origin +refs/heads/main:refs/remotes/origin/main"
merge_snapshot =
'git merge-tree --write-tree --no-messages --merge-base="${DIFF_BASE_SHA}"'
switch_to_publish_base =
'git switch --discard-changes --force-create "${branch}" "${PUBLISH_BASE_SHA}"'
expect(publish).to include(fetch_main)
expect(publish).to include('PUBLISH_BASE_SHA="$(git rev-parse origin/main)"')
expect(publish).to include(merge_snapshot)
expect(publish).to include(')" || merge_status=$?')
expect(publish).to include("if (( merge_status == 1 ))")
expect(publish).to include("Matched advisory changes conflict with main")
expect(publish).to include("git merge-tree failed with exit status ${merge_status}")
expect(publish).to include('echo "${PUBLISH_SNAPSHOT_TREE}"')
expect(publish).to include('exit "${merge_status}"')
expect(publish).to include(switch_to_publish_base)
expect(publish.index(fetch_main)).to be < publish.index(merge_snapshot)
expect(publish.index(merge_snapshot)).to be < publish.index(switch_to_publish_base)
expect(steps.fetch(publish_index).dig("env", "DIFF_BASE_SHA")).to eq(
"${{ steps.snapshot.outputs.diff_base }}",
)
expect(steps.fetch(publish_index).fetch("env")).not_to have_key("BASE_SHA")
expect(publish).to include("git clean --force -d -- advisories/")
expect(publish).to include(
'git restore --source="${PUBLISH_SNAPSHOT_TREE}" --pathspec-from-file="${manifest}"',
)
expect(publish).to include("--pathspec-file-nul")
expect(publish).not_to include("bundle exec rake advisories:concat")
expect(publish).to include("git add advisories/")
expect(publish).not_to include("git add advisories/ data/")
empty_guard = "if git diff --cached --quiet; then"
expect(publish).to include(empty_guard)
expect(publish).to include('echo "Shard ${shard} is already on main; nothing to publish."')
expect(publish.index("git add advisories/")).to be < publish.index(empty_guard)
expect(publish.index(empty_guard)).to be < publish.index('git commit -m "${title}"')
expect(publish).to include('branch="matched-advisories"')
expect(publish).to include('branch="matched-advisories-${shard}"')
expect(publish).to include('git push --force origin "HEAD:refs/heads/${branch}"')
expect(publish).to include("Base: ${PUBLISH_BASE_SHA}")
expect(publish).to include("No advisory shards were produced")
expect(publish).to include("failed=0")
expect(publish).to include('if publish_shard "${manifest}"')
expect(publish).to include("Superseded")
expect(publish).to include("--delete-branch")
expect(publish).to include("matched-advisories(-[0-9a-f]{2})?$")
expect(publish).to include('exit "${failed}"')
expect(publish).to include("including those already on main")
# Reconciliation only runs after full publication, enumerates open PRs
# with a checked paginated command, and propagates close failures.
expect(publish.index("if (( failed != 0 ))")).to be < publish.index("Superseded")
expect(publish).to include("gh api --paginate")
expect(publish).to include('> "${open_prs}"')
expect(publish).not_to include("gh pr list --state open --limit")
expect(publish).to include("Failed to close superseded PR")
expect(publish.scan("failed=1").length).to be >= 2
end
it "retries candidate branch pushes before failing publication" do
path = File.expand_path("../.github/workflows/ingest.yml", __dir__)
workflow = YAML.safe_load_file(path, aliases: true)
steps = workflow.dig("jobs", "match", "steps")
publish = steps.find do |step|
step["name"] == "Commit, push and open sharded pull requests"
end.fetch("run")
expect(publish).to include("push_branch()")
expect(publish).to include("readonly PUSH_RETRIES_EXHAUSTED=75")
expect(publish).to include("for attempt in 1 2 3 4 5")
expect(publish).to include('if git push --force origin "HEAD:refs/heads/${branch}"; then')
expect(publish).to include('sleep "${delay}"')
expect(publish).to include('push_branch "${branch}" || return')
expect(publish).to include("Push failed after ${attempt} attempts")
expect(publish).to include('return "${PUSH_RETRIES_EXHAUSTED}"')
expect(publish).to include("publish_status=$?")
expect(publish).to include("if (( publish_status == PUSH_RETRIES_EXHAUSTED ))")
expect(publish).to match(/if \(\( publish_status == PUSH_RETRIES_EXHAUSTED \)\); then\s+#.*\n\s+#.*\n\s+break/m)
end
it "retains the matched snapshot after post-snapshot failures" do
path = File.expand_path("../.github/workflows/ingest.yml", __dir__)
workflow = YAML.safe_load_file(path, aliases: true)
steps = workflow.dig("jobs", "match", "steps")
snapshot_index = steps.index { |step| step["name"] == "Snapshot matched advisories" }
publish_index = steps.index do |step|
step["name"] == "Commit, push and open sharded pull requests"
end
upload_index = steps.index { |step| step["name"] == "Upload failed matched snapshot" }
snapshot = steps.fetch(snapshot_index).fetch("run")
upload = steps.fetch(upload_index)
expect(snapshot).to include("git format-patch --stdout --full-index --binary")
expect(snapshot).to include('"${GITHUB_SHA}..HEAD" > "${RUNNER_TEMP}/matched-advisories-snapshot.patch"')
expect(publish_index).to be < upload_index
expect(upload.fetch("if")).to eq("failure() && steps.snapshot.outputs.created == 'true'")
expect(upload.fetch("uses")).to match(%r{\Aactions/upload-artifact@[0-9a-f]{40}\z})
expect(upload.fetch("with")).to include(
"name" => "matched-advisories-snapshot-${{ github.run_id }}-${{ github.run_attempt }}",
"path" => "${{ runner.temp }}/matched-advisories-snapshot.patch",
"if-no-files-found" => "error",
)
end
it "validates advisories in bounded batches" do
path = File.expand_path("../.github/workflows/validate.yml", __dir__)
workflow = YAML.safe_load_file(path, aliases: true)
steps = workflow.dig("jobs", "schema", "steps")
validation = steps.find { |step| step["name"] == "Validate advisories against OSV schema" }
run = validation.fetch("run")
expect(run).to match(/find advisories [^\n]*-print0/)
expect(run).to match(/xargs -0 [^\n]*-n [1-9]\d* check-jsonschema/)
expect(run).to include("--schemafile")
expect(run).not_to include("advisories/*.json")
end
end