|
1
|
+ |
defmodule OpenAgents.Issues.ReleasesTest do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
#10, the half of the traceability chain a sha comparison cannot answer.
|
|
4
|
+ |
|
|
5
|
+ |
An issue's closing commit is an ancestor of the revision a release was
|
|
6
|
+ |
promoted at, never that revision itself, so `OpenAgents.Forge.receipts_for/2`
|
|
7
|
+ |
— which matches a fleet target by comparing shas — finds nothing for the
|
|
8
|
+ |
commit that actually shipped. These tests run against a real bare forge
|
|
9
|
+ |
repository with a real commit graph and real promoted targets, because the
|
|
10
|
+ |
only thing that makes the answer true is git's own containment relation.
|
|
11
|
+ |
|
|
12
|
+ |
The graph every test reads:
|
|
13
|
+ |
|
|
14
|
+ |
c0 ── c1 ── c2 ── c3 (refs/heads/main = c3)
|
|
15
|
+ |
└── side (refs/heads/side, never merged)
|
|
16
|
+ |
"""
|
|
17
|
+ |
|
|
18
|
+ |
use OpenAgents.DataCase, async: false
|
|
19
|
+ |
|
|
20
|
+ |
alias OpenAgents.Forge.Repos
|
|
21
|
+ |
alias OpenAgents.Forge.Targets
|
|
22
|
+ |
alias OpenAgents.Issues
|
|
23
|
+ |
alias OpenAgents.Issues.{Activity, ClosingReference, Releases}
|
|
24
|
+ |
alias OpenAgents.Repo
|
|
25
|
+ |
alias OpenAgents.Repositories
|
|
26
|
+ |
|
|
27
|
+ |
@repo "openagents.com"
|
|
28
|
+ |
|
|
29
|
+ |
setup do
|
|
30
|
+ |
Ecto.Adapters.SQL.Sandbox.mode(OpenAgents.Repo, {:shared, self()})
|
|
31
|
+ |
|
|
32
|
+ |
base = Path.join(System.tmp_dir!(), "issue-releases-#{System.unique_integer([:positive])}")
|
|
33
|
+ |
File.mkdir_p!(base)
|
|
34
|
+ |
|
|
35
|
+ |
previous_data = Application.get_env(:openagents, :forge_data_dir)
|
|
36
|
+ |
previous_wal = Application.get_env(:openagents, :forge_wal_dir)
|
|
37
|
+ |
Application.put_env(:openagents, :forge_data_dir, Path.join(base, "data"))
|
|
38
|
+ |
Application.put_env(:openagents, :forge_wal_dir, Path.join(base, "wal"))
|
|
39
|
+ |
|
|
40
|
+ |
on_exit(fn ->
|
|
41
|
+ |
restore(:forge_data_dir, previous_data)
|
|
42
|
+ |
restore(:forge_wal_dir, previous_wal)
|
|
43
|
+ |
File.rm_rf(base)
|
|
44
|
+ |
end)
|
|
45
|
+ |
|
|
46
|
+ |
repository = Repositories.get_by_path!("OpenAgentsInc", @repo)
|
|
47
|
+ |
{:ok, issue} = Issues.create_issue(repository, %{title: "Ship the release link"})
|
|
48
|
+ |
|
|
49
|
+ |
Map.merge(seed_graph(), %{repository: repository, issue: issue})
|
|
50
|
+ |
end
|
|
51
|
+ |
|
|
52
|
+ |
defp restore(key, nil), do: Application.delete_env(:openagents, key)
|
|
53
|
+ |
defp restore(key, value), do: Application.put_env(:openagents, key, value)
|
|
54
|
+ |
|
|
55
|
+ |
describe "for_issue/2" do
|
|
56
|
+ |
test "an issue no commit claims has no commits and no release", context do
|
|
57
|
+ |
assert Releases.for_issue(context.repository, context.issue) == Releases.empty()
|
|
58
|
+ |
end
|
|
59
|
+ |
|
|
60
|
+ |
test "a release promoted at a descendant of the issue's commit carried it", context do
|
|
61
|
+ |
claim(context, context.c1)
|
|
62
|
+ |
target = release(context.c2, "live")
|
|
63
|
+ |
|
|
64
|
+ |
assert %{commits: [commit], released_in: released, truncated: false} =
|
|
65
|
+ |
Releases.for_issue(context.repository, context.issue)
|
|
66
|
+ |
|
|
67
|
+ |
assert commit.sha == context.c1
|
|
68
|
+ |
assert [carried] = commit.releases
|
|
69
|
+ |
assert carried.sha == context.c2
|
|
70
|
+ |
assert carried.status == "live"
|
|
71
|
+ |
assert released.id == target.id
|
|
72
|
+ |
assert released.sha == context.c2
|
|
73
|
+ |
end
|
|
74
|
+ |
|
|
75
|
+ |
test "a release promoted before the commit existed did not carry it", context do
|
|
76
|
+ |
claim(context, context.c2)
|
|
77
|
+ |
release(context.c0, "live")
|
|
78
|
+ |
|
|
79
|
+ |
assert %{commits: [commit], released_in: nil} =
|
|
80
|
+ |
Releases.for_issue(context.repository, context.issue)
|
|
81
|
+ |
|
|
82
|
+ |
assert commit.releases == []
|
|
83
|
+ |
end
|
|
84
|
+ |
|
|
85
|
+ |
test "a commit that never reached the mainline is carried by nothing", context do
|
|
86
|
+ |
claim(context, context.side)
|
|
87
|
+ |
release(context.c3, "live")
|
|
88
|
+ |
|
|
89
|
+ |
assert %{commits: [commit], released_in: nil} =
|
|
90
|
+ |
Releases.for_issue(context.repository, context.issue)
|
|
91
|
+ |
|
|
92
|
+ |
assert commit.releases == []
|
|
93
|
+ |
end
|
|
94
|
+ |
|
|
95
|
+ |
test "every release that contains the commit is listed, oldest first", context do
|
|
96
|
+ |
claim(context, context.c1)
|
|
97
|
+ |
first = release(context.c2, "live")
|
|
98
|
+ |
second = release(context.c3, "live")
|
|
99
|
+ |
|
|
100
|
+ |
assert %{commits: [commit], released_in: released} =
|
|
101
|
+ |
Releases.for_issue(context.repository, context.issue)
|
|
102
|
+ |
|
|
103
|
+ |
assert Enum.map(commit.releases, & &1.id) == [first.id, second.id]
|
|
104
|
+ |
assert released.id == first.id
|
|
105
|
+ |
end
|
|
106
|
+ |
|
|
107
|
+ |
test "released_in is the oldest live release that carried every commit", context do
|
|
108
|
+ |
claim(context, context.c1)
|
|
109
|
+ |
claim(context, context.c3)
|
|
110
|
+ |
partial = release(context.c2, "live")
|
|
111
|
+ |
whole = release(context.c3, "live")
|
|
112
|
+ |
|
|
113
|
+ |
assert %{commits: commits, released_in: released} =
|
|
114
|
+ |
Releases.for_issue(context.repository, context.issue)
|
|
115
|
+ |
|
|
116
|
+ |
by_sha = Map.new(commits, &{&1.sha, &1})
|
|
117
|
+ |
|
|
118
|
+ |
assert Enum.map(by_sha[context.c1].releases, & &1.id) == [partial.id, whole.id]
|
|
119
|
+ |
assert Enum.map(by_sha[context.c3].releases, & &1.id) == [whole.id]
|
|
120
|
+ |
assert released.id == whole.id
|
|
121
|
+ |
end
|
|
122
|
+ |
|
|
123
|
+ |
test "a promoted release that never went live is listed but never released_in", context do
|
|
124
|
+ |
claim(context, context.c1)
|
|
125
|
+ |
promoted = release(context.c3, "promoted")
|
|
126
|
+ |
|
|
127
|
+ |
assert %{commits: [commit], released_in: nil} =
|
|
128
|
+ |
Releases.for_issue(context.repository, context.issue)
|
|
129
|
+ |
|
|
130
|
+ |
assert [carried] = commit.releases
|
|
131
|
+ |
assert carried.id == promoted.id
|
|
132
|
+ |
assert carried.status == "promoted"
|
|
133
|
+ |
end
|
|
134
|
+ |
|
|
135
|
+ |
test "the target window is bounded and says so when it cuts something off", context do
|
|
136
|
+ |
claim(context, context.c1)
|
|
137
|
+ |
for _each <- 1..13, do: release(context.c3, "live")
|
|
138
|
+ |
|
|
139
|
+ |
assert %{commits: [commit], truncated: true} =
|
|
140
|
+ |
Releases.for_issue(context.repository, context.issue)
|
|
141
|
+ |
|
|
142
|
+ |
assert length(commit.releases) == 12
|
|
143
|
+ |
end
|
|
144
|
+ |
|
|
145
|
+ |
test "a repository that is not the issue's own answers with nothing", context do
|
|
146
|
+ |
claim(context, context.c1)
|
|
147
|
+ |
release(context.c2, "live")
|
|
148
|
+ |
|
|
149
|
+ |
other = %{context.repository | id: Ecto.UUID.generate()}
|
|
150
|
+ |
|
|
151
|
+ |
assert Releases.for_issue(other, context.issue) == Releases.empty()
|
|
152
|
+ |
end
|
|
153
|
+ |
end
|
|
154
|
+ |
|
|
155
|
+ |
describe "the activity read and its JSON" do
|
|
156
|
+ |
test "activity carries the release that shipped the issue", context do
|
|
157
|
+ |
claim(context, context.c1)
|
|
158
|
+ |
target = release(context.c2, "live")
|
|
159
|
+ |
|
|
160
|
+ |
activity = Activity.for_issue(context.issue)
|
|
161
|
+ |
|
|
162
|
+ |
assert activity.releases.released_in.id == target.id
|
|
163
|
+ |
|
|
164
|
+ |
rendered = OpenAgentsWeb.IssueJSON.render("activity.json", %{activity: activity})
|
|
165
|
+ |
|
|
166
|
+ |
assert %{released_in: %{sha: sha, status: "live"}, truncated: false} = rendered.releases
|
|
167
|
+ |
assert sha == context.c2
|
|
168
|
+ |
assert [%{sha: ^sha}] = hd(rendered.releases.commits).releases
|
|
169
|
+ |
end
|
|
170
|
+ |
|
|
171
|
+ |
test "an issue nothing shipped renders an empty release answer", context do
|
|
172
|
+ |
rendered =
|
|
173
|
+ |
OpenAgentsWeb.IssueJSON.render("activity.json", %{
|
|
174
|
+ |
activity: Activity.for_issue(context.issue)
|
|
175
|
+ |
})
|
|
176
|
+ |
|
|
177
|
+ |
assert rendered.releases == %{commits: [], released_in: nil, truncated: false}
|
|
178
|
+ |
end
|
|
179
|
+ |
end
|
|
180
|
+ |
|
|
181
|
+ |
# ── fixture ──────────────────────────────────────────────────────────────
|
|
182
|
+ |
|
|
183
|
+ |
# One `Closes #N` reference, written the way `OpenAgents.Issues.ClosingReferences`
|
|
184
|
+ |
# writes it. What the reference means is proved by `OpenAgents.Forge.PushClosesIssuesTest`;
|
|
185
|
+ |
# what it is worth here is the commit it names.
|
|
186
|
+ |
defp claim(%{repository: repository, issue: issue}, sha) do
|
|
187
|
+ |
%ClosingReference{}
|
|
188
|
+ |
|> ClosingReference.changeset(%{
|
|
189
|
+ |
repository_id: repository.id,
|
|
190
|
+ |
issue_id: issue.id,
|
|
191
|
+ |
commit_sha: sha,
|
|
192
|
+ |
repo: @repo,
|
|
193
|
+ |
principal: "test:releases",
|
|
194
|
+ |
verb: "closes",
|
|
195
|
+ |
closed: true
|
|
196
|
+ |
})
|
|
197
|
+ |
|> Repo.insert!()
|
|
198
|
+ |
end
|
|
199
|
+ |
|
|
200
|
+ |
# One promotion through the real lane, so the sha precondition and the
|
|
201
|
+ |
# transition table both run. `status` is where the target is left.
|
|
202
|
+ |
defp release(sha, status) do
|
|
203
|
+ |
{:ok, target} = Targets.promote(@repo, sha, "operator:releases-test")
|
|
204
|
+ |
|
|
205
|
+ |
Enum.reduce_while(["building", "built", "deploying", "live"], target, fn step, current ->
|
|
206
|
+ |
if current.status == status, do: {:halt, current}, else: {:cont, advance(current, step)}
|
|
207
|
+ |
end)
|
|
208
|
+ |
end
|
|
209
|
+ |
|
|
210
|
+ |
defp advance(target, step) do
|
|
211
|
+ |
{:ok, advanced} = Targets.advance(target.id, step)
|
|
212
|
+ |
advanced
|
|
213
|
+ |
end
|
|
214
|
+ |
|
|
215
|
+ |
defp seed_graph do
|
|
216
|
+ |
path = Repos.ensure_repo!(@repo)
|
|
217
|
+ |
|
|
218
|
+ |
c0 = commit(path, [{"f.txt", "zero\n"}], [])
|
|
219
|
+ |
c1 = commit(path, [{"f.txt", "zero\n"}, {"one.txt", "one\n"}], ["-p", c0])
|
|
220
|
+ |
c2 = commit(path, [{"f.txt", "zero\n"}, {"two.txt", "two\n"}], ["-p", c1])
|
|
221
|
+ |
c3 = commit(path, [{"f.txt", "zero\n"}, {"three.txt", "three\n"}], ["-p", c2])
|
|
222
|
+ |
side = commit(path, [{"f.txt", "zero\n"}, {"side.txt", "side\n"}], ["-p", c0])
|
|
223
|
+ |
|
|
224
|
+ |
{_output, 0} = Repos.git(path, ["update-ref", "refs/heads/main", c3])
|
|
225
|
+ |
{_output, 0} = Repos.git(path, ["update-ref", "refs/heads/side", side])
|
|
226
|
+ |
|
|
227
|
+ |
%{path: path, c0: c0, c1: c1, c2: c2, c3: c3, side: side}
|
|
228
|
+ |
end
|
|
229
|
+ |
|
|
230
|
+ |
defp commit(path, files, parents) do
|
|
231
|
+ |
listing =
|
|
232
|
+ |
files
|
|
233
|
+ |
|> Enum.map(fn {name, content} -> "100644 blob #{blob(path, content)}\t#{name}\n" end)
|
|
234
|
+ |
|> Enum.join()
|
|
235
|
+ |
|
|
236
|
+ |
{tree, 0} = plumb(path, ["mktree"], listing)
|
|
237
|
+ |
|
|
238
|
+ |
{sha, 0} =
|
|
239
|
+ |
plumb(path, ["commit-tree", String.trim(tree)] ++ parents, "commit #{listing}",
|
|
240
|
+ |
env: [
|
|
241
|
+ |
{"GIT_AUTHOR_NAME", "Release Test"},
|
|
242
|
+ |
{"GIT_AUTHOR_EMAIL", "release@example.test"},
|
|
243
|
+ |
{"GIT_AUTHOR_DATE", "2026-01-01T00:00:00Z"},
|
|
244
|
+ |
{"GIT_COMMITTER_NAME", "Release Test"},
|
|
245
|
+ |
{"GIT_COMMITTER_EMAIL", "release@example.test"},
|
|
246
|
+ |
{"GIT_COMMITTER_DATE", "2026-01-01T00:00:00Z"}
|
|
247
|
+ |
]
|
|
248
|
+ |
)
|
|
249
|
+ |
|
|
250
|
+ |
String.trim(sha)
|
|
251
|
+ |
end
|
|
252
|
+ |
|
|
253
|
+ |
defp blob(path, content) do
|
|
254
|
+ |
{sha, 0} = plumb(path, ["hash-object", "-w", "--stdin"], content)
|
|
255
|
+ |
String.trim(sha)
|
|
256
|
+ |
end
|
|
257
|
+ |
|
|
258
|
+ |
defp plumb(path, args, stdin, opts \\ []) do
|
|
259
|
+ |
input = Path.join(System.tmp_dir!(), "plumb-#{System.unique_integer([:positive])}")
|
|
260
|
+ |
File.write!(input, stdin)
|
|
261
|
+ |
|
|
262
|
+ |
try do
|
|
263
|
+ |
System.cmd(
|
|
264
|
+ |
"sh",
|
|
265
|
+ |
["-c", ~s(exec git --git-dir "$GD" "$@" < "$IN"), "sh"] ++ args,
|
|
266
|
+ |
env: [{"GD", path}, {"IN", input}] ++ Keyword.get(opts, :env, [])
|
|
267
|
+ |
)
|
|
268
|
+ |
after
|
|
269
|
+ |
File.rm(input)
|
|
270
|
+ |
end
|
|
271
|
+ |
end
|
|
272
|
+ |
end
|