Skip to content

Commit

Permalink
feat: add policy check summary in wrapped messages (#2452)
Browse files Browse the repository at this point in the history
Co-authored-by: nitrocode <[email protected]>
  • Loading branch information
tlorreyte and nitrocode authored Nov 13, 2022
1 parent ff3f721 commit 7f75002
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion server/events/markdown_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type planSuccessData struct {

type policyCheckSuccessData struct {
models.PolicyCheckSuccess
PolicyCheckSummary string
}

type projectResultTmplData struct {
Expand Down Expand Up @@ -199,7 +200,7 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult,
numPlanSuccesses++
} else if result.PolicyCheckSuccess != nil {
if m.shouldUseWrappedTmpl(vcsHost, result.PolicyCheckSuccess.PolicyCheckOutput) {
resultData.Rendered = m.renderTemplate(templates.Lookup("policyCheckSuccessWrapped"), policyCheckSuccessData{PolicyCheckSuccess: *result.PolicyCheckSuccess})
resultData.Rendered = m.renderTemplate(templates.Lookup("policyCheckSuccessWrapped"), policyCheckSuccessData{PolicyCheckSuccess: *result.PolicyCheckSuccess, PolicyCheckSummary: result.PolicyCheckSuccess.Summary()})
} else {
resultData.Rendered = m.renderTemplate(templates.Lookup("policyCheckSuccessUnwrapped"), policyCheckSuccessData{PolicyCheckSuccess: *result.PolicyCheckSuccess})
}
Expand Down
11 changes: 11 additions & 0 deletions server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,17 @@ type PolicyCheckSuccess struct {
HasDiverged bool
}

// Summary extracts one line summary of policy check.
func (p *PolicyCheckSuccess) Summary() string {
note := ""

r := regexp.MustCompile(`\d+ tests, \d+ passed, \d+ warnings, \d+ failures, \d+ exceptions`)
if match := r.FindString(p.PolicyCheckOutput); match != "" {
return note + match
}
return note
}

type VersionSuccess struct {
VersionOutput string
}
Expand Down
11 changes: 11 additions & 0 deletions server/events/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,17 @@ func TestAzureDevopsSplitRepoFullName(t *testing.T) {
})
}
}

func TestPolicyCheckSuccess_Summary(t *testing.T) {
pcs := models.PolicyCheckSuccess{
PolicyCheckOutput: `WARN - <redacted plan file> - main - example main package
20 tests, 19 passed, 1 warnings, 0 failures, 0 exceptions`,
}

Equals(t, "20 tests, 19 passed, 1 warnings, 0 failures, 0 exceptions", pcs.Summary())
}

func TestPullStatus_StatusCount(t *testing.T) {
ps := models.PullStatus{
Projects: []models.ProjectStatus{
Expand Down

0 comments on commit 7f75002

Please sign in to comment.