@@ -25,6 +25,38 @@ import (
2525 "k8s.io/kubernetes/pkg/util/sets"
2626)
2727
28+ const (
29+ // BotName is the name of merge-bot
30+ BotName = "k8s-merge-robot"
31+ // JenkinsBotName is the name of kubekins bot
32+ JenkinsBotName = "k8s-bot"
33+ priorityPrefix = "priority/P"
34+ // PriorityP0 represents Priority P0
35+ PriorityP0 = Priority (0 )
36+ // PriorityP1 represents Priority P1
37+ PriorityP1 = Priority (1 )
38+ // PriorityP2 represents Priority P2
39+ PriorityP2 = Priority (2 )
40+ // PriorityP3 represents Priority P3
41+ PriorityP3 = Priority (3 )
42+ )
43+
44+ // RobotUser is a set of name of robot user
45+ var RobotUser = sets .NewString (JenkinsBotName , BotName )
46+
47+ // Priority represents the priority label in an issue
48+ type Priority int
49+
50+ // String return the priority label in string
51+ func (p Priority ) String () string {
52+ return fmt .Sprintf (priorityPrefix + "%d" , p )
53+ }
54+
55+ // Priority returns the priority in int
56+ func (p Priority ) Priority () int {
57+ return int (p )
58+ }
59+
2860// OwnerMapper finds an owner for a given test name.
2961type OwnerMapper interface {
3062 // TestOwner returns a GitHub username for a test, or "" if none are found.
@@ -59,6 +91,9 @@ type IssueSource interface {
5991
6092 // If an issue is filed, these labels will be applied.
6193 Labels () []string
94+
95+ // Priority calculates and returns the priority of an flake issue
96+ Priority (obj * github.MungeObject ) (Priority , error )
6297}
6398
6499// IssueSyncer implements robust issue syncing logic and won't file duplicates etc.
@@ -105,23 +140,25 @@ func (s *IssueSyncer) Sync(source IssueSource) error {
105140 return nil
106141 }
107142
143+ var obj * github.MungeObject
108144 // Update an issue if possible.
109145 if len (updatableIssues ) > 0 {
110- obj : = updatableIssues [0 ]
146+ obj = updatableIssues [0 ]
111147 // Update the chosen issue
112- if err : = s .updateIssue (obj , source ); err != nil {
148+ if err = s .updateIssue (obj , source ); err != nil {
113149 return fmt .Errorf ("error updating issue %v for %v: %v" , * obj .Issue .Number , source .ID (), err )
114150 }
115151 s .synced .Insert (source .ID ())
116152 return nil
117153 }
118154
119155 // No issue could be updated, create a new issue.
120- n , err : = s .createIssue (source )
156+ obj , err = s .createIssue (source )
121157 if err != nil {
122158 return fmt .Errorf ("error making issue for %v: %v" , source .ID , err )
123159 }
124- s .finder .Created (source .Title (), n )
160+ issueNum := * obj .Issue .Number
161+ s .finder .Created (source .Title (), issueNum )
125162 s .synced .Insert (source .ID ())
126163 return nil
127164}
@@ -196,12 +233,20 @@ func (s *IssueSyncer) updateIssue(obj *github.MungeObject, source IssueSource) e
196233 panic (fmt .Errorf ("Programmer error: %v does not contain %v!" , body , id ))
197234 }
198235 glog .Infof ("Updating issue %v with item %v" , * obj .Issue .Number , source .ID ())
199- return obj .WriteComment (body )
236+ if err := obj .WriteComment (body ); err != nil {
237+ return err
238+ }
239+ p , err := source .Priority (obj )
240+ if err != nil {
241+ return err
242+ }
243+ return s .syncPriority (obj , p )
244+
200245}
201246
202247// createIssue makes a new issue for the given item. If we know about other
203248// issues for the item, then they'll be referenced.
204- func (s * IssueSyncer ) createIssue (source IssueSource ) (issueNumber int , err error ) {
249+ func (s * IssueSyncer ) createIssue (source IssueSource ) (* github. MungeObject , error ) {
205250 body := source .Body (true )
206251 id := source .ID ()
207252 if ! strings .Contains (body , source .ID ()) {
@@ -221,8 +266,27 @@ func (s *IssueSyncer) createIssue(source IssueSource) (issueNumber int, err erro
221266 owner ,
222267 )
223268 if err != nil {
224- return 0 , err
269+ return nil , err
225270 }
226271 glog .Infof ("Created issue %v:\n %v" , * obj .Issue .Number , body )
227- return * obj .Issue .Number , nil
272+ return obj , nil
273+ }
274+
275+ // syncPriority will sync the input priority to the issue if the input priority is higher than the existing ones
276+ func (s * IssueSyncer ) syncPriority (obj * github.MungeObject , priority Priority ) error {
277+ if obj .Priority () <= priority .Priority () {
278+ return nil
279+ }
280+ plabels := github .GetLabelsWithPrefix (obj .Issue .Labels , priorityPrefix )
281+ err := obj .AddLabel (priority .String ())
282+ if err != nil {
283+ return nil
284+ }
285+ for _ , l := range plabels {
286+ err = obj .RemoveLabel (l )
287+ if err != nil {
288+ return err
289+ }
290+ }
291+ return nil
228292}
0 commit comments