github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/robots/coverage/diff/view.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package diff 18 19 import ( 20 "fmt" 21 "strings" 22 23 "golang.org/x/tools/cover" 24 "k8s.io/test-infra/gopherage/pkg/cov/junit/calculation" 25 ) 26 27 // formatPercentage converts a coverage ratio into a string value to be displayed by coverage robot 28 func formatPercentage(ratio float32) string { 29 if ratio < 0 { 30 return "Does not exist" 31 } 32 return fmt.Sprintf("%.1f%%", ratio*100) 33 } 34 35 // deltaDisplayed converts a coverage ratio delta into a string value to be displayed by coverage robot 36 func deltaDisplayed(change *coverageChange) string { 37 if change.baseRatio < 0 { 38 return "" 39 } 40 return fmt.Sprintf("%.1f", (change.newRatio-change.baseRatio)*100) 41 } 42 43 // makeTable checks each coverage change and produce the table content for coverage bot post 44 // It also report on whether any coverage fells below the given threshold 45 func makeTable(baseCovList, newCovList *calculation.CoverageList, coverageThreshold float32) (string, bool) { 46 var rows []string 47 isCoverageLow := false 48 for _, change := range findChanges(baseCovList, newCovList) { 49 filePath := change.name 50 rows = append(rows, fmt.Sprintf("%s | %s | %s | %s", 51 filePath, 52 formatPercentage(change.baseRatio), 53 formatPercentage(change.newRatio), 54 deltaDisplayed(change))) 55 56 if change.newRatio < coverageThreshold { 57 isCoverageLow = true 58 } 59 } 60 return strings.Join(rows, "\n"), isCoverageLow 61 } 62 63 // ContentForGithubPost constructs the message covbot posts 64 func ContentForGithubPost(baseProfiles, newProfiles []*cover.Profile, jobName string, coverageThreshold float32) ( 65 string, bool) { 66 67 rows := []string{ 68 "The following is the code coverage report", 69 fmt.Sprintf("Say `/test %s` to re-run this coverage report", jobName), 70 "", 71 "File | Old Coverage | New Coverage | Delta", 72 "---- |:------------:|:------------:|:-----:", 73 } 74 75 table, isCoverageLow := makeTable(calculation.ProduceCovList(baseProfiles), calculation.ProduceCovList(newProfiles), coverageThreshold) 76 77 if table == "" { 78 return "", false 79 } 80 81 rows = append(rows, table) 82 rows = append(rows, "") 83 84 return strings.Join(rows, "\n"), isCoverageLow 85 }