Skip to content

Commit

Permalink
add github e2e
Browse files Browse the repository at this point in the history
Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel committed Dec 11, 2024
1 parent 999c950 commit 3f26707
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
58 changes: 58 additions & 0 deletions test/github_pullrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
13 changes: 7 additions & 6 deletions test/pkg/github/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}

Expand All @@ -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),
Expand Down

0 comments on commit 3f26707

Please sign in to comment.