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: MIT
 4
 5package model
 6
 7import "github.com/tidwall/gjson"
 8
 9type ReportLevel string
10
11const (
12	ReportLevelInfo    ReportLevel = "info"
13	ReportLevelWarning ReportLevel = "warning"
14	ReportLevelError   ReportLevel = "error"
15)
16
17type ReportToGitroot struct {
18	Level   ReportLevel `json:"level"`
19	Content []string    `json:"content"`
20}
21
22type Report struct {
23	FromPlugin string      `json:"fromPlugin"`
24	FromBranch string      `json:"fromBranch"`
25	FromCommit string      `json:"fromCommit"`
26	Level      ReportLevel `json:"level"`
27	Content    []string    `json:"content"`
28}
29
30func ReportFromString(report string) Report {
31	jsonReport := gjson.Parse(report)
32	res := Report{
33		FromPlugin: jsonReport.Get("fromPlugin").String(),
34		FromBranch: jsonReport.Get("fromBranch").String(),
35		FromCommit: jsonReport.Get("fromCommit").String(),
36		Level:      ReportLevelInfo,
37		Content:    []string{},
38	}
39	switch jsonReport.Get("level").String() {
40	case "info":
41		res.Level = ReportLevelInfo
42	case "warning":
43		res.Level = ReportLevelWarning
44	case "error":
45		res.Level = ReportLevelError
46	}
47	for _, c := range jsonReport.Get("content").Array() {
48		res.Content = append(res.Content, c.String())
49	}
50	return res
51}