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/plugin" ) func handleNewIssue(hp plugin.HandlerParams, event AddRemoveEvent) error { log := hp.Log ctx := hp.Ctx client := hp.Client log.Info("Handling a new issue") log.Info(event.EpicBody) log.Info(fmt.Sprintf("%+v", len(event.EpicBody))) if len(event.EpicBody) <= 0 { log.Info("body is empty exiting") return nil } // Check the new issues body for jackbot commands commands := guestservices.GetCommands(event.EpicBody) log.Info(fmt.Sprintf("%+v", commands)) // If no commands continue if len(commands.Parents.Added) == 0 && len(commands.Children.Added) == 0 { log.Info("No commands found") return nil } // If there are issues then this is an epic isParent := false if len(commands.Children.Added) >= 1 && len(commands.Parents.Added) == 0 { isParent = true log.Info("its a parent") } // If the body has both /child and /parent commands we shouldnt add any and inform the user if len(commands.Parents.Added) > 0 && len(commands.Children.Added) > 0 { log.Info("Cannot mix /child and /parent commands") addEpics := []string{} addIssues := []string{} for _, item := range commands.Parents.Added { addEpics = append(addEpics, "#"+item) } for _, item := range commands.Children.Added { addIssues = append(addIssues, "#"+item) } addEpicsString := "/parent " + strings.Join(addEpics, " ") addIssuesString := "/child " + strings.Join(addIssues, " ") msg := fmt.Sprintf( "%s\n%s\n```%s```\n```%s```", "You cannot mix epic and issue commands.", "Pick one of the following and submit it in the comments below", addEpicsString, addIssuesString, ) issueComment := github.IssueComment{Body: &msg} if _, _, err := client.Issues().CreateComment(ctx, event.RepoOwner, event.RepoName, event.EpicNumber, &issueComment); err != nil { log.Error(err, "Failed to comment on issue") return err } return nil } log.Info("not mixed commands") // Add the list to the appropriate source errorList := []error{} //checks to see if the command is to create an epic or an issue if isParent { log.Info("adding child to parent") err := addIssue(hp, event, commands.Children.Added, false) if err != nil { errorList = append(errorList, err) } } else if len(commands.Parents.Added) > 0 { log.Info("adding parent to child") err := addIssue(hp, event, commands.Parents.Added, true) if err != nil { errorList = append(errorList, err) } } log.Info("after add calls") // Check if any errors occurred and log them if so if len(errorList) > 0 { log.Info("Some errors have occurred while adding a new issue:") for _, e := range errorList { log.Error(e, fmt.Sprintf("Error: %v", e)) } } return nil }