Skip to content

Commit 65b0de5

Browse files
committed
Add e2e test for GitlabMergeRequestValidationErrors
Signed-off-by: Shubham Bhardwaj <[email protected]>
1 parent 92f8551 commit 65b0de5

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

test/gitlab_merge_request_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,135 @@ func TestGitlabMergeRequestOnUpdateAtAndLabelChange(t *testing.T) {
690690
}
691691
}
692692

693+
func TestGitlabMergeRequestValidationErrorsFromFork(t *testing.T) {
694+
targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns")
695+
ctx := context.Background()
696+
runcnx, opts, glprovider, err := tgitlab.Setup(ctx)
697+
assert.NilError(t, err)
698+
ctx, err = cctx.GetControllerCtxInfo(ctx, runcnx)
699+
assert.NilError(t, err)
700+
runcnx.Clients.Log.Info("Testing GitLab validation error commenting from fork scenario")
701+
702+
// Get the original project onboarded to PaC
703+
originalProject, resp, err := glprovider.Client().Projects.GetProject(opts.ProjectID, nil)
704+
assert.NilError(t, err)
705+
if resp != nil && resp.StatusCode == http.StatusNotFound {
706+
t.Errorf("Repository %d not found", opts.ProjectID)
707+
}
708+
709+
err = tgitlab.CreateCRD(ctx, originalProject, runcnx, opts, targetNS, nil)
710+
assert.NilError(t, err)
711+
712+
// Create a fork of the original project
713+
forkName := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-fork-test")
714+
forkProject, _, err := glprovider.Client().Projects.ForkProject(opts.ProjectID, &clientGitlab.ForkProjectOptions{
715+
Name: &forkName,
716+
Path: &forkName,
717+
})
718+
assert.NilError(t, err)
719+
runcnx.Clients.Log.Infof("Created fork project: %s (ID: %d) from original: %s (ID: %d)",
720+
forkProject.PathWithNamespace, forkProject.ID, originalProject.PathWithNamespace, originalProject.ID)
721+
722+
// Wait sometime for fork to be fully ready
723+
time.Sleep(3 * time.Second)
724+
725+
// Cleanup fork when test finishes
726+
defer func() {
727+
runcnx.Clients.Log.Infof("Cleaning up fork project: %d", forkProject.ID)
728+
_, err := glprovider.Client().Projects.DeleteProject(forkProject.ID, nil)
729+
if err != nil {
730+
runcnx.Clients.Log.Warnf("Failed to delete fork project: %v", err)
731+
}
732+
}()
733+
734+
// Commit invalid .tekton files to the fork
735+
entries, err := payload.GetEntries(map[string]string{
736+
".tekton/bad-yaml.yaml": "testdata/failures/bad-yaml.yaml",
737+
}, targetNS, forkProject.DefaultBranch,
738+
triggertype.PullRequest.String(), map[string]string{})
739+
assert.NilError(t, err)
740+
741+
targetRefName := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-fork-test")
742+
forkCloneURL, err := scm.MakeGitCloneURL(forkProject.WebURL, opts.UserName, opts.Password)
743+
assert.NilError(t, err)
744+
745+
commitTitle := "Add invalid .tekton file from fork - " + targetRefName
746+
scmOpts := &scm.Opts{
747+
GitURL: forkCloneURL,
748+
CommitTitle: commitTitle,
749+
Log: runcnx.Clients.Log,
750+
WebURL: forkProject.WebURL,
751+
TargetRefName: targetRefName,
752+
BaseRefName: originalProject.DefaultBranch,
753+
}
754+
_ = scm.PushFilesToRefGit(t, scmOpts, entries)
755+
runcnx.Clients.Log.Infof("Pushed invalid .tekton files to fork branch: %s", targetRefName)
756+
757+
// Create merge request from fork to original project
758+
mrTitle := "TestValidationErrorsFromFork - " + targetRefName
759+
mrOptions := &clientGitlab.CreateMergeRequestOptions{
760+
Title: &mrTitle,
761+
SourceBranch: &targetRefName,
762+
TargetBranch: &originalProject.DefaultBranch,
763+
// Create MR on the target project (original), not the source (fork)
764+
TargetProjectID: &originalProject.ID,
765+
}
766+
767+
mr, _, err := glprovider.Client().MergeRequests.CreateMergeRequest(forkProject.ID, mrOptions)
768+
assert.NilError(t, err)
769+
runcnx.Clients.Log.Infof("Created merge request from fork to original: %s/-/merge_requests/%d",
770+
originalProject.WebURL, mr.IID)
771+
772+
defer func() {
773+
// Close the MR and clean up branch
774+
runcnx.Clients.Log.Infof("Closing MR %d", mr.IID)
775+
_, _, err := glprovider.Client().MergeRequests.UpdateMergeRequest(originalProject.ID, mr.IID,
776+
&clientGitlab.UpdateMergeRequestOptions{StateEvent: clientGitlab.Ptr("close")})
777+
if err != nil {
778+
runcnx.Clients.Log.Warnf("Failed to close MR: %v", err)
779+
}
780+
781+
runcnx.Clients.Log.Infof("Deleting branch %s from fork", targetRefName)
782+
_, err = glprovider.Client().Branches.DeleteBranch(forkProject.ID, targetRefName)
783+
if err != nil {
784+
runcnx.Clients.Log.Warnf("Failed to delete branch: %v", err)
785+
}
786+
}()
787+
788+
runcnx.Clients.Log.Info("Waiting for webhook validation to process MR and post validation comment...")
789+
790+
maxLoop := 12 // Wait up to 72 seconds for webhook processing
791+
foundValidationComment := false
792+
793+
for i := 0; i < maxLoop; i++ {
794+
notes, _, err := glprovider.Client().Notes.ListMergeRequestNotes(originalProject.ID, mr.IID, nil)
795+
assert.NilError(t, err)
796+
797+
for _, note := range notes {
798+
// Look for the validation error comment that PaC should post via webhook
799+
if regexp.MustCompile(`.*There are some errors in your PipelineRun template.*`).MatchString(note.Body) &&
800+
regexp.MustCompile(`.*bad-yaml\.yaml.*yaml validation error.*`).MatchString(note.Body) {
801+
runcnx.Clients.Log.Info("SUCCESS: Found validation error comment on original project's MR!")
802+
runcnx.Clients.Log.Info("This proves the TargetProjectID fix works in the full webhook validation flow!")
803+
foundValidationComment = true
804+
805+
// Verify the comment format matches PaC's validation error format
806+
assert.Assert(t, regexp.MustCompile(`\[!CAUTION\]`).MatchString(note.Body), "Comment should contain caution header")
807+
break
808+
}
809+
}
810+
811+
if foundValidationComment {
812+
break
813+
}
814+
815+
runcnx.Clients.Log.Infof("Loop %d/%d: Waiting for webhook validation to post comment (testing TargetProjectID fix)", i+1, maxLoop)
816+
time.Sleep(6 * time.Second)
817+
}
818+
819+
assert.Assert(t, foundValidationComment, "Validation error comment should appear on original project's MR. ")
820+
}
821+
693822
// Local Variables:
694823
// compile-command: "go test -tags=e2e -v -run ^TestGitlabMergeRequest$"
695824
// End:

0 commit comments

Comments
 (0)