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// IssueResolved is published when an issue was resolved.
241type IssueResolved struct {
242 Issue Issue
243 At time.Time
244 Visibility Visibility
245 CreatedAt time.Time
246}
247
248// EventKind implements Event.
249func (IssueResolved) EventKind() string { return "issue.resolved" }
250
251// IssueReopened is published when a closed issue was reopened.
252type IssueReopened struct {
253 Issue Issue
254 At time.Time
255 Visibility Visibility
256 CreatedAt time.Time
257}
258
259// EventKind implements Event.
260func (IssueReopened) EventKind() string { return "issue.reopened" }
261
262// IssueCommented is published when a comment was added to an issue.
263type IssueCommented struct {
264 Repo RepoName
265 Number int64
266 Comment IssueComment
267 At time.Time
268 Visibility Visibility
269 CreatedAt time.Time
270}
271
272// EventKind implements Event.
273func (IssueCommented) EventKind() string { return "issue.commented" }
274
275// IssueSubmitted is published when an anonymous submission awaits moderation.
276// It carries no content, and is delivered to the owner topic only: never to
277// home or to a repository topic.
278type IssueSubmitted struct {
279 Repo RepoName
280 ID PendingIssueID
281 At time.Time
282}
283
284// EventKind implements Event.
285func (IssueSubmitted) EventKind() string { return "issue.submitted" }
286
287// IssuePendingRemoved is published when a pending submission is rejected, so
288// the other open inboxes drop it. Like IssueSubmitted it carries no content
289// and is delivered to the owner topic only.
290type IssuePendingRemoved struct {
291 Repo RepoName
292 ID PendingIssueID
293 At time.Time
294}
295
296// EventKind implements Event.
297func (IssuePendingRemoved) EventKind() string { return "issue.pending_removed" }
298
299// CommentEdited is published when the text of a note was edited.
300type CommentEdited struct {
301 Repo RepoName
302 Number int64
303 Comment Comment
304 At time.Time
305 Visibility Visibility
306 CreatedAt time.Time
307}
308
309// EventKind implements Event.
310func (CommentEdited) EventKind() string { return "mr.comment_edited" }
311
312// CommentDeleted is published when the text of a note was deleted.
313type CommentDeleted struct {
314 Repo RepoName
315 Number int64
316 Comment Comment
317 At time.Time
318 Visibility Visibility
319 CreatedAt time.Time
320}
321
322// EventKind implements Event.
323func (CommentDeleted) EventKind() string { return "mr.comment_deleted" }
324
325// CommentResolved is published when a thread was resolved or reopened.
326type CommentResolved struct {
327 Repo RepoName
328 Number int64
329 Comment Comment
330 At time.Time
331 Visibility Visibility
332 CreatedAt time.Time
333}
334
335// EventKind implements Event.
336func (CommentResolved) EventKind() string { return "mr.comment_resolved" }
337
338// MergeRequestMergeability is published when the mergeability of an open merge
339// request changed.
340type MergeRequestMergeability struct {
341 Repo RepoName
342 Number int64
343 Mergeability Mergeability
344 At time.Time
345 Visibility Visibility
346 CreatedAt time.Time
347}
348
349// EventKind implements Event.
350func (MergeRequestMergeability) EventKind() string { return "mr.mergeability" }
351
352// EventGap tells a subscriber that it missed events because it was too slow;
353// it must refresh its whole view.
354type EventGap struct{}
355
356// EventKind implements Event.
357func (EventGap) EventKind() string { return "gap" }
358
359// MirrorFailed is published when a mirror enters failing or paused (transitions only).
360type MirrorFailed struct {
361 Repo RepoName
362 State MirrorState
363 Class MirrorFailureClass
364 At time.Time
365 Visibility Visibility
366 CreatedAt time.Time
367}
368
369func (MirrorFailed) EventKind() string { return "mirror.failed" }
370
371// MirrorRecovered is published when a failing or paused mirror succeeds again.
372type MirrorRecovered struct {
373 Repo RepoName
374 At time.Time
375 Visibility Visibility
376 CreatedAt time.Time
377}
378
379func (MirrorRecovered) EventKind() string { return "mirror.recovered" }
380
381// ReleasePublished records a release lifecycle change.
382type ReleasePublished struct {
383 Release Release
384 At time.Time
385 Visibility Visibility
386 CreatedAt time.Time
387}
388
389// EventKind implements Event.
390func (ReleasePublished) EventKind() string { return "release.published" }
391
392// ReleaseUpdated records a release lifecycle change.
393type ReleaseUpdated struct {
394 Release Release
395 At time.Time
396 Visibility Visibility
397 CreatedAt time.Time
398}
399
400// EventKind implements Event.
401func (ReleaseUpdated) EventKind() string { return "release.updated" }
402
403// ReleaseDeleted records a release lifecycle change.
404type ReleaseDeleted struct {
405 Release Release
406 At time.Time
407 Visibility Visibility
408 CreatedAt time.Time
409}
410
411// EventKind implements Event.
412func (ReleaseDeleted) EventKind() string { return "release.deleted" }
413
414// ReleaseUnpublished records a release lifecycle change.
415type ReleaseUnpublished struct {
416 Release Release
417 At time.Time
418 Visibility Visibility
419 CreatedAt time.Time
420}
421
422// EventKind implements Event.
423func (ReleaseUnpublished) EventKind() string { return "release.unpublished" }