Skip to content

Commit

Permalink
Add argument parsing and filter construction to MessageFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuixz committed Aug 15, 2024
1 parent 43f7a90 commit 2221b80
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
4 changes: 4 additions & 0 deletions pkg/github/jobs/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type WorkflowRun struct {
CommitMessageTitle string
CommitURL string

Branch string

Jobs []*WorkflowJob
}

Expand Down Expand Up @@ -94,6 +96,8 @@ func newState(runs map[Key]cell[github.WorkflowRun], jobs map[Key]cell[github.Wo
StartedAt: run.GetRunStartedAt().Time,
CommitMessageTitle: commitMsgTitle,
CommitURL: commitURL,

Branch: run.GetHeadBranch(),
}
}
for key, c := range jobs {
Expand Down
69 changes: 52 additions & 17 deletions pkg/slack/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

type messageFilterLayer struct {
Conclusions []string `json:"conclusions"`
// branches []string
Workflows []string `json:"workflows"`
Branches []string `json:"branches"`
Workflows []string `json:"workflows"`
}

type MessageFilter struct {
Expand Down Expand Up @@ -42,6 +42,9 @@ func (mfl messageFilterLayer) Pass(run *jobs.WorkflowRun) bool {
if len(mfl.Conclusions) > 0 && !slices.Contains(mfl.Conclusions, run.Conclusion) {
return false
}
if len(mfl.Branches) > 0 && !slices.Contains(mfl.Branches, run.Branch) {
return false
}
if len(mfl.Workflows) > 0 && !slices.Contains(mfl.Workflows, run.Name) {
return false
}
Expand All @@ -61,22 +64,16 @@ func (mf MessageFilter) Any(run *jobs.WorkflowRun) bool {
}

func (mfl *messageFilterLayer) setConclusions(conclusions []string) error {
// Ref: https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#create-a-check-run--parameters
conclusionsEnum := []string{"action_required", "cancelled", "failure", "neutral", "success", "skipped", "stale", "timed_out"}
// var supportedConclusions, unsupportedConclusions []string
var unsupportedConclusions []string
for _, c := range conclusions {
// if slices.Contains(conclusionsEnum, c) {
// supportedConclusions = append(supportedConclusions, c)
// } else {
if !slices.Contains(conclusionsEnum, c) {
unsupportedConclusions = append(unsupportedConclusions, c)
}
}

if len(unsupportedConclusions) > 0 {
if slices.Contains(unsupportedConclusions, " ") {
return fmt.Errorf("Do not space-separate conclusions. Use format conclusion1,conclusion2")
}
return fmt.Errorf("unsupported conclusions: %s", strings.Join(unsupportedConclusions, ", "))
}

Expand All @@ -88,18 +85,56 @@ func NewFilter(filterLayers []string) (*MessageFilter, error) {
filter := MessageFilter{
filters: []messageFilterLayer{},
}
// Ref: https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#create-a-check-run--parameters

for _, layer := range filterLayers {
used := struct{ keys []string }{keys: []string{}}
definition := strings.Split(layer, ":")

definition = definition
// switch definition[0]
// case ""
switch len(definition) {
case 1: // Assumed format "conclusion1,conclusion2,..."
mfl := messageFilterLayer{}
if slices.Contains(used.keys, "none") {
return nil, fmt.Errorf("do not space-separate conclusions")
}
conclusions := strings.Split(definition[0], ",")

err := mfl.setConclusions(conclusions)
if err != nil {
return nil, nil
}

used.keys = append(used.keys, "none")
filter.filters = append(filter.filters, mfl)
case 2, 3: // Assumed format "filterKey:filterValue1,filterValue2,..."
mfl := messageFilterLayer{}
filterType := definition[0]
if slices.Contains(used.keys, filterType) {
return nil, fmt.Errorf("duplicated filter type: %s", filterType)
}
switch filterType {
case "branches":
branches := strings.Split(definition[1], ",")
mfl.Branches = branches
case "workflows":
workflows := strings.Split(definition[1], ",")
mfl.Workflows = workflows
default:
return nil, fmt.Errorf("unsupported filter type: %s", filterType)
}

if len(definition) == 3 {
conclusions := strings.Split(definition[2], ",")

err := mfl.setConclusions(conclusions)
if err != nil {
return nil, nil
}
}

used.keys = append(used.keys, filterType)
filter.filters = append(filter.filters, mfl)
}
}

return &filter, nil
}

// func (mf MessageFilter) String() string {
// output = ""
// }

0 comments on commit 2221b80

Please sign in to comment.