go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/internal/config/gerrit.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 config
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"go.chromium.org/luci/bisection/model"
    22  	configpb "go.chromium.org/luci/bisection/proto/config"
    23  	bisectionpb "go.chromium.org/luci/bisection/proto/v1"
    24  	"go.chromium.org/luci/bisection/util/datastoreutil"
    25  )
    26  
    27  func GetGerritCfgForSuspect(ctx context.Context, suspect *model.Suspect, project string) (*configpb.GerritConfig, error) {
    28  	cfg, err := Project(ctx, project)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	switch suspect.AnalysisType {
    33  	case bisectionpb.AnalysisType_COMPILE_FAILURE_ANALYSIS:
    34  		return cfg.CompileAnalysisConfig.GerritConfig, nil
    35  	case bisectionpb.AnalysisType_TEST_FAILURE_ANALYSIS:
    36  		return cfg.TestAnalysisConfig.GerritConfig, nil
    37  	}
    38  	return nil, fmt.Errorf("unknown analysis type of suspect %s", suspect.AnalysisType.String())
    39  }
    40  
    41  // CanCreateRevert returns:
    42  //   - whether a revert can be created;
    43  //   - the reason it cannot be created if applicable; and
    44  //   - the error if one occurred.
    45  func CanCreateRevert(ctx context.Context, gerritCfg *configpb.GerritConfig, analysisType bisectionpb.AnalysisType) (bool, string, error) {
    46  	// Check if Gerrit actions are enabled
    47  	if !gerritCfg.ActionsEnabled {
    48  		reason := "all Gerrit actions are disabled"
    49  		return false, reason, nil
    50  	}
    51  
    52  	// Check if revert creation is enabled
    53  	if !gerritCfg.CreateRevertSettings.Enabled {
    54  		reason := "LUCI Bisection's revert creation has been disabled"
    55  		return false, reason, nil
    56  	}
    57  
    58  	// Check the daily limit for revert creations has not been reached
    59  	createdCount, err := datastoreutil.CountLatestRevertsCreated(ctx, 24, analysisType)
    60  	if err != nil {
    61  		return false, "", err
    62  	}
    63  	if createdCount >= int64(gerritCfg.CreateRevertSettings.DailyLimit) {
    64  		// revert creation daily limit has been reached
    65  		reason := fmt.Sprintf("LUCI Bisection's daily limit for revert creation"+
    66  			" (%d) has been reached; %d reverts have already been created",
    67  			gerritCfg.CreateRevertSettings.DailyLimit, createdCount)
    68  		return false, reason, nil
    69  	}
    70  
    71  	return true, "", nil
    72  }
    73  
    74  // CanSubmitRevert returns:
    75  //   - whether a revert can be submitted;
    76  //   - the reason it cannot be submitted if applicable; and
    77  //   - the error if one occurred.
    78  func CanSubmitRevert(ctx context.Context, gerritCfg *configpb.GerritConfig) (bool, string, error) {
    79  	// Check if Gerrit actions are enabled
    80  	if !gerritCfg.ActionsEnabled {
    81  		reason := "all Gerrit actions are disabled"
    82  		return false, reason, nil
    83  	}
    84  
    85  	// Check if revert submission is enabled
    86  	if !gerritCfg.SubmitRevertSettings.Enabled {
    87  		reason := "LUCI Bisection's revert submission has been disabled"
    88  		return false, reason, nil
    89  	}
    90  
    91  	// Check the daily limit for revert submissions has not been reached
    92  	committedCount, err := datastoreutil.CountLatestRevertsCommitted(ctx, 24)
    93  	if err != nil {
    94  		return false, "", err
    95  	}
    96  	if committedCount >= int64(gerritCfg.SubmitRevertSettings.DailyLimit) {
    97  		reason := fmt.Sprintf("LUCI Bisection's daily limit for revert submission"+
    98  			" (%d) has been reached; %d reverts have already been submitted",
    99  			gerritCfg.SubmitRevertSettings.DailyLimit, committedCount)
   100  		return false, reason, nil
   101  	}
   102  
   103  	return true, "", nil
   104  }