|
| 1 | +// Copyright 2022 SLSA Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "bufio" |
| 18 | + "encoding/json" |
| 19 | + "log" |
| 20 | + "os" |
| 21 | + "strconv" |
| 22 | + "strings" |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + thresholdFile := os.Getenv("THRESHOLD_FILE") |
| 27 | + if thresholdFile == "" { |
| 28 | + log.Fatalf("THRESHOLD_FILE environment variable is not set") |
| 29 | + } |
| 30 | + thresholdMap, err := parseCoverageThreshold(thresholdFile) |
| 31 | + if err != nil { |
| 32 | + log.Fatalf("Error parsing threshold file: %v", err) |
| 33 | + } |
| 34 | + coveragePercentage := os.Getenv("COVERAGE_PERCENTAGE") |
| 35 | + if coveragePercentage == "" { |
| 36 | + log.Fatalf("COVERAGE_PERCENTAGE environment variable is not set") |
| 37 | + } |
| 38 | + coveragePercentageFloat, err := strconv.ParseFloat(coveragePercentage, 32) |
| 39 | + if err != nil { |
| 40 | + log.Fatalf("Error parsing coverage percentage: %v", err) |
| 41 | + } |
| 42 | + // read stream from stdin |
| 43 | + scanner := bufio.NewScanner(os.Stdin) |
| 44 | + for scanner.Scan() { |
| 45 | + line := scanner.Text() |
| 46 | + if strings.Contains(line, "coverage: ") { |
| 47 | + parts := strings.Fields(line) |
| 48 | + if len(parts) < 5 { |
| 49 | + continue |
| 50 | + } |
| 51 | + percentage, err := strconv.ParseFloat(strings.Trim(parts[4], "%"), 32) |
| 52 | + if err != nil { |
| 53 | + log.Fatalf("invalid line: %s", line) |
| 54 | + } |
| 55 | + pack := parts[1] |
| 56 | + if val, ok := thresholdMap[pack]; !ok { |
| 57 | + if float32(int(percentage*100)/100) < float32(int(coveragePercentageFloat*100)/100) { |
| 58 | + log.Fatalf("coverage for %s is below threshold: %f < %f", pack, percentage, coveragePercentageFloat) |
| 59 | + } |
| 60 | + } else { |
| 61 | + if float32(int(percentage*100)/100) < float32(int(val*100)/100) { |
| 62 | + log.Fatalf("coverage for %s is below threshold: %f < %f", pack, percentage, val) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// parseCoverageThreshold parses the threshold file and returns a map. |
| 70 | +func parseCoverageThreshold(fileName string) (map[string]float64, error) { |
| 71 | + // Here is an example of the threshold file: |
| 72 | + /* |
| 73 | + { |
| 74 | + "github.com/foo/bar/pkg/cryptoutils": 71.2, |
| 75 | + } |
| 76 | + */ |
| 77 | + f, err := os.Open(fileName) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + defer f.Close() |
| 82 | + thresholdMap := make(map[string]float64) |
| 83 | + if err := json.NewDecoder(f).Decode(&thresholdMap); err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + return thresholdMap, nil |
| 87 | +} |
0 commit comments