sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/plugins/heart/heart_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package heart
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/sirupsen/logrus"
    23  
    24  	"sigs.k8s.io/prow/pkg/github"
    25  	"sigs.k8s.io/prow/pkg/github/fakegithub"
    26  	"sigs.k8s.io/prow/pkg/plugins/ownersconfig"
    27  )
    28  
    29  func TestHandlePR(t *testing.T) {
    30  	basicPR := github.PullRequest{
    31  		Number: 1,
    32  		Base: github.PullRequestBranch{
    33  			Repo: github.Repo{
    34  				Owner: github.User{
    35  					Login: "kubernetes",
    36  				},
    37  				Name: "kubernetes",
    38  			},
    39  		},
    40  	}
    41  
    42  	testcases := []struct {
    43  		prAction              github.PullRequestEventAction
    44  		changes               []github.PullRequestChange
    45  		expectedReactionAdded bool
    46  	}{
    47  		// PR opened against kubernetes/kubernetes that adds 1 line to
    48  		// an OWNERS file
    49  		{
    50  			prAction: github.PullRequestActionOpened,
    51  			changes: []github.PullRequestChange{
    52  				{
    53  					Filename:  "foo/bar/OWNERS",
    54  					Additions: 1,
    55  				},
    56  			},
    57  			expectedReactionAdded: true,
    58  		},
    59  		// PR opened against kubernetes/kubernetes that deletes 1 line
    60  		// from an OWNERS file
    61  		{
    62  			prAction: github.PullRequestActionOpened,
    63  			changes: []github.PullRequestChange{
    64  				{
    65  					Filename:  "foo/bar/OWNERS",
    66  					Deletions: 1,
    67  				},
    68  			},
    69  			expectedReactionAdded: false,
    70  		},
    71  		// PR opened against kubernetes/kubernetes with no changes to
    72  		// OWNERS
    73  		{
    74  			prAction: github.PullRequestActionOpened,
    75  			changes: []github.PullRequestChange{
    76  				{
    77  					Filename:  "foo/bar/foo.go",
    78  					Additions: 1,
    79  				},
    80  			},
    81  			expectedReactionAdded: false,
    82  		},
    83  		// PR reopened against kubernetes/kubernetes
    84  		{
    85  			prAction: github.PullRequestActionReopened,
    86  			changes: []github.PullRequestChange{
    87  				{
    88  					Filename:  "foo/bar/OWNERS",
    89  					Additions: 1,
    90  				},
    91  			},
    92  			expectedReactionAdded: false,
    93  		},
    94  		// PR opened against kubernetes/kubernetes that adds 1 line to
    95  		// an OWNERS_ALIASES file
    96  		{
    97  			prAction: github.PullRequestActionOpened,
    98  			changes: []github.PullRequestChange{
    99  				{
   100  					Filename:  "foo/bar/OWNERS_ALIASES",
   101  					Additions: 1,
   102  				},
   103  			},
   104  			expectedReactionAdded: true,
   105  		},
   106  	}
   107  
   108  	for _, tc := range testcases {
   109  		event := github.PullRequestEvent{
   110  			Action:      tc.prAction,
   111  			Number:      basicPR.Number,
   112  			PullRequest: basicPR,
   113  		}
   114  		fakeGitHubClient := fakegithub.NewFakeClient()
   115  		fakeGitHubClient.PullRequests = map[int]*github.PullRequest{
   116  			basicPR.Number: &basicPR,
   117  		}
   118  		fakeGitHubClient.PullRequestChanges = map[int][]github.PullRequestChange{
   119  			basicPR.Number: tc.changes,
   120  		}
   121  		fakeClient := client{
   122  			GitHubClient: fakeGitHubClient,
   123  			Logger:       logrus.WithField("plugin", pluginName),
   124  		}
   125  
   126  		err := handlePR(fakeClient, event, ownersconfig.FakeResolver)
   127  		if err != nil {
   128  			t.Fatal(err)
   129  		}
   130  
   131  		if len(fakeGitHubClient.IssueReactionsAdded) > 0 && !tc.expectedReactionAdded {
   132  			t.Fatalf("Expected no reactions to be added for %+v", tc)
   133  
   134  		} else if len(fakeGitHubClient.IssueReactionsAdded) == 0 && tc.expectedReactionAdded {
   135  			t.Fatalf("Expected reaction to be added for %+v", tc)
   136  		}
   137  	}
   138  }