go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/impl/handler/post_gerrit_msg.go (about)

     1  // Copyright 2023 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 handler
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"time"
    21  
    22  	"go.chromium.org/luci/cv/internal/run"
    23  	"go.chromium.org/luci/cv/internal/run/eventpb"
    24  	"go.chromium.org/luci/cv/internal/run/impl/state"
    25  )
    26  
    27  const (
    28  	// maxPostMessageDuration is the max time that a Run will be waiting for
    29  	// a message to be posted on every CL.
    30  	maxPostMessageDuration = 8 * time.Minute
    31  
    32  	logEntryLabelPostGerritMessage = "Posting Gerrit Message"
    33  )
    34  
    35  func (impl *Impl) onCompletedPostGerritMessage(ctx context.Context, rs *state.RunState, op *run.OngoingLongOps_Op, result *eventpb.LongOpCompleted) (*Result, error) {
    36  	opID := result.GetOperationId()
    37  	rs = rs.ShallowCopy()
    38  	rs.RemoveCompletedLongOp(opID)
    39  
    40  	switch result.GetStatus() {
    41  	case eventpb.LongOpCompleted_FAILED:
    42  		rs.LogInfof(ctx, logEntryLabelPostGerritMessage, "Failed to post gerrit message: %s", op.GetPostGerritMessage().GetMessage())
    43  	case eventpb.LongOpCompleted_EXPIRED:
    44  		rs.LogInfo(ctx, logEntryLabelPostGerritMessage, fmt.Sprintf("Failed to post the message to gerrit within the %s deadline", maxPostMessageDuration))
    45  	case eventpb.LongOpCompleted_SUCCEEDED:
    46  		rs.LogInfofAt(result.GetPostGerritMessage().GetTime().AsTime(), logEntryLabelPostGerritMessage, "posted the gerrit message on each CL: %s", op.GetPostGerritMessage().GetMessage())
    47  	case eventpb.LongOpCompleted_CANCELLED:
    48  		rs.LogInfo(ctx, logEntryLabelPostGerritMessage, "cancelled posting gerrit message on CL(s)")
    49  	default:
    50  		panic(fmt.Errorf("unexpected LongOpCompleted status: %s", result.GetStatus()))
    51  	}
    52  
    53  	return &Result{State: rs}, nil
    54  }