1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package domain
4
5import (
6 "fmt"
7 "regexp"
8 "time"
9)
10
11// ZeroOID is the object ID that stands for "no object" in a ref update.
12const ZeroOID = "0000000000000000000000000000000000000000"
13
14// RefUpdate is one ref change of a push, as Git reports it to the hooks.
15type RefUpdate struct {
16 // Old is the previous object ID, ZeroOID when the ref is created.
17 Old string
18 // New is the new object ID, ZeroOID when the ref is deleted.
19 New string
20 // Ref is the full ref name, such as refs/heads/main.
21 Ref string
22}
23
24var oidRE = regexp.MustCompile(`^[0-9a-f]{40}$`)
25
26// maxRefLen bounds a ref name.
27const maxRefLen = 4096
28
29// NewRefUpdate validates the parts of a ref update as Git reports them: two
30// 40-digit lowercase hexadecimal object IDs (sha1) and a non-empty ref name.
31func NewRefUpdate(oldOID, newOID, ref string) (RefUpdate, error) {
32 if !oidRE.MatchString(oldOID) || !oidRE.MatchString(newOID) {
33 return RefUpdate{}, fmt.Errorf("invalid object id in the update of %q", ref)
34 }
35 if ref == "" || len(ref) > maxRefLen {
36 return RefUpdate{}, fmt.Errorf("invalid ref name %q", ref)
37 }
38 return RefUpdate{Old: oldOID, New: newOID, Ref: ref}, nil
39}
40
41// IsCreate reports whether the update creates the ref.
42func (u RefUpdate) IsCreate() bool { return u.Old == ZeroOID }
43
44// IsDelete reports whether the update deletes the ref.
45func (u RefUpdate) IsDelete() bool { return u.New == ZeroOID }
46
47// Event is something that happened in the forge, published on the event bus.
48type Event interface {
49 // EventKind names the kind of event.
50 EventKind() string
51}
52
53// RepoCreated is published when a repository comes into being.
54type RepoCreated struct {
55 Repo RepoName
56 Visibility Visibility
57 At time.Time
58}
59
60// EventKind implements Event.
61func (RepoCreated) EventKind() string { return "repo.created" }
62
63// RepoUpdated is published when the metadata of a repository, or its default
64// branch, changed. The new state is read from the repository.
65//
66// The visibility fields tell who could read the repository before and after
67// the change, and CreatedAt tells which repository of that name it was (a
68// name can be deleted and created again): the event stream decides who may be
69// told from them.
70type RepoUpdated struct {
71 Repo RepoName
72 At time.Time
73 // Visibility is the visibility after the change, Previous the one before.
74 Visibility, Previous Visibility
75 // CreatedAt is the creation time of the repository.
76 CreatedAt time.Time
77}
78
79// EventKind implements Event.
80func (RepoUpdated) EventKind() string { return "repo.updated" }
81
82// RepoDeleted is published after a repository was deleted. It records the
83// visibility the repository had, which decides who may be told.
84type RepoDeleted struct {
85 Repo RepoName
86 Visibility Visibility
87 At time.Time
88 // CreatedAt is the creation time of the repository that was deleted.
89 CreatedAt time.Time
90}
91
92// EventKind implements Event.
93func (RepoDeleted) EventKind() string { return "repo.deleted" }
94
95// PushEvent is published after a push has been accepted.
96//
97// It carries the visibility and the creation time of the repository as read
98// when the push was recorded, so the event stream does not read them later,
99// when the repository may have changed or been recreated. A zero CreatedAt
100// means the repository was unknown: the event is not delivered.
101type PushEvent struct {
102 Repo RepoName
103 Updates []RefUpdate
104 At time.Time
105 // Visibility is the visibility of the repository at publish time.
106 Visibility Visibility
107 // CreatedAt is the creation time of the repository that was pushed to.
108 CreatedAt time.Time
109}
110
111// EventKind implements Event.
112func (PushEvent) EventKind() string { return "repo.pushed" }
113
114// MergeRequestOpened is published when a merge request is opened.
115type MergeRequestOpened struct {
116 MergeRequest MergeRequest
117 At time.Time
118 // Visibility and CreatedAt are those of the repository when the event
119 // was published, like PushEvent's.
120 Visibility Visibility
121 CreatedAt time.Time
122}
123
124// EventKind implements Event.
125func (MergeRequestOpened) EventKind() string { return "mr.opened" }
126
127// MergeRequestMerged is published when a merge request was merged, before the
128// PushEvent of the target branch.
129type MergeRequestMerged struct {
130 MergeRequest MergeRequest
131 At time.Time
132 Visibility Visibility
133 CreatedAt time.Time
134 // Auto says that auto-merge made the merge.
135 Auto bool
136}
137
138// EventKind implements Event.
139func (MergeRequestMerged) EventKind() string { return "mr.merged" }
140
141// MergeRequestAutoMergeEnabled is published when auto-merge was enabled on a
142// merge request.
143type MergeRequestAutoMergeEnabled struct {
144 MergeRequest MergeRequest
145 At time.Time
146 Visibility Visibility
147 CreatedAt time.Time
148}
149
150// EventKind implements Event.
151func (MergeRequestAutoMergeEnabled) EventKind() string { return "mr.auto_merge_enabled" }
152
153// MergeRequestAutoMergeCancelled is published when auto-merge was cancelled
154// on a merge request, by hand or by the server.
155type MergeRequestAutoMergeCancelled struct {
156 MergeRequest MergeRequest
157 // Reason says why, in words.
158 Reason string
159 At time.Time
160 Visibility Visibility
161 CreatedAt time.Time
162}
163
164// EventKind implements Event.
165func (MergeRequestAutoMergeCancelled) EventKind() string { return "mr.auto_merge_cancelled" }
166
167// MergeRequestClosed is published when a merge request was closed without
168// merging.
169type MergeRequestClosed struct {
170 MergeRequest MergeRequest
171 At time.Time
172 Visibility Visibility
173 CreatedAt time.Time
174}
175
176// EventKind implements Event.
177func (MergeRequestClosed) EventKind() string { return "mr.closed" }
178
179// MergeRequestReady is published when a draft merge request was marked ready
180// for review: its title lost the draft prefix.
181type MergeRequestReady struct {
182 MergeRequest MergeRequest
183 At time.Time
184 Visibility Visibility
185 CreatedAt time.Time
186}
187
188// EventKind implements Event.
189func (MergeRequestReady) EventKind() string { return "mr.ready" }
190
191// CommentAdded is published when a comment was added to a merge request.
192type CommentAdded struct {
193 Repo RepoName
194 Number int64
195 Comment Comment
196 At time.Time
197 Visibility Visibility
198 CreatedAt time.Time
199}
200
201// EventKind implements Event.
202func (CommentAdded) EventKind() string { return "mr.comment_added" }
203
204// IssueOpened is published when an issue is opened.
205type IssueOpened struct {
206 Issue Issue
207 At time.Time
208 // Visibility and CreatedAt are those of the repository when the event
209 // was published, like PushEvent's.
210 Visibility Visibility
211 CreatedAt time.Time
212}
213
214// EventKind implements Event.
215func (IssueOpened) EventKind() string { return "issue.opened" }
216
217// IssueReferenced refreshes an issue's merge request back-references.
218type IssueReferenced struct {
219 Repo RepoName
220 Issue, MergeRequest int64
221 At time.Time
222 Visibility Visibility
223 CreatedAt time.Time
224}
225
226// EventKind implements Event.
227func (IssueReferenced) EventKind() string { return "issue.referenced" }
228
229// IssueClosed is published when an issue was closed.
230type IssueClosed struct {
231 Issue Issue
232 At time.Time
233 Visibility Visibility
234 CreatedAt time.Time
235}
236
237// EventKind implements Event.
238func (IssueClosed) EventKind() string { return "issue.closed" }
239
240// IssueReopened is published when a closed issue was reopened.
241type IssueReopened struct {
242 Issue Issue
243 At time.Time
244 Visibility Visibility
245 CreatedAt time.Time
246}
247
248// EventKind implements Event.
249func (IssueReopened) EventKind() string { return "issue.reopened" }
250
251// IssueCommented is published when a comment was added to an issue.
252type IssueCommented struct {
253 Repo RepoName
254 Number int64
255 Comment IssueComment
256 At time.Time
257 Visibility Visibility
258 CreatedAt time.Time
259}
260
261// EventKind implements Event.
262func (IssueCommented) EventKind() string { return "issue.commented" }
263
264// IssueSubmitted is published when an anonymous submission awaits moderation.
265// It carries no content, and is delivered to the owner topic only: never to
266// home or to a repository topic.
267type IssueSubmitted struct {
268 Repo RepoName
269 ID PendingIssueID
270 At time.Time
271}
272
273// EventKind implements Event.
274func (IssueSubmitted) EventKind() string { return "issue.submitted" }
275
276// IssuePendingRemoved is published when a pending submission is rejected, so
277// the other open inboxes drop it. Like IssueSubmitted it carries no content
278// and is delivered to the owner topic only.
279type IssuePendingRemoved struct {
280 Repo RepoName
281 ID PendingIssueID
282 At time.Time
283}
284
285// EventKind implements Event.
286func (IssuePendingRemoved) EventKind() string { return "issue.pending_removed" }
287
288// CommentEdited is published when the text of a note was edited.
289type CommentEdited struct {
290 Repo RepoName
291 Number int64
292 Comment Comment
293 At time.Time
294 Visibility Visibility
295 CreatedAt time.Time
296}
297
298// EventKind implements Event.
299func (CommentEdited) EventKind() string { return "mr.comment_edited" }
300
301// CommentDeleted is published when the text of a note was deleted.
302type CommentDeleted struct {
303 Repo RepoName
304 Number int64
305 Comment Comment
306 At time.Time
307 Visibility Visibility
308 CreatedAt time.Time
309}
310
311// EventKind implements Event.
312func (CommentDeleted) EventKind() string { return "mr.comment_deleted" }
313
314// CommentResolved is published when a thread was resolved or reopened.
315type CommentResolved struct {
316 Repo RepoName
317 Number int64
318 Comment Comment
319 At time.Time
320 Visibility Visibility
321 CreatedAt time.Time
322}
323
324// EventKind implements Event.
325func (CommentResolved) EventKind() string { return "mr.comment_resolved" }
326
327// MergeRequestMergeability is published when the mergeability of an open merge
328// request changed.
329type MergeRequestMergeability struct {
330 Repo RepoName
331 Number int64
332 Mergeability Mergeability
333 At time.Time
334 Visibility Visibility
335 CreatedAt time.Time
336}
337
338// EventKind implements Event.
339func (MergeRequestMergeability) EventKind() string { return "mr.mergeability" }
340
341// EventGap tells a subscriber that it missed events because it was too slow;
342// it must refresh its whole view.
343type EventGap struct{}
344
345// EventKind implements Event.
346func (EventGap) EventKind() string { return "gap" }
347
348// MirrorFailed is published when a mirror enters failing or paused (transitions only).
349type MirrorFailed struct {
350 Repo RepoName
351 State MirrorState
352 Class MirrorFailureClass
353 At time.Time
354 Visibility Visibility
355 CreatedAt time.Time
356}
357
358func (MirrorFailed) EventKind() string { return "mirror.failed" }
359
360// MirrorRecovered is published when a failing or paused mirror succeeds again.
361type MirrorRecovered struct {
362 Repo RepoName
363 At time.Time
364 Visibility Visibility
365 CreatedAt time.Time
366}
367
368func (MirrorRecovered) EventKind() string { return "mirror.recovered" }
369
370// ReleasePublished records a release lifecycle change.
371type ReleasePublished struct {
372 Release Release
373 At time.Time
374 Visibility Visibility
375 CreatedAt time.Time
376}
377
378// EventKind implements Event.
379func (ReleasePublished) EventKind() string { return "release.published" }
380
381// ReleaseUpdated records a release lifecycle change.
382type ReleaseUpdated struct {
383 Release Release
384 At time.Time
385 Visibility Visibility
386 CreatedAt time.Time
387}
388
389// EventKind implements Event.
390func (ReleaseUpdated) EventKind() string { return "release.updated" }
391
392// ReleaseDeleted records a release lifecycle change.
393type ReleaseDeleted struct {
394 Release Release
395 At time.Time
396 Visibility Visibility
397 CreatedAt time.Time
398}
399
400// EventKind implements Event.
401func (ReleaseDeleted) EventKind() string { return "release.deleted" }
402
403// ReleaseUnpublished records a release lifecycle change.
404type ReleaseUnpublished struct {
405 Release Release
406 At time.Time
407 Visibility Visibility
408 CreatedAt time.Time
409}
410
411// EventKind implements Event.
412func (ReleaseUnpublished) EventKind() string { return "release.unpublished" }