go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/config/config.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 implements app-level configs for LUCI Analysis.
    16  package config
    17  
    18  import (
    19  	"context"
    20  
    21  	"google.golang.org/protobuf/proto"
    22  
    23  	"go.chromium.org/luci/common/errors"
    24  	"go.chromium.org/luci/config"
    25  	"go.chromium.org/luci/config/server/cfgcache"
    26  	"go.chromium.org/luci/config/validation"
    27  
    28  	configpb "go.chromium.org/luci/analysis/proto/config"
    29  )
    30  
    31  // Cached service config.
    32  var cachedCfg = cfgcache.Register(&cfgcache.Entry{
    33  	Path: "config.cfg",
    34  	Type: (*configpb.Config)(nil),
    35  	Validator: func(ctx *validation.Context, msg proto.Message) error {
    36  		validateConfig(ctx, msg.(*configpb.Config))
    37  		return nil
    38  	},
    39  })
    40  
    41  // Update fetches the latest config and puts it into the datastore.
    42  func Update(ctx context.Context) error {
    43  	var errs []error
    44  	if _, err := cachedCfg.Update(ctx, nil); err != nil {
    45  		errs = append(errs, err)
    46  	}
    47  	if err := updateProjects(ctx); err != nil {
    48  		errs = append(errs, err)
    49  	}
    50  	if len(errs) > 0 {
    51  		return errors.NewMultiError(errs...)
    52  	}
    53  	return nil
    54  }
    55  
    56  // Get returns the service-level config.
    57  func Get(ctx context.Context) (*configpb.Config, error) {
    58  	cfg, err := cachedCfg.Get(ctx, nil)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return cfg.(*configpb.Config), err
    63  }
    64  
    65  // SetTestConfig set test configs in the cachedCfg.
    66  func SetTestConfig(ctx context.Context, cfg *configpb.Config) error {
    67  	return cachedCfg.Set(ctx, cfg, &config.Meta{})
    68  }
    69  
    70  func WithBothBugSystems(f func(system configpb.BugSystem, name string)) func() {
    71  	return func() {
    72  		f(configpb.BugSystem_MONORAIL, "monorail")
    73  		f(configpb.BugSystem_BUGANIZER, "buganizer")
    74  	}
    75  }