nitter/nitters/gitea/pr_nitter.go
Peter Kurfer c1c1667d9f
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is passing
feat(gitea): add lint-pr command
lint-pr runs golangci-lint, captures the result and uses it directly without temporary files
It also allows to automatically retrieve the PRs base commit and run golangci-lint only against changes in the current PR
2023-03-09 12:15:35 +01:00

93 lines
2.2 KiB
Go

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 --filename pull_review_creator.mock.go
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: p.cfg.ReviewState,
Body: templateBuf.String(),
Comments: make([]gitea.CreatePullReviewComment, 0, len(issues)),
}
if len(issues) == 0 {
pullReviewOptions.State = gitea.ReviewStateApproved
} else {
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
}