Proof of concept #1
13 changed files with 167 additions and 46 deletions
|
@ -16,11 +16,16 @@ steps:
|
|||
GO111MODULE: "on"
|
||||
CGO_ENABLED: "0"
|
||||
GOMEMLIMIT: "1150MiB"
|
||||
NITTER_BASE_ADDRESS: https://code.icb4dc0.de
|
||||
NITTER_TOKEN:
|
||||
from_secret: gitea_token
|
||||
volumes:
|
||||
- name: go-cache
|
||||
path: /go
|
||||
commands:
|
||||
- golangci-lint run -v
|
||||
- mkdir out
|
||||
- golangci-lint run --out-format json --issues-exit-code 0 > out/results.json
|
||||
- if [ -n "$DRONE_PULL_REQUEST}" ]; then go run main.go gitea pr --namespace "$${DRONE_REPO_NAMESPACE}" --repo "$${DRONE_REPO_NAME}" --result-file out/results.json --pull-index "$${DRONE_PULL_REQUEST}"; fi
|
||||
|
||||
- name: Test
|
||||
image: docker.io/golang:1.20-bullseye
|
||||
|
@ -45,7 +50,7 @@ volumes:
|
|||
- name: go-cache
|
||||
temp: { }
|
||||
|
||||
---
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: housekeeping
|
||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -17,6 +17,7 @@
|
|||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
out/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
|
|
@ -5,7 +5,7 @@ linters-settings:
|
|||
lines: 100
|
||||
statements: 50
|
||||
gci:
|
||||
local-prefixes: code.icb4dc0.de/prskr/nurse
|
||||
local-prefixes: code.icb4dc0.de/prskr/nitter
|
||||
goconst:
|
||||
min-len: 2
|
||||
min-occurrences: 2
|
||||
|
@ -28,7 +28,7 @@ linters-settings:
|
|||
gocyclo:
|
||||
min-complexity: 15
|
||||
goimports:
|
||||
local-prefixes: code.1533b4dc0.de/prskr/nurse
|
||||
local-prefixes: code.icb4dc0.de/prskr/nitter
|
||||
golint:
|
||||
min-confidence: 0
|
||||
gomnd:
|
||||
|
|
6
go.mod
6
go.mod
|
@ -6,12 +6,11 @@ require (
|
|||
code.gitea.io/sdk/gitea v0.15.1
|
||||
github.com/golangci/golangci-lint v1.51.2
|
||||
github.com/spf13/cobra v1.6.1
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/spf13/viper v1.12.0
|
||||
github.com/urfave/cli/v2 v2.25.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/fatih/color v1.14.1 // indirect
|
||||
github.com/fsnotify/fsnotify v1.5.4 // indirect
|
||||
|
@ -26,16 +25,13 @@ require (
|
|||
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/sirupsen/logrus v1.9.0 // indirect
|
||||
github.com/spf13/afero v1.8.2 // indirect
|
||||
github.com/spf13/cast v1.5.0 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/stretchr/objx v0.5.0 // indirect
|
||||
github.com/stretchr/testify v1.8.1 // indirect
|
||||
github.com/subosito/gotenv v1.4.1 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||
golang.org/x/mod v0.8.0 // indirect
|
||||
golang.org/x/sys v0.5.0 // indirect
|
||||
golang.org/x/text v0.6.0 // indirect
|
||||
|
|
6
go.sum
6
go.sum
|
@ -49,7 +49,6 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
|
|||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
||||
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
|
@ -167,7 +166,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
|||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
|
@ -197,10 +195,6 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
|
|||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
|
||||
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
|
||||
github.com/urfave/cli/v2 v2.25.0 h1:ykdZKuQey2zq0yin/l7JOm9Mh+pg72ngYMeB0ABn6q8=
|
||||
github.com/urfave/cli/v2 v2.25.0/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
||||
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
|
|
|
@ -25,6 +25,10 @@ func Gitea() *cobra.Command {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
report, issues, err := ReadResultsFile(cmd.Flag("result-file").Value.String())
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -41,7 +45,12 @@ func Gitea() *cobra.Command {
|
|||
},
|
||||
}
|
||||
|
||||
pr.Flags().Int64P("pull-index", "i", -1, "PR index to add reviews to - note, this is not the ID of the PR but its number [$NITTER_PULL_INDEX]")
|
||||
pr.Flags().Int64P(
|
||||
"pull-index",
|
||||
"i",
|
||||
-1,
|
||||
"PR index to add reviews to - note, this is not the ID of the PR but its number [$NITTER_PULL_INDEX]",
|
||||
)
|
||||
|
||||
giteaCmd.AddCommand(pr)
|
||||
|
||||
|
|
|
@ -11,9 +11,7 @@ import (
|
|||
"github.com/golangci/golangci-lint/pkg/result"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNoResult = errors.New("could not read result")
|
||||
)
|
||||
var ErrNoResult = errors.New("could not read result")
|
||||
|
||||
func ReadResultsFile(filePath string) (report *report.Data, issues []result.Issue, err error) {
|
||||
if filePath == "" {
|
||||
|
|
6
main.go
6
main.go
|
@ -11,12 +11,10 @@ import (
|
|||
"code.icb4dc0.de/prskr/nitter/internal/commands"
|
||||
)
|
||||
|
||||
var (
|
||||
root = &cobra.Command{
|
||||
var root = &cobra.Command{
|
||||
Use: "nitter",
|
||||
TraverseChildren: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
func main() {
|
||||
root.PersistentFlags().StringP("namespace", "n", "", "Namespace a.k.a. organization/owner/group of the repository [$NITTER_NAMESPACE]")
|
||||
|
|
|
@ -1,6 +1,25 @@
|
|||
package nitters
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrMissingNamespace = errors.New("namespace is required")
|
||||
ErrMissingRepo = errors.New("repo name is required")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Namespace string `mapstructure:"namespace"`
|
||||
Repo string `mapstructure:"repo"`
|
||||
}
|
||||
|
||||
func (c Config) Validate() error {
|
||||
if c.Namespace == "" {
|
||||
return ErrMissingNamespace
|
||||
}
|
||||
|
||||
if c.Repo == "" {
|
||||
return ErrMissingRepo
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,16 +1,52 @@
|
|||
package gitea
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"code.icb4dc0.de/prskr/nitter/nitters"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrMissingBaseAddress = errors.New("API base address is required")
|
||||
ErrMissingToken = errors.New("API token is required")
|
||||
ErrMissingPRIndex = errors.New("PR index is required")
|
||||
)
|
||||
|
||||
type GiteaConfig struct {
|
||||
nitters.Config `mapstructure:",squash"`
|
||||
BaseAddress string `mapstructure:"base-address"`
|
||||
Token string `mapstructure:"token"`
|
||||
}
|
||||
|
||||
func (gc GiteaConfig) Validate() error {
|
||||
if err := gc.Config.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if gc.BaseAddress == "" {
|
||||
return ErrMissingBaseAddress
|
||||
}
|
||||
|
||||
if gc.Token == "" {
|
||||
return ErrMissingToken
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type GiteaPRConfig struct {
|
||||
GiteaConfig `mapstructure:",squash"`
|
||||
PRIndex int64 `mapstructure:"pull-index"`
|
||||
}
|
||||
|
||||
func (gpc GiteaPRConfig) Validate() error {
|
||||
if err := gpc.GiteaConfig.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if gpc.PRIndex < 0 {
|
||||
return ErrMissingPRIndex
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package gitea
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"bytes"
|
||||
"embed"
|
||||
"text/template"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/golangci/golangci-lint/pkg/report"
|
||||
|
@ -11,43 +12,73 @@ import (
|
|||
"code.icb4dc0.de/prskr/nitter/nitters"
|
||||
)
|
||||
|
||||
var _ nitters.Nitter = (*prNitter)(nil)
|
||||
var (
|
||||
_ nitters.Nitter = (*prNitter)(nil)
|
||||
|
||||
func NewPRNitter(cli *gitea.Client, cfg *GiteaPRConfig) *prNitter {
|
||||
//go:embed templates/*
|
||||
templatesFs embed.FS
|
||||
templates *template.Template
|
||||
)
|
||||
|
||||
func init() {
|
||||
if tmpl, err := template.New("").ParseFS(templatesFs, "templates/*.tmpl.md"); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
templates = tmpl
|
||||
}
|
||||
}
|
||||
|
||||
func NewPRNitter(cli PullReviewCreator, cfg *GiteaPRConfig) *prNitter {
|
||||
return &prNitter{
|
||||
Client: cli,
|
||||
PullReviewCreator: cli,
|
||||
cfg: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
type PullReviewCreator interface {
|
||||
CreatePullReview(owner, repo string, index int64, opt gitea.CreatePullReviewOptions) (*gitea.PullReview, *gitea.Response, error)
|
||||
}
|
||||
|
||||
type prNitter struct {
|
||||
*gitea.Client
|
||||
PullReviewCreator
|
||||
cfg *GiteaPRConfig
|
||||
}
|
||||
|
||||
func (p prNitter) Report(report *report.Data, issues []result.Issue) error {
|
||||
warningsBuilder := strings.Builder{}
|
||||
for i := range report.Warnings {
|
||||
warningsBuilder.WriteString(fmt.Sprintf("\t- %s (%s)", report.Warnings[i].Text, report.Warnings[i].Tag))
|
||||
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: fmt.Sprintf(`golangci-lint review results:
|
||||
Error: %s
|
||||
Warnings:
|
||||
%s
|
||||
`, report.Error, warningsBuilder.String()),
|
||||
Body: templateBuf.String(),
|
||||
Comments: make([]gitea.CreatePullReviewComment, 0, len(issues)),
|
||||
}
|
||||
|
||||
templateBuf.Reset()
|
||||
|
||||
for i := range issues {
|
||||
issue := issues[i]
|
||||
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: issue.Pos.Filename,
|
||||
Body: "",
|
||||
NewLineNum: int64(issue.Pos.Line),
|
||||
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)
|
||||
|
|
18
nitters/gitea/templates/issue_comment.tmpl.md
Normal file
18
nitters/gitea/templates/issue_comment.tmpl.md
Normal file
|
@ -0,0 +1,18 @@
|
|||
{{- if .Issue.Severity }}
|
||||
Linter({{ .Issue.severity }}): {{ .Issue.FromLinter }}
|
||||
{{- else }}
|
||||
Linter: {{ .Issue.FromLinter }}
|
||||
{{- end }}
|
||||
|
||||
{{ .Issue.Text }}
|
||||
|
||||
{{- if .Issue.Replacement }}
|
||||
|
||||
**Suggestion:**
|
||||
|
||||
```go
|
||||
{{- range $i, $replacement := .Issue.Replacement.NewLines }}
|
||||
{{ $replacement }}
|
||||
{{- end }}
|
||||
```
|
||||
{{- end }}
|
16
nitters/gitea/templates/issue_summary.tmpl.md
Normal file
16
nitters/gitea/templates/issue_summary.tmpl.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
golangci-lint review results:
|
||||
{{- if .Report.Error }}
|
||||
|
||||
**Error:** {{ .Report.Error }}
|
||||
{{- end }}
|
||||
{{- if .Report.Warnings }}
|
||||
|
||||
**Warnings:**
|
||||
{{- range $i, $warning := .Report.Warnings }}
|
||||
- {{ $warning.Text }}{{ if $warning.Tag }} ({{ $warning.Tag }}){{ end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- if .Issues }}
|
||||
|
||||
Found {{ len .Issues }} issues.
|
||||
{{- end }}
|
Loading…
Reference in a new issue