-
Notifications
You must be signed in to change notification settings - Fork 89
/
github_pullrequest_rerequest_test.go
152 lines (140 loc) · 4.37 KB
/
github_pullrequest_rerequest_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//go:build e2e
// +build e2e
package test
import (
"context"
"net/http"
"os"
"strconv"
"testing"
"github.com/google/go-github/v66/github"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
tgithub "github.com/openshift-pipelines/pipelines-as-code/test/pkg/github"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// TestGithubPullRerequest is a test that will create a pull request and check
// if we can rerequest a specific check or the full check suite.
func TestGithubPullRerequest(t *testing.T) {
if os.Getenv("NIGHTLY_E2E_TEST") != "true" {
t.Skip("Skipping test since only enabled for nightly")
}
ctx := context.TODO()
g := &tgithub.PRTest{
Label: "Github Rerequest",
YamlFiles: []string{"testdata/pipelinerun.yaml"},
}
g.RunPullRequest(ctx, t)
defer g.TearDown(ctx, t)
repoinfo, resp, err := g.Provider.Client.Repositories.Get(ctx, g.Options.Organization, g.Options.Repo)
assert.NilError(t, err)
if resp != nil && resp.StatusCode == http.StatusNotFound {
t.Errorf("Repository %s not found in %s", g.Options.Organization, g.Options.Repo)
}
runinfo := info.Event{
DefaultBranch: repoinfo.GetDefaultBranch(),
HeadBranch: g.TargetRefName,
Organization: g.Options.Organization,
Repository: g.Options.Repo,
URL: repoinfo.GetHTMLURL(),
SHA: g.SHA,
Sender: g.Options.Organization,
}
installID, err := strconv.ParseInt(os.Getenv("TEST_GITHUB_REPO_INSTALLATION_ID"), 10, 64)
assert.NilError(t, err)
event := github.CheckRunEvent{
Action: github.String("rerequested"),
Installation: &github.Installation{
ID: &installID,
},
CheckRun: &github.CheckRun{
CheckSuite: &github.CheckSuite{
HeadBranch: &runinfo.HeadBranch,
HeadSHA: &runinfo.SHA,
PullRequests: []*github.PullRequest{
{
Number: github.Int(g.PRNumber),
},
},
},
},
Repo: &github.Repository{
DefaultBranch: &runinfo.DefaultBranch,
HTMLURL: &runinfo.URL,
Name: &runinfo.Repository,
Owner: &github.User{Login: &runinfo.Organization},
},
Sender: &github.User{
Login: &runinfo.Sender,
},
}
err = payload.Send(ctx,
g.Cnx,
os.Getenv("TEST_EL_URL"),
os.Getenv("TEST_EL_WEBHOOK_SECRET"),
os.Getenv("TEST_GITHUB_API_URL"),
os.Getenv("TEST_GITHUB_REPO_INSTALLATION_ID"),
event,
"check_run",
)
assert.NilError(t, err)
g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated")
_, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, twait.Opts{
RepoName: g.TargetNamespace,
Namespace: g.TargetNamespace,
MinNumberStatus: 1,
PollTimeout: twait.DefaultTimeout,
TargetSHA: g.SHA,
})
assert.NilError(t, err)
g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded")
repo, err := g.Cnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(g.TargetNamespace).Get(ctx, g.TargetNamespace, metav1.GetOptions{})
assert.NilError(t, err)
assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue)
csEvent := github.CheckSuiteEvent{
Action: github.String("rerequested"),
Installation: &github.Installation{
ID: &installID,
},
CheckSuite: &github.CheckSuite{
HeadBranch: &runinfo.HeadBranch,
HeadSHA: &runinfo.SHA,
PullRequests: []*github.PullRequest{
{
Number: github.Int(g.PRNumber),
},
},
},
Repo: &github.Repository{
DefaultBranch: &runinfo.DefaultBranch,
HTMLURL: &runinfo.URL,
Name: &runinfo.Repository,
Owner: &github.User{Login: &runinfo.Organization},
},
Sender: &github.User{
Login: &runinfo.Sender,
},
}
err = payload.Send(ctx,
g.Cnx,
os.Getenv("TEST_EL_URL"),
os.Getenv("TEST_EL_WEBHOOK_SECRET"),
os.Getenv("TEST_GITHUB_API_URL"),
os.Getenv("TEST_GITHUB_REPO_INSTALLATION_ID"),
csEvent,
"check_suite",
)
assert.NilError(t, err)
g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated")
_, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, twait.Opts{
RepoName: g.TargetNamespace,
Namespace: g.TargetNamespace,
MinNumberStatus: 2,
PollTimeout: twait.DefaultTimeout,
TargetSHA: g.SHA,
})
assert.NilError(t, err)
}