go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/culpritaction/revertculprit/irrevertibleauthor_test.go (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package revertculprit
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  
    23  	gerritpb "go.chromium.org/luci/common/proto/gerrit"
    24  )
    25  
    26  func TestHasIrrevertibleAuthor(t *testing.T) {
    27  	ctx := context.Background()
    28  
    29  	Convey("HasIrrevertibleAuthor", t, func() {
    30  		change := &gerritpb.ChangeInfo{
    31  			Project:         "chromium/test/src",
    32  			Number:          234567,
    33  			CurrentRevision: "deadbeef",
    34  			Revisions: map[string]*gerritpb.RevisionInfo{
    35  				"deadbeef": {
    36  					Number: 1,
    37  					Kind:   gerritpb.RevisionInfo_REWORK,
    38  					Uploader: &gerritpb.AccountInfo{
    39  						AccountId:       1000096,
    40  						Name:            "John Doe",
    41  						Email:           "jdoe@example.com",
    42  						SecondaryEmails: []string{"johndoe@chromium.org"},
    43  						Username:        "jdoe",
    44  					},
    45  					Ref:         "refs/changes/123",
    46  					Description: "first upload",
    47  					Files: map[string]*gerritpb.FileInfo{
    48  						"go/to/file.go": {
    49  							LinesInserted: 32,
    50  							LinesDeleted:  44,
    51  							SizeDelta:     -567,
    52  							Size:          11984,
    53  						},
    54  					},
    55  					Commit: &gerritpb.CommitInfo{
    56  						Id:      "",
    57  						Message: "Title.\n\nBody is here.\n\nNOAUTOREVERT=true\n\nChange-Id: I100deadbeef",
    58  						Parents: []*gerritpb.CommitInfo_Parent{
    59  							{Id: "deadbeef00"},
    60  						},
    61  					},
    62  				},
    63  			},
    64  		}
    65  
    66  		Convey("author is revertible", func() {
    67  			change.Revisions["deadbeef"].Commit.Author = &gerritpb.GitPersonInfo{
    68  				Name:  "John Doe",
    69  				Email: "jdoe@example.com",
    70  			}
    71  
    72  			cannotRevert, err := HasIrrevertibleAuthor(ctx, change)
    73  			So(err, ShouldBeNil)
    74  			So(cannotRevert, ShouldEqual, false)
    75  		})
    76  
    77  		Convey("author is irrevertible with exact match", func() {
    78  			change.Revisions["deadbeef"].Commit.Author = &gerritpb.GitPersonInfo{
    79  				Name:  "ChromeOS Commit Bot",
    80  				Email: "chromeos-commit-bot@chromium.org",
    81  			}
    82  
    83  			cannotRevert, err := HasIrrevertibleAuthor(ctx, change)
    84  			So(err, ShouldBeNil)
    85  			So(cannotRevert, ShouldEqual, true)
    86  		})
    87  
    88  		Convey("author is irrevertible with pattern match", func() {
    89  			change.Revisions["deadbeef"].Commit.Author = &gerritpb.GitPersonInfo{
    90  				Name:  "Example Service Account",
    91  				Email: "examplechromiumtest-autoroll@skia-buildbots.iam.gserviceaccount.com",
    92  			}
    93  
    94  			cannotRevert, err := HasIrrevertibleAuthor(ctx, change)
    95  			So(err, ShouldBeNil)
    96  			So(cannotRevert, ShouldEqual, true)
    97  		})
    98  
    99  		Convey("author is irrevertible with pattern match extended", func() {
   100  			change.Revisions["deadbeef"].Commit.Author = &gerritpb.GitPersonInfo{
   101  				Name:  "Another Example Service Account",
   102  				Email: "chromium-autoroll@skia-corp.google.com.iam.gserviceaccount.com",
   103  			}
   104  
   105  			cannotRevert, err := HasIrrevertibleAuthor(ctx, change)
   106  			So(err, ShouldBeNil)
   107  			So(cannotRevert, ShouldEqual, true)
   108  		})
   109  	})
   110  }