Skip to content

Commit 8f5c0ab

Browse files
committed
fix: /ok-to-test /retest pipelineruns are not created if last successful
when executing /ok-to-test or /retest gitops commands, check whether the last pipelinerun created for the same SHA was successful. If the run was successful, skip new pipelinerun creation - update the github_max_keep_runs test to use retest single instead of retest_all to ensure that the test doesn't fail - update gitlab_merge_request test to use retest single as well
1 parent adb6e8c commit 8f5c0ab

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

pkg/pipelineascode/pipelineascode.go

+28
Original file line numberDiff line numberDiff line change
@@ -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

2830
const (
@@ -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{})

test/github_config_maxkeepruns_test.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,27 @@ func TestGithubMaxKeepRuns(t *testing.T) {
2626
g.RunPullRequest(ctx, t)
2727
defer g.TearDown(ctx, t)
2828

29-
g.Cnx.Clients.Log.Infof("Creating /retest in PullRequest")
30-
_, _, err := g.Provider.Client.Issues.CreateComment(ctx, g.Options.Organization, g.Options.Repo, g.PRNumber,
31-
&ghlib.IssueComment{Body: ghlib.Ptr("/retest")})
29+
// Wait for the first pipeline run to be created
30+
time.Sleep(5 * time.Second)
31+
32+
// Get the name of the PipelineRun to retest specifically
33+
prList, err := g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{})
34+
assert.NilError(t, err)
35+
assert.Assert(t, len(prList.Items) > 0, "Expected at least one PipelineRun to be created")
36+
37+
pipelineRunName := ""
38+
for _, pr := range prList.Items {
39+
if originalName, ok := pr.GetAnnotations()[keys.OriginalPRName]; ok {
40+
pipelineRunName = originalName
41+
break
42+
}
43+
}
44+
assert.Assert(t, pipelineRunName != "", "Could not find the original PipelineRun name")
45+
46+
// Use retest with specific PipelineRun name
47+
g.Cnx.Clients.Log.Infof("Creating /retest %s in PullRequest", pipelineRunName)
48+
_, _, err = g.Provider.Client.Issues.CreateComment(ctx, g.Options.Organization, g.Options.Repo, g.PRNumber,
49+
&ghlib.IssueComment{Body: ghlib.Ptr("/retest " + pipelineRunName)})
3250
assert.NilError(t, err)
3351

3452
g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated")

test/gitlab_merge_request_test.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,32 @@ func TestGitlabMergeRequest(t *testing.T) {
101101
assert.Equal(t, "Merge Request", prsNew.Items[i].Annotations[keys.EventType])
102102
}
103103

104-
runcnx.Clients.Log.Infof("Sending /retest comment on MergeRequest %s/-/merge_requests/%d", projectinfo.WebURL, mrID)
104+
// Wait a moment to ensure all PipelineRuns are fully created
105+
time.Sleep(5 * time.Second)
106+
107+
// Query for an existing PipelineRun to retest specifically
108+
prList, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{})
109+
assert.NilError(t, err)
110+
assert.Assert(t, len(prList.Items) > 0, "Expected at least one PipelineRun to be created")
111+
112+
pipelineRunName := ""
113+
for _, pr := range prList.Items {
114+
if originalName, ok := pr.GetAnnotations()[keys.OriginalPRName]; ok {
115+
pipelineRunName = originalName
116+
break
117+
}
118+
}
119+
assert.Assert(t, pipelineRunName != "", "Could not find the original PipelineRun name")
120+
121+
runcnx.Clients.Log.Infof("Sending /retest %s comment on MergeRequest %s/-/merge_requests/%d", pipelineRunName, projectinfo.WebURL, mrID)
105122
_, _, err = glprovider.Client.Notes.CreateMergeRequestNote(opts.ProjectID, mrID, &clientGitlab.CreateMergeRequestNoteOptions{
106-
Body: clientGitlab.Ptr("/retest"),
123+
Body: clientGitlab.Ptr("/retest " + pipelineRunName),
107124
})
108125
assert.NilError(t, err)
109126

110127
sopt = twait.SuccessOpt{
111128
Title: commitTitle,
112-
OnEvent: opscomments.RetestAllCommentEventType.String(),
129+
OnEvent: opscomments.RetestSingleCommentEventType.String(),
113130
TargetNS: targetNS,
114131
NumberofPRMatch: 5, // this is the max we get in repos status
115132
SHA: "",

0 commit comments

Comments
 (0)