GitRoot
Craft your forge, Build your project, Grow your community freely
1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5package plugin
6
7import (
8 "context"
9 "encoding/json"
10 "io/fs"
11
12 "github.com/go-git/go-git/v6/plumbing"
13 grfs "gitroot.dev/server/fs"
14 "gitroot.dev/server/logger"
15 "gitroot.dev/server/repository"
16)
17
18func (r *runtime) sendReports(ctx context.Context, repo *repository.GitRootRepository, repoWriter *repository.GitRootRepositoryWrite, plugins []Plugin) {
19 r.logger.Debug("start sendReports", logger.NewLoggerPair("nb reports", len(r.reports)))
20 r.repo = repo
21 r.repoWriter = repoWriter
22 r.command = nil
23
24 fs := grfs.NewMultiple(ctx, map[string]fs.FS{
25 "worktree": r.repoWriter.ToFs(ctx),
26 "webcontent": r.manager.conf.DataWeb(r.repo.Name()),
27 })
28
29 for _, plugin := range plugins {
30 fs.UpdateSubFs("cache", r.manager.conf.Cache(r.repo.Name(), plugin.NamespaceAndName()))
31 r.plugin = plugin
32 m, err := r.loadModule(r.ctx, plugin, fs)
33 if err != nil {
34 r.logger.Error("loadModule for sendReports error", err, logger.NewLoggerPair("name", plugin.Log()))
35 continue
36 }
37
38 reporter := m.ExportedFunction("report")
39 if reporter == nil {
40 r.logger.Debug("not a reporter", logger.NewLoggerPair("plugin", plugin.Log()))
41 continue
42 }
43
44 malloc := m.ExportedFunction("gitrootAlloc")
45 if malloc == nil {
46 malloc = m.ExportedFunction("malloc")
47 }
48
49 for _, report := range r.reports {
50 for _, pluginRun := range plugin.Run {
51 isAuthorized := checkBranch(pluginRun, plumbing.NewBranchReferenceName(report.FromBranch))
52 if !isAuthorized {
53 continue
54 }
55 r.pluginRun = pluginRun
56
57 reportJson, err := json.Marshal(report)
58 if err != nil {
59 r.logger.Error("can't marshal report", err, logger.NewLoggerPair("name", plugin.Log()))
60 continue
61 }
62
63 if err := r.writeMemoryAndCall(m, reporter, malloc, string(reportJson)); err != nil {
64 r.logger.Error("can't report to plugin", err, logger.NewLoggerPair("plugin", plugin.Log()))
65 continue
66 }
67 }
68 }
69 }
70}