Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to improve maintainability #145

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23'
go-version: 'stable'

- name: Check out code into the Go module directory
uses: actions/checkout@v4
Expand Down
26 changes: 12 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
FROM golang:1.23.3 AS build-env
FROM --platform=$BUILDPLATFORM golang:1.23.3 AS build-env

ARG ACTION_VERSION=unknown
ARG REVIVE_VERSION=v1.5.0
ARG VERSION

ENV CGO_ENABLED=0
ARG TARGETOS
ARG TARGETARCH

RUN go install -v -ldflags="-X 'github.com/mgechev/revive/cli.version=${REVIVE_VERSION}'" \
github.com/mgechev/revive@${REVIVE_VERSION}
ENV CGO_ENABLED=0

WORKDIR /tmp/github.com/morphy2k/revive-action
WORKDIR /src
COPY . .

RUN go install -ldflags="-X 'main.version=${ACTION_VERSION}'"
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH \
go build -ldflags="-X 'main.version=${VERSION}'"

FROM alpine:3.20.3
FROM ghcr.io/mgechev/revive:1.5.0

LABEL repository="https://github.com/morphy2k/revive-action"
LABEL homepage="https://github.com/morphy2k/revive-action"
LABEL maintainer="Markus Wiegand <mail@morphy.dev>"

LABEL org.opencontainers.image.title="Revive Action"
LABEL org.opencontainers.image.source="https://github.com/morphy2k/revive-action"
LABEL org.opencontainers.image.description="GitHub Action that runs Revive on your Go code"
LABEL org.opencontainers.image.licenses=MIT
Expand All @@ -28,9 +29,6 @@ LABEL com.github.actions.description="GitHub Action that runs Revive on your Go
LABEL com.github.actions.icon="code"
LABEL com.github.actions.color="blue"

COPY --from=build-env ["/go/bin/revive", "/go/bin/revive-action", "/bin/"]
COPY --from=build-env /tmp/github.com/morphy2k/revive-action/entrypoint.sh /

RUN apk add --no-cache bash gawk
COPY --from=build-env ["/src/revive-action", "/revive-action"]

ENTRYPOINT ["/entrypoint.sh"]
ENTRYPOINT ["/revive-action"]
25 changes: 0 additions & 25 deletions entrypoint.sh

This file was deleted.

40 changes: 40 additions & 0 deletions failure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"
"go/token"
"strings"
)

type failure struct {
Position struct {
Start token.Position
End token.Position
}
Failure string
Severity string
}

func (f *failure) Format() string {
var sb strings.Builder

if f.Severity == "warning" {
sb.WriteString("::warning ")
} else {
sb.WriteString("::error ")
}

fmt.Fprintf(&sb, "file=%s,line=%d,endLine=%d,col=%d,endColumn=%d::%s",
f.Position.Start.Filename, f.Position.Start.Line, f.Position.End.Line, f.Position.Start.Column, f.Position.End.Column, f.Failure)

return sb.String()
}

type statistics struct {
Total, Warnings, Errors int
}

func (s statistics) String() string {
return fmt.Sprintf("%d failures (%d warnings, %d errors)",
s.Total, s.Warnings, s.Errors)
}
54 changes: 54 additions & 0 deletions failure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"go/token"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFormat(t *testing.T) {
tests := []struct {
name string
failure failure
expected string
}{
{
name: "error severity",
failure: failure{
Position: struct {
Start token.Position
End token.Position
}{
Start: token.Position{Filename: "main.go", Line: 10, Column: 5},
End: token.Position{Line: 10, Column: 10},
},
Failure: "some error",
Severity: "error",
},
expected: "::error file=main.go,line=10,endLine=10,col=5,endColumn=10::some error",
},
{
name: "warning severity",
failure: failure{
Position: struct {
Start token.Position
End token.Position
}{
Start: token.Position{Filename: "main.go", Line: 20, Column: 15},
End: token.Position{Line: 20, Column: 20},
},
Failure: "some warning",
Severity: "warning",
},
expected: "::warning file=main.go,line=20,endLine=20,col=15,endColumn=20::some warning",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.failure.Format()
assert.Equal(t, tt.expected, result)
})
}
}
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/morphy2k/revive-action

go 1.16
go 1.23

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
37 changes: 37 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"os"
"strings"
)

const (
defaultPath = "./..."
)

type input struct {
exclude []string
config string
path string
}

func parseInput() *input {
input := &input{
exclude: make([]string, 0),
path: defaultPath,
}

if v, ok := os.LookupEnv("INPUT_EXCLUDE"); ok {
input.exclude = strings.Split(v, ";")
}

if v, ok := os.LookupEnv("INPUT_CONFIG"); ok {
input.config = v
}

if v, ok := os.LookupEnv("INPUT_PATH"); ok {
input.path = v
}

return input
}
96 changes: 96 additions & 0 deletions input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseInput(t *testing.T) {
tests := []struct {
name string
envVars map[string]string
want *input
}{
{
name: "default values",
envVars: map[string]string{},
want: &input{
exclude: []string{},
config: "",
path: defaultPath,
},
},
{
name: "exclude with single value",
envVars: map[string]string{
"INPUT_EXCLUDE": "test",
},
want: &input{
exclude: []string{"test"},
config: "",
path: defaultPath,
},
},
{
name: "exclude with multiple values",
envVars: map[string]string{
"INPUT_EXCLUDE": "test1;test2;test3",
},
want: &input{
exclude: []string{"test1", "test2", "test3"},
config: "",
path: defaultPath,
},
},
{
name: "config with value",
envVars: map[string]string{
"INPUT_CONFIG": "config.yaml",
},
want: &input{
exclude: []string{},
config: "config.yaml",
path: defaultPath,
},
},
{
name: "custom path",
envVars: map[string]string{
"INPUT_PATH": "./custom",
},
want: &input{
exclude: []string{},
config: "",
path: "./custom",
},
},
{
name: "all values set",
envVars: map[string]string{
"INPUT_EXCLUDE": "test1;test2",
"INPUT_CONFIG": "config.yaml",
"INPUT_PATH": "./custom",
},
want: &input{
exclude: []string{"test1", "test2"},
config: "config.yaml",
path: "./custom",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Clearenv()
for k, v := range tt.envVars {
os.Setenv(k, v)
}
defer os.Clearenv()

got := parseInput()
assert.Equal(t, tt.want, got)
})
}
}
Loading
Loading