@@ -13,6 +13,7 @@ import (
1313 "github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
1414 "github.com/openshift-pipelines/pipelines-as-code/pkg/kubeinteraction"
1515 "github.com/openshift-pipelines/pipelines-as-code/pkg/matcher"
16+ "github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
1617 "github.com/openshift-pipelines/pipelines-as-code/pkg/params"
1718 "github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients"
1819 "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
@@ -23,6 +24,7 @@ import (
2324 tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
2425 "go.uber.org/zap"
2526 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+ "knative.dev/pkg/apis"
2628)
2729
2830const (
@@ -187,6 +189,32 @@ func (p *PacRun) startPR(ctx context.Context, match matcher.Match) (*tektonv1.Pi
187189 match .PipelineRun .Annotations [keys .State ] = kubeinteraction .StateQueued
188190 }
189191
192+ // check for existing pipelineruns with the same SHA for ok-to-test or retest events
193+ if (p .event .EventType == opscomments .RetestAllCommentEventType .String () ||
194+ p .event .EventType == opscomments .OkToTestCommentEventType .String ()) &&
195+ p .event .SHA != "" {
196+ labelSelector := fmt .Sprintf ("%s=%s" , keys .SHA , formatting .CleanValueKubernetes (p .event .SHA ))
197+ existingPRs , err := p .run .Clients .Tekton .TektonV1 ().PipelineRuns (match .Repo .GetNamespace ()).List (ctx , metav1.ListOptions {
198+ LabelSelector : labelSelector ,
199+ })
200+ if err == nil && len (existingPRs .Items ) > 0 {
201+ var lastRun tektonv1.PipelineRun
202+ lastRun = existingPRs .Items [0 ]
203+
204+ for _ , pr := range existingPRs .Items {
205+ if pr .CreationTimestamp .After (lastRun .CreationTimestamp .Time ) {
206+ lastRun = pr
207+ }
208+ }
209+
210+ if lastRun .Status .GetCondition (apis .ConditionSucceeded ).IsTrue () {
211+ p .logger .Infof ("skipping creation of new pipelinerun for sha %s as the last pipelinerun '%s' has already succeeded" ,
212+ p .event .SHA , lastRun .Name )
213+ return & lastRun , nil
214+ }
215+ }
216+ }
217+
190218 // Create the actual pipelineRun
191219 pr , err := p .run .Clients .Tekton .TektonV1 ().PipelineRuns (match .Repo .GetNamespace ()).Create (ctx ,
192220 match .PipelineRun , metav1.CreateOptions {})
0 commit comments