go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/culpritaction/revertculprit/createrevert_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  	"fmt"
    20  	"testing"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  	"go.chromium.org/luci/bisection/model"
    24  	bisectionpb "go.chromium.org/luci/bisection/proto/v1"
    25  	"go.chromium.org/luci/bisection/util"
    26  	"go.chromium.org/luci/bisection/util/testutil"
    27  
    28  	buildbucketpb "go.chromium.org/luci/buildbucket/proto"
    29  	gerritpb "go.chromium.org/luci/common/proto/gerrit"
    30  	"go.chromium.org/luci/gae/impl/memory"
    31  	"go.chromium.org/luci/gae/service/datastore"
    32  )
    33  
    34  func TestGenerateRevertDescription(t *testing.T) {
    35  	t.Parallel()
    36  
    37  	Convey("generateRevertDescription", t, func() {
    38  		ctx := memory.Use(context.Background())
    39  
    40  		// Setup datastore
    41  		failedBuild, _, analysis := testutil.CreateCompileFailureAnalysisAnalysisChain(
    42  			ctx, 88128398584903, "chromium", 444)
    43  		heuristicAnalysis := &model.CompileHeuristicAnalysis{
    44  			ParentAnalysis: datastore.KeyForObj(ctx, analysis),
    45  		}
    46  		So(datastore.Put(ctx, heuristicAnalysis), ShouldBeNil)
    47  		datastore.GetTestable(ctx).CatchupIndexes()
    48  		suspect := &model.Suspect{
    49  			Id:             1,
    50  			Type:           model.SuspectType_Heuristic,
    51  			Score:          10,
    52  			ParentAnalysis: datastore.KeyForObj(ctx, heuristicAnalysis),
    53  			GitilesCommit: buildbucketpb.GitilesCommit{
    54  				Host:    "test.googlesource.com",
    55  				Project: "chromium/src",
    56  				Id:      "deadbeef",
    57  			},
    58  			ReviewUrl:          "https://test-review.googlesource.com/c/chromium/test/+/876543",
    59  			VerificationStatus: model.SuspectVerificationStatus_ConfirmedCulprit,
    60  			AnalysisType:       bisectionpb.AnalysisType_COMPILE_FAILURE_ANALYSIS,
    61  		}
    62  		So(datastore.Put(ctx, suspect), ShouldBeNil)
    63  		datastore.GetTestable(ctx).CatchupIndexes()
    64  
    65  		analysisURL := util.ConstructCompileAnalysisURL("chromium", failedBuild.Id)
    66  		buildURL := util.ConstructBuildURL(ctx, failedBuild.Id)
    67  		bugURL := util.ConstructBuganizerURLForAnalysis(analysisURL,
    68  			"https://test-review.googlesource.com/c/chromium/test/+/876543")
    69  
    70  		culprit := &gerritpb.ChangeInfo{
    71  			Number:          876543,
    72  			Project:         "chromium/src",
    73  			Status:          gerritpb.ChangeStatus_MERGED,
    74  			Subject:         "[TestTag] Added new feature",
    75  			CurrentRevision: "deadbeef",
    76  			Revisions: map[string]*gerritpb.RevisionInfo{
    77  				"deadbeef": {
    78  					Commit: &gerritpb.CommitInfo{
    79  						Author: &gerritpb.GitPersonInfo{
    80  							Name:  "John Doe",
    81  							Email: "jdoe@example.com",
    82  						},
    83  					},
    84  				},
    85  			},
    86  		}
    87  		Convey("culprit has no bug specified", func() {
    88  			culprit.Revisions["deadbeef"].Commit.Message = `[TestTag] Added new feature
    89  
    90  This is the body of the culprit CL.
    91  
    92  Change-Id: I100deadbeef`
    93  			description, err := generateRevertDescription(ctx, suspect, culprit)
    94  			So(err, ShouldBeNil)
    95  			So(description, ShouldEqual, fmt.Sprintf(`Revert "[TestTag] Added new feature"
    96  
    97  This reverts commit deadbeef.
    98  
    99  Reason for revert:
   100  LUCI Bisection has identified this change as the culprit of a build failure. See the analysis: %s
   101  
   102  Sample failed build: %s
   103  
   104  If this is a false positive, please report it at %s
   105  
   106  Original change's description:
   107  > [TestTag] Added new feature
   108  >
   109  > This is the body of the culprit CL.
   110  >
   111  > Change-Id: I100deadbeef
   112  
   113  No-Presubmit: true
   114  No-Tree-Checks: true
   115  No-Try: true`, analysisURL, buildURL, bugURL))
   116  		})
   117  
   118  		Convey("culprit has a bug specified with BUG =", func() {
   119  			culprit.Revisions["deadbeef"].Commit.Message = `[TestTag] Added new feature
   120  
   121  This is the body of the culprit CL.
   122  
   123  BUG = 563412
   124  Change-Id: I100deadbeef`
   125  			description, err := generateRevertDescription(ctx, suspect, culprit)
   126  			So(err, ShouldBeNil)
   127  			So(description, ShouldEqual, fmt.Sprintf(
   128  				`Revert "[TestTag] Added new feature"
   129  
   130  This reverts commit deadbeef.
   131  
   132  Reason for revert:
   133  LUCI Bisection has identified this change as the culprit of a build failure. See the analysis: %s
   134  
   135  Sample failed build: %s
   136  
   137  If this is a false positive, please report it at %s
   138  
   139  Original change's description:
   140  > [TestTag] Added new feature
   141  >
   142  > This is the body of the culprit CL.
   143  >
   144  > BUG = 563412
   145  > Change-Id: I100deadbeef
   146  
   147  BUG = 563412
   148  No-Presubmit: true
   149  No-Tree-Checks: true
   150  No-Try: true`, analysisURL, buildURL, bugURL))
   151  		})
   152  
   153  		Convey("culprit has bugs specified with Bug:", func() {
   154  			culprit.Revisions["deadbeef"].Commit.Message = `[TestTag] Added new feature
   155  
   156  This is the body of the culprit CL.
   157  
   158  Bug: 123
   159  Bug: 765
   160  Change-Id: I100deadbeef`
   161  			description, err := generateRevertDescription(ctx, suspect, culprit)
   162  			So(err, ShouldBeNil)
   163  			So(description, ShouldEqual, fmt.Sprintf(
   164  				`Revert "[TestTag] Added new feature"
   165  
   166  This reverts commit deadbeef.
   167  
   168  Reason for revert:
   169  LUCI Bisection has identified this change as the culprit of a build failure. See the analysis: %s
   170  
   171  Sample failed build: %s
   172  
   173  If this is a false positive, please report it at %s
   174  
   175  Original change's description:
   176  > [TestTag] Added new feature
   177  >
   178  > This is the body of the culprit CL.
   179  >
   180  > Bug: 123
   181  > Bug: 765
   182  > Change-Id: I100deadbeef
   183  
   184  Bug: 123
   185  Bug: 765
   186  No-Presubmit: true
   187  No-Tree-Checks: true
   188  No-Try: true`, analysisURL, buildURL, bugURL))
   189  		})
   190  
   191  		Convey("culprit has bug delimiter in description", func() {
   192  			culprit.Revisions["deadbeef"].Commit.Message = `[TestTag] Added new feature
   193  
   194  This is the body of the culprit CL.
   195  Bug link: https://bug-handler.test.com/b/id=1000123.
   196  
   197  Bug: 123
   198  Change-Id: I100deadbeef`
   199  			description, err := generateRevertDescription(ctx, suspect, culprit)
   200  			So(err, ShouldBeNil)
   201  			So(description, ShouldEqual, fmt.Sprintf(
   202  				`Revert "[TestTag] Added new feature"
   203  
   204  This reverts commit deadbeef.
   205  
   206  Reason for revert:
   207  LUCI Bisection has identified this change as the culprit of a build failure. See the analysis: %s
   208  
   209  Sample failed build: %s
   210  
   211  If this is a false positive, please report it at %s
   212  
   213  Original change's description:
   214  > [TestTag] Added new feature
   215  >
   216  > This is the body of the culprit CL.
   217  > Bug link: https://bug-handler.test.com/b/id=1000123.
   218  >
   219  > Bug: 123
   220  > Change-Id: I100deadbeef
   221  
   222  Bug: 123
   223  No-Presubmit: true
   224  No-Tree-Checks: true
   225  No-Try: true`, analysisURL, buildURL, bugURL))
   226  		})
   227  	})
   228  }