From e2650aad6b87e8354f881c15159e7b6876a1e3c0 Mon Sep 17 00:00:00 2001 From: DanG100 Date: Thu, 20 Oct 2022 20:57:56 +0000 Subject: [PATCH 1/4] ondatra lint test --- .github/workflows/pull_request.yml | 6 +++ tools/lint2annotation/lint2annotation.go | 52 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tools/lint2annotation/lint2annotation.go diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 2d7f08638a4..a001b5ffeeb 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -164,3 +164,9 @@ jobs: run: go install honnef.co/go/tools/cmd/staticcheck@latest - name: Run staticcheck run: GOGC=30 staticcheck ./... + - name: Get Ondatra linter + run: go install github.com/openconfig/ondatra/gnmi/migrator@latest + - name: Run Ondatra linter + run: | + migrator -json ./feature/... > lint.json + go run ./tools/lint2annotation lint.json diff --git a/tools/lint2annotation/lint2annotation.go b/tools/lint2annotation/lint2annotation.go new file mode 100644 index 00000000000..20d1fc99a1f --- /dev/null +++ b/tools/lint2annotation/lint2annotation.go @@ -0,0 +1,52 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "encoding/json" + "fmt" + "log" + "os" + "strings" +) + +type diag struct { + Category string `json:"category,omitempty"` + Posn string `json:"posn"` + Message string `json:"message"` +} + +type jsonOutput map[string]map[string][]diag + +func main() { + outfile := os.Args[1] + outBytes, err := os.ReadFile(outfile) + if err != nil { + log.Fatal(err) + } + + out := jsonOutput{} + if err := json.Unmarshal(outBytes, &out); err != nil { + log.Fatal(err) + } + for _, pkg := range out { + for _, diags := range pkg { + for _, diag := range diags { + pos := strings.Split(diag.Posn, ":") + fmt.Printf("::error file=%s,line=%s,col=%s::%s\n", pos[0], pos[1], pos[2], diag.Message) + } + } + } +} From b38556fb07d516c1cb97dbe8e6f29123ec7b3433 Mon Sep 17 00:00:00 2001 From: DanG100 Date: Thu, 20 Oct 2022 21:49:09 +0000 Subject: [PATCH 2/4] stdin --- .github/workflows/pull_request.yml | 4 +-- tools/lint2annotation/lint2annotation.go | 31 ++++++++++++------------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index a001b5ffeeb..6fd6ff8ccae 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -167,6 +167,4 @@ jobs: - name: Get Ondatra linter run: go install github.com/openconfig/ondatra/gnmi/migrator@latest - name: Run Ondatra linter - run: | - migrator -json ./feature/... > lint.json - go run ./tools/lint2annotation lint.json + run: migrator -json ./feature/... | go run ./tools/lint2annotation diff --git a/tools/lint2annotation/lint2annotation.go b/tools/lint2annotation/lint2annotation.go index 20d1fc99a1f..0a872d40032 100644 --- a/tools/lint2annotation/lint2annotation.go +++ b/tools/lint2annotation/lint2annotation.go @@ -12,11 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +// The lint2annotation command converts Go analysis Diagnostic messages into GitHub annotations. package main import ( "encoding/json" "fmt" + "io" "log" "os" "strings" @@ -31,21 +33,20 @@ type diag struct { type jsonOutput map[string]map[string][]diag func main() { - outfile := os.Args[1] - outBytes, err := os.ReadFile(outfile) - if err != nil { - log.Fatal(err) - } - - out := jsonOutput{} - if err := json.Unmarshal(outBytes, &out); err != nil { - log.Fatal(err) - } - for _, pkg := range out { - for _, diags := range pkg { - for _, diag := range diags { - pos := strings.Split(diag.Posn, ":") - fmt.Printf("::error file=%s,line=%s,col=%s::%s\n", pos[0], pos[1], pos[2], diag.Message) + dec := json.NewDecoder(os.Stdin) + for { + out := jsonOutput{} + if err := dec.Decode(&out); err == io.EOF { + break + } else if err != nil { + log.Fatal(err) + } + for _, pkg := range out { + for _, diags := range pkg { + for _, diag := range diags { + pos := strings.Split(diag.Posn, ":") + fmt.Printf("::error file=%s,line=%s,col=%s::%s\n", pos[0], pos[1], pos[2], diag.Message) + } } } } From 2a0c07c27e998fc0b3a0996c02435ee37c858b24 Mon Sep 17 00:00:00 2001 From: DanG100 Date: Mon, 24 Oct 2022 17:39:13 +0000 Subject: [PATCH 3/4] feedback --- tools/lint2annotation/lint2annotation.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/lint2annotation/lint2annotation.go b/tools/lint2annotation/lint2annotation.go index 0a872d40032..30718fe7741 100644 --- a/tools/lint2annotation/lint2annotation.go +++ b/tools/lint2annotation/lint2annotation.go @@ -25,9 +25,8 @@ import ( ) type diag struct { - Category string `json:"category,omitempty"` - Posn string `json:"posn"` - Message string `json:"message"` + Posn string `json:"posn"` + Message string `json:"message"` } type jsonOutput map[string]map[string][]diag From 01de0b8624533b16312b520f580d434022ad4afc Mon Sep 17 00:00:00 2001 From: DanG100 Date: Mon, 31 Oct 2022 17:34:31 +0000 Subject: [PATCH 4/4] severity flag --- tools/lint2annotation/lint2annotation.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/lint2annotation/lint2annotation.go b/tools/lint2annotation/lint2annotation.go index 30718fe7741..a31537e5f31 100644 --- a/tools/lint2annotation/lint2annotation.go +++ b/tools/lint2annotation/lint2annotation.go @@ -17,6 +17,7 @@ package main import ( "encoding/json" + "flag" "fmt" "io" "log" @@ -31,7 +32,12 @@ type diag struct { type jsonOutput map[string]map[string][]diag +var ( + severity = flag.String("severity", "notice", "Sets the severity of the Github annotations") +) + func main() { + flag.Parse() dec := json.NewDecoder(os.Stdin) for { out := jsonOutput{} @@ -44,7 +50,7 @@ func main() { for _, diags := range pkg { for _, diag := range diags { pos := strings.Split(diag.Posn, ":") - fmt.Printf("::error file=%s,line=%s,col=%s::%s\n", pos[0], pos[1], pos[2], diag.Message) + fmt.Printf("::%s file=%s,line=%s,col=%s::%s\n", *severity, pos[0], pos[1], pos[2], diag.Message) } } }