From d1cff10be3c613acb6547f4f74ca7409543e02c6 Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Tue, 10 Dec 2024 22:49:33 +0100 Subject: [PATCH] add github e2e Signed-off-by: Chmouel Boudjnah --- test/github_pullrequest_test.go | 58 +++++++++++++++++++++++++++++++++ test/pkg/github/pr.go | 13 ++++---- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/test/github_pullrequest_test.go b/test/github_pullrequest_test.go index 106a7c140..e2ebb26de 100644 --- a/test/github_pullrequest_test.go +++ b/test/github_pullrequest_test.go @@ -14,6 +14,8 @@ import ( "github.com/google/go-github/v66/github" "github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments" + "github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings" + "github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype" tgithub "github.com/openshift-pipelines/pipelines-as-code/test/pkg/github" "github.com/openshift-pipelines/pipelines-as-code/test/pkg/options" twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait" @@ -69,6 +71,62 @@ func TestGithubPullRequestMatchOnCEL(t *testing.T) { defer g.TearDown(ctx, t) } +func TestGithubPullRequestOnLabel(t *testing.T) { + ctx := context.Background() + g := &tgithub.PRTest{ + Label: "Github On Label", + YamlFiles: []string{"testdata/pipelinerun-on-label.yaml"}, + NoStatusCheck: true, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) + + // wait a bit that GitHub processed or we will get double events + time.Sleep(5 * time.Second) + + g.Cnx.Clients.Log.Infof("Creating a label bug on PullRequest") + _, _, err := g.Provider.Client.Issues.AddLabelsToIssue(ctx, + g.Options.Organization, + g.Options.Repo, g.PRNumber, + []string{"bug"}) + assert.NilError(t, err) + + sopt := twait.SuccessOpt{ + Title: g.CommitTitle, + OnEvent: triggertype.LabelUpdate.String(), + TargetNS: g.TargetNamespace, + NumberofPRMatch: len(g.YamlFiles), + SHA: g.SHA, + } + twait.Succeeded(ctx, t, g.Cnx, g.Options, sopt) + + opt := github.ListOptions{} + res := &github.ListCheckRunsResults{} + resp := &github.Response{} + counter := 0 + for { + res, resp, err = g.Provider.Client.Checks.ListCheckRunsForRef(ctx, g.Options.Organization, g.Options.Repo, g.SHA, &github.ListCheckRunsOptions{ + AppID: g.Provider.ApplicationID, + ListOptions: opt, + }) + assert.NilError(t, err) + assert.Equal(t, resp.StatusCode, 200) + if len(res.CheckRuns) > 0 { + break + } + g.Cnx.Clients.Log.Infof("Waiting for the check run to be created") + if counter > 10 { + t.Errorf("Check run not created after 10 tries") + break + } + time.Sleep(5 * time.Second) + } + assert.Equal(t, len(res.CheckRuns), 1) + expected := fmt.Sprintf("%s / %s", settings.PACApplicationNameDefaultValue, "pipelinerun-on-label-") + checkName := res.CheckRuns[0].GetName() + assert.Assert(t, strings.HasPrefix(checkName, expected), "checkName %s != expected %s", checkName, expected) +} + func TestGithubPullRequestCELMatchOnTitle(t *testing.T) { ctx := context.Background() g := &tgithub.PRTest{ diff --git a/test/pkg/github/pr.go b/test/pkg/github/pr.go index 95db6bfb8..543dd433a 100644 --- a/test/pkg/github/pr.go +++ b/test/pkg/github/pr.go @@ -118,6 +118,7 @@ type PRTest struct { PRNumber int SHA string Logger *zap.SugaredLogger + CommitTitle string } func (g *PRTest) RunPullRequest(ctx context.Context, t *testing.T) { @@ -127,12 +128,12 @@ func (g *PRTest) RunPullRequest(ctx context.Context, t *testing.T) { assert.NilError(t, err) g.Logger = runcnx.Clients.Log - logmsg := fmt.Sprintf("Testing %s with Github APPS integration on %s", g.Label, targetNS) - g.Logger.Info(logmsg) + g.CommitTitle = fmt.Sprintf("Testing %s with Github APPS integration on %s", g.Label, targetNS) + g.Logger.Info(g.CommitTitle) repoinfo, resp, err := ghcnx.Client.Repositories.Get(ctx, opts.Organization, opts.Repo) assert.NilError(t, err) - if resp != nil && resp.Response.StatusCode == http.StatusNotFound { + if resp != nil && resp.StatusCode == http.StatusNotFound { t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo) } @@ -151,17 +152,17 @@ func (g *PRTest) RunPullRequest(ctx context.Context, t *testing.T) { targetRefName := fmt.Sprintf("refs/heads/%s", names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test")) - sha, vref, err := PushFilesToRef(ctx, ghcnx.Client, logmsg, repoinfo.GetDefaultBranch(), targetRefName, + sha, vref, err := PushFilesToRef(ctx, ghcnx.Client, g.CommitTitle, repoinfo.GetDefaultBranch(), targetRefName, opts.Organization, opts.Repo, entries) assert.NilError(t, err) g.Logger.Infof("Commit %s has been created and pushed to %s", sha, vref.GetURL()) number, err := PRCreate(ctx, runcnx, ghcnx, opts.Organization, - opts.Repo, targetRefName, repoinfo.GetDefaultBranch(), logmsg) + opts.Repo, targetRefName, repoinfo.GetDefaultBranch(), g.CommitTitle) assert.NilError(t, err) if !g.NoStatusCheck { sopt := wait.SuccessOpt{ - Title: logmsg, + Title: g.CommitTitle, OnEvent: triggertype.PullRequest.String(), TargetNS: targetNS, NumberofPRMatch: len(g.YamlFiles),