@@ -21,6 +21,7 @@ import (
2121 "github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
2222 "github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
2323 bbtest "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketdatacenter/test"
24+ bbtypes "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketdatacenter/types"
2425 "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/status"
2526 "go.opentelemetry.io/otel"
2627
@@ -707,48 +708,71 @@ func TestGetFiles(t *testing.T) {
707708 PullRequestNumber : 1 ,
708709 }
709710
710- pushFiles := []* bbtest .DiffStat {
711+ pushFiles := []* bbtypes .DiffStat {
711712 {
712- Path : bbtest .DiffPath {ToString : "added.md" },
713+ Path : bbtypes .DiffPath {ToString : "added.md" },
713714 Type : "ADD" ,
714715 },
715716 {
716- Path : bbtest .DiffPath {ToString : "modified.txt" },
717+ Path : bbtypes .DiffPath {ToString : "modified.txt" },
717718 Type : "MODIFY" ,
718719 },
719720 {
720- Path : bbtest .DiffPath {ToString : "renamed.yaml" },
721+ Path : bbtypes .DiffPath {ToString : "renamed.yaml" },
721722 Type : "MOVE" ,
722723 },
723724 {
724- Path : bbtest .DiffPath {ToString : "deleted.go" },
725+ Path : bbtypes .DiffPath {ToString : "deleted.go" },
725726 Type : "DELETE" ,
726727 },
727728 }
728729
729- pullRequestFiles := []* bbtest .DiffStat {
730+ pullRequestFiles := []* bbtypes .DiffStat {
730731 {
731- Path : bbtest .DiffPath {ToString : "added.go" },
732+ Path : bbtypes .DiffPath {ToString : "added.go" },
732733 Type : "ADD" ,
733734 },
734735 {
735- Path : bbtest .DiffPath {ToString : "modified.yaml" },
736+ Path : bbtypes .DiffPath {ToString : "modified.yaml" },
736737 Type : "MODIFY" ,
737738 },
738739 {
739- Path : bbtest .DiffPath {ToString : "renamed.txt" },
740+ Path : bbtypes .DiffPath {ToString : "renamed.txt" },
740741 Type : "MOVE" ,
741742 },
742743 {
743- Path : bbtest.DiffPath {ToString : "deleted.md" },
744+ Path : bbtypes.DiffPath {ToString : "deleted.md" },
745+ Type : "DELETE" ,
746+ },
747+ }
748+
749+ mergeCommitPushEvent := & info.Event {
750+ SHA : "MERGESHA456" ,
751+ Organization : "pac" ,
752+ Repository : "test" ,
753+ TriggerTarget : triggertype .Push ,
754+ }
755+
756+ mergeCommitFiles := []* bbtypes.DiffStat {
757+ {
758+ Path : bbtypes.DiffPath {ToString : "merge-added.go" },
759+ Type : "ADD" ,
760+ },
761+ {
762+ Path : bbtypes.DiffPath {ToString : "merge-modified.txt" },
763+ Type : "MODIFY" ,
764+ },
765+ {
766+ Path : bbtypes.DiffPath {ToString : "merge-deleted.md" },
744767 Type : "DELETE" ,
745768 },
746769 }
747770
748771 tests := []struct {
749772 name string
750773 event * info.Event
751- changeFiles []* bbtest.DiffStat
774+ changeFiles []* bbtypes.DiffStat
775+ previousHeadCommit string
752776 wantAddedFilesCount int
753777 wantDeletedFilesCount int
754778 wantModifiedFilesCount int
@@ -794,18 +818,49 @@ func TestGetFiles(t *testing.T) {
794818 wantError : true ,
795819 errMsg : "failed to list changes for pull request: No message available" ,
796820 },
821+ {
822+ name : "good/merge commit push event" ,
823+ event : mergeCommitPushEvent ,
824+ changeFiles : mergeCommitFiles ,
825+ previousHeadCommit : "PREVIOUSHEAD789" ,
826+ wantAddedFilesCount : 1 ,
827+ wantDeletedFilesCount : 1 ,
828+ wantModifiedFilesCount : 1 ,
829+ wantRenamedFilesCount : 0 ,
830+ },
831+ {
832+ name : "bad/merge commit push event api error" ,
833+ event : mergeCommitPushEvent ,
834+ previousHeadCommit : "PREVIOUSHEAD789" ,
835+ wantError : true ,
836+ errMsg : "failed to list changes for commit MERGESHA456: failed to get merge commit changes: status code: 401" ,
837+ },
797838 }
798839 for _ , tt := range tests {
799840 t .Run (tt .name , func (t * testing.T ) {
800841 ctx , _ := rtesting .SetupFakeContext (t )
801842 client , mux , tearDown , tURL := bbtest .SetupBBDataCenterClient ()
802843 defer tearDown ()
803844
804- stats := & bbtest.DiffStats {
845+ stats := & bbtypes.DiffStats {
846+ Pagination : bbtypes.Pagination {
847+ LastPage : true ,
848+ },
805849 Values : tt .changeFiles ,
806850 }
807851
808- if tt .event .TriggerTarget == triggertype .Push {
852+ if tt .event .TriggerTarget == triggertype .Push && tt .previousHeadCommit != "" {
853+ mux .HandleFunc ("/projects/pac/repos/test/changes" , func (w http.ResponseWriter , r * http.Request ) {
854+ if tt .wantError {
855+ w .WriteHeader (http .StatusUnauthorized )
856+ return
857+ }
858+ assert .Equal (t , r .URL .Query ().Get ("since" ), tt .previousHeadCommit )
859+ assert .Equal (t , r .URL .Query ().Get ("until" ), tt .event .SHA )
860+ b , _ := json .Marshal (stats )
861+ fmt .Fprint (w , string (b ))
862+ })
863+ } else if tt .event .TriggerTarget == triggertype .Push {
809864 mux .HandleFunc ("/projects/pac/repos/test/commits/IAMSHA123/changes" , func (w http.ResponseWriter , _ * http.Request ) {
810865 if tt .wantError {
811866 w .WriteHeader (http .StatusUnauthorized )
@@ -831,7 +886,7 @@ func TestGetFiles(t *testing.T) {
831886 provider := sdkmetric .NewMeterProvider (sdkmetric .WithReader (reader ))
832887 otel .SetMeterProvider (provider )
833888
834- v := & Provider {client : client , baseURL : tURL , triggerEvent : string (tt .event .TriggerTarget )}
889+ v := & Provider {client : client , baseURL : tURL , triggerEvent : string (tt .event .TriggerTarget ), previousHeadCommit : tt . previousHeadCommit }
835890 changedFiles , err := v .GetFiles (ctx , tt .event )
836891 if tt .wantError {
837892 assert .Equal (t , err .Error (), tt .errMsg )
@@ -855,33 +910,37 @@ func TestGetFiles(t *testing.T) {
855910 }
856911 }
857912
858- var rm metricdata.ResourceMetrics
859- err = reader .Collect (ctx , & rm )
860- assert .NilError (t , err , "error collecting metrics" )
861-
862- assert .Equal (t , len (rm .ScopeMetrics ), 1 )
863- assert .Equal (t , len (rm .ScopeMetrics [0 ].Metrics ), 1 )
864- assert .Equal (t , rm .ScopeMetrics [0 ].Metrics [0 ].Name , "pipelines_as_code_git_provider_api_request_count" )
865- count , ok := rm .ScopeMetrics [0 ].Metrics [0 ].Data .(metricdata.Sum [int64 ])
866- assert .Assert (t , ok )
867- assert .Equal (t , count .DataPoints [0 ].Value , int64 (1 ))
868-
869- _ , _ = v .GetFiles (ctx , tt .event )
870- // recollect the metrics af
871- err = reader .Collect (ctx , & rm )
872- assert .NilError (t , err , "error collecting metrics" )
873-
874- assert .Equal (t , len (rm .ScopeMetrics ), 1 )
875- assert .Equal (t , len (rm .ScopeMetrics [0 ].Metrics ), 1 )
876- assert .Equal (t , rm .ScopeMetrics [0 ].Metrics [0 ].Name , "pipelines_as_code_git_provider_api_request_count" )
877- count , ok = rm .ScopeMetrics [0 ].Metrics [0 ].Data .(metricdata.Sum [int64 ])
878- assert .Assert (t , ok )
879- if tt .wantError {
880- // no caching on error so we expect 2 metrics
881- assert .Equal (t , count .DataPoints [0 ].Value , int64 (2 ))
882- } else {
883- // caching on success so we expect 1 metric
913+ // getMergeCommitChanges uses v.client.Do directly (not v.Client()),
914+ // so no API usage metrics are recorded for that path.
915+ if tt .previousHeadCommit == "" {
916+ var rm metricdata.ResourceMetrics
917+ err = reader .Collect (ctx , & rm )
918+ assert .NilError (t , err , "error collecting metrics" )
919+
920+ assert .Equal (t , len (rm .ScopeMetrics ), 1 )
921+ assert .Equal (t , len (rm .ScopeMetrics [0 ].Metrics ), 1 )
922+ assert .Equal (t , rm .ScopeMetrics [0 ].Metrics [0 ].Name , "pipelines_as_code_git_provider_api_request_count" )
923+ count , ok := rm .ScopeMetrics [0 ].Metrics [0 ].Data .(metricdata.Sum [int64 ])
924+ assert .Assert (t , ok )
884925 assert .Equal (t , count .DataPoints [0 ].Value , int64 (1 ))
926+
927+ _ , _ = v .GetFiles (ctx , tt .event )
928+ // recollect the metrics af
929+ err = reader .Collect (ctx , & rm )
930+ assert .NilError (t , err , "error collecting metrics" )
931+
932+ assert .Equal (t , len (rm .ScopeMetrics ), 1 )
933+ assert .Equal (t , len (rm .ScopeMetrics [0 ].Metrics ), 1 )
934+ assert .Equal (t , rm .ScopeMetrics [0 ].Metrics [0 ].Name , "pipelines_as_code_git_provider_api_request_count" )
935+ count , ok = rm .ScopeMetrics [0 ].Metrics [0 ].Data .(metricdata.Sum [int64 ])
936+ assert .Assert (t , ok )
937+ if tt .wantError {
938+ // no caching on error so we expect 2 metrics
939+ assert .Equal (t , count .DataPoints [0 ].Value , int64 (2 ))
940+ } else {
941+ // caching on success so we expect 1 metric
942+ assert .Equal (t , count .DataPoints [0 ].Value , int64 (1 ))
943+ }
885944 }
886945 })
887946 }
0 commit comments