...

Source file src/edge-infra.dev/pkg/f8n/devinfra/jack/plugin/size/issue_comment.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/jack/plugin/size

     1  package size
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/google/go-github/v47/github"
     8  
     9  	guestservices "edge-infra.dev/pkg/f8n/devinfra/jack/guest_services"
    10  
    11  	"edge-infra.dev/pkg/f8n/devinfra/jack/constants"
    12  	"edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
    13  )
    14  
    15  // handleNewComment handles new comment events for the size plugin
    16  func handleNewComment(hp plugin.HandlerParams, ce github.IssueCommentEvent, commands []string) error {
    17  	// if its an epic exit but comment that its not permitted
    18  	hasParentLabel, _ := guestservices.CheckForParentLabel("", ce.GetIssue().Labels)
    19  	if hasParentLabel {
    20  		hp.Log.Info("its an epic dont add a size label")
    21  		msg := "Epics cannot have size labels. Remove the epic label if you need to add a size."
    22  		err := addCommentToIssue(hp.Ctx, hp.Client, ce.Issue.GetNumber(), ce.Repo.GetOwner().GetLogin(), ce.Repo.GetName(), msg)
    23  		if err != nil {
    24  			hp.Log.Error(err, "Failed to comment on issue")
    25  			return err
    26  		}
    27  		return nil
    28  	}
    29  
    30  	// grab the first command if the first word is extra
    31  	// assume it needs to be appended to the second word (extra large)
    32  	tempCommand := commands[0]
    33  	if len(commands) > 1 && commands[0] == "extra" {
    34  		tempCommand = fmt.Sprintf("%s-%s", commands[0], commands[1])
    35  	}
    36  
    37  	// Validate the size and if its not a valid size bounce
    38  	size, validSize := validateSizeCommand(tempCommand)
    39  	if !validSize {
    40  		hp.Log.Info("command is not a valid size")
    41  		return nil
    42  	}
    43  
    44  	// Gather the label names
    45  	labelNames := []string{}
    46  	labels := ce.GetIssue().Labels
    47  	for _, label := range labels {
    48  		name := label.GetName()
    49  
    50  		// if the size command is for a size thats already there exit
    51  		if name == size {
    52  			hp.Log.Info(fmt.Sprintf("%s label already exists. Exiting.", size))
    53  			return nil
    54  		}
    55  
    56  		// only add none size labels to the list
    57  		if !strings.Contains(name, "size/") && name != constants.TriageSizeLabel {
    58  			labelNames = append(labelNames, name)
    59  		}
    60  	}
    61  
    62  	// remove the old size and add the new
    63  	labelNames = append(labelNames, size)
    64  	githubIssueBody := &github.IssueRequest{Labels: &labelNames}
    65  	_, _, err := hp.Client.Issues().Edit(hp.Ctx, ce.Repo.GetOwner().GetLogin(), ce.Repo.GetName(), ce.GetIssue().GetNumber(), githubIssueBody)
    66  	if err != nil {
    67  		hp.Log.Error(err, "Failed to update issue desc")
    68  		return err
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  // check if the given size is valid return the constant for the appro size
    75  func validateSizeCommand(command string) (string, bool) {
    76  	switch command {
    77  	case "xs", "extra-small", "extra-smol", constants.ExtraSmallLabel:
    78  		return constants.ExtraSmallLabel, true
    79  	case "s", "small", "smol", constants.SmallLabel:
    80  		return constants.SmallLabel, true
    81  	case "m", "med", "medium", constants.MediumLabel:
    82  		return constants.MediumLabel, true
    83  	case "l", "large", constants.LargeLabel:
    84  		return constants.LargeLabel, true
    85  	case "xl", "extra-large", constants.ExtraLargeLabel:
    86  		return constants.ExtraLargeLabel, true
    87  	case constants.TriageSizeLabel:
    88  		return constants.TriageSizeLabel, true
    89  	}
    90  	return "", false
    91  }
    92  

View as plain text