@@ -3,6 +3,7 @@ package status
33import (
44 "context"
55 "errors"
6+ "fmt"
67 "reflect"
78 "testing"
89
@@ -15,6 +16,13 @@ func stringPtr(str string) *string {
1516 return & str
1617}
1718
19+ func min (a , b int ) int {
20+ if a < b {
21+ return a
22+ }
23+ return b
24+ }
25+
1826func TestCreateValidator (t * testing.T ) {
1927 tests := map [string ]struct {
2028 c github.Client
@@ -734,6 +742,171 @@ func Test_statusValidator_listStatues(t *testing.T) {
734742 },
735743 }
736744 }(),
745+ "succeeds to retrieve 100 statuses" : func () test {
746+ num_statuses := 100
747+ statuses := make ([]* github.RepoStatus , num_statuses )
748+ checkRuns := make ([]* github.CheckRun , num_statuses )
749+ expectedGhaStatuses := make ([]* ghaStatus , num_statuses )
750+ for i := 0 ; i < num_statuses ; i ++ {
751+ statuses [i ] = & github.RepoStatus {
752+ Context : stringPtr (fmt .Sprintf ("job-%d" , i )),
753+ State : stringPtr (successState ),
754+ }
755+
756+ checkRuns [i ] = & github.CheckRun {
757+ Name : stringPtr (fmt .Sprintf ("job-%d" , i )),
758+ Status : stringPtr (checkRunCompletedStatus ),
759+ Conclusion : stringPtr (checkRunNeutralConclusion ),
760+ }
761+
762+ expectedGhaStatuses [i ] = & ghaStatus {
763+ Job : fmt .Sprintf ("job-%d" , i ),
764+ State : successState ,
765+ }
766+ }
767+
768+ c := & mock.Client {
769+ GetCombinedStatusFunc : func (ctx context.Context , owner , repo , ref string , opts * github.ListOptions ) (* github.CombinedStatus , * github.Response , error ) {
770+ max := min (opts .Page * opts .PerPage , len (statuses ))
771+ sts := statuses [(opts .Page - 1 )* opts .PerPage : max ]
772+ l := len (sts )
773+ return & github.CombinedStatus {
774+ Statuses : sts ,
775+ TotalCount : & l ,
776+ }, nil , nil
777+ },
778+ ListCheckRunsForRefFunc : func (ctx context.Context , owner , repo , ref string , opts * github.ListCheckRunsOptions ) (* github.ListCheckRunsResults , * github.Response , error ) {
779+ max := min (opts .ListOptions .Page * opts .ListOptions .PerPage , len (checkRuns ))
780+ sts := checkRuns [(opts .ListOptions .Page - 1 )* opts .ListOptions .PerPage : max ]
781+ l := len (sts )
782+ return & github.ListCheckRunsResults {
783+ CheckRuns : checkRuns ,
784+ Total : & l ,
785+ }, nil , nil
786+ },
787+ }
788+ return test {
789+ fields : fields {
790+ client : c ,
791+ selfJobName : "self-job" ,
792+ owner : "test-owner" ,
793+ repo : "test-repo" ,
794+ ref : "main" ,
795+ },
796+ wantErr : false ,
797+ want : expectedGhaStatuses ,
798+ }
799+ }(),
800+ "succeeds to retrieve 162 statuses" : func () test {
801+ num_statuses := 162
802+ statuses := make ([]* github.RepoStatus , num_statuses )
803+ checkRuns := make ([]* github.CheckRun , num_statuses )
804+ expectedGhaStatuses := make ([]* ghaStatus , num_statuses )
805+ for i := 0 ; i < num_statuses ; i ++ {
806+ statuses [i ] = & github.RepoStatus {
807+ Context : stringPtr (fmt .Sprintf ("job-%d" , i )),
808+ State : stringPtr (successState ),
809+ }
810+
811+ checkRuns [i ] = & github.CheckRun {
812+ Name : stringPtr (fmt .Sprintf ("job-%d" , i )),
813+ Status : stringPtr (checkRunCompletedStatus ),
814+ Conclusion : stringPtr (checkRunNeutralConclusion ),
815+ }
816+
817+ expectedGhaStatuses [i ] = & ghaStatus {
818+ Job : fmt .Sprintf ("job-%d" , i ),
819+ State : successState ,
820+ }
821+ }
822+
823+ c := & mock.Client {
824+ GetCombinedStatusFunc : func (ctx context.Context , owner , repo , ref string , opts * github.ListOptions ) (* github.CombinedStatus , * github.Response , error ) {
825+ max := min (opts .Page * opts .PerPage , len (statuses ))
826+ sts := statuses [(opts .Page - 1 )* opts .PerPage : max ]
827+ l := len (sts )
828+ return & github.CombinedStatus {
829+ Statuses : sts ,
830+ TotalCount : & l ,
831+ }, nil , nil
832+ },
833+ ListCheckRunsForRefFunc : func (ctx context.Context , owner , repo , ref string , opts * github.ListCheckRunsOptions ) (* github.ListCheckRunsResults , * github.Response , error ) {
834+ max := min (opts .ListOptions .Page * opts .ListOptions .PerPage , len (checkRuns ))
835+ sts := checkRuns [(opts .ListOptions .Page - 1 )* opts .ListOptions .PerPage : max ]
836+ l := len (sts )
837+ return & github.ListCheckRunsResults {
838+ CheckRuns : checkRuns ,
839+ Total : & l ,
840+ }, nil , nil
841+ },
842+ }
843+ return test {
844+ fields : fields {
845+ client : c ,
846+ selfJobName : "self-job" ,
847+ owner : "test-owner" ,
848+ repo : "test-repo" ,
849+ ref : "main" ,
850+ },
851+ wantErr : false ,
852+ want : expectedGhaStatuses ,
853+ }
854+ }(),
855+ "succeeds to retrieve 587 statuses" : func () test {
856+ num_statuses := 587
857+ statuses := make ([]* github.RepoStatus , num_statuses )
858+ checkRuns := make ([]* github.CheckRun , num_statuses )
859+ expectedGhaStatuses := make ([]* ghaStatus , num_statuses )
860+ for i := 0 ; i < num_statuses ; i ++ {
861+ statuses [i ] = & github.RepoStatus {
862+ Context : stringPtr (fmt .Sprintf ("job-%d" , i )),
863+ State : stringPtr (successState ),
864+ }
865+
866+ checkRuns [i ] = & github.CheckRun {
867+ Name : stringPtr (fmt .Sprintf ("job-%d" , i )),
868+ Status : stringPtr (checkRunCompletedStatus ),
869+ Conclusion : stringPtr (checkRunNeutralConclusion ),
870+ }
871+
872+ expectedGhaStatuses [i ] = & ghaStatus {
873+ Job : fmt .Sprintf ("job-%d" , i ),
874+ State : successState ,
875+ }
876+ }
877+
878+ c := & mock.Client {
879+ GetCombinedStatusFunc : func (ctx context.Context , owner , repo , ref string , opts * github.ListOptions ) (* github.CombinedStatus , * github.Response , error ) {
880+ max := min (opts .Page * opts .PerPage , len (statuses ))
881+ sts := statuses [(opts .Page - 1 )* opts .PerPage : max ]
882+ l := len (sts )
883+ return & github.CombinedStatus {
884+ Statuses : sts ,
885+ TotalCount : & l ,
886+ }, nil , nil
887+ },
888+ ListCheckRunsForRefFunc : func (ctx context.Context , owner , repo , ref string , opts * github.ListCheckRunsOptions ) (* github.ListCheckRunsResults , * github.Response , error ) {
889+ max := min (opts .ListOptions .Page * opts .ListOptions .PerPage , len (checkRuns ))
890+ sts := checkRuns [(opts .ListOptions .Page - 1 )* opts .ListOptions .PerPage : max ]
891+ l := len (sts )
892+ return & github.ListCheckRunsResults {
893+ CheckRuns : checkRuns ,
894+ Total : & l ,
895+ }, nil , nil
896+ },
897+ }
898+ return test {
899+ fields : fields {
900+ client : c ,
901+ selfJobName : "self-job" ,
902+ owner : "test-owner" ,
903+ repo : "test-repo" ,
904+ ref : "main" ,
905+ },
906+ wantErr : false ,
907+ want : expectedGhaStatuses ,
908+ }
909+ }(),
737910 }
738911 for name , tt := range tests {
739912 t .Run (name , func (t * testing.T ) {
0 commit comments