go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/bugs/buganizer/buganizer_tester.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 buganizer
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"math/rand"
    21  
    22  	"go.chromium.org/luci/common/errors"
    23  	"go.chromium.org/luci/common/logging"
    24  	"go.chromium.org/luci/third_party/google.golang.org/genproto/googleapis/devtools/issuetracker/v1"
    25  )
    26  
    27  func CreateSampleIssue(ctx context.Context) (int64, error) {
    28  	buganizerClientMode := ctx.Value(&BuganizerClientModeKey)
    29  	if buganizerClientMode == nil || buganizerClientMode != ModeProvided {
    30  		return 0, errors.New("Sample issues are only availabe in buganizer Mode \"provided\".")
    31  	}
    32  
    33  	componentId := ctx.Value(&BuganizerTestComponentIdKey)
    34  
    35  	if componentId == nil {
    36  		return 0, errors.New("Test component id is required to create sample issues.")
    37  	}
    38  
    39  	issue := &issuetracker.Issue{
    40  		IssueComment: &issuetracker.IssueComment{
    41  			Comment: fmt.Sprintf("A test issue description %d", rand.Int()),
    42  		},
    43  		IssueState: &issuetracker.IssueState{
    44  			ComponentId: componentId.(int64),
    45  			Type:        issuetracker.Issue_BUG,
    46  			Priority:    issuetracker.Issue_P2,
    47  			Severity:    issuetracker.Issue_S2,
    48  			Title:       fmt.Sprintf("A test issue %d", rand.Int()),
    49  		},
    50  	}
    51  	client, err := NewRPCClient(ctx)
    52  
    53  	if err != nil {
    54  		return 0, errors.Annotate(err, "create sample issue rppc client").Err()
    55  	}
    56  
    57  	createdIssue, err := client.Client.CreateIssue(ctx, &issuetracker.CreateIssueRequest{
    58  		Issue: issue,
    59  	})
    60  	if err != nil {
    61  		return 0, errors.Annotate(err, "create sample issue sending request").Err()
    62  	}
    63  	logging.Infof(ctx, "Created a sample issue with id: %d", createdIssue.IssueId)
    64  	return createdIssue.IssueId, nil
    65  }