github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/flagutil/bugzilla.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package flagutil
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"net/url"
    23  
    24  	"github.com/sirupsen/logrus"
    25  
    26  	"sigs.k8s.io/prow/pkg/bugzilla"
    27  	"sigs.k8s.io/prow/pkg/config/secret"
    28  )
    29  
    30  // BugzillaOptions holds options for interacting with Bugzilla.
    31  type BugzillaOptions struct {
    32  	endpoint                string
    33  	githubExternalTrackerId uint
    34  	ApiKeyPath              string
    35  	authMethod              string
    36  }
    37  
    38  // AddFlags injects Bugzilla options into the given FlagSet.
    39  func (o *BugzillaOptions) AddFlags(fs *flag.FlagSet) {
    40  	fs.StringVar(&o.endpoint, "bugzilla-endpoint", "", "Bugzilla's API endpoint.")
    41  	fs.UintVar(&o.githubExternalTrackerId, "bugzilla-github-external-tracker-id", 0, "The ext_type_id for GitHub external bugs, optional.")
    42  	fs.StringVar(&o.ApiKeyPath, "bugzilla-api-key-path", "", "Path to the file containing the Bugzilla API key.")
    43  	fs.StringVar(&o.authMethod, "bugzilla-auth-method", "", "Which authorization method will be used. Values can be bearer, query or x-bugzilla-api-key.")
    44  }
    45  
    46  // Validate validates Bugzilla options.
    47  func (o *BugzillaOptions) Validate(dryRun bool) error {
    48  	if o.endpoint == "" {
    49  		logrus.Info("empty -bugzilla-endpoint, will not create Bugzilla client")
    50  		return nil
    51  	}
    52  
    53  	if _, err := url.ParseRequestURI(o.endpoint); err != nil {
    54  		return fmt.Errorf("invalid -bugzilla-endpoint URI: %q", o.endpoint)
    55  	}
    56  
    57  	if o.ApiKeyPath == "" {
    58  		logrus.Info("empty -bugzilla-api-key-path, will use anonymous Bugzilla client")
    59  	}
    60  
    61  	if o.authMethod != "" {
    62  		if o.authMethod != "bearer" && o.authMethod != "query" && o.authMethod != "x-bugzilla-api-key" {
    63  			return fmt.Errorf("invalid --auth-method %s. Valid values are bearer,query or x-bugzilla-api-key", o.authMethod)
    64  		}
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  // BugzillaClient returns a Bugzilla client.
    71  func (o *BugzillaOptions) BugzillaClient() (bugzilla.Client, error) {
    72  	if o.endpoint == "" {
    73  		return nil, fmt.Errorf("empty -bugzilla-endpoint, cannot create Bugzilla client")
    74  	}
    75  
    76  	var generator *func() []byte
    77  	if o.ApiKeyPath == "" {
    78  		generatorFunc := func() []byte {
    79  			return []byte{}
    80  		}
    81  		generator = &generatorFunc
    82  	} else {
    83  		generatorFunc := secret.GetTokenGenerator(o.ApiKeyPath)
    84  		generator = &generatorFunc
    85  	}
    86  
    87  	return bugzilla.NewClient(*generator, o.endpoint, o.githubExternalTrackerId, o.authMethod), nil
    88  }