...
1 package list
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 "edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
11 )
12
13 func handleNewIssue(hp plugin.HandlerParams, event AddRemoveEvent) error {
14 log := hp.Log
15 ctx := hp.Ctx
16 client := hp.Client
17
18 log.Info("Handling a new issue")
19
20 log.Info(event.EpicBody)
21 log.Info(fmt.Sprintf("%+v", len(event.EpicBody)))
22
23 if len(event.EpicBody) <= 0 {
24 log.Info("body is empty exiting")
25 return nil
26 }
27
28
29 commands := guestservices.GetCommands(event.EpicBody)
30 log.Info(fmt.Sprintf("%+v", commands))
31
32
33 if len(commands.Parents.Added) == 0 && len(commands.Children.Added) == 0 {
34 log.Info("No commands found")
35 return nil
36 }
37
38
39 isParent := false
40 if len(commands.Children.Added) >= 1 && len(commands.Parents.Added) == 0 {
41 isParent = true
42 log.Info("its a parent")
43 }
44
45
46 if len(commands.Parents.Added) > 0 && len(commands.Children.Added) > 0 {
47 log.Info("Cannot mix /child and /parent commands")
48
49 addEpics := []string{}
50 addIssues := []string{}
51 for _, item := range commands.Parents.Added {
52 addEpics = append(addEpics, "#"+item)
53 }
54
55 for _, item := range commands.Children.Added {
56 addIssues = append(addIssues, "#"+item)
57 }
58
59 addEpicsString := "/parent " + strings.Join(addEpics, " ")
60 addIssuesString := "/child " + strings.Join(addIssues, " ")
61
62 msg := fmt.Sprintf(
63 "%s\n%s\n```%s```\n```%s```",
64 "You cannot mix epic and issue commands.",
65 "Pick one of the following and submit it in the comments below",
66 addEpicsString,
67 addIssuesString,
68 )
69 issueComment := github.IssueComment{Body: &msg}
70
71 if _, _, err := client.Issues().CreateComment(ctx, event.RepoOwner, event.RepoName, event.EpicNumber, &issueComment); err != nil {
72 log.Error(err, "Failed to comment on issue")
73 return err
74 }
75 return nil
76 }
77 log.Info("not mixed commands")
78
79
80 errorList := []error{}
81
82 if isParent {
83 log.Info("adding child to parent")
84 err := addIssue(hp, event, commands.Children.Added, false)
85 if err != nil {
86 errorList = append(errorList, err)
87 }
88 } else if len(commands.Parents.Added) > 0 {
89 log.Info("adding parent to child")
90 err := addIssue(hp, event, commands.Parents.Added, true)
91 if err != nil {
92 errorList = append(errorList, err)
93 }
94 }
95
96 log.Info("after add calls")
97
98
99 if len(errorList) > 0 {
100 log.Info("Some errors have occurred while adding a new issue:")
101 for _, e := range errorList {
102 log.Error(e, fmt.Sprintf("Error: %v", e))
103 }
104 }
105
106 return nil
107 }
108
View as plain text