diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 3b35cf1..c697ecf 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -8,8 +8,39 @@ jobs: name: Build runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Build docker image env: REF: ${{ github. sha }} run: docker build -t radix-github-webhook:${REF##*/} . + + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 2 + - uses: actions/setup-go@v4 + with: + go-version: '1.21' + - name: Install dependencies + run: go mod download + - name: Install GolangCI Lint + run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2 + + - name: golangci-lint + run: golangci-lint run --timeout=30m --max-same-issues=0 --out-format=github-actions + + test: + name: Unit Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v4 + with: + go-version: '1.21' + - name: Install dependencies + run: go mod download + - name: Run Tests + run: go test -cover `go list ./...` diff --git a/Dockerfile b/Dockerfile index 8673194..32848d5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,8 +4,7 @@ ENV GO111MODULE=on RUN apk update && \ apk add ca-certificates && \ - apk add --no-cache gcc musl-dev && \ - go install honnef.co/go/tools/cmd/staticcheck@2023.1.3 + apk add --no-cache gcc musl-dev WORKDIR /go/src/github.com/equinor/radix-github-webhook/ @@ -14,19 +13,15 @@ COPY go.mod go.sum ./ RUN go mod download COPY . . -# run tests and linting -RUN staticcheck ./... && \ - go vet ./... && \ - CGO_ENABLED=0 GOOS=linux go test `go list ./...` # build -RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -a -installsuffix cgo -o /usr/local/bin/radix-github-webhook +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -a -installsuffix cgo -o /radix-github-webhook RUN addgroup -S -g 1000 radix-github-webhook RUN adduser -S -u 1000 -G radix-github-webhook radix-github-webhook FROM scratch COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ -COPY --from=builder /usr/local/bin/radix-github-webhook /usr/local/bin/radix-github-webhook +COPY --from=builder /radix-github-webhook /usr/local/bin/radix-github-webhook COPY --from=builder /etc/passwd /etc/passwd EXPOSE 3001 USER 1000 diff --git a/Makefile b/Makefile index ba0a1e1..c29af52 100644 --- a/Makefile +++ b/Makefile @@ -18,34 +18,12 @@ build: $(BINS) test: go test -cover `go list ./...` -staticcheck: - staticcheck ./... - -.PHONY: deploy -deploy: - # Download deploy key + webhook shared secret - az keyvault secret download -f radix-github-webhook-radixregistration-values.yaml -n radix-github-webhook-radixregistration-values --vault-name radix-boot-dev-vault - # Add (also refreshes access tokens) and update helm repo - az acr helm repo add --name radixdev && helm repo update - # Install RR referring to the downloaded secrets - helm upgrade --install radix-github-webhook -f radix-github-webhook-radixregistration-values.yaml radixdev/radix-registration - # Delete secret file to avvoid being checked in - rm radix-github-webhook-radixregistration-values.yaml - # Allow operator to pick up RR. TODO should be handled with waiting for app namespace - sleep 5 - # Create pipeline job - helm upgrade --install radix-pipeline-github-webhook radixdev/radix-pipeline-invocation \ - --set name="radix-github-webhook" \ - --set cloneURL="git@github.com:equinor/radix-github-webhook.git" \ - --set cloneBranch="master" - -.PHONY: undeploy -undeploy: - helm del --purge radix-github-webhook - helm del --purge radix-pipeline-github-webhook +.PHONY: lint +lint: bootstrap + golangci-lint run --max-same-issues 0 .PHONY: $(BINS) -$(BINS): vendor +$(BINS): go build -ldflags '$(LDFLAGS)' -o bin/$@ . .PHONY: docker-build @@ -60,26 +38,22 @@ docker-push: $(addsuffix -push,$(IMAGES)) %-push: docker push $(DOCKER_REGISTRY)/$*:$(IMAGE_TAG) -HAS_GOMETALINTER := $(shell command -v gometalinter;) -HAS_DEP := $(shell command -v dep;) -HAS_GIT := $(shell command -v git;) +.PHONY: mocks +mocks: bootstrap + mockgen -source ./radix/api_server.go -destination ./radix/api_server_mock.go -package radix + +HAS_SWAGGER := $(shell command -v swagger;) +HAS_GOLANGCI_LINT := $(shell command -v golangci-lint;) +HAS_MOCKGEN := $(shell command -v mockgen;) -vendor: -ifndef HAS_GIT - $(error You must install git) +.PHONY: bootstrap +bootstrap: +ifndef HAS_SWAGGER + go install github.com/go-swagger/go-swagger/cmd/swagger@v0.30.5 endif -ifndef HAS_DEP - go get -u github.com/golang/dep/cmd/dep +ifndef HAS_GOLANGCI_LINT + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2 endif -ifndef HAS_GOMETALINTER - go get -u github.com/alecthomas/gometalinter - gometalinter --install +ifndef HAS_MOCKGEN + go install github.com/golang/mock/mockgen@v1.6.0 endif - dep ensure - -.PHONY: bootstrap -bootstrap: vendor - -.PHONY: mocks -mocks: - mockgen -source ./radix/api_server.go -destination ./radix/api_server_mock.go -package radix diff --git a/go.mod b/go.mod index 54edcf7..847c7c2 100644 --- a/go.mod +++ b/go.mod @@ -8,34 +8,33 @@ require ( github.com/golang-jwt/jwt/v4 v4.5.0 github.com/golang/mock v1.6.0 github.com/google/go-github/v53 v53.2.0 - github.com/gorilla/mux v1.8.0 + github.com/gorilla/mux v1.8.1 github.com/pkg/errors v0.9.1 - github.com/prometheus/client_golang v1.16.0 + github.com/prometheus/client_golang v1.18.0 github.com/sirupsen/logrus v1.9.3 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.4 - golang.org/x/oauth2 v0.8.0 + golang.org/x/oauth2 v0.15.0 ) require ( - github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect + github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect - github.com/cloudflare/circl v1.3.3 // indirect + github.com/cloudflare/circl v1.3.7 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/kr/text v0.2.0 // indirect - github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect + github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_model v0.3.0 // indirect - github.com/prometheus/common v0.42.0 // indirect - github.com/prometheus/procfs v0.10.1 // indirect + github.com/prometheus/client_model v0.5.0 // indirect + github.com/prometheus/common v0.45.0 // indirect + github.com/prometheus/procfs v0.12.0 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect - golang.org/x/crypto v0.14.0 // indirect - golang.org/x/net v0.17.0 // indirect - golang.org/x/sys v0.13.0 // indirect - google.golang.org/appengine v1.6.7 // indirect - google.golang.org/protobuf v1.30.0 // indirect + golang.org/x/crypto v0.17.0 // indirect + golang.org/x/sys v0.15.0 // indirect + google.golang.org/appengine v1.6.8 // indirect + google.golang.org/protobuf v1.32.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index c8a3970..d0cdff5 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,13 @@ -github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA= -github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g= +github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c h1:kMFnB0vCcX7IL/m9Y5LO+KQYv+t1CQOiFe6+SV2J7bE= +github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= +github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I= -github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= +github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= +github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 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= @@ -16,10 +16,8 @@ github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOW github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -30,26 +28,26 @@ github.com/google/go-github/v53 v53.2.0 h1:wvz3FyF53v4BK+AsnvCmeNhf8AkTaeh2SoYu/ github.com/google/go-github/v53 v53.2.0/go.mod h1:XhFRObz+m/l+UCm9b7KSIC3lT3NWSXGt7mOsAWEloao= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= -github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= -github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= -github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= +github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= +github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8= -github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc= -github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4= -github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= -github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM= -github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc= -github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg= -github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM= +github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= +github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= +github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= +github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= +github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= +github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= +github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= +github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= @@ -61,51 +59,73 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= -golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= -golang.org/x/oauth2 v0.8.0 h1:6dkIjl3j3LtZ/O3sTgZTMsLKSftL/B8Zgq4huOIIUu8= -golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/oauth2 v0.15.0 h1:s8pnnxNVzjWyrvYdFUQq5llS1PX2zhPXmccZv99h7uQ= +golang.org/x/oauth2 v0.15.0/go.mod h1:q48ptWNTY5XWf+JNten23lcvHpLJ0ZSxF5ttTHKVCAM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= -google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM= +google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/handler/webhook_handler.go b/handler/webhook_handler.go index 40f705d..feb6ea3 100644 --- a/handler/webhook_handler.go +++ b/handler/webhook_handler.go @@ -301,5 +301,8 @@ func render(w http.ResponseWriter, v interface{}) { http.Error(w, err.Error(), 500) return } - w.Write(data) + _, err = w.Write(data) + if err != nil { + log.Errorf("Failed to write respones: %v", err) + } } diff --git a/handler/webhook_handler_test.go b/handler/webhook_handler_test.go index 8052930..92ad649 100644 --- a/handler/webhook_handler_test.go +++ b/handler/webhook_handler_test.go @@ -17,6 +17,7 @@ import ( "github.com/golang/mock/gomock" "github.com/google/go-github/v53/github" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -51,7 +52,8 @@ func (s *handlerTestSuite) Test_MissingEventTypeHeader() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(notAGithubEventMessage, res.Error) } @@ -67,7 +69,8 @@ func (s *handlerTestSuite) Test_UnhandledEventType() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(unhandledEventTypeMessage("pull_request"), res.Error) } @@ -85,7 +88,8 @@ func (s *handlerTestSuite) Test_PingEventShowApplicationsReturnError() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal("any error", res.Error) s.ctrl.Finish() } @@ -104,7 +108,8 @@ func (s *handlerTestSuite) Test_PingEventUnmatchedRepo() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(unmatchedRepoMessage, res.Error) s.ctrl.Finish() } @@ -123,7 +128,8 @@ func (s *handlerTestSuite) Test_PingEventMultipleRepos() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(multipleMatchingReposMessageWithoutAppName, res.Error) s.ctrl.Finish() } @@ -148,7 +154,8 @@ func (s *handlerTestSuite) Test_PingEventGetApplicationReturnsError() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal("any error", res.Error) s.ctrl.Finish() } @@ -171,7 +178,8 @@ func (s *handlerTestSuite) Test_PingEventIncorrectSecret() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(webhookIncorrectConfiguration(appName, errors.New("payload signature check failed")), res.Error) s.ctrl.Finish() } @@ -198,7 +206,8 @@ func (s *handlerTestSuite) Test_PingEventWithCorrectSecret() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusOK, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(webhookCorrectConfiguration(appName), res.Message) s.ctrl.Finish() } @@ -218,7 +227,8 @@ func (s *handlerTestSuite) Test_PushEventShowApplicationsReturnsError() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal("any error", res.Error) s.ctrl.Finish() } @@ -304,7 +314,8 @@ func (s *handlerTestSuite) Test_PushEventUnmatchedRepo() { router.New(sut).ServeHTTP(s.w, req) s.Equal(scenario.expectedHttpCode, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(scenario.expectedError, res.Error) s.ctrl.Finish() } @@ -325,7 +336,8 @@ func (s *handlerTestSuite) Test_PushEventMultipleReposWithoutAppName() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(multipleMatchingReposMessageWithoutAppName, res.Error) s.ctrl.Finish() } @@ -349,7 +361,8 @@ func (s *handlerTestSuite) Test_PushEventIncorrectSecret() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(webhookIncorrectConfiguration(appName, errors.New("payload signature check failed")), res.Error) s.ctrl.Finish() } @@ -374,7 +387,8 @@ func (s *handlerTestSuite) Test_PushEventGetApplicationReturnsError() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusBadRequest, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal("any error", res.Error) s.ctrl.Finish() } @@ -455,7 +469,8 @@ func (s *handlerTestSuite) Test_PushEventTriggerPipelineReturnsError() { router.New(sut).ServeHTTP(s.w, req) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(scenario.expectedHttpCode, s.w.Code) s.Equal(scenario.expectedError, res.Error) s.Equal(scenario.expectedMessage, res.Message) @@ -486,7 +501,8 @@ func (s *handlerTestSuite) Test_PushEventCorrectSecret() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusOK, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(createPipelineJobSuccessMessage(jobSummary.Name, jobSummary.AppName, jobSummary.Branch, jobSummary.CommitID), res.Message) s.ctrl.Finish() } @@ -506,7 +522,8 @@ func (s *handlerTestSuite) Test_PushEventWithRefDeleted() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusAccepted, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(refDeletionPushEventUnsupportedMessage(ref), res.Message) s.ctrl.Finish() } @@ -544,7 +561,8 @@ func (s *handlerTestSuite) Test_PushEventWithAnnotatedTag() { router.New(sut).ServeHTTP(s.w, req) s.Equal(http.StatusOK, s.w.Code) var res response - json.Unmarshal(s.w.Body.Bytes(), &res) + err := json.Unmarshal(s.w.Body.Bytes(), &res) + require.NoError(s.T(), err) s.Equal(createPipelineJobSuccessMessage(jobSummary.Name, jobSummary.AppName, jobSummary.Branch, jobSummary.CommitID), res.Message) s.ctrl.Finish() }