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
11 "edge-infra.dev/pkg/f8n/devinfra/jack/constants"
12 "edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
13 )
14
15 type AddRemoveEvent struct {
16 EpicNumber int
17 EpicTitle string
18 EpicBody string
19 RepoOwner string
20 RepoName string
21 RepoID int64
22 Labels []*github.Label
23 Milestone *github.Milestone
24 }
25
26 func init() {
27 plugin.RegisterIssueCommentHandler(constants.PluginList, handleIssueComment)
28 plugin.RegisterIssueHandler(constants.PluginList, handleIssue)
29 }
30
31 func handleIssueComment(hp plugin.HandlerParams, ce github.IssueCommentEvent) {
32 hp.Log.WithName(constants.PluginList)
33
34 log := hp.Log
35 ctx := hp.Ctx
36 client := hp.Client
37
38 if ce.GetAction() != "created" {
39 return
40 }
41
42 if ce.GetIssue().IsPullRequest() {
43 return
44 }
45
46 errorList := []error{}
47
48 log.Info("list plugin running")
49
50 body := ce.GetComment().GetBody()
51 commands := guestservices.GetCommands(body)
52
53 if commands.IsEmpty() {
54 log.Info("no command found in comment exiting")
55 return
56 }
57
58
59 hasParentLabel, _ := guestservices.CheckForParentLabel("", ce.Issue.Labels)
60 if !hasParentLabel {
61 log.Info("its a child")
62 }
63
64 event := AddRemoveEvent{
65 EpicNumber: ce.GetIssue().GetNumber(),
66 EpicTitle: ce.GetIssue().GetTitle(),
67 EpicBody: ce.GetIssue().GetBody(),
68 RepoOwner: ce.GetRepo().GetOwner().GetLogin(),
69 RepoName: ce.GetRepo().GetName(),
70 RepoID: ce.GetRepo().GetID(),
71 Labels: ce.GetIssue().Labels,
72 Milestone: ce.GetIssue().GetMilestone(),
73 }
74
75
76 if !hasParentLabel && commands.HasChildCommands() {
77 log.Info("children cant have children >:|")
78
79
80 parents := []string{}
81 for _, parent := range constants.Parents {
82 parents = append(parents, "`"+string(parent)+"`")
83 }
84 parentsString := strings.Join(parents, ", ")
85
86
87 msg := fmt.Sprintf(
88 "%s\n%s%v\n___\n%s",
89 "Cannot add children to an issue that is not a valid parent.",
90 "Valid parents:", parentsString,
91 "[Check the docs for more info](https://docs.edge-infra.dev/dev/project/#hierarchy)",
92 )
93 issueComment := github.IssueComment{Body: github.String(msg)}
94
95
96 if _, _, err := client.Issues().CreateComment(ctx, event.RepoOwner, event.RepoName, event.EpicNumber, &issueComment); err != nil {
97 log.Error(err, "Failed to comment on issue")
98 }
99 return
100 }
101
102
103 if len(commands.Children.Removed) > 0 {
104 log.Info("removing an issue")
105 err := removeIssue(hp, event, commands.Children.Removed, false)
106 if err != nil {
107 errorList = append(errorList, err)
108 }
109 }
110 if len(commands.Parents.Removed) > 0 {
111 log.Info("removing an issue")
112 err := removeIssue(hp, event, commands.Parents.Removed, true)
113 if err != nil {
114 errorList = append(errorList, err)
115 }
116 }
117
118
119 if len(commands.Children.Added) > 0 {
120 log.Info("adding an issue")
121 err := addIssue(hp, event, commands.Children.Added, false)
122 if err != nil {
123 errorList = append(errorList, err)
124 }
125 }
126 if len(commands.Parents.Added) > 0 {
127 log.Info("adding an issue")
128 err := addIssue(hp, event, commands.Parents.Added, true)
129 if err != nil {
130 errorList = append(errorList, err)
131 }
132 }
133
134
135 if len(errorList) > 0 {
136 log.Info("Some errors have occurred while adding or removing:")
137 for _, e := range errorList {
138 log.Error(e, fmt.Sprintf("Error: %v", e))
139 }
140 }
141 }
142
143 func handleIssue(hp plugin.HandlerParams, ce github.IssuesEvent) {
144 log := hp.Log
145
146 switch action := ce.GetAction(); action {
147 case "opened":
148 log.Info("------- got a new issue event")
149 event := AddRemoveEvent{
150 EpicNumber: ce.GetIssue().GetNumber(),
151 EpicTitle: ce.GetIssue().GetTitle(),
152 EpicBody: ce.GetIssue().GetBody(),
153 RepoOwner: ce.GetRepo().GetOwner().GetLogin(),
154 RepoName: ce.GetRepo().GetName(),
155 RepoID: ce.GetRepo().GetID(),
156 Labels: ce.GetIssue().Labels,
157 Milestone: ce.GetIssue().GetMilestone(),
158 }
159 err := handleNewIssue(hp, event)
160 if err != nil {
161 log.Error(err, "Failed to handle new issue")
162 }
163 }
164 }
165
View as plain text