go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/config_service/internal/ui/config_set.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 ui
    16  
    17  import (
    18  	"strings"
    19  
    20  	"google.golang.org/grpc/codes"
    21  	"google.golang.org/grpc/status"
    22  	"google.golang.org/protobuf/types/known/fieldmaskpb"
    23  
    24  	"go.chromium.org/luci/common/logging"
    25  	"go.chromium.org/luci/config"
    26  	"go.chromium.org/luci/server/router"
    27  	"go.chromium.org/luci/server/templates"
    28  
    29  	"go.chromium.org/luci/config_service/internal/acl"
    30  	configpb "go.chromium.org/luci/config_service/proto"
    31  )
    32  
    33  var getConfigSetMask *fieldmaskpb.FieldMask
    34  
    35  func init() {
    36  	var err error
    37  	getConfigSetMask, err = fieldmaskpb.New(&configpb.ConfigSet{},
    38  		"name", "url", "revision", "last_import_attempt", "configs")
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  }
    43  
    44  func configSetPage(c *router.Context) error {
    45  	csName := config.Set(strings.Trim(c.Params.ByName("ConfigSet"), "/"))
    46  	if err := csName.Validate(); err != nil {
    47  		return status.Errorf(codes.InvalidArgument, "invalid config set %q: %s", csName, err)
    48  	}
    49  	ctx := c.Request.Context()
    50  	server := configsServer(ctx)
    51  	cs, err := server.GetConfigSet(ctx, &configpb.GetConfigSetRequest{
    52  		ConfigSet: string(csName),
    53  		Fields:    getConfigSetMask,
    54  	})
    55  	if err != nil {
    56  		return err
    57  	}
    58  	var canReimport bool
    59  	if canReimport, err = acl.CanReimportConfigSet(ctx, csName); err != nil {
    60  		logging.Warningf(ctx, "failed to check reimport acl: %s. continue without rendering reimport button", err)
    61  	}
    62  	templates.MustRender(ctx, c.Writer, "pages/config_set.html", map[string]any{
    63  		"CanReimport": canReimport,
    64  		"ConfigSet":   cs,
    65  	})
    66  	return nil
    67  }