Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: compact format for if, then and actions #824

Merged
merged 3 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 112 additions & 36 deletions engine/inline_rules_normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/mitchellh/mapstructure"
)

func inlineRulesNormalizer() *NormalizeRule {
func inlineNormalizer() *NormalizeRule {
normalizedRule := NewNormalizeRule()
normalizedRule.WithModificators(inlineRulesModificator)
normalizedRule.WithModificators(inlineModificator)
return normalizedRule
}

func inlineRulesModificator(file *ReviewpadFile) (*ReviewpadFile, error) {
func inlineModificator(file *ReviewpadFile) (*ReviewpadFile, error) {
reviewpadFile := &ReviewpadFile{
Mode: file.Mode,
IgnoreErrors: file.IgnoreErrors,
Expand All @@ -32,7 +32,7 @@ func inlineRulesModificator(file *ReviewpadFile) (*ReviewpadFile, error) {
}

for i, workflow := range reviewpadFile.Workflows {
processedWorkflow, rules, err := processInlineRulesOnWorkflow(workflow, reviewpadFile.Rules)
processedWorkflow, rules, err := processWorkflow(workflow, reviewpadFile.Rules)
if err != nil {
return nil, err
}
Expand All @@ -41,10 +41,22 @@ func inlineRulesModificator(file *ReviewpadFile) (*ReviewpadFile, error) {
reviewpadFile.Workflows[i] = *processedWorkflow
}

for _, pipeline := range reviewpadFile.Pipelines {
for _, stage := range pipeline.Stages {
actions, err := normalizeActions(stage.NonNormalizedActions)
if err != nil {
return nil, err
}

stage.Actions = actions
stage.NonNormalizedActions = nil
}
}

return reviewpadFile, nil
}

func processInlineRulesOnWorkflow(workflow PadWorkflow, currentRules []PadRule) (*PadWorkflow, []PadRule, error) {
func processWorkflow(workflow PadWorkflow, currentRules []PadRule) (*PadWorkflow, []PadRule, error) {
wf := &PadWorkflow{
Name: workflow.Name,
Description: workflow.Description,
Expand All @@ -53,40 +65,20 @@ func processInlineRulesOnWorkflow(workflow PadWorkflow, currentRules []PadRule)
Actions: workflow.Actions,
On: workflow.On,
}
rules := make([]PadRule, 0)

for _, rawRule := range workflow.NonNormalizedRules {
var rule *PadRule
var workflowRule *PadWorkflowRule

switch r := rawRule.(type) {
case string:
rule = decodeRule(r)
workflowRule = &PadWorkflowRule{
Rule: rule.Name,
ExtraActions: []string{},
}
case map[string]interface{}:
decodedWorkflowRule, err := decodeWorkflowRule(r)
if err != nil {
return nil, nil, err
}
workflowRule = decodedWorkflowRule
rule = decodeRule(decodedWorkflowRule.Rule)
default:
return nil, nil, fmt.Errorf("unknown rule type %T", r)
}

if _, exists := findRule(currentRules, rule.Name); !exists {
rules = append(rules, *rule)
}
actions, err := normalizeActions(workflow.NonNormalizedActions)
if err != nil {
return nil, nil, err
}

if workflowRule != nil {
wf.Rules = append(wf.Rules, *workflowRule)
}
wf.Actions = actions

rules, workflowRules, err := normalizeRules(workflow.NonNormalizedRules, currentRules)
if err != nil {
return nil, nil, err
}

workflow.NonNormalizedRules = nil
wf.Rules = workflowRules

return wf, rules, nil
}
Expand All @@ -99,8 +91,92 @@ func decodeRule(rule string) *PadRule {
}
}

func decodeWorkflowRule(rule map[string]interface{}) (*PadWorkflowRule, error) {
func decodeWorkflowRule(rule map[string]any) (*PadWorkflowRule, error) {
workflowRule := &PadWorkflowRule{}
err := mapstructure.Decode(rule, workflowRule)
return workflowRule, err
}

func normalizeRules(rawRule any, currentRules []PadRule) ([]PadRule, []PadWorkflowRule, error) {
var rule *PadRule
var workflowRule *PadWorkflowRule
var rules []PadRule
var workflowRules []PadWorkflowRule

switch r := rawRule.(type) {
// a rule can be a plain string which can be an inline rule
// or a name of a predefined rules
// - '$size() < 10'
// - small
case string:
zolamk marked this conversation as resolved.
Show resolved Hide resolved
rule = decodeRule(r)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If my understanding is correct being string can mean the name of a specified rule under rules, which means that this decode is not correct. It works because on line 136 we look for all rules and we will end up with the correct spec.

We don't need to correct this since it's working and we will be addressing this issue later.

Just wanted to create awareness.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is just creating a rule structure using the spec as a name and we verify it doesn't exist at a later stage, it was a way of avoiding having duplicate rules if the same spec is repated

workflowRule = &PadWorkflowRule{
Rule: rule.Name,
}
// we can also have a list of rules in the format
// - '$size() < 10'
// - small
case []any:
for _, ru := range r {
processedRules, processedWorkflowRules, err := normalizeRules(ru, currentRules)
if err != nil {
return nil, nil, err
}

rules = append(rules, processedRules...)
workflowRules = append(workflowRules, processedWorkflowRules...)
}
// we can also specify a rule as map with extra actions like
// - rule: small
// extra-actions:
// - $comment("small")
case map[string]any:
decodedWorkflowRule, err := decodeWorkflowRule(r)
if err != nil {
return nil, nil, err
}

workflowRule = decodedWorkflowRule
rule = decodeRule(decodedWorkflowRule.Rule)
default:
return nil, nil, fmt.Errorf("unknown rule type %T", r)
}

if rule != nil {
if _, exists := findRule(currentRules, rule.Name); !exists {
rules = append(rules, *rule)
}
}

if workflowRule != nil {
workflowRules = append(workflowRules, *workflowRule)
}

return rules, workflowRules, nil
}

func normalizeActions(nonNormalizedActions any) ([]string, error) {
var actions []string

switch action := nonNormalizedActions.(type) {
case string:
actions = append(actions, action)
case []any:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be []string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it's an interface{} type we are processing the type will be []any instead of []string

for _, rawAction := range action {
processedActions, err := normalizeActions(rawAction)
if err != nil {
return nil, err
}

actions = append(actions, processedActions...)
}
// we might have a workflow that doesn't have any actions
// but only has extra actions in the workflows
case nil:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why nil? What is the use case for nil? In this case shouldn't we return an empty list, i.e. actions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's in case the workflow doesn't have any actions but only extra actions associated with the rules

return nil, nil
default:
return nil, fmt.Errorf("unknown action type: %T", action)
}

return actions, nil
}
20 changes: 11 additions & 9 deletions engine/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ func (p PadLabel) equals(o PadLabel) bool {
}

type PadWorkflow struct {
Name string `yaml:"name"`
On []handler.TargetEntityKind `yaml:"on"`
Description string `yaml:"description"`
AlwaysRun bool `yaml:"always-run"`
Rules []PadWorkflowRule `yaml:"-"`
Actions []string `yaml:"then"`
NonNormalizedRules []interface{} `yaml:"if"`
Name string `yaml:"name"`
On []handler.TargetEntityKind `yaml:"on"`
Description string `yaml:"description"`
AlwaysRun bool `yaml:"always-run"`
Rules []PadWorkflowRule `yaml:"-"`
Actions []string `yaml:"-"`
NonNormalizedRules any `yaml:"if"`
NonNormalizedActions any `yaml:"then"`
}

func (p PadWorkflow) equals(o PadWorkflow) bool {
Expand Down Expand Up @@ -218,8 +219,9 @@ type PadPipeline struct {
}

type PadStage struct {
Actions []string `yaml:"actions"`
Until string `yaml:"until"`
Actions []string `yaml:"-"`
NonNormalizedActions any `yaml:"actions"`
Until string `yaml:"until"`
}

func (p PadPipeline) equals(o PadPipeline) bool {
Expand Down
2 changes: 1 addition & 1 deletion engine/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func Load(ctx context.Context, logger *logrus.Entry, githubClient *gh.GithubClie
return nil, err
}

file, err = normalize(file, inlineRulesNormalizer())
file, err = normalize(file, inlineNormalizer())
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions engine/loader_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ pipelines:
Trigger: "$size() <= 30",
Stages: []PadStage{
{
Actions: []string{
NonNormalizedActions: []any{
"$assignReviewer([\"marcelosousa\"])",
},
Until: "$reviewerStatus(\"marcelosousa\") == \"APPROVED\"",
},
{
Actions: []string{
NonNormalizedActions: []any{
"$assignReviewer([\"ferreiratiago\"])",
},
},
Expand Down
8 changes: 8 additions & 0 deletions engine/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ func TestLoad(t *testing.T) {
),
wantErr: "loader: cyclic extends dependency",
},
"when the file has multiple inline actions": {
inputReviewpadFilePath: "testdata/loader/process/reviewpad_with_multiple_inline_actions.yml",
wantReviewpadFilePath: "testdata/loader/process/reviewpad_with_multiple_inline_actions_after_processing.yml",
},
"when the file has multiple pipelines with compact actions": {
inputReviewpadFilePath: "testdata/loader/process/reviewpad_with_multiple_pipelines_with_inline_actions.yml",
wantReviewpadFilePath: "testdata/loader/process/reviewpad_with_multiple_pipelines_with_inline_actions_after_processing.yml",
},
}

for name, test := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2022 Explore.dev Unipessoal Lda. All Rights Reserved.
# Use of this source code is governed by a license that can be
# found in the LICENSE file.

api-version: reviewpad.com/v3.x

workflows:
- name: test-inline-action
if: '$size() <= 30'
then: '$addLabel("critical")'
- name: test-inline-action-2
if:
- '$size() <= 50'
then: '$addLabel("large")'
- name: test-inline-action-3
if:
- '$size() >= 90'
then:
- '$addLabel("very-large")'
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2022 Explore.dev Unipessoal Lda. All Rights Reserved.
# Use of this source code is governed by a license that can be
# found in the LICENSE file.

api-version: reviewpad.com/v3.x

rules:
- name: '$size() <= 30'
kind: patch
spec: '$size() <= 30'
- name: '$size() <= 50'
kind: patch
spec: '$size() <= 50'
- name: '$size() >= 90'
kind: patch
spec: '$size() >= 90'

workflows:
- name: test-inline-action
if:
- rule: '$size() <= 30'
then:
- '$addLabel("critical")'
- name: test-inline-action-2
if:
- rule: '$size() <= 50'
then:
- '$addLabel("large")'
- name: test-inline-action-3
if:
- rule: '$size() >= 90'
then:
- $addLabel("very-large")
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2022 Explore.dev Unipessoal Lda. All Rights Reserved.
# Use of this source code is governed by a license that can be
# found in the LICENSE file.

pipelines:
- name: renovate-bot
trigger: $author() == "renovate[bot]"
stages:
- actions: $assignAssignees($group("owners"), 1)
until: $assignees() != []
- actions:
- $comment("Pull request is not up to date with the base branch. Reviewpad will rebase it for you. Please wait for the rebase to complete.")
- $rebase()
until: $isUpdatedWithBaseBranch() && $hasLinearHistory()
- actions: $review("REQUEST_CHANGES", "The build is failing. Please fix it before continuing.")
until: $checkRunConclusion("pr-build") == "success"
- actions: $assignReviewer($group("owners"), 1)
until: $hasRequiredApprovals(1, $group("owners"))
- actions:
- $approve("Pull request is ready to be merged. Reviewpad will merge it for you. Please wait for the merge to complete.")
- $merge("rebase")
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2022 Explore.dev Unipessoal Lda. All Rights Reserved.
# Use of this source code is governed by a license that can be
# found in the LICENSE file.

pipelines:
- name: renovate-bot
trigger: $author() == "renovate[bot]"
stages:
- actions:
- $assignAssignees($group("owners"), 1)
until: $assignees() != []
- actions:
- $comment("Pull request is not up to date with the base branch. Reviewpad will rebase it for you. Please wait for the rebase to complete.")
- $rebase()
until: $isUpdatedWithBaseBranch() && $hasLinearHistory()
- actions:
- $review("REQUEST_CHANGES", "The build is failing. Please fix it before continuing.")
until: $checkRunConclusion("pr-build") == "success"
- actions:
- $assignReviewer($group("owners"), 1)
until: $hasRequiredApprovals(1, $group("owners"))
- actions:
- $approve("Pull request is ready to be merged. Reviewpad will merge it for you. Please wait for the merge to complete.")
- $merge("rebase")