Skip to content

Commit a181c68

Browse files
committed
gitlab: Fix posting prun status on gitops comments
we were not posting the status on gitops comments, this is because we were not catching the Note events as defined by the gitlab provider. better functional tests to check the actual comments has been posted properly on gitops comments for gitea. This is for the release-v0.24.x branch since we have generics function for this in the main branch. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent ad1f3e2 commit a181c68

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

pkg/provider/gitlab/gitlab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusOpts
204204
_, _, _ = v.Client.Commits.SetCommitStatus(event.SourceProjectID, event.SHA, opt)
205205

206206
// only add a note when we are on a MR
207-
if event.EventType == "pull_request" || event.EventType == "Merge_Request" || event.EventType == "Merge Request" {
207+
if event.EventType == "pull_request" || event.EventType == "Merge_Request" || event.EventType == "Merge Request" || event.EventType == "Note" {
208208
mopt := &gitlab.CreateMergeRequestNoteOptions{Body: gitlab.Ptr(body)}
209209
_, _, err := v.Client.Notes.CreateMergeRequestNote(event.TargetProjectID, event.PullRequestNumber, mopt)
210210
return err

pkg/provider/gitlab/gitlab_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ func TestCreateStatus(t *testing.T) {
134134
postStr: "has completed",
135135
},
136136
},
137+
{
138+
name: "gitops comments completed",
139+
wantClient: true,
140+
wantErr: false,
141+
args: args{
142+
statusOpts: provider.StatusOpts{
143+
Conclusion: "completed",
144+
},
145+
event: &info.Event{
146+
TriggerTarget: "Note",
147+
},
148+
postStr: "has completed",
149+
},
150+
},
137151
{
138152
name: "completed with a details url",
139153
wantClient: true,

test/gitea_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import (
4242
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4343
)
4444

45-
var successRegexp = regexp.MustCompile(`^Pipelines as Code CI.*has.*successfully`)
45+
// successRegexp will match a success text paac comment,sometime it includes html tags so we need to consider that.
46+
var successRegexp = regexp.MustCompile(`.*Pipelines as Code CI.*has.*successfully.*validated your commit.*`)
4647

4748
func TestGiteaPullRequestTaskAnnotations(t *testing.T) {
4849
topts := &tgitea.TestOpts{

test/gitlab_merge_request_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ func TestGitlabMergeRequest(t *testing.T) {
5050

5151
gitCloneURL, err := scm.MakeGitCloneURL(projectinfo.WebURL, opts.UserName, opts.Password)
5252
assert.NilError(t, err)
53+
commitTitle := "Committing files from test on " + targetRefName
5354
scmOpts := &scm.Opts{
5455
GitURL: gitCloneURL,
56+
CommitTitle: commitTitle,
5557
Log: runcnx.Clients.Log,
5658
WebURL: projectinfo.WebURL,
5759
TargetRefName: targetRefName,
@@ -97,6 +99,32 @@ func TestGitlabMergeRequest(t *testing.T) {
9799
for i := 0; i < len(prsNew.Items); i++ {
98100
assert.Equal(t, "Merge Request", prsNew.Items[i].Annotations[keys.EventType])
99101
}
102+
103+
runcnx.Clients.Log.Infof("Sending /retest comment on MergeRequest %s/-/merge_requests/%d", projectinfo.WebURL, mrID)
104+
_, _, err = glprovider.Client.Notes.CreateMergeRequestNote(opts.ProjectID, mrID, &clientGitlab.CreateMergeRequestNoteOptions{
105+
Body: clientGitlab.Ptr("/retest"),
106+
})
107+
assert.NilError(t, err)
108+
sopt = wait.SuccessOpt{
109+
Title: commitTitle,
110+
OnEvent: "Note",
111+
TargetNS: targetNS,
112+
NumberofPRMatch: 5, // this is the max we get in repos status
113+
SHA: "",
114+
}
115+
runcnx.Clients.Log.Info("Checking that PAC has posted successful comments for all PR that has been tested")
116+
wait.Succeeded(ctx, t, runcnx, opts, sopt)
117+
118+
notes, _, err := glprovider.Client.Notes.ListMergeRequestNotes(opts.ProjectID, mrID, nil)
119+
assert.NilError(t, err)
120+
successCommentsPost := 0
121+
for _, n := range notes {
122+
if successRegexp.MatchString(n.Body) {
123+
successCommentsPost++
124+
}
125+
}
126+
// we get 2 PRS initially, 2 prs from the push update and 2 prs from the /retest == 6
127+
assert.Equal(t, 6, successCommentsPost)
100128
}
101129

102130
// Local Variables:

test/pkg/wait/check.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package wait
33
import (
44
"context"
55
"path/filepath"
6+
"strings"
67
"testing"
78
"time"
89

@@ -52,7 +53,7 @@ func Succeeded(ctx context.Context, t *testing.T, runcnx *params.Run, opts optio
5253
assert.Equal(t, sopt.SHA, *laststatus.SHA)
5354
assert.Equal(t, sopt.SHA, filepath.Base(*laststatus.SHAURL))
5455
}
55-
assert.Equal(t, sopt.Title, *laststatus.Title)
56+
assert.Equal(t, sopt.Title, strings.TrimSpace(*laststatus.Title))
5657
assert.Assert(t, *laststatus.LogURL != "")
5758

5859
pr, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(sopt.TargetNS).Get(ctx, laststatus.PipelineRunName, v1.GetOptions{})
@@ -71,7 +72,7 @@ func Succeeded(ctx context.Context, t *testing.T, runcnx *params.Run, opts optio
7172
assert.Equal(t, sopt.SHA, pr.Annotations[keys.SHA])
7273
assert.Equal(t, sopt.SHA, filepath.Base(pr.Annotations[keys.ShaURL]))
7374
}
74-
assert.Equal(t, sopt.Title, pr.Annotations[keys.ShaTitle])
75+
assert.Equal(t, sopt.Title, strings.TrimSpace(pr.Annotations[keys.ShaTitle]))
7576

7677
runcnx.Clients.Log.Infof("Success, number of status %d has been matched", sopt.NumberofPRMatch)
7778
}

0 commit comments

Comments
 (0)