|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +package test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "net/http" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/cctx" |
| 12 | + tgitlab "github.com/openshift-pipelines/pipelines-as-code/test/pkg/gitlab" |
| 13 | + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload" |
| 14 | + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/scm" |
| 15 | + twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait" |
| 16 | + "github.com/tektoncd/pipeline/pkg/names" |
| 17 | + clientGitlab "gitlab.com/gitlab-org/api/client-go" |
| 18 | + "gotest.tools/v3/assert" |
| 19 | +) |
| 20 | + |
| 21 | +// TestGitlabOpsCommentInThreadReply verifies that a /test command placed |
| 22 | +// in a reply within a discussion thread on a Merge Request is honored. |
| 23 | +func TestGitlabOpsCommentInThreadReply(t *testing.T) { |
| 24 | + targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns") |
| 25 | + ctx := context.Background() |
| 26 | + runcnx, opts, glprovider, err := tgitlab.Setup(ctx) |
| 27 | + assert.NilError(t, err) |
| 28 | + ctx, err = cctx.GetControllerCtxInfo(ctx, runcnx) |
| 29 | + assert.NilError(t, err) |
| 30 | + runcnx.Clients.Log.Info("Testing GitLab /test <PipelineRunName> in thread replies") |
| 31 | + |
| 32 | + projectinfo, resp, err := glprovider.Client().Projects.GetProject(opts.ProjectID, nil) |
| 33 | + assert.NilError(t, err) |
| 34 | + if resp != nil && resp.StatusCode == http.StatusNotFound { |
| 35 | + t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo) |
| 36 | + } |
| 37 | + |
| 38 | + // Create Repository CRD |
| 39 | + err = tgitlab.CreateCRD(ctx, projectinfo, runcnx, opts, targetNS, nil) |
| 40 | + assert.NilError(t, err) |
| 41 | + |
| 42 | + // Create a basic PipelineRun |
| 43 | + entries, err := payload.GetEntries(map[string]string{ |
| 44 | + ".tekton/pipelinerun.yaml": "testdata/pipelinerun.yaml", |
| 45 | + }, targetNS, projectinfo.DefaultBranch, "pull_request", map[string]string{}) |
| 46 | + assert.NilError(t, err) |
| 47 | + |
| 48 | + // Create a branch with files and open a merge request |
| 49 | + targetRefName := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test") |
| 50 | + gitCloneURL, err := scm.MakeGitCloneURL(projectinfo.WebURL, opts.UserName, opts.Password) |
| 51 | + assert.NilError(t, err) |
| 52 | + commitTitle := "Committing files from test on " + targetRefName |
| 53 | + scmOpts := &scm.Opts{ |
| 54 | + GitURL: gitCloneURL, |
| 55 | + CommitTitle: commitTitle, |
| 56 | + Log: runcnx.Clients.Log, |
| 57 | + WebURL: projectinfo.WebURL, |
| 58 | + TargetRefName: targetRefName, |
| 59 | + BaseRefName: projectinfo.DefaultBranch, |
| 60 | + } |
| 61 | + _ = scm.PushFilesToRefGit(t, scmOpts, entries) |
| 62 | + mrTitle := "TestMergeRequest - " + targetRefName |
| 63 | + mrID, err := tgitlab.CreateMR(glprovider.Client(), opts.ProjectID, targetRefName, projectinfo.DefaultBranch, mrTitle) |
| 64 | + assert.NilError(t, err) |
| 65 | + defer tgitlab.TearDown(ctx, t, runcnx, glprovider, mrID, targetRefName, targetNS, opts.ProjectID) |
| 66 | + |
| 67 | + // Create a discussion thread with an initial note |
| 68 | + disc, _, err := glprovider.Client().Discussions.CreateMergeRequestDiscussion(opts.ProjectID, mrID, &clientGitlab.CreateMergeRequestDiscussionOptions{ |
| 69 | + Body: clientGitlab.Ptr("random initial note"), |
| 70 | + }) |
| 71 | + assert.NilError(t, err) |
| 72 | + |
| 73 | + // Wait for repository status to reflect a successful run triggered by the comment |
| 74 | + waitOpts := twait.Opts{ |
| 75 | + RepoName: targetNS, |
| 76 | + Namespace: targetNS, |
| 77 | + MinNumberStatus: 1, |
| 78 | + PollTimeout: twait.DefaultTimeout, |
| 79 | + TargetSHA: "", |
| 80 | + } |
| 81 | + _, err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) |
| 82 | + assert.NilError(t, err) |
| 83 | + |
| 84 | + runcnx.Clients.Log.Info("Updating discussion with /test comment in a reply thread") |
| 85 | + // Add a reply to the discussion containing /test |
| 86 | + _, _, err = glprovider.Client().Discussions.AddMergeRequestDiscussionNote(opts.ProjectID, mrID, disc.ID, &clientGitlab.AddMergeRequestDiscussionNoteOptions{ |
| 87 | + Body: clientGitlab.Ptr("/test"), |
| 88 | + }) |
| 89 | + assert.NilError(t, err) |
| 90 | + waitOpts.MinNumberStatus = 2 |
| 91 | + _, err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) |
| 92 | + assert.NilError(t, err) |
| 93 | + |
| 94 | + runcnx.Clients.Log.Info("Repository status updated after /test comment") |
| 95 | +} |
0 commit comments