From 1d399749e4e3bc7bd63016ec1ba7d395b6f4f1a9 Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Thu, 5 Sep 2024 12:42:51 +0900 Subject: [PATCH] (feat) reduce freq of PR comments --- README.md | 8 ++++++++ action.yml | 4 +++- app/pkg/pr.go | 12 ++++++++++++ app/pkg/pr_handler.go | 1 + app/pkg/utils.go | 9 +++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a5cccb9..efed6a3 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,14 @@ Before using this action, enable Github Actions - uses: kevincobain2000/action-coveritup@v2 with: pr_comment: true + # optional + ## report only these types on PR comment, empty means all + types: coverage,go-sec-issues,go-lint-errors + # optional + ## report only these types after 1st comment + # 1st comment will have all types or types specified in `types` + # 2nd comment onwards will have only these types + diff_types: coverage ``` ## Time taken diff --git a/action.yml b/action.yml index ca30e65..ee4858a 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,8 @@ inputs: description: "Type of score" types: description: "Types to report on PR comment. If empty then it will report all types" + diff_types: + description: "Only types to report on PR comment after 1st comment. 1st comment will have all types" metric: description: "Metric of score" command: @@ -154,7 +156,7 @@ runs: -x "${{ env.COVERITUP_HTTP_PROXY }}" \ -H 'Content-Type: application/json' \ -w "%{http_code}" \ - "${{env.COVERITUP_HOST}}/pr?org=${{github.repository_owner}}&repo=${{ github.event.repository.name }}&types=${{ inputs.types }}&theme=${{ inputs.theme }}&pr_num=${{ steps.pr-number-id.outputs.pr }}&branch=${{ env.BRANCH_OR_TAG_NAME }}&base_branch=${{ steps.branch-name.outputs.base_ref_branch }}" \ + "${{env.COVERITUP_HOST}}/pr?org=${{github.repository_owner}}&repo=${{ github.event.repository.name }}&types=${{ inputs.types }}&diff_types=${{ inputs.diff_types }}&theme=${{ inputs.theme }}&pr_num=${{ steps.pr-number-id.outputs.pr }}&branch=${{ env.BRANCH_OR_TAG_NAME }}&base_branch=${{ steps.branch-name.outputs.base_ref_branch }}" \ -o comment_body.txt) echo "PR_HTTP_RESPONSE=$response" >> $GITHUB_ENV diff --git a/app/pkg/pr.go b/app/pkg/pr.go index 36d7042..d119fce 100644 --- a/app/pkg/pr.go +++ b/app/pkg/pr.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "strings" "github.com/fbiville/markdown-table-formatter/pkg/markdown" "github.com/kevincobain2000/action-coveritup/models" @@ -45,6 +46,17 @@ func (p *PR) Get(req *PRRequest, types []models.Type) (string, error) { commitHistoryImgUrls := []string{} // stores urls for commit history trends (line charts) userHistoryImgUrls := []string{} // stores urls for user history trends (line charts) mdText.H4("CoverItUp Report") + // if it is not first PR, then only report the types that are different from diff_types + if !isFirstPR && req.DiffTypes != "" { + onlyReportTypes := []models.Type{} + diffTypes := strings.Split(req.DiffTypes, ",") + for _, t := range types { + if Contains(diffTypes, t.Name) { + onlyReportTypes = append(onlyReportTypes, t) + } + } + types = onlyReportTypes + } for _, t := range types { y := make([]float64, 2) diff --git a/app/pkg/pr_handler.go b/app/pkg/pr_handler.go index 5ce678b..e4ca23f 100644 --- a/app/pkg/pr_handler.go +++ b/app/pkg/pr_handler.go @@ -26,6 +26,7 @@ type PRRequest struct { PRNum int `json:"pr_num" query:"pr_num" validate:"required,numeric" message:"pr_num is required"` Theme string `json:"theme" query:"theme" default:"light" validate:"oneof=light dark" message:"theme must be light or dark"` Types string `json:"types" query:"types" validate:"ascii" message:"ascii types are required"` + DiffTypes string `json:"diff_types" query:"diff_types" validate:"ascii" message:"ascii diff_types are required"` host string scheme string diff --git a/app/pkg/utils.go b/app/pkg/utils.go index bbb14d8..fc844ce 100644 --- a/app/pkg/utils.go +++ b/app/pkg/utils.go @@ -82,3 +82,12 @@ func TrimStringFields(v interface{}) { } } } + +func Contains(slice []string, item string) bool { + for _, element := range slice { + if element == item { + return true + } + } + return false +}