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	"fmt"
 11	"io/fs"
 12	"slices"
 13	"time"
 14
 15	"github.com/go-git/go-git/v6/plumbing"
 16	"github.com/samber/oops"
 17	"github.com/tetratelabs/wazero/api"
 18	pluginLib "gitroot.dev/libs/golang/plugin/model"
 19	grfs "gitroot.dev/server/fs"
 20	"gitroot.dev/server/logger"
 21	"gitroot.dev/server/repository"
 22	"gitroot.dev/server/user"
 23)
 24
 25type callPlugin struct {
 26	manager    *Manager
 27	plugin     Plugin
 28	repo       *repository.GitRootRepository
 29	repoWriter *repository.GitRootRepositoryWrite
 30	module     api.Module
 31	logger     *logger.Logger
 32}
 33
 34func (r *runtime) start(ctx context.Context, repo *repository.GitRootRepository, repoWriter *repository.GitRootRepositoryWrite, plugins []Plugin, command CommandForDiff, mergeHook func(cmd *repository.MergeRes, pusher user.SimpleUser, toDeleteBranchName string)) error {
 35	r.repo = repo
 36	r.repoWriter = repoWriter
 37	r.command = nil
 38	r.commit = nil
 39
 40	toRunLater := []CommandForDiff{}
 41
 42	r.commitHook = func(hash plumbing.Hash) {
 43		lastCom, err := repoWriter.GetLastCommit(hash)
 44		if err != nil {
 45			r.logger.Error("can't GetLastCommit in start runtime", err)
 46			return
 47		}
 48		newCmd, err := CommandForDiffFromCommitCmd(ctx, r.plugin.commiter.SimpleUser, lastCom, r.command.branch)
 49		if err != nil {
 50			r.logger.Error("can't CommandForDiffFromCommitCmd in start runtime", err)
 51			return
 52		}
 53		toRunLater = append(toRunLater, newCmd)
 54	}
 55
 56	r.mergeHook = mergeHook
 57	defer func() {
 58		r.commitHook = nil
 59		r.mergeHook = nil
 60	}()
 61
 62	r.runCmd(ctx, plugins, repo, repoWriter, command)
 63
 64	for _, cmd := range toRunLater {
 65		r.runCmd(ctx, plugins, repo, repoWriter, cmd)
 66	}
 67
 68	return nil
 69}
 70
 71func (r *runtime) runCmd(ctx context.Context, plugins []Plugin, repo *repository.GitRootRepository, repoWriter *repository.GitRootRepositoryWrite, command CommandForDiff) {
 72	fs := grfs.NewMultiple(ctx, map[string]fs.FS{
 73		"worktree":   r.repoWriter.ToFs(ctx),
 74		"webcontent": r.manager.conf.DataWeb(r.repo.Name()),
 75	})
 76	for _, plugin := range plugins {
 77		fs.UpdateSubFs("cache", r.manager.conf.Cache(r.repo.Name(), plugin.NamespaceAndName()))
 78		r.plugin = plugin
 79		timerStop := r.logger.Time(fmt.Sprintf("Timer %s", plugin.Log()))
 80		r.logger.Debug("start plugin", logger.NewLoggerPair("repo", repo.Name()), logger.NewLoggerPair("name", plugin.Log()))
 81		m, err := r.loadModule(ctx, plugin, fs)
 82		if err != nil {
 83			r.logger.Error("loadModule for start error", err, logger.NewLoggerPair("name", plugin.Log()))
 84			continue
 85		}
 86		l := logger.NewLogger(logger.WASM)
 87		l.Debug("memory before", logger.NewLoggerPair("size", m.Memory().Size()), logger.NewLoggerPair("plugin", plugin.Log()))
 88		cp := callPlugin{
 89			manager:    r.manager,
 90			plugin:     plugin,
 91			repo:       repo,
 92			repoWriter: repoWriter,
 93			module:     m,
 94			logger:     r.logger.NewSubLogger(plugin.Log()),
 95		}
 96		if err := cp.callPluginForDiff(ctx, r, command); err != nil {
 97			r.logger.Error("finish plugin with error", err, logger.NewLoggerPair("name", plugin.Log()))
 98		}
 99		l.Debug("memory after", logger.NewLoggerPair("size", m.Memory().Size()), logger.NewLoggerPair("plugin", plugin.Log()))
100		r.logger.Debug("finish plugin", logger.NewLoggerPair("name", plugin.Log()))
101		timerStop()
102	}
103}
104
105func (c callPlugin) callPluginForDiff(ctx context.Context, r *runtime, cmd CommandForDiff) error {
106	startCommit := c.module.ExportedFunction("startCommit")
107	addFile := c.module.ExportedFunction("addFile")
108	modFile := c.module.ExportedFunction("modFile")
109	delFile := c.module.ExportedFunction("delFile")
110	endCommit := c.module.ExportedFunction("endCommit")
111	malloc := c.module.ExportedFunction("gitrootAlloc")
112	if malloc == nil {
113		malloc = c.module.ExportedFunction("malloc")
114	}
115
116	r.command = &cmd
117
118	for _, pluginRun := range r.plugin.Run {
119		r.pluginRun = pluginRun
120		callOnAdd := addFile != nil && slices.Contains(pluginRun.When, pluginLib.PluginRunWhenAdd)
121		callOnMod := modFile != nil && slices.Contains(pluginRun.When, pluginLib.PluginRunWhenMod)
122		callOnDel := delFile != nil && slices.Contains(pluginRun.When, pluginLib.PluginRunWhenDel)
123		isAuthorized := checkBranch(pluginRun, cmd.branch)
124		if !isAuthorized {
125			continue
126		}
127
128		if cmd.branchAction == commitForDiffActionDel {
129			c.logger.Info("delete branch", logger.NewLoggerPair("branch", cmd.branch))
130			continue
131		}
132
133		atLeastOneFile := slices.ContainsFunc(cmd.commits, func(com commitForDiffCommit) bool {
134			return slices.ContainsFunc(com.files, func(f pluginLib.File) bool {
135				return pluginRun.glob.Match(f.Path)
136			})
137		})
138		if !atLeastOneFile {
139			c.logger.Info("no file match, skip execution", logger.NewLoggerPair("plugin", r.plugin.Log()))
140			continue
141		}
142
143		if init := c.module.ExportedFunction("init"); init != nil {
144			arg, err := pluginRun.Marshal()
145			if err != nil {
146				c.logger.Error("can't Marshal pluginRun", err, logger.NewLoggerPair("branch", cmd.branch))
147				continue
148			}
149			c.logger.Debug("init plugin", logger.NewLoggerPair("name", c.plugin.Log()), logger.NewLoggerPair("arg", arg))
150			if err := r.writeMemoryAndCall(c.module, init, malloc, c.repo.Name(), "false", string(arg)); err != nil {
151				c.logger.Error("can't init plugin", err, logger.NewLoggerPair("branch", cmd.branch), logger.NewLoggerPair("plugin", c.plugin.Log()))
152				continue
153			}
154		} else {
155			c.logger.Info("no init fn to call", logger.NewLoggerPair("plugin", r.plugin.Log()))
156		}
157
158		for _, com := range cmd.commits {
159			r.commit = &com
160			comMarshalled, err := MarshallOne(cmd.branch.Short(), com)
161			if err != nil {
162				c.logger.Error("can't marshall commit", err, logger.NewLoggerPair("branch", cmd.branch))
163				continue
164			}
165			c.logger.Debug("diff start commit", logger.NewLoggerPair("com", comMarshalled))
166			if startCommit != nil {
167				c.logger.Debug("startCommit", logger.NewLoggerPair("branch", cmd.branch.Short()), logger.NewLoggerPair("hash", com.hash.String()), logger.NewLoggerPair("message", com.message), logger.NewLoggerPair("date", com.date.Format(time.RFC3339)))
168				if err := r.writeMemoryAndCall(c.module, startCommit, malloc, comMarshalled); err != nil {
169					c.logger.Error("startCommit error", err, logger.NewLoggerPair("branch", cmd.branch), logger.NewLoggerPair("plugin", c.plugin.Log()))
170					continue
171				}
172			}
173
174			for _, f := range com.files {
175				c.logger.Debug("diff start commit file", logger.NewLoggerPair("file", f.Path), logger.NewLoggerPair("action", f.Action))
176				if pluginRun.glob.Match(f.Path) {
177					jsonFile, err := json.Marshal(f)
178					if err != nil {
179						c.logger.Error("can marshal file", err, logger.NewLoggerPair("branch", cmd.branch), logger.NewLoggerPair("plugin", c.plugin.Log()))
180						continue
181					}
182					if f.Action == pluginLib.FileActionTypeAdd && callOnAdd {
183						c.logger.Debug("add", logger.NewLoggerPair("confPath", pluginRun.Path), logger.NewLoggerPair("currentPath", f.Path))
184						// creation
185						r.writeMemoryAndCall(c.module, addFile, malloc, string(jsonFile))
186					} else {
187						if f.Action == pluginLib.FileActionTypeDel && callOnDel {
188							// deletion
189							r.writeMemoryAndCall(c.module, delFile, malloc, string(jsonFile))
190						} else if f.Action == pluginLib.FileActionTypeMod && callOnMod {
191							//modification
192							r.writeMemoryAndCall(c.module, modFile, malloc, string(jsonFile))
193						}
194					}
195				}
196			}
197
198			if endCommit != nil {
199				if err := r.writeMemoryAndCall(c.module, endCommit, malloc, comMarshalled); err != nil {
200					c.logger.Error("endCommit error", err, logger.NewLoggerPair("branch", cmd.branch), logger.NewLoggerPair("plugin", c.plugin.Log()))
201					continue
202				}
203			} else {
204				c.logger.Info("no endCommit fn to call", logger.NewLoggerPair("plugin", r.plugin.Log()))
205			}
206		}
207
208		if finish := c.module.ExportedFunction("finish"); finish != nil {
209			if _, err := finish.Call(ctx); err != nil {
210				c.logger.Error("finish error", err, logger.NewLoggerPair("branch", cmd.branch), logger.NewLoggerPair("plugin", c.plugin.Log()))
211				continue
212			}
213		} else {
214			c.logger.Info("no finish fn to call", logger.NewLoggerPair("plugin", r.plugin.Log()))
215		}
216	}
217	return nil
218}
219
220func (r *runtime) writeMemoryAndCall(module api.Module, toCall api.Function, malloc api.Function, message ...string) error {
221	_, err := r.writeMemoryAndCallWithRes(module, toCall, malloc, message...)
222	if err != nil {
223		return err
224	}
225	return nil
226}
227
228func (r *runtime) writeMemoryAndCallWithRes(module api.Module, toCall api.Function, malloc api.Function, message ...string) (uint64, error) {
229	params := make([]uint64, 0)
230	for _, m := range message {
231		size := uint64(len(m))
232
233		results, err := malloc.Call(r.ctx, size)
234		if err != nil {
235			return 0, oops.Wrapf(err, "can't malloc memory for %s with %s", toCall.Definition().Name(), m)
236		}
237		ptr := results[0]
238
239		// The pointer is a linear memory offset, which is where we write the name.
240		if !module.Memory().Write(uint32(ptr), []byte(m)) {
241			return 0, oops.Wrapf(err, "can't write memory")
242		}
243
244		params = append(params, ptr, size)
245	}
246
247	defer func() {
248		ptrSizes := make([]ptrSize, 0)
249		for i, d := range params {
250			if i%2 == 0 {
251				ptrSizes = append(ptrSizes, ptrSize{ptr: d, size: params[i+1]})
252			}
253		}
254		r.free(module, ptrSizes)
255	}()
256
257	if res, err := toCall.Call(r.ctx, params...); err != nil {
258		return 0, oops.With("method", toCall.Definition().ExportNames()).Wrapf(err, "can't call")
259	} else if len(res) > 0 {
260		return res[0], nil
261	}
262
263	return 0, nil
264}