@@ -720,3 +720,128 @@ func TestRunCreateOptions_Marshal(t *testing.T) {
720
720
721
721
assert .Equal (t , string (bodyBytes ), expectedBody )
722
722
}
723
+
724
+ func TestRunsListForOrganization (t * testing.T ) {
725
+ client := testClient (t )
726
+ ctx := context .Background ()
727
+
728
+ orgTest , orgTestCleanup := createOrganization (t , client )
729
+ defer orgTestCleanup ()
730
+
731
+ apTest , _ := createAgentPool (t , client , orgTest )
732
+
733
+ wTest , _ := createWorkspaceWithOptions (t , client , orgTest , WorkspaceCreateOptions {
734
+ Name : String (randomString (t )),
735
+ ExecutionMode : String ("agent" ),
736
+ AgentPoolID : & apTest .ID ,
737
+ })
738
+ rTest1 , _ := createRun (t , client , wTest )
739
+ rTest2 , _ := createRun (t , client , wTest )
740
+
741
+ t .Run ("without list options" , func (t * testing.T ) {
742
+ rl , err := client .Runs .ListForOrganization (ctx , orgTest .Name , nil )
743
+ require .NoError (t , err )
744
+
745
+ found := []string {}
746
+ for _ , r := range rl .Items {
747
+ found = append (found , r .ID )
748
+ }
749
+
750
+ assert .Contains (t , found , rTest1 .ID )
751
+ assert .Contains (t , found , rTest2 .ID )
752
+ assert .Equal (t , 1 , rl .CurrentPage )
753
+ assert .Equal (t , 2 , rl .TotalCount )
754
+ })
755
+
756
+ t .Run ("without list options and include as nil" , func (t * testing.T ) {
757
+ rl , err := client .Runs .ListForOrganization (ctx , orgTest .Name , & RunListForOrganizationOptions {
758
+ Include : []RunIncludeOpt {},
759
+ })
760
+ require .NoError (t , err )
761
+ require .NotEmpty (t , rl .Items )
762
+
763
+ found := []string {}
764
+ for _ , r := range rl .Items {
765
+ found = append (found , r .ID )
766
+ }
767
+
768
+ assert .Contains (t , found , rTest1 .ID )
769
+ assert .Contains (t , found , rTest2 .ID )
770
+ assert .Equal (t , 1 , rl .CurrentPage )
771
+ assert .Equal (t , 2 , rl .TotalCount )
772
+ })
773
+
774
+ t .Run ("with list options" , func (t * testing.T ) {
775
+ t .Skip ("paging not supported yet in API" )
776
+
777
+ // Request a page number which is out of range. The result should
778
+ // be successful, but return no results if the paging options are
779
+ // properly passed along.
780
+ rl , err := client .Runs .ListForOrganization (ctx , orgTest .Name , & RunListForOrganizationOptions {
781
+ ListOptions : ListOptions {
782
+ PageNumber : 999 ,
783
+ PageSize : 100 ,
784
+ },
785
+ })
786
+ require .NoError (t , err )
787
+ assert .Empty (t , rl .Items )
788
+ assert .Equal (t , 999 , rl .CurrentPage )
789
+ assert .Equal (t , 2 , rl .TotalCount )
790
+ })
791
+
792
+ t .Run ("with workspace included" , func (t * testing.T ) {
793
+ rl , err := client .Runs .ListForOrganization (ctx , orgTest .Name , & RunListForOrganizationOptions {
794
+ Include : []RunIncludeOpt {RunWorkspace },
795
+ })
796
+ require .NoError (t , err )
797
+
798
+ require .NotEmpty (t , rl .Items )
799
+ require .NotNil (t , rl .Items [0 ].Workspace )
800
+ assert .NotEmpty (t , rl .Items [0 ].Workspace .Name )
801
+ })
802
+
803
+ t .Run ("without a valid organization name" , func (t * testing.T ) {
804
+ rl , err := client .Runs .ListForOrganization (ctx , badIdentifier , nil )
805
+ assert .Nil (t , rl )
806
+ assert .EqualError (t , err , ErrInvalidOrg .Error ())
807
+ })
808
+
809
+ t .Run ("with filter by agent pool" , func (t * testing.T ) {
810
+ rl , err := client .Runs .ListForOrganization (ctx , orgTest .Name , & RunListForOrganizationOptions {
811
+ AgentPoolNames : apTest .Name ,
812
+ })
813
+ require .NoError (t , err )
814
+
815
+ found := make ([]string , len (rl .Items ))
816
+ for i , r := range rl .Items {
817
+ found [i ] = r .ID
818
+ }
819
+
820
+ assert .Contains (t , found , rTest1 .ID )
821
+ assert .Contains (t , found , rTest2 .ID )
822
+ assert .Equal (t , 1 , rl .CurrentPage )
823
+ assert .Equal (t , 2 , rl .TotalCount )
824
+ })
825
+
826
+ t .Run ("with filter by workspace" , func (t * testing.T ) {
827
+ rl , err := client .Runs .ListForOrganization (ctx , orgTest .Name , & RunListForOrganizationOptions {
828
+ WorkspaceNames : wTest .Name ,
829
+ Include : []RunIncludeOpt {RunWorkspace },
830
+ })
831
+ require .NoError (t , err )
832
+
833
+ found := make ([]string , len (rl .Items ))
834
+ for i , r := range rl .Items {
835
+ found [i ] = r .ID
836
+ }
837
+
838
+ assert .Contains (t , found , rTest1 .ID )
839
+ assert .Contains (t , found , rTest2 .ID )
840
+ require .NotNil (t , rl .Items [0 ].Workspace )
841
+ assert .NotEmpty (t , rl .Items [0 ].Workspace .Name )
842
+ require .NotNil (t , rl .Items [1 ].Workspace )
843
+ assert .NotEmpty (t , rl .Items [1 ].Workspace .Name )
844
+ assert .Equal (t , 1 , rl .CurrentPage )
845
+ assert .Equal (t , 2 , rl .TotalCount )
846
+ })
847
+ }
0 commit comments