Skip to content

Commit

Permalink
implement regex matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryQ committed Apr 22, 2024
1 parent 6b6867c commit 0d8987a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ jobs:
- run: go run main.go
env:
INPUT_REQUIRED: |
- unit-tests
- tests
INPUT_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41 changes: 37 additions & 4 deletions pkg/reqcheck/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reqcheck
import (
"context"
"fmt"
"regexp"
"strings"
"time"

Expand All @@ -28,6 +29,12 @@ func Run(ctx context.Context, cfg *Config, action *githubactions.Action, gh *git
return err
}

rules, err := NewRuleset(cfg.RequiredWorkflowPatterns)
if err != nil {
return err
}

foundSelf := false
for {
checks, err := pr.ListChecks(ctx, cfg.TargetSHA, nil)
if err != nil {
Expand All @@ -41,18 +48,21 @@ func Run(ctx context.Context, cfg *Config, action *githubactions.Action, gh *git
for _, c := range checks {
if strings.Contains(c.GetDetailsURL(), fmt.Sprintf("runs/%d/job", ghCtx.RunID)) {
// skip waiting for this check if this is named the same as another check
action.Infof("Skipping check: %q", c.GetName())
if !foundSelf {
action.Infof("Skipping check: %q", c.GetName())
foundSelf = true
}
continue
}
if _, ok := requiredSet[c.GetName()]; ok {
if found := rules.First(c.GetName()); found != nil {
toCheck = append(toCheck, c)
requiredSet[c.GetName()] = true
requiredSet[found.String()] = true
}
}

requiredNotFound := lo.OmitByValues(requiredSet, []bool{true})
if len(requiredNotFound) > 0 {
return fmt.Errorf("required checks not found: %q in %q", lo.Keys(requiredNotFound))
return fmt.Errorf("required checks not found: %q", lo.Keys(requiredNotFound))
}

// any failed
Expand Down Expand Up @@ -112,3 +122,26 @@ const (
)

var failedConclusions = []string{ConclusionFailure, ConclusionCancelled, ConclusionTimedOut}

type Ruleset []*regexp.Regexp

func NewRuleset(patterns []string) (Ruleset, error) {
r := make([]*regexp.Regexp, 0, len(patterns))
for _, p := range patterns {
re, err := regexp.Compile(p)
if err != nil {
return nil, err
}
r = append(r, re)
}
return r, nil
}

func (r Ruleset) First(test string) *regexp.Regexp {
for _, re := range r {
if re.MatchString(test) {
return re
}
}
return nil
}

0 comments on commit 0d8987a

Please sign in to comment.