go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/bugs/buganizer/module.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 buganizer
    16  
    17  import (
    18  	"context"
    19  	"flag"
    20  
    21  	"go.chromium.org/luci/server/module"
    22  )
    23  
    24  // The modes for the buganizer integration client.
    25  // Each mode defines how LUCI Analysis will behave
    26  // with Buganizer.
    27  // `ModeDisable` is the default mode, and ignores Buganizer bugs.
    28  // `ModeProvided` uses the provided Buganizer integration client.
    29  const (
    30  	ModeDisable  = "disable"
    31  	ModeProvided = "provided"
    32  )
    33  
    34  // ModuleName can be used to refer to this module when declaring dependencies.
    35  var ModuleName = module.RegisterName("go.chromium.org/luci/analysis/internal/bugs/buganizer")
    36  
    37  // ModuleOptions contain configuartion for the Buganizer module.
    38  type ModuleOptions struct {
    39  	// Option for the buganizer management, defaults to `disable`
    40  	// Acceptable values are:
    41  	// `disable` - Indicates that Buganizer integration should not be used.
    42  	// `provided` - Indicates that Buganizer integration should use provided client.
    43  	// `fake` - Indicates that Buganizer integration should use the fake Buganizer implementation.
    44  	BuganizerClientMode string
    45  
    46  	// Option that indicates the base subdomain for the Buganizer endpoint.
    47  	// For example: placeholder-issuetracker-c2p, that value will
    48  	// be used by the clients to initiate connections with Buganizer.
    49  	BuganizerEndpointBase string
    50  
    51  	// Option for which OAuth scope to use for PerRPC credentials
    52  	// for the Buganizer client to use for authenticating requests.
    53  	// Example: "https://www.googleapis.com/auth/placeholder".
    54  	BuganizerEndpointOAuthScope string
    55  
    56  	// The component ID used for creating test issues in Buganizer.
    57  	BuganizerTestComponentId int64
    58  
    59  	// The email used by LUCI Analysis to file bugs.
    60  	BuganizerSelfEmail string
    61  
    62  	// Whether Buganizer is being used in a test environment.
    63  	BuganizerTestMode bool
    64  }
    65  
    66  // Register registers the command line flags.
    67  func (o *ModuleOptions) Register(f *flag.FlagSet) {
    68  	f.StringVar(
    69  		&o.BuganizerClientMode,
    70  		"buganizer-mode",
    71  		o.BuganizerClientMode,
    72  		"The Buganizer integration client mode to use.",
    73  	)
    74  	f.StringVar(
    75  		&o.BuganizerEndpointBase,
    76  		"buganizer-endpoint-base",
    77  		o.BuganizerEndpointBase,
    78  		"The base subdomain for Buganizer endpoint.",
    79  	)
    80  
    81  	f.StringVar(
    82  		&o.BuganizerEndpointOAuthScope,
    83  		"buganizer-endpoint-oauth-scope",
    84  		o.BuganizerEndpointOAuthScope,
    85  		"The Buganizer oauth scope to use for authenticating requests.",
    86  	)
    87  
    88  	f.Int64Var(
    89  		&o.BuganizerTestComponentId,
    90  		"buganizer-test-component-id",
    91  		o.BuganizerTestComponentId,
    92  		"The Buganizer component to be used for creating test issues.",
    93  	)
    94  
    95  	f.StringVar(
    96  		&o.BuganizerSelfEmail,
    97  		"buganizer-self-email",
    98  		o.BuganizerSelfEmail,
    99  		"The email that LUCI Analysis uses to file bugs. Used to distinguish actions taken by LUCI Analysis from user actions.",
   100  	)
   101  
   102  	f.BoolVar(
   103  		&o.BuganizerTestMode,
   104  		"buganizer-test-mode",
   105  		o.BuganizerTestMode,
   106  		"Indicates whether Buganizer is used in test mode.",
   107  	)
   108  }
   109  
   110  // NewModule returns a server module that sets a context value for Buganizer
   111  // integration mode.
   112  // That value can be used to determine how the server will interact
   113  // with Buganizer.
   114  func NewModule(opts *ModuleOptions) module.Module {
   115  	if opts == nil {
   116  		opts = &ModuleOptions{}
   117  	}
   118  	return &buganizerModule{opts: opts}
   119  }
   120  
   121  // NewModuleFromFlags is a variant of NewModule that initializes options through
   122  // command line flags.
   123  //
   124  // Calling this function registers flags in flag.CommandLine. They are usually
   125  // parsed in server.Main(...).
   126  func NewModuleFromFlags() module.Module {
   127  	opts := &ModuleOptions{}
   128  	opts.Register(flag.CommandLine)
   129  	return NewModule(opts)
   130  }
   131  
   132  // buganizerModule implements module.Module.
   133  type buganizerModule struct {
   134  	opts *ModuleOptions
   135  }
   136  
   137  // Name is part of module.Module interface.
   138  func (*buganizerModule) Name() module.Name {
   139  	return ModuleName
   140  }
   141  
   142  // Dependencies is part of module.Module interface.
   143  func (*buganizerModule) Dependencies() []module.Dependency {
   144  	return nil
   145  }
   146  
   147  // BuganizerClientModeKey the key to get the value for Buganizer integration
   148  // mode from the Context.
   149  var BuganizerClientModeKey = "go.chromium.org/luci/analysis/internal/bugs/buganizer:buganizerMode"
   150  
   151  // BuganizerEndpointBaseKey the key to get the value for Buganizer's endpoint
   152  // base subdomain from the Context.
   153  var BuganizerEndpointBaseKey = "go.chromium.org/luci/analysis/internal/bugs/buganizer:buganizerEndpointBase"
   154  
   155  // BuganizerEndpointOAuthScopeKey the key to get the value for Buganier OAuth scope
   156  // from the context.
   157  var BuganizerEndpointOAuthScopeKey = "go.chromium.org/luci/analysis/internal/bugs/buganizer:buganizerEndpointOAuthScopeKey"
   158  
   159  // BuganizerTestComponentIdKey the context key to get Buganizer test component id.
   160  var BuganizerTestComponentIdKey = "go.chromium.org/luci/analysis/internal/bugs/buganizer:buganizerTestComponentIdKey"
   161  
   162  // BuganizerSelfEmailKey the context key to get email that
   163  // LUCI Analysis uses to file bugs in Buganizer.
   164  var BuganizerSelfEmailKey = "go.chromium.org/luci/analysis/internal/bugs/buganizer:luciAnalysisBuganizerEmailKey"
   165  
   166  // BuganizerTestModeKey the context key to get whether Buganizer is in test mode.
   167  var BuganizerTestModeKey = "go.chromium.org/luci/analysis/internal/bugs/buganizer:buganizerTestModeKey"
   168  
   169  // Initialize is part of module.Module interface.
   170  func (m *buganizerModule) Initialize(ctx context.Context, host module.Host, opts module.HostOptions) (context.Context, error) {
   171  	ctx = context.WithValue(ctx, &BuganizerClientModeKey, m.opts.BuganizerClientMode)
   172  	ctx = context.WithValue(ctx, &BuganizerEndpointBaseKey, m.opts.BuganizerEndpointBase)
   173  	ctx = context.WithValue(ctx, &BuganizerEndpointOAuthScopeKey, m.opts.BuganizerEndpointOAuthScope)
   174  	ctx = context.WithValue(ctx, &BuganizerTestComponentIdKey, m.opts.BuganizerTestComponentId)
   175  	ctx = context.WithValue(ctx, &BuganizerSelfEmailKey, m.opts.BuganizerSelfEmail)
   176  	ctx = context.WithValue(ctx, &BuganizerTestModeKey, m.opts.BuganizerTestMode)
   177  	return ctx, nil
   178  }