@@ -77,15 +77,37 @@ func triggerNewBuild(t testing.TB, ctx context.Context, owner, project, token st
7777 return job .ID , nil
7878}
7979
80- // GetLastJobStatus returns the status of the latest executed job
81- func (g GL ) GetLastJobStatus (t testing.TB , ctx context.Context , owner , project , token string ) (string , int , error ) {
80+ // GetLastJobStatusForSHA finds the latest job associated with a specific commit SHA
81+ func (g GL ) GetLastJobStatusForSHA (t testing.TB , ctx context.Context , owner , project , token , sha string ) (string , int , error ) {
8282 git , err := gitlab .NewClient (token )
8383 if err != nil {
8484 return "" , 0 , fmt .Errorf ("failed to create client: %v" , err )
8585 }
8686
87+ projectPath := fmt .Sprintf ("%s/%s" , owner , project )
88+
89+ pipelineOpts := & gitlab.ListProjectPipelinesOptions {
90+ ListOptions : gitlab.ListOptions {
91+ PerPage : 1 ,
92+ Page : 1 ,
93+ OrderBy : "id" ,
94+ Sort : "desc" ,
95+ },
96+ SHA : & sha ,
97+ }
98+
99+ pipelines , _ , err := git .Pipelines .ListProjectPipelines (projectPath , pipelineOpts , gitlab .WithContext (ctx ))
100+ if err != nil {
101+ return "" , 0 , fmt .Errorf ("error listing pipelines for SHA %s: %w" , sha , err )
102+ }
103+
104+ if len (pipelines ) == 0 {
105+ return "" , 0 , fmt .Errorf ("no pipelines found for project '%s' at SHA '%s'" , projectPath , sha )
106+ }
107+
108+ latestPipelineID := pipelines [0 ].ID
87109 includeRetried := true
88- opts := & gitlab.ListJobsOptions {
110+ jobOpts := & gitlab.ListJobsOptions {
89111 ListOptions : gitlab.ListOptions {
90112 PerPage : 1 ,
91113 Page : 1 ,
@@ -95,13 +117,13 @@ func (g GL) GetLastJobStatus(t testing.TB, ctx context.Context, owner, project,
95117 IncludeRetried : & includeRetried ,
96118 }
97119
98- jobs , _ , err := git .Jobs .ListProjectJobs ( fmt . Sprintf ( "%s/%s" , owner , project ), opts , gitlab .WithContext (ctx ))
120+ jobs , _ , err := git .Jobs .ListPipelineJobs ( projectPath , latestPipelineID , jobOpts , gitlab .WithContext (ctx ))
99121 if err != nil {
100- return "" , 0 , fmt .Errorf ("error listing project jobs: %v" , err )
122+ return "" , 0 , fmt .Errorf ("error listing jobs for pipeline %d : %w" , latestPipelineID , err )
101123 }
102124
103125 if len (jobs ) == 0 {
104- return "" , 0 , fmt .Errorf ("no jobs found for project: %s/%s " , owner , project )
126+ return "" , 0 , fmt .Errorf ("no jobs found for pipeline %d " , latestPipelineID )
105127 }
106128
107129 return jobs [0 ].Status , jobs [0 ].ID , nil
@@ -175,7 +197,7 @@ func (g GL) GetFinalJobStatus(t testing.TB, ctx context.Context, owner, project,
175197}
176198
177199// WaitBuildSuccess waits for the current job in a project to finish.
178- func (g GL ) WaitBuildSuccess (t testing.TB , owner , project , token , failureMsg string , maxBuildRetry , maxErrorRetries int , timeBetweenErrorRetries time.Duration ) error {
200+ func (g GL ) WaitBuildSuccess (t testing.TB , owner , project , token , commitSha , failureMsg string , maxBuildRetry , maxErrorRetries int , timeBetweenErrorRetries time.Duration ) error {
179201 var status string
180202 var jobID int
181203 var err error
@@ -184,7 +206,7 @@ func (g GL) WaitBuildSuccess(t testing.TB, owner, project, token, failureMsg str
184206 // wait job creation
185207 time .Sleep (30 * time .Second )
186208
187- status , jobID , err = g .GetLastJobStatus (t , ctx , owner , project , token )
209+ status , jobID , err = g .GetLastJobStatusForSHA (t , ctx , owner , project , token , commitSha )
188210 if err != nil {
189211 return err
190212 }
0 commit comments