1 package triageparent
2
3 import (
4 "fmt"
5 "reflect"
6 "sort"
7
8 "github.com/google/go-github/v47/github"
9
10 "edge-infra.dev/pkg/f8n/devinfra/jack/constants"
11 guestservices "edge-infra.dev/pkg/f8n/devinfra/jack/guest_services"
12 "edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
13 "edge-infra.dev/pkg/lib/logging"
14 )
15
16 func init() {
17 plugin.RegisterIssueHandler(constants.PluginParent, handleIssue)
18 plugin.RegisterIssueCommentHandler(constants.PluginParent, handleIssueComment)
19 }
20
21 func handleIssueComment(hp plugin.HandlerParams, ce github.IssueCommentEvent) {
22 hp.Log.WithName(constants.PluginParent)
23
24 if ce.GetAction() != "created" {
25 return
26 }
27
28 if ce.GetIssue().IsPullRequest() {
29 return
30 }
31
32 labels := ce.Issue.Labels
33 isEpic := guestservices.HasEpicLabel(labels)
34
35
36 if isEpic {
37 return
38 }
39 hp.Log.Info("Running triage_parent plugin")
40
41 body := ce.Issue.GetBody()
42 commentBody := ce.Comment.GetBody()
43 repoID := ce.GetRepo().GetID()
44
45 sender := guestservices.ParentChild{}
46 sender.New(body, labels, constants.Preamble, constants.Postamble, repoID)
47
48 parentCommands := guestservices.GetCommands(commentBody)
49
50
51
52
53 if len(sender.List.Parent) > 0 && len(parentCommands.Parents.Removed) < len(sender.List.Parent) {
54 hp.Log.Info(fmt.Sprintf("parents exist and no parents being removed. \nattempting to remove %s", constants.TriageParentLabel))
55 _, err := hp.Client.Issues().RemoveLabelForIssue(hp.Ctx, hp.Org, hp.Repo, ce.Issue.GetNumber(), constants.TriageParentLabel)
56 if err != nil {
57 hp.Log.Error(err, fmt.Sprintf("Failed to remove %s", constants.TriageParentLabel))
58 }
59 return
60 }
61
62 tmpList := []string{}
63 for _, rent := range sender.List.Parent {
64 tmpList = append(tmpList, fmt.Sprint(rent.Number))
65 }
66
67
68 sort.Strings(parentCommands.Parents.Removed)
69
70
71
72 if len(parentCommands.Parents.Removed) > 0 && reflect.DeepEqual(tmpList, parentCommands.Parents.Removed) {
73 hp.Log.Info(fmt.Sprintf("all parents are being removed. \nattempting to add %s", constants.TriageParentLabel))
74
75 newLabels := []string{constants.TriageParentLabel}
76 _, _, err := hp.Client.Issues().AddLabelsToIssue(hp.Ctx, hp.Org, hp.Repo, ce.Issue.GetNumber(), newLabels)
77 if err != nil {
78 hp.Log.Error(err, fmt.Sprintf("Failed to add %s", constants.TriageParentLabel))
79 }
80 return
81 }
82
83
84 if len(parentCommands.Parents.Added) == 0 && len(sender.List.Parent) == 0 {
85 hp.Log.Info(fmt.Sprintf("no parents being added \nattempting to add %s", constants.TriageParentLabel))
86
87 newLabels := []string{constants.TriageParentLabel}
88 _, _, err := hp.Client.Issues().AddLabelsToIssue(hp.Ctx, hp.Org, hp.Repo, ce.Issue.GetNumber(), newLabels)
89 if err != nil {
90 hp.Log.Error(err, fmt.Sprintf("Failed to add %s", constants.TriageParentLabel))
91 }
92 return
93 }
94
95
96 if len(parentCommands.Parents.Added) > 0 {
97 hp.Log.Info(fmt.Sprintf("parents exist \nattempting to remove %s", constants.TriageParentLabel))
98 _, err := hp.Client.Issues().RemoveLabelForIssue(hp.Ctx, hp.Org, hp.Repo, ce.Issue.GetNumber(), constants.TriageParentLabel)
99 if err != nil {
100 hp.Log.Error(err, fmt.Sprintf("Failed to remove %s", constants.TriageParentLabel))
101 }
102 return
103 }
104 }
105
106 func handleIssue(hp plugin.HandlerParams, ie github.IssuesEvent) {
107 hp.Log.WithName(constants.PluginParent)
108
109 log := *logging.NewLogger()
110 ctx := hp.Ctx
111 client := hp.Client
112
113 switch action := ie.GetAction(); action {
114 case "opened":
115 if err := checkForParentInNewIssue(ctx, log, client, ie, hp.Org, hp.Repo); err != nil {
116 log.Error(err, "Parent: Failed to check for parent in new issue")
117 }
118 case "labeled":
119 if err := checkLabelsAndCall(hp, ie, true); err != nil {
120 log.Error(err, "Parent: Failed to remove label from issue")
121 }
122 case "unlabeled":
123 if err := checkLabelsAndCall(hp, ie, false); err != nil {
124 log.Error(err, "Parent: Failed to add label to issue")
125 }
126 }
127 }
128
129
130 func checkLabelsAndCall(hp plugin.HandlerParams, ie github.IssuesEvent, add bool) error {
131 label := ie.GetLabel().GetName()
132
133
134 if label != string(constants.Epic) && label != constants.TriageParentLabel {
135 return nil
136 }
137
138 hp.Log.Info(fmt.Sprintf("its a label we care about: %s", label))
139
140 if add {
141 return labelAdded(hp, ie)
142 }
143 return labelRemoved(hp, ie)
144 }
145
View as plain text