feat: run PoC in pipeline
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Peter 2023-03-08 16:13:49 +01:00
parent d0e2850445
commit 6507d44725
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
13 changed files with 167 additions and 46 deletions

View file

@ -16,11 +16,16 @@ steps:
GO111MODULE: "on" GO111MODULE: "on"
CGO_ENABLED: "0" CGO_ENABLED: "0"
GOMEMLIMIT: "1150MiB" GOMEMLIMIT: "1150MiB"
NITTER_BASE_ADDRESS: https://code.icb4dc0.de
NITTER_TOKEN:
from_secret: gitea_token
volumes: volumes:
- name: go-cache - name: go-cache
path: /go path: /go
commands: 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 - name: Test
image: docker.io/golang:1.20-bullseye image: docker.io/golang:1.20-bullseye
@ -45,7 +50,7 @@ volumes:
- name: go-cache - name: go-cache
temp: { } temp: { }
--- ---
kind: pipeline kind: pipeline
type: docker type: docker
name: housekeeping name: housekeeping

1
.gitignore vendored
View file

@ -17,6 +17,7 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
out/
# Go workspace file # Go workspace file
go.work go.work

View file

@ -5,7 +5,7 @@ linters-settings:
lines: 100 lines: 100
statements: 50 statements: 50
gci: gci:
local-prefixes: code.icb4dc0.de/prskr/nurse local-prefixes: code.icb4dc0.de/prskr/nitter
goconst: goconst:
min-len: 2 min-len: 2
min-occurrences: 2 min-occurrences: 2
@ -28,7 +28,7 @@ linters-settings:
gocyclo: gocyclo:
min-complexity: 15 min-complexity: 15
goimports: goimports:
local-prefixes: code.1533b4dc0.de/prskr/nurse local-prefixes: code.icb4dc0.de/prskr/nitter
golint: golint:
min-confidence: 0 min-confidence: 0
gomnd: gomnd:

6
go.mod
View file

@ -6,12 +6,11 @@ require (
code.gitea.io/sdk/gitea v0.15.1 code.gitea.io/sdk/gitea v0.15.1
github.com/golangci/golangci-lint v1.51.2 github.com/golangci/golangci-lint v1.51.2
github.com/spf13/cobra v1.6.1 github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.12.0 github.com/spf13/viper v1.12.0
github.com/urfave/cli/v2 v2.25.0
) )
require ( require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.14.1 // indirect github.com/fatih/color v1.14.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // 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 v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // 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/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/afero v1.8.2 // indirect github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.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/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect github.com/stretchr/testify v1.8.1 // indirect
github.com/subosito/gotenv v1.4.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/mod v0.8.0 // indirect
golang.org/x/sys v0.5.0 // indirect golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.6.0 // indirect golang.org/x/text v0.6.0 // indirect

6
go.sum
View file

@ -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-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-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/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/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.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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/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.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= 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/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 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= 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/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 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= 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.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/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= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

View file

@ -25,6 +25,10 @@ func Gitea() *cobra.Command {
return err return err
} }
if err := cfg.Validate(); err != nil {
return err
}
report, issues, err := ReadResultsFile(cmd.Flag("result-file").Value.String()) report, issues, err := ReadResultsFile(cmd.Flag("result-file").Value.String())
if err != nil { if err != nil {
return err 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) giteaCmd.AddCommand(pr)

View file

@ -11,9 +11,7 @@ import (
"github.com/golangci/golangci-lint/pkg/result" "github.com/golangci/golangci-lint/pkg/result"
) )
var ( var ErrNoResult = errors.New("could not read result")
ErrNoResult = errors.New("could not read result")
)
func ReadResultsFile(filePath string) (report *report.Data, issues []result.Issue, err error) { func ReadResultsFile(filePath string) (report *report.Data, issues []result.Issue, err error) {
if filePath == "" { if filePath == "" {

View file

@ -11,12 +11,10 @@ import (
"code.icb4dc0.de/prskr/nitter/internal/commands" "code.icb4dc0.de/prskr/nitter/internal/commands"
) )
var ( var root = &cobra.Command{
root = &cobra.Command{
Use: "nitter", Use: "nitter",
TraverseChildren: true, TraverseChildren: true,
} }
)
func main() { func main() {
root.PersistentFlags().StringP("namespace", "n", "", "Namespace a.k.a. organization/owner/group of the repository [$NITTER_NAMESPACE]") root.PersistentFlags().StringP("namespace", "n", "", "Namespace a.k.a. organization/owner/group of the repository [$NITTER_NAMESPACE]")

View file

@ -1,6 +1,25 @@
package nitters package nitters
import "errors"
var (
ErrMissingNamespace = errors.New("namespace is required")
ErrMissingRepo = errors.New("repo name is required")
)
type Config struct { type Config struct {
Namespace string `mapstructure:"namespace"` Namespace string `mapstructure:"namespace"`
Repo string `mapstructure:"repo"` Repo string `mapstructure:"repo"`
} }
func (c Config) Validate() error {
if c.Namespace == "" {
return ErrMissingNamespace
}
if c.Repo == "" {
return ErrMissingRepo
}
return nil
}

View file

@ -1,16 +1,52 @@
package gitea package gitea
import ( import (
"errors"
"code.icb4dc0.de/prskr/nitter/nitters" "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 { type GiteaConfig struct {
nitters.Config `mapstructure:",squash"` nitters.Config `mapstructure:",squash"`
BaseAddress string `mapstructure:"base-address"` BaseAddress string `mapstructure:"base-address"`
Token string `mapstructure:"token"` 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 { type GiteaPRConfig struct {
GiteaConfig `mapstructure:",squash"` GiteaConfig `mapstructure:",squash"`
PRIndex int64 `mapstructure:"pull-index"` 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
}

View file

@ -1,8 +1,9 @@
package gitea package gitea
import ( import (
"fmt" "bytes"
"strings" "embed"
"text/template"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"github.com/golangci/golangci-lint/pkg/report" "github.com/golangci/golangci-lint/pkg/report"
@ -11,43 +12,73 @@ import (
"code.icb4dc0.de/prskr/nitter/nitters" "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{ return &prNitter{
Client: cli, PullReviewCreator: cli,
cfg: cfg, cfg: cfg,
} }
} }
type PullReviewCreator interface {
CreatePullReview(owner, repo string, index int64, opt gitea.CreatePullReviewOptions) (*gitea.PullReview, *gitea.Response, error)
}
type prNitter struct { type prNitter struct {
*gitea.Client PullReviewCreator
cfg *GiteaPRConfig cfg *GiteaPRConfig
} }
func (p prNitter) Report(report *report.Data, issues []result.Issue) error { func (p prNitter) Report(report *report.Data, issues []result.Issue) error {
warningsBuilder := strings.Builder{} templateBuf := bytes.Buffer{}
for i := range report.Warnings {
warningsBuilder.WriteString(fmt.Sprintf("\t- %s (%s)", report.Warnings[i].Text, report.Warnings[i].Tag)) 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{ pullReviewOptions := gitea.CreatePullReviewOptions{
State: "comment", State: "comment",
Body: fmt.Sprintf(`golangci-lint review results: Body: templateBuf.String(),
Error: %s
Warnings:
%s
`, report.Error, warningsBuilder.String()),
Comments: make([]gitea.CreatePullReviewComment, 0, len(issues)), Comments: make([]gitea.CreatePullReviewComment, 0, len(issues)),
} }
templateBuf.Reset()
for i := range issues { 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{ pullReviewOptions.Comments = append(pullReviewOptions.Comments, gitea.CreatePullReviewComment{
Path: issue.Pos.Filename, Path: issues[i].Pos.Filename,
Body: "", Body: templateBuf.String(),
NewLineNum: int64(issue.Pos.Line), NewLineNum: int64(issues[i].Pos.Line),
}) })
templateBuf.Reset()
} }
_, _, err := p.CreatePullReview(p.cfg.Namespace, p.cfg.Repo, p.cfg.PRIndex, pullReviewOptions) _, _, err := p.CreatePullReview(p.cfg.Namespace, p.cfg.Repo, p.cfg.PRIndex, pullReviewOptions)

View 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 }}

View 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 }}