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: add extends capability to engine #515

Merged
merged 6 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: add unit test for extends
  • Loading branch information
marcelosousa committed Dec 9, 2022
commit 99c4853afba8fc07908f82b5bd8a656c7c77330c
95 changes: 64 additions & 31 deletions engine/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,77 +341,110 @@ func (r *ReviewpadFile) appendPipelines(o *ReviewpadFile) {
r.Pipelines = append(r.Pipelines, o.Pipelines...)
}

func (r *ReviewpadFile) extends(o *ReviewpadFile) {
// extend labels
if r.Labels == nil {
r.Labels = make(map[string]PadLabel)
func extendLabels(left, right map[string]PadLabel) map[string]PadLabel {
if left == nil && right == nil {
return nil
}

for labelName, label := range o.Labels {
if _, ok := r.Labels[labelName]; !ok {
r.Labels[labelName] = label
if left == nil {
left = make(map[string]PadLabel)
}

for labelName, label := range right {
if _, ok := left[labelName]; !ok {
left[labelName] = label
}
}

// extend groups
if r.Groups == nil {
r.Groups = make([]PadGroup, 0)
return left
}

func extendGroups(left, right []PadGroup) []PadGroup {
if left == nil && right == nil {
return nil
}

if left == nil {
left = make([]PadGroup, 0)
}

filteredGroups := make([]PadGroup, 0)

for _, group := range o.Groups {
if _, ok := findGroup(r.Groups, group.Name); !ok {
for _, group := range right {
if _, ok := findGroup(left, group.Name); !ok {
filteredGroups = append(filteredGroups, group)
}
}

r.Groups = append(filteredGroups, r.Groups...)
return append(filteredGroups, left...)
}

// extend rules
if r.Rules == nil {
r.Rules = make([]PadRule, 0)
func extendRules(left, right []PadRule) []PadRule {
if left == nil && right == nil {
return nil
}

if left == nil {
left = make([]PadRule, 0)
}

filteredRules := make([]PadRule, 0)

for _, rule := range o.Rules {
if _, ok := findRule(r.Rules, rule.Name); !ok {
for _, rule := range right {
if _, ok := findRule(left, rule.Name); !ok {
filteredRules = append(filteredRules, rule)
}
}

r.Rules = append(filteredRules, r.Rules...)
return append(filteredRules, left...)
}

// extend workflows
if r.Workflows == nil {
r.Workflows = make([]PadWorkflow, 0)
func extendWorkflows(left, right []PadWorkflow) []PadWorkflow {
if left == nil && right == nil {
return nil
}

if left == nil {
left = make([]PadWorkflow, 0)
}

filteredWorkflows := make([]PadWorkflow, 0)

for _, workflow := range o.Workflows {
if _, ok := findWorkflow(r.Workflows, workflow.Name); !ok {
for _, workflow := range right {
if _, ok := findWorkflow(left, workflow.Name); !ok {
filteredWorkflows = append(filteredWorkflows, workflow)
}
}

r.Workflows = append(filteredWorkflows, r.Workflows...)
return append(filteredWorkflows, left...)
}

// extend pipelines
if r.Pipelines == nil {
r.Pipelines = make([]PadPipeline, 0)
func extendPipelines(left, right []PadPipeline) []PadPipeline {
if left == nil && right == nil {
return nil
}

if left == nil {
left = make([]PadPipeline, 0)
}

filteredPipelines := make([]PadPipeline, 0)

for _, pipeline := range o.Pipelines {
if _, ok := findPipeline(r.Pipelines, pipeline.Name); !ok {
for _, pipeline := range right {
if _, ok := findPipeline(left, pipeline.Name); !ok {
filteredPipelines = append(filteredPipelines, pipeline)
}
}

r.Pipelines = append(filteredPipelines, r.Pipelines...)
return append(filteredPipelines, left...)
}

func (r *ReviewpadFile) extends(o *ReviewpadFile) {
r.Labels = extendLabels(r.Labels, o.Labels)
r.Groups = extendGroups(r.Groups, o.Groups)
r.Rules = extendRules(r.Rules, o.Rules)
r.Workflows = extendWorkflows(r.Workflows, o.Workflows)
r.Pipelines = extendPipelines(r.Pipelines, o.Pipelines)
}

func findGroup(groups []PadGroup, name string) (*PadGroup, bool) {
Expand Down
42 changes: 40 additions & 2 deletions engine/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ package engine_test
import (
"context"
"fmt"
"net/http"
"testing"

"github.com/google/go-github/v48/github"
"github.com/jarcoal/httpmock"
"github.com/migueleliasweb/go-github-mock/src/mock"
gh "github.com/reviewpad/reviewpad/v3/codehost/github"
"github.com/reviewpad/reviewpad/v3/engine"
"github.com/reviewpad/reviewpad/v3/engine/testutils"
"github.com/reviewpad/reviewpad/v3/lang/aladino"
"github.com/reviewpad/reviewpad/v3/utils"
"github.com/stretchr/testify/assert"
)
Expand All @@ -25,6 +30,8 @@ func TestLoad(t *testing.T) {
tests := map[string]struct {
httpMockResponders []httpMockResponder
inputReviewpadFilePath string
inputContext context.Context
inputGitHubClient *gh.GithubClient
wantReviewpadFilePath string
wantErr string
}{
Expand Down Expand Up @@ -116,6 +123,36 @@ func TestLoad(t *testing.T) {
inputReviewpadFilePath: "testdata/loader/process/reviewpad_with_multiple_inline_rules.yml",
wantReviewpadFilePath: "testdata/loader/process/reviewpad_with_multiple_inline_rules_after_processing.yml",
},
"when the file has an extends": {
inputReviewpadFilePath: "testdata/loader/reviewpad_with_extends.yml",
wantReviewpadFilePath: "testdata/loader/process/reviewpad_with_extends_after_processing.yml",
inputContext: context.Background(),
inputGitHubClient: aladino.MockDefaultGithubClient(
[]mock.MockBackendOption{
mock.WithRequestMatchHandler(
mock.GetReposContentsByOwnerByRepoByPath,
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
utils.MustWriteBytes(w, mock.MustMarshal(
[]github.RepositoryContent{
{
Name: github.String("reviewpad_with_no_extends.yml"),
DownloadURL: github.String("https://raw.githubusercontent.com/reviewpad_with_no_extends.yml"),
},
},
))
}),
),
mock.WithRequestMatch(
mock.EndpointPattern{
Pattern: "/reviewpad_with_no_extends.yml",
Method: "GET",
},
httpmock.File("testdata/loader/reviewpad_with_no_extends.yml").Bytes(),
),
},
nil,
),
},
}

for name, test := range tests {
Expand All @@ -134,7 +171,7 @@ func TestLoad(t *testing.T) {
assert.FailNow(t, "Error reading reviewpad file: %v", err)
}

wantReviewpadFile, err = testutils.ParseReviewpadFile(context.Background(), nil, wantReviewpadFileData)
wantReviewpadFile, err = testutils.ParseReviewpadFile(test.inputContext, test.inputGitHubClient, wantReviewpadFileData)
if err != nil {
assert.FailNow(t, "Error parsing reviewpad file: %v", err)
}
Expand All @@ -145,11 +182,12 @@ func TestLoad(t *testing.T) {
assert.FailNow(t, "Error reading reviewpad file: %v", err)
}

gotReviewpadFile, gotErr := engine.Load(context.Background(), nil, reviewpadFileData)
gotReviewpadFile, gotErr := engine.Load(test.inputContext, test.inputGitHubClient, reviewpadFileData)

if gotErr != nil && gotErr.Error() != test.wantErr {
assert.FailNow(t, "Load() error = %v, wantErr %v", gotErr, test.wantErr)
}

assert.Equal(t, wantReviewpadFile, gotReviewpadFile)
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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

labels:
small:
color: #aa12ab
medium:
color: #a8c3f7

groups:
- name: owners
kind: developers
spec: '["anonymous"]'

rules:
- name: is-medium
kind: patch
spec: $size() > 30 && $size() <= 100

- name: is-small
kind: patch
spec: $size() < 30

workflows:
- name: add-label-with-medium-size
alwaysRun: false
if:
- rule: is-medium
then:
- $addLabel("medium")

- name: info-owners
if:
- rule: $isElementOf($author(), $group("owners"))
then:
- $info("owner has authored a PR")

- name: add-label-with-small-size
if:
- rule: is-small
then:
- $addLabel("small")
29 changes: 29 additions & 0 deletions engine/testdata/loader/reviewpad_with_extends.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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

extends:
- https://github.com/reviewpad/reviewpad/blob/main/engine/testdata/reviewpad_with_no_extends.yml

labels:
small:
color: #aa12ab

groups:
- name: owners
kind: developers
spec: '["anonymous"]'

rules:
- name: is-small
kind: patch
spec: $size() < 30

workflows:
- name: add-label-with-small-size
if:
- rule: is-small
then:
- '$addLabel("small")'
45 changes: 45 additions & 0 deletions engine/testdata/loader/reviewpad_with_no_extends.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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

labels:
small:
color: #aaf1aa
medium:
color: #a8c3f7

groups:
- name: owners
kind: developers
spec: '["jane", "john"]'

rules:
- name: is-small
kind: patch
spec: $size() < 10

- name: is-medium
kind: patch
spec: $size() > 30 && $size() <= 100

workflows:
- name: add-label-with-small-size
if:
- rule: is-small
then:
- $addLabel("small")

- name: add-label-with-medium-size
alwaysRun: false
if:
- rule: is-medium
then:
- $addLabel("medium")

- name: info-owners
if:
- rule: $isElementOf($author(), $group("owners"))
then:
- $info("owner has authored a PR")