package size import ( "fmt" "strings" "github.com/google/go-github/v47/github" guestservices "edge-infra.dev/pkg/f8n/devinfra/jack/guest_services" "edge-infra.dev/pkg/f8n/devinfra/jack/constants" "edge-infra.dev/pkg/f8n/devinfra/jack/plugin" ) // handleNewComment handles new comment events for the size plugin func handleNewComment(hp plugin.HandlerParams, ce github.IssueCommentEvent, commands []string) error { // if its an epic exit but comment that its not permitted hasParentLabel, _ := guestservices.CheckForParentLabel("", ce.GetIssue().Labels) if hasParentLabel { hp.Log.Info("its an epic dont add a size label") msg := "Epics cannot have size labels. Remove the epic label if you need to add a size." err := addCommentToIssue(hp.Ctx, hp.Client, ce.Issue.GetNumber(), ce.Repo.GetOwner().GetLogin(), ce.Repo.GetName(), msg) if err != nil { hp.Log.Error(err, "Failed to comment on issue") return err } return nil } // grab the first command if the first word is extra // assume it needs to be appended to the second word (extra large) tempCommand := commands[0] if len(commands) > 1 && commands[0] == "extra" { tempCommand = fmt.Sprintf("%s-%s", commands[0], commands[1]) } // Validate the size and if its not a valid size bounce size, validSize := validateSizeCommand(tempCommand) if !validSize { hp.Log.Info("command is not a valid size") return nil } // Gather the label names labelNames := []string{} labels := ce.GetIssue().Labels for _, label := range labels { name := label.GetName() // if the size command is for a size thats already there exit if name == size { hp.Log.Info(fmt.Sprintf("%s label already exists. Exiting.", size)) return nil } // only add none size labels to the list if !strings.Contains(name, "size/") && name != constants.TriageSizeLabel { labelNames = append(labelNames, name) } } // remove the old size and add the new labelNames = append(labelNames, size) githubIssueBody := &github.IssueRequest{Labels: &labelNames} _, _, err := hp.Client.Issues().Edit(hp.Ctx, ce.Repo.GetOwner().GetLogin(), ce.Repo.GetName(), ce.GetIssue().GetNumber(), githubIssueBody) if err != nil { hp.Log.Error(err, "Failed to update issue desc") return err } return nil } // check if the given size is valid return the constant for the appro size func validateSizeCommand(command string) (string, bool) { switch command { case "xs", "extra-small", "extra-smol", constants.ExtraSmallLabel: return constants.ExtraSmallLabel, true case "s", "small", "smol", constants.SmallLabel: return constants.SmallLabel, true case "m", "med", "medium", constants.MediumLabel: return constants.MediumLabel, true case "l", "large", constants.LargeLabel: return constants.LargeLabel, true case "xl", "extra-large", constants.ExtraLargeLabel: return constants.ExtraLargeLabel, true case constants.TriageSizeLabel: return constants.TriageSizeLabel, true } return "", false }