Skip to content

Commit

Permalink
Add argument parsing and filter construction to MessageFilter
Browse files Browse the repository at this point in the history
Fix location of used.keys

a bit of a rename

Update argument description for subscription filter
  • Loading branch information
Kuixz committed Aug 16, 2024
1 parent 3349149 commit e4e8ff8
Show file tree
Hide file tree
Showing 2 changed files with 57 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
70 changes: 53 additions & 17 deletions pkg/slack/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (

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

type MessageFilter struct {
whitelists []messageFilterRule
// can be extended to include blacklists []messageFilterRule
}

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

func (rule *messageFilterRule) 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 +86,56 @@ func NewFilter(filterLayers []string) (*MessageFilter, error) {
filter := MessageFilter{
whitelists: []messageFilterRule{},
}
// Ref: https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#create-a-check-run--parameters

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

definition = definition
// switch definition[0]
// case ""
switch len(definition) {
case 1: // Assumed format "conclusion1,conclusion2,..."
rule := messageFilterRule{}
if slices.Contains(used.keys, "none") {
return nil, fmt.Errorf("duplicated conclusion strings; use commas to separate conclusions")
}
conclusions := strings.Split(definition[0], ",")

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

used.keys = append(used.keys, "none")
filter.whitelists = append(filter.whitelists, rule)
case 2, 3: // Assumed format "filterKey:filterValue1,filterValue2,..."
rule := messageFilterRule{}
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], ",")
rule.Branches = branches
case "workflows":
workflows := strings.Split(definition[1], ",")
rule.Workflows = workflows
default:
return nil, fmt.Errorf("unsupported filter type: %s", filterType)
}

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

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

used.keys = append(used.keys, filterType)
filter.whitelists = append(filter.whitelists, rule)
}
}

return &filter, nil
}

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

0 comments on commit e4e8ff8

Please sign in to comment.