package gitea import ( "bytes" "embed" "text/template" "code.gitea.io/sdk/gitea" "github.com/golangci/golangci-lint/pkg/report" "github.com/golangci/golangci-lint/pkg/result" "code.icb4dc0.de/prskr/nitter/nitters" ) var ( _ nitters.Nitter = (*prNitter)(nil) //go:embed templates/* templatesFS embed.FS templates *template.Template ) func init() { if tmpl, err := template.New("issue_templates").ParseFS(templatesFS, "templates/*.tmpl.md"); err != nil { panic(err) } else { templates = tmpl } } func NewPRNitter(cli PullReviewCreator, cfg *GiteaPRConfig) *prNitter { return &prNitter{ PullReviewCreator: cli, cfg: cfg, } } //go:generate mockery --name PullReviewCreator type PullReviewCreator interface { CreatePullReview(owner, repo string, index int64, opt gitea.CreatePullReviewOptions) (*gitea.PullReview, *gitea.Response, error) } type prNitter struct { PullReviewCreator cfg *GiteaPRConfig } func (p prNitter) Report(report *report.Data, issues []result.Issue) error { templateBuf := bytes.Buffer{} summaryData := map[string]any{ "Report": report, "Issues": issues, } if err := templates.ExecuteTemplate(&templateBuf, "issue_summary.tmpl.md", summaryData); err != nil { return err } pullReviewOptions := gitea.CreatePullReviewOptions{ State: "comment", Body: templateBuf.String(), Comments: make([]gitea.CreatePullReviewComment, 0, len(issues)), } templateBuf.Reset() for i := range issues { templateData := map[string]any{ "Issue": issues[i], } if err := templates.ExecuteTemplate(&templateBuf, "issue_comment.tmpl.md", templateData); err != nil { return err } pullReviewOptions.Comments = append(pullReviewOptions.Comments, gitea.CreatePullReviewComment{ Path: issues[i].Pos.Filename, Body: templateBuf.String(), NewLineNum: int64(issues[i].Pos.Line), }) templateBuf.Reset() } _, _, err := p.CreatePullReview(p.cfg.Namespace, p.cfg.Repo, p.cfg.PRIndex, pullReviewOptions) if err != nil { return err } return nil }