package triageparent import ( "context" "fmt" "edge-infra.dev/pkg/lib/logging" "github.com/google/go-github/v47/github" "edge-infra.dev/pkg/f8n/devinfra/jack/constants" guestservices "edge-infra.dev/pkg/f8n/devinfra/jack/guest_services" "edge-infra.dev/pkg/f8n/devinfra/jack/plugin" ) // handle parent command in body when creating an issue // if there's no /parent command, and it's not an epic add triage parent func checkForParentInNewIssue(ctx context.Context, log logging.EdgeLogger, client plugin.GithubClientInterface, event github.IssuesEvent, org, repo string) error { labels := event.Issue.Labels isEpic := false for _, label := range labels { if label.GetName() == string(constants.Epic) { isEpic = true } } parentCommands := guestservices.GetCommands(event.GetIssue().GetBody()) if len(parentCommands.Parents.Added) <= 0 && !isEpic { newLabels := []string{constants.TriageParentLabel} _, _, err := client.Issues().AddLabelsToIssue(ctx, org, repo, event.Issue.GetNumber(), newLabels) if err != nil { log.Error(err, fmt.Sprintf("Failed to add %s", constants.TriageParentLabel)) return err } } return nil } func labelAdded(hp plugin.HandlerParams, event github.IssuesEvent) error { hp.Log.Info("triage labeled event") // if a bot added a label we don't want to handle it twice if event.GetSender().GetType() == "Bot" { return nil } // if epic label is added and the triage parent label exists remove it labels := event.Issue.Labels repoID := event.GetRepo().GetID() sender := guestservices.ParentChild{} sender.New(event.GetIssue().GetBody(), labels, constants.Preamble, constants.Postamble, repoID) isEpic := false for _, label := range labels { if label.GetName() == string(constants.Epic) { isEpic = true } } // if there's an epic label or // if there are parents in the list and the new label is triage parent // then just remove the triage parent label if isEpic || (len(sender.List.Parent) > 0 && event.Label.GetName() == constants.TriageParentLabel) { hp.Log.Info("removing needs parent label") _, err := hp.Client.Issues().RemoveLabelForIssue(hp.Ctx, hp.Org, hp.Repo, event.Issue.GetNumber(), constants.TriageParentLabel) if err != nil { hp.Log.Error(err, fmt.Sprintf("Failed to remove %s", constants.TriageParentLabel)) return err } } return nil } func labelRemoved(hp plugin.HandlerParams, event github.IssuesEvent) error { hp.Log.Info("triage unlabeled event") labels := event.Issue.Labels isEpic := false for _, label := range labels { if label.GetName() == string(constants.Epic) { isEpic = true } } repoID := event.GetRepo().GetID() sender := guestservices.ParentChild{} sender.New(event.GetIssue().GetBody(), labels, constants.Preamble, constants.Postamble, repoID) // if there's no epic label and no parents in the list add the triage parent label if !isEpic && len(sender.List.Parent) <= 0 { hp.Log.Info("adding needs parent label") // add triage newLabels := []string{"triage/needs-parent"} _, _, err := hp.Client.Issues().AddLabelsToIssue(hp.Ctx, hp.Org, hp.Repo, event.Issue.GetNumber(), newLabels) if err != nil { hp.Log.Error(err, fmt.Sprintf("Failed to add %s", constants.TriageParentLabel)) return err } } return nil }