|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype" |
| 13 | + tgitlab "github.com/openshift-pipelines/pipelines-as-code/test/pkg/gitlab" |
| 14 | + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload" |
| 15 | + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/scm" |
| 16 | + "github.com/tektoncd/pipeline/pkg/names" |
| 17 | + clientGitlab "gitlab.com/gitlab-org/api/client-go" |
| 18 | + "gotest.tools/v3/assert" |
| 19 | +) |
| 20 | + |
| 21 | +// TestGitlabSuccessStatusAfterOkToTest tests that when an unauthorized user |
| 22 | +// creates a fork MR, the CI status starts as pending/skipped, and after an |
| 23 | +// authorized user posts /ok-to-test the status transitions to success. |
| 24 | +func TestGitlabSuccessStatusAfterOkToTest(t *testing.T) { |
| 25 | + ctx := context.Background() |
| 26 | + if !tgitlab.HasSecondIdentity() { |
| 27 | + t.Skip("Skipping: TEST_GITLAB_SECOND_TOKEN is not configured") |
| 28 | + } |
| 29 | + |
| 30 | + topts := &tgitlab.TestOpts{ |
| 31 | + NoMRCreation: true, |
| 32 | + TargetEvent: triggertype.PullRequest.String(), |
| 33 | + } |
| 34 | + |
| 35 | + runcnx, opts, glprovider, err := tgitlab.Setup(ctx) |
| 36 | + assert.NilError(t, err, fmt.Errorf("cannot do gitlab setup: %w", err)) |
| 37 | + topts.GLProvider = glprovider |
| 38 | + topts.ParamsRun = runcnx |
| 39 | + topts.Opts = opts |
| 40 | + topts.TargetRefName = names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test") |
| 41 | + topts.TargetNS = names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns") |
| 42 | + |
| 43 | + // Create a fresh GitLab project |
| 44 | + groupPath := os.Getenv("TEST_GITLAB_GROUP") |
| 45 | + hookURL := os.Getenv("TEST_GITLAB_SMEEURL") |
| 46 | + webhookSecret := os.Getenv("TEST_EL_WEBHOOK_SECRET") |
| 47 | + project, err := tgitlab.CreateGitLabProject(topts.GLProvider.Client(), groupPath, topts.TargetRefName, hookURL, webhookSecret, false, topts.ParamsRun.Clients.Log) |
| 48 | + assert.NilError(t, err) |
| 49 | + topts.ProjectID = int(project.ID) |
| 50 | + topts.ProjectInfo = project |
| 51 | + topts.GitHTMLURL = project.WebURL |
| 52 | + topts.DefaultBranch = project.DefaultBranch |
| 53 | + |
| 54 | + defer func() { |
| 55 | + if os.Getenv("TEST_NOCLEANUP") != "true" { |
| 56 | + tgitlab.TearDown(ctx, t, topts) |
| 57 | + } |
| 58 | + }() |
| 59 | + |
| 60 | + assert.NilError(t, tgitlab.SetupSecondIdentity(ctx, topts)) |
| 61 | + |
| 62 | + err = tgitlab.CreateCRD(ctx, topts) |
| 63 | + assert.NilError(t, err) |
| 64 | + |
| 65 | + // Fork project as second user |
| 66 | + forkProject, err := tgitlab.ForkGitLabProject( |
| 67 | + topts.SecondGLProvider.Client(), |
| 68 | + topts.ProjectID, |
| 69 | + os.Getenv("TEST_GITLAB_SECOND_GROUP"), |
| 70 | + false, |
| 71 | + topts.ParamsRun.Clients.Log, |
| 72 | + ) |
| 73 | + assert.NilError(t, err) |
| 74 | + defer func() { |
| 75 | + topts.ParamsRun.Clients.Log.Infof("Deleting fork project %d", forkProject.ID) |
| 76 | + _, err := topts.SecondGLProvider.Client().Projects.DeleteProject(forkProject.ID, nil) |
| 77 | + if err != nil { |
| 78 | + t.Logf("Error deleting fork project %d: %v", forkProject.ID, err) |
| 79 | + } |
| 80 | + }() |
| 81 | + |
| 82 | + // Grant first user access to fork so controller can read .tekton |
| 83 | + firstUser, _, err := topts.GLProvider.Client().Users.CurrentUser() |
| 84 | + assert.NilError(t, err) |
| 85 | + assert.NilError(t, tgitlab.AddGitLabProjectMember( |
| 86 | + topts.SecondGLProvider.Client(), |
| 87 | + int(forkProject.ID), |
| 88 | + firstUser.ID, |
| 89 | + clientGitlab.DeveloperPermissions, |
| 90 | + topts.ParamsRun.Clients.Log, |
| 91 | + )) |
| 92 | + |
| 93 | + time.Sleep(5 * time.Second) |
| 94 | + |
| 95 | + entries, err := payload.GetEntries(map[string]string{ |
| 96 | + ".tekton/pr.yaml": "testdata/pipelinerun.yaml", |
| 97 | + }, topts.TargetNS, topts.DefaultBranch, triggertype.PullRequest.String(), map[string]string{}) |
| 98 | + assert.NilError(t, err) |
| 99 | + |
| 100 | + targetRefName := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-fork-oktotest") |
| 101 | + forkCloneURL, err := scm.MakeGitCloneURL(forkProject.WebURL, topts.SecondOpts.UserName, topts.SecondOpts.Password) |
| 102 | + assert.NilError(t, err) |
| 103 | + |
| 104 | + _ = scm.PushFilesToRefGit(t, &scm.Opts{ |
| 105 | + GitURL: forkCloneURL, |
| 106 | + CommitTitle: "Add fork ok-to-test fixtures - " + targetRefName, |
| 107 | + Log: topts.ParamsRun.Clients.Log, |
| 108 | + WebURL: forkProject.WebURL, |
| 109 | + TargetRefName: targetRefName, |
| 110 | + BaseRefName: topts.DefaultBranch, |
| 111 | + }, entries) |
| 112 | + |
| 113 | + // Create MR from fork to original project |
| 114 | + mrTitle := "TestGitlabSuccessStatusAfterOkToTest - " + targetRefName |
| 115 | + mr, _, err := topts.SecondGLProvider.Client().MergeRequests.CreateMergeRequest(forkProject.ID, &clientGitlab.CreateMergeRequestOptions{ |
| 116 | + Title: &mrTitle, |
| 117 | + SourceBranch: &targetRefName, |
| 118 | + TargetBranch: &topts.ProjectInfo.DefaultBranch, |
| 119 | + TargetProjectID: &topts.ProjectInfo.ID, |
| 120 | + }) |
| 121 | + assert.NilError(t, err) |
| 122 | + defer func() { |
| 123 | + _, _, err := topts.GLProvider.Client().MergeRequests.UpdateMergeRequest(topts.ProjectID, mr.IID, |
| 124 | + &clientGitlab.UpdateMergeRequestOptions{StateEvent: clientGitlab.Ptr("close")}) |
| 125 | + if err != nil { |
| 126 | + t.Logf("Error closing MR %d: %v", mr.IID, err) |
| 127 | + } |
| 128 | + }() |
| 129 | + |
| 130 | + mr, _, err = topts.GLProvider.Client().MergeRequests.GetMergeRequest(topts.ProjectID, mr.IID, nil) |
| 131 | + assert.NilError(t, err) |
| 132 | + topts.ParamsRun.Clients.Log.Infof("Created fork MR %q with SHA %s", mr.WebURL, mr.SHA) |
| 133 | + |
| 134 | + // Post /ok-to-test as authorized user (first user / admin) |
| 135 | + topts.ParamsRun.Clients.Log.Infof("Posting /ok-to-test comment as authorized user on MR %d", mr.IID) |
| 136 | + _, _, err = topts.GLProvider.Client().Notes.CreateMergeRequestNote(topts.ProjectID, mr.IID, |
| 137 | + &clientGitlab.CreateMergeRequestNoteOptions{Body: clientGitlab.Ptr("/ok-to-test")}) |
| 138 | + assert.NilError(t, err) |
| 139 | + |
| 140 | + // Wait for the pending/skipped status from the unauthorized fork MR |
| 141 | + topts.ParamsRun.Clients.Log.Infof("Waiting for pending status on fork MR from unauthorized user") |
| 142 | + sourceStatusCount, err := tgitlab.WaitForGitLabCommitStatusCount(ctx, topts.SecondGLProvider.Client(), topts.ParamsRun.Clients.Log, int(forkProject.ID), mr.SHA, "success", 2) |
| 143 | + assert.NilError(t, err) |
| 144 | + assert.Assert(t, sourceStatusCount == 2, "expected 2 success commit status on fork, got %d", sourceStatusCount) |
| 145 | +} |
0 commit comments