Skip to content

Commit

Permalink
Merge pull request #126 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 6.0.0
  • Loading branch information
andyone authored Jul 21, 2023
2 parents d840e92 + d707c29 commit e6f4529
Show file tree
Hide file tree
Showing 20 changed files with 539 additions and 244 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,12 @@ jobs:

needs: Go

continue-on-error: true

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Check spelling
continue-on-error: true
uses: crate-ci/typos@master

DockerBuild:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ permissions:
actions: read
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
analyse:
name: Analyse
Expand Down
106 changes: 84 additions & 22 deletions check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ package check

import (
"sort"
"strings"

"github.com/essentialkaos/ek/v12/sliceutil"
"github.com/essentialkaos/ek/v12/sortutil"
"github.com/essentialkaos/ek/v12/strutil"
"github.com/essentialkaos/ek/v12/system"

"github.com/essentialkaos/perfecto/spec"
)
Expand All @@ -30,19 +33,23 @@ const (

// Report contains info about all alerts
type Report struct {
Notices Alerts `json:"notices,omitempty"`
Warnings Alerts `json:"warnings,omitempty"`
Errors Alerts `json:"errors,omitempty"`
Criticals Alerts `json:"criticals,omitempty"`
Notices Alerts `json:"notices,omitempty"`
Warnings Alerts `json:"warnings,omitempty"`
Errors Alerts `json:"errors,omitempty"`
Criticals Alerts `json:"criticals,omitempty"`
IgnoredChecks []string `json:"ignored_checks,omitempty"`
NoLint bool `json:"no_lint"`
IsPerfect bool `json:"is_perfect"`
IsSkipped bool `json:"is_skipped"`
}

// Alert contains basic alert info
type Alert struct {
ID string `json:"id"`
Level uint8 `json:"level"`
Info string `json:"info"`
Line spec.Line `json:"line"`
Ignored bool `json:"ignored"`
ID string `json:"id"`
Level uint8 `json:"level"`
Info string `json:"info"`
Line spec.Line `json:"line"`
IsIgnored bool `json:"is_ignored"`
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -56,18 +63,17 @@ func (s Alerts) Less(i, j int) bool { return s[i].Line.Index < s[j].Line.Index }

// ////////////////////////////////////////////////////////////////////////////////// //

var osInfoFunc = system.GetOSInfo

// ////////////////////////////////////////////////////////////////////////////////// //

// NewAlert creates new alert
func NewAlert(id string, level uint8, info string, line spec.Line) Alert {
return Alert{id, level, info, line, false}
}

// ////////////////////////////////////////////////////////////////////////////////// //

// IsPerfect returns true if report doesn't have any alerts
func (r *Report) IsPerfect() bool {
return r.Total() == 0
}

// Total returns total number of alerts (including ignored)
func (r *Report) Total() int {
return r.Notices.Total() +
Expand Down Expand Up @@ -128,7 +134,7 @@ func (r *Report) IDs() []string {
// HasAlerts returns true if alerts contains at least one non-ignored alert
func (a Alerts) HasAlerts() bool {
for _, alert := range a {
if alert.Ignored {
if alert.IsIgnored {
continue
}

Expand All @@ -148,7 +154,7 @@ func (a Alerts) Ignored() int {
var counter int

for _, alert := range a {
if alert.Ignored {
if alert.IsIgnored {
counter++
}
}
Expand All @@ -158,12 +164,18 @@ func (a Alerts) Ignored() int {

// ////////////////////////////////////////////////////////////////////////////////// //

// Check check spec
// Check executes different checks over given spec
func Check(s *spec.Spec, lint bool, linterConfig string, ignored []string) *Report {
report := &Report{}
report := &Report{NoLint: !lint, IgnoredChecks: ignored}

if !isApplicableTarget(s) {
report.IsSkipped = true
return report
}

checkers := getCheckers()

if lint {
if lint && !sliceutil.Contains(ignored, RPMLINT_CHECK_ID) {
alerts := Lint(s, linterConfig)
appendLinterAlerts(report, alerts)
}
Expand All @@ -178,8 +190,8 @@ func Check(s *spec.Spec, lint bool, linterConfig string, ignored []string) *Repo
ignore := sliceutil.Contains(ignored, id)

for _, alert := range alerts {
if ignore || alert.Line.Skip {
alert.Ignored = true
if ignore || alert.Line.Ignore {
alert.IsIgnored = true
}

switch alert.Level {
Expand All @@ -200,19 +212,69 @@ func Check(s *spec.Spec, lint bool, linterConfig string, ignored []string) *Repo
sort.Sort(Alerts(report.Errors))
sort.Sort(Alerts(report.Criticals))

report.IsPerfect = report.Total()-report.Ignored() == 0

return report
}

// ////////////////////////////////////////////////////////////////////////////////// //

// isApplicableTarget checks if current system is applicable for tests
func isApplicableTarget(s *spec.Spec) bool {
if len(s.Targets) == 0 {
return true
}

osInfo, err := osInfoFunc()

if err != nil {
return false
}

for _, target := range s.Targets {
if isTargetFit(osInfo, target) {
return true
}
}

return false
}

// isTargetFit returns true if current system is applicable for tests
func isTargetFit(osInfo *system.OSInfo, target string) bool {
if osInfo.ID == target {
return true
}

majorVersion, _, _ := strings.Cut(osInfo.VersionID, ".")

if osInfo.ID+majorVersion == target {
return true
}

_, platform, _ := strings.Cut(osInfo.PlatformID, ":")

if target == platform {
return true
}

for _, id := range strutil.Fields(osInfo.IDLike) {
if "@"+id == target {
return true
}
}

return false
}

// appendLinterAlerts append rpmlint alerts to report
func appendLinterAlerts(r *Report, alerts []Alert) {
if len(alerts) == 0 {
return
}

for _, alert := range alerts {
if alert.Line.Skip {
if alert.Line.Ignore {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions check/checkers.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ func checkForUselessSpaces(id string, s *spec.Spec) []Alert {
for _, line := range s.Data {
if contains(line, " ") {
if strings.TrimSpace(line.Text) == "" {
impLine := spec.Line{line.Index, strings.Replace(line.Text, " ", "", -1), line.Skip}
impLine := spec.Line{line.Index, strings.Replace(line.Text, " ", "", -1), line.Ignore}
result = append(result, NewAlert(id, LEVEL_NOTICE, "Line contains useless spaces", impLine))
} else if strings.TrimRight(line.Text, " ") != line.Text {
cleanLine := strings.TrimRight(line.Text, " ")
spaces := len(line.Text) - len(cleanLine)
impLine := spec.Line{line.Index, cleanLine + strings.Repeat("", spaces), line.Skip}
impLine := spec.Line{line.Index, cleanLine + strings.Repeat("", spaces), line.Ignore}
result = append(result, NewAlert(id, LEVEL_NOTICE, "Line contains spaces at the end of line", impLine))
}
}
Expand Down
Loading

0 comments on commit e6f4529

Please sign in to comment.