Proof of concept #1

Merged
prskr merged 8 commits from feature/proof-of-concept into main 2023-03-10 10:17:07 +00:00
8 changed files with 208 additions and 12 deletions
Showing only changes of commit a71976604a - Show all commits

View file

@ -10,41 +10,57 @@ trigger:
- tag - tag
steps: steps:
- name: Setup
image: docker.io/golang:1.20-bullseye
network_mode: host
commands:
- apt-get update && apt-get install -y jq
- go mod download
- go install "github.com/vektra/mockery/v2@$(curl https://api.github.com/repos/vektra/mockery/releases | jq -r '. | first | .tag_name')"
- go generate ./...
volumes:
- name: go-cache
path: /go
- name: Lint - name: Lint
image: docker.io/golangci/golangci-lint:latest image: docker.io/golangci/golangci-lint:latest
environment: environment:
GO111MODULE: "on"
CGO_ENABLED: "0" CGO_ENABLED: "0"
GOMEMLIMIT: "1150MiB" GOMEMLIMIT: "1150MiB"
NITTER_BASE_ADDRESS: https://code.icb4dc0.de NITTER_BASE_ADDRESS: https://code.icb4dc0.de
NITTER_TOKEN: NITTER_TOKEN:
from_secret: gitea_token from_secret: gitea_token
volumes:
- name: go-cache
path: /go
commands: commands:
- mkdir out - mkdir out
- golangci-lint run --out-format json --issues-exit-code 0 > out/results.json - 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 - 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
depends_on:
- Setup
volumes:
- name: go-cache
path: /go
- name: Test - name: Test
image: docker.io/golang:1.20-bullseye image: docker.io/golang:1.20-bullseye
network_mode: host network_mode: host
environment: environment:
GO111MODULE: "on"
CGO_ENABLED: "1" CGO_ENABLED: "1"
DOCKER_HOST: tcp://localhost:2375 DOCKER_HOST: tcp://localhost:2375
volumes:
- name: go-cache
path: /go
commands: commands:
- go install gotest.tools/gotestsum@latest - go install gotest.tools/gotestsum@latest
- gotestsum --junitfile out/results.xml --format pkgname-and-test-fails -- -race -shuffle=on ./... - gotestsum --junitfile out/results.xml --format pkgname-and-test-fails -- -race -shuffle=on ./...
depends_on:
- Setup
volumes:
- name: go-cache
path: /go
- name: junit-reports - name: junit-reports
image: ghcr.io/rohit-gohri/drone-junit:v0 image: ghcr.io/rohit-gohri/drone-junit:v0
settings: settings:
paths: out/results.xml paths: out/results.xml
depends_on:
- Test
volumes: volumes:
- name: go-cache - name: go-cache

1
.gitignore vendored
View file

@ -11,6 +11,7 @@
# Test binary, built with `go test -c` # Test binary, built with `go test -c`
*.test *.test
mock_*_test.go
# Output of the go coverage tool, specifically when used with LiteIDE # Output of the go coverage tool, specifically when used with LiteIDE
*.out *.out

4
.mockery.yaml Normal file
View file

@ -0,0 +1,4 @@
inpackage: true
with-expecter: true
testonly: true
keeptree: false

View file

@ -25,7 +25,7 @@ func Gitea() *cobra.Command {
return err return err
} }
if err := cfg.Validate(); err != nil { if err = cfg.Validate(); err != nil {
return err return err
} }

View file

@ -0,0 +1,107 @@
package gitea_test
import (
"errors"
"testing"
"code.icb4dc0.de/prskr/nitter/nitters"
"code.icb4dc0.de/prskr/nitter/nitters/gitea"
)
func TestGiteaPRConfig_Validate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cfg gitea.GiteaPRConfig
errorMatcher func(err error) error
}{
{
name: "Missing namespace",
cfg: gitea.GiteaPRConfig{},
errorMatcher: expectError(nitters.ErrMissingNamespace),
},
{
name: "Missing repo",
cfg: gitea.GiteaPRConfig{
GiteaConfig: gitea.GiteaConfig{
Config: nitters.Config{
Namespace: "some",
},
},
},
errorMatcher: expectError(nitters.ErrMissingRepo),
},
{
name: "Missing base address",
cfg: gitea.GiteaPRConfig{
GiteaConfig: gitea.GiteaConfig{
Config: nitters.Config{
Namespace: "some",
Repo: "repo",
},
},
},
errorMatcher: expectError(gitea.ErrMissingBaseAddress),
},
{
name: "Missing token",
cfg: gitea.GiteaPRConfig{
GiteaConfig: gitea.GiteaConfig{
Config: nitters.Config{
Namespace: "some",
Repo: "repo",
},
BaseAddress: "http://my-forgejo:3000",
},
},
errorMatcher: expectError(gitea.ErrMissingToken),
},
{
name: "Missing pull request index",
cfg: gitea.GiteaPRConfig{
GiteaConfig: gitea.GiteaConfig{
Config: nitters.Config{
Namespace: "some",
Repo: "repo",
},
BaseAddress: "http://my-forgejo:3000",
Token: "sup3rS3cur3",
},
},
errorMatcher: expectError(gitea.ErrMissingPRIndex),
},
{
name: "All set",
cfg: gitea.GiteaPRConfig{
GiteaConfig: gitea.GiteaConfig{
Config: nitters.Config{
Namespace: "some",
Repo: "repo",
},
BaseAddress: "http://my-forgejo:3000",
Token: "sup3rS3cur3",
},
PRIndex: 11,
},
errorMatcher: expectError(nil),
},
}
for _, tc := range tests {
tt := tc
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if err := tt.errorMatcher(tt.cfg.Validate()); err != nil {
t.Errorf("Validate() error = %v", err)
}
})
}
}
func expectError(errorToIgnore error) func(err error) error {
return func(err error) error {
if errors.Is(err, errorToIgnore) {
return nil
}
return err
}
}

View file

@ -16,12 +16,12 @@ var (
_ nitters.Nitter = (*prNitter)(nil) _ nitters.Nitter = (*prNitter)(nil)
//go:embed templates/* //go:embed templates/*
templatesFs embed.FS templatesFS embed.FS
templates *template.Template templates *template.Template
) )
func init() { func init() {
if tmpl, err := template.New("").ParseFS(templatesFs, "templates/*.tmpl.md"); err != nil { if tmpl, err := template.New("issue_templates").ParseFS(templatesFS, "templates/*.tmpl.md"); err != nil {
panic(err) panic(err)
} else { } else {
templates = tmpl templates = tmpl
@ -35,6 +35,7 @@ func NewPRNitter(cli PullReviewCreator, cfg *GiteaPRConfig) *prNitter {
} }
} }
//go:generate mockery --name PullReviewCreator
type PullReviewCreator interface { type PullReviewCreator interface {
CreatePullReview(owner, repo string, index int64, opt gitea.CreatePullReviewOptions) (*gitea.PullReview, *gitea.Response, error) CreatePullReview(owner, repo string, index int64, opt gitea.CreatePullReviewOptions) (*gitea.PullReview, *gitea.Response, error)
} }

View file

@ -1,7 +1,7 @@
{{- if .Issue.Severity }} {{- if .Issue.Severity }}
Linter({{ .Issue.severity }}): {{ .Issue.FromLinter }} Linter({{ .Issue.severity }}): {{ .Issue.FromLinter }}
{{- else }} {{- else }}
Linter: {{ .Issue.FromLinter }} **Linter:** {{ .Issue.FromLinter }}
{{- end }} {{- end }}
{{ .Issue.Text }} {{ .Issue.Text }}

View file

@ -0,0 +1,67 @@
package templates_test
import (
"embed"
"io"
"testing"
"text/template"
"github.com/golangci/golangci-lint/pkg/report"
"github.com/golangci/golangci-lint/pkg/result"
)
var (
//go:embed *
templatesFS embed.FS
templates *template.Template
)
func init() {
if tmpl, err := template.New("issue_templates").ParseFS(templatesFS, "*.tmpl.md"); err != nil {
panic(err)
} else {
templates = tmpl
}
}
func TestRenderTemplates(t *testing.T) {
t.Parallel()
tests := []struct {
name string
templateName string
templateData any
}{
{
name: "Render issue_summary.tmpl.md",
templateName: "issue_summary.tmpl.md",
templateData: map[string]any{
"Report": &report.Data{
Error: "Some error",
},
"Issue": result.Issue{
FromLinter: "Sample",
Text: "Sample description",
},
},
},
{
name: "Render issue_comment.tmpl.md",
templateName: "issue_comment.tmpl.md",
templateData: map[string]any{
"Issue": result.Issue{
FromLinter: "Sample",
Text: "Sample description",
},
},
},
}
for _, tc := range tests {
tt := tc
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if err := templates.ExecuteTemplate(io.Discard, tt.templateName, tt.templateData); err != nil {
t.Errorf("Failed to render emplate: %v", err)
}
})
}
}