-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68167a6
commit 1a48bdb
Showing
1 changed file
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright 2021 Diego Lima. All rights reserved. | ||
|
||
// Use of this source code is governed by a Apache license. | ||
// license that can be found in the LICENSE file. | ||
|
||
package model | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
// Result represents the result of a test case. | ||
type Result struct { | ||
Title string | ||
Passed bool | ||
Body string | ||
} | ||
|
||
// IsNamePatternValid check if the pull request name is valid. | ||
func (cfg *Layout) IsNamePatternValid(name string) *Result { | ||
fmt.Println(cfg) | ||
if len(cfg.PullRequest.TestSuite.NamePattern) > 0 { | ||
match, _ := regexp.MatchString(cfg.PullRequest.TestSuite.NamePattern, name) | ||
var body string | ||
if !match { | ||
body = fmt.Sprintf("The pull request format should be: [%s]", cfg.PullRequest.TestSuite.NamePattern) | ||
} | ||
return &Result{ | ||
Title: "Pull request pattern", | ||
Passed: match, | ||
Body: body, | ||
} | ||
} | ||
return &Result{} | ||
} | ||
|
||
// HasReviewers check if the pull request has reviewers. | ||
func (cfg *Layout) HasReviewers(hasReviewers bool) *Result { | ||
if cfg.PullRequest.TestSuite.Reviewers { | ||
if !hasReviewers { | ||
return &Result{ | ||
Title: "Reviewers", | ||
Passed: false, | ||
Body: "Must have at least one reviewer", | ||
} | ||
} | ||
return &Result{ | ||
Title: "Reviewers", | ||
Passed: true, | ||
} | ||
} | ||
return &Result{} | ||
} | ||
|
||
// HasAssignees check if the pull request has assignees. | ||
func (cfg *Layout) HasAssignees(hasAssignees bool) *Result { | ||
if cfg.PullRequest.TestSuite.Assignees { | ||
if !hasAssignees { | ||
return &Result{ | ||
Title: "Assignees", | ||
Passed: false, | ||
Body: "Must have at least one assignee", | ||
} | ||
} | ||
return &Result{ | ||
Title: "Assignees", | ||
Passed: true, | ||
} | ||
} | ||
return &Result{} | ||
} | ||
|
||
// HasLabels check if the pull request has labels. | ||
func (cfg *Layout) HasLabels(hasLabels bool) *Result { | ||
if cfg.PullRequest.TestSuite.Assignees { | ||
if !hasLabels { | ||
return &Result{ | ||
Title: "Labels", | ||
Passed: false, | ||
Body: "Must have at least one label", | ||
} | ||
} | ||
return &Result{ | ||
Title: "Labels", | ||
Passed: true, | ||
} | ||
} | ||
return &Result{} | ||
} | ||
|
||
func (cfg *Layout) GetOverralResults(results []*Result) string { | ||
var title string | ||
var body bytes.Buffer | ||
overralResults := cfg.AllResultsPassed(results) | ||
if overralResults { | ||
title = `<h3 align="center">Laboratory test results [:heavy_check_mark:]</h3> | ||
--- | ||
` | ||
} else { | ||
title = `<h3 align="center">Laboratory test results [:x:]</h3> | ||
--- | ||
` | ||
} | ||
body.WriteString(title) | ||
for _, r := range results { | ||
if r.Passed { | ||
body.WriteString(fmt.Sprintf("[:white_check_mark:] **%s** <br/>", r.Title)) | ||
} else { | ||
body.WriteString(fmt.Sprintf("[:heavy_exclamation_mark:] **%s** → %s <br/>", r.Title, r.Body)) | ||
} | ||
} | ||
return body.String() | ||
} | ||
|
||
// AllResultsPassed check if all the results passed. | ||
func (cfg *Layout) AllResultsPassed(results []*Result) bool { | ||
overralResults := true | ||
for _, r := range results { | ||
if !r.Passed { | ||
overralResults = false | ||
break | ||
} | ||
} | ||
return overralResults | ||
} |