package list 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" ) type AddRemoveEvent struct { EpicNumber int EpicTitle string EpicBody string RepoOwner string RepoName string RepoID int64 Labels []*github.Label Milestone *github.Milestone } func init() { plugin.RegisterIssueCommentHandler(constants.PluginList, handleIssueComment) plugin.RegisterIssueHandler(constants.PluginList, handleIssue) } func handleIssueComment(hp plugin.HandlerParams, ce github.IssueCommentEvent) { hp.Log.WithName(constants.PluginList) log := hp.Log ctx := hp.Ctx client := hp.Client if ce.GetAction() != "created" { return } if ce.GetIssue().IsPullRequest() { return } errorList := []error{} log.Info("list plugin running") body := ce.GetComment().GetBody() commands := guestservices.GetCommands(body) if commands.IsEmpty() { log.Info("no command found in comment exiting") return } // check if there's a parent label hasParentLabel, _ := guestservices.CheckForParentLabel("", ce.Issue.Labels) if !hasParentLabel { log.Info("its a child") } event := AddRemoveEvent{ EpicNumber: ce.GetIssue().GetNumber(), EpicTitle: ce.GetIssue().GetTitle(), EpicBody: ce.GetIssue().GetBody(), RepoOwner: ce.GetRepo().GetOwner().GetLogin(), RepoName: ce.GetRepo().GetName(), RepoID: ce.GetRepo().GetID(), Labels: ce.GetIssue().Labels, Milestone: ce.GetIssue().GetMilestone(), } // if its not a parent and has either add or remove child commands bounce if !hasParentLabel && commands.HasChildCommands() { log.Info("children cant have children >:|") // grab the parents and format them parents := []string{} for _, parent := range constants.Parents { parents = append(parents, "`"+string(parent)+"`") } parentsString := strings.Join(parents, ", ") // create the comment string msg := fmt.Sprintf( "%s\n%s%v\n___\n%s", "Cannot add children to an issue that is not a valid parent.", "Valid parents:", parentsString, "[Check the docs for more info](https://docs.edge-infra.dev/dev/project/#hierarchy)", ) issueComment := github.IssueComment{Body: github.String(msg)} // send the comment if _, _, err := client.Issues().CreateComment(ctx, event.RepoOwner, event.RepoName, event.EpicNumber, &issueComment); err != nil { log.Error(err, "Failed to comment on issue") } return } // Removing issue(s) if len(commands.Children.Removed) > 0 { log.Info("removing an issue") err := removeIssue(hp, event, commands.Children.Removed, false) if err != nil { errorList = append(errorList, err) } } if len(commands.Parents.Removed) > 0 { log.Info("removing an issue") err := removeIssue(hp, event, commands.Parents.Removed, true) if err != nil { errorList = append(errorList, err) } } // Adding issue(s) if len(commands.Children.Added) > 0 { log.Info("adding an issue") err := addIssue(hp, event, commands.Children.Added, false) if err != nil { errorList = append(errorList, err) } } if len(commands.Parents.Added) > 0 { log.Info("adding an issue") err := addIssue(hp, event, commands.Parents.Added, true) if err != nil { errorList = append(errorList, err) } } // If any of the funcs returned an error print it here if len(errorList) > 0 { log.Info("Some errors have occurred while adding or removing:") for _, e := range errorList { log.Error(e, fmt.Sprintf("Error: %v", e)) } } } func handleIssue(hp plugin.HandlerParams, ce github.IssuesEvent) { log := hp.Log switch action := ce.GetAction(); action { case "opened": log.Info("------- got a new issue event") event := AddRemoveEvent{ EpicNumber: ce.GetIssue().GetNumber(), EpicTitle: ce.GetIssue().GetTitle(), EpicBody: ce.GetIssue().GetBody(), RepoOwner: ce.GetRepo().GetOwner().GetLogin(), RepoName: ce.GetRepo().GetName(), RepoID: ce.GetRepo().GetID(), Labels: ce.GetIssue().Labels, Milestone: ce.GetIssue().GetMilestone(), } err := handleNewIssue(hp, event) if err != nil { log.Error(err, "Failed to handle new issue") } } }