@@ -18,6 +18,11 @@ type MergeableEventData interface {
1818	Merge (mergeMethod  string )
1919}
2020
21+ type  HasReviewersAPIEventData  interface  {
22+ 	GetReviewers () map [string ]string 
23+ 	GetApprovals () []string 
24+ }
25+ 
2126func  (a  * action ) merge (meta  bot.EventData ) {
2227	if  a .rule .Label  ==  ""  {
2328		mergeable , ok  :=  meta .(MergeableEventData )
@@ -32,12 +37,28 @@ func (a *action) merge(meta bot.EventData) {
3237	}
3338}
3439
35- func  (a  * action ) Apply ( config  bot. Configuration ,  meta  bot.EventData ) {
40+ func  (a  * action ) getApprovalsFromAPI ( meta  bot.EventData ) ( int ,  bool ) {
3641	assigneesList  :=  meta .GetAssignees ()
37- 	if  len (assigneesList ) ==  0  {
38- 		util .Logger .Debug ("No assignees to issue - skipping" )
39- 		return 
42+ 	approvals  :=  0 
43+ 	assignees  :=  util.StringSet {}
44+ 	assignees .AddAll (assigneesList )
45+ 	reviewApi , ok  :=  meta .(HasReviewersAPIEventData )
46+ 	if  ! ok  {
47+ 		util .Logger .Warning ("Event data does not support reviewers API. Check your configuration" )
48+ 		a .err  =  fmt .Errorf ("Event data does not support reviewers API" )
49+ 	} else  {
50+ 		for  _ , approver  :=  range  reviewApi .GetApprovals () {
51+ 			if  assignees .Contains (approver ) {
52+ 				assignees .Remove (approver )
53+ 				approvals ++ 
54+ 			}
55+ 		}
4056	}
57+ 	return  approvals , assignees .Len () ==  0 
58+ }
59+ 
60+ func  (a  * action ) getApprovalsFromComments (meta  bot.EventData ) (int , bool ) {
61+ 	assigneesList  :=  meta .GetAssignees ()
4162	approvals  :=  0 
4263	assignees  :=  util.StringSet {}
4364	assignees .AddAll (assigneesList )
@@ -51,12 +72,30 @@ func (a *action) Apply(config bot.Configuration, meta bot.EventData) {
5172			approvals ++ 
5273		}
5374	}
54- 	if  a .rule .Require  ==  0  &&  assignees .Len () ==  0  {
55- 		util .Logger .Debug ("All assignees have approved the PR - merging" )
56- 		a .merge (meta )
57- 	} else  if  a .rule .Require  >  0  &&  approvals  >=  a .rule .Require  {
58- 		util .Logger .Debug ("Got %d required approvals for PR - merging" , a .rule .Require )
59- 		a .merge (meta )
75+ 	return  approvals , assignees .Len () ==  0 
76+ }
77+ 
78+ func  (a  * action ) Apply (config  bot.Configuration , meta  bot.EventData ) {
79+ 	assigneesList  :=  meta .GetAssignees ()
80+ 	if  len (assigneesList ) ==  0  {
81+ 		util .Logger .Debug ("No assignees to issue - skipping" )
82+ 		return 
83+ 	}
84+ 	calls  :=  []func (bot.EventData ) (int , bool ){
85+ 		a .getApprovalsFromAPI ,
86+ 		a .getApprovalsFromComments ,
87+ 	}
88+ 	for  _ , call  :=  range  calls  {
89+ 		approvals , all  :=  call (meta )
90+ 		if  a .rule .Require  ==  0  &&  all  {
91+ 			util .Logger .Debug ("All assignees have approved the PR - merging" )
92+ 			a .merge (meta )
93+ 			return 
94+ 		} else  if  a .rule .Require  >  0  &&  approvals  >=  a .rule .Require  {
95+ 			util .Logger .Debug ("Got %d required approvals for PR - merging" , a .rule .Require )
96+ 			a .merge (meta )
97+ 			return 
98+ 		}
6099	}
61100}
62101
0 commit comments