forked from openshift-pipelines/pipelines-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_pullrequest_rerequest_test.go
170 lines (152 loc) · 5.08 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//go:build e2e
// +build e2e
package test
import (
"context"
"fmt"
"net/http"
"os"
"strconv"
"testing"
"github.com/google/go-github/v42/github"
pacv1alpha1 "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
"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"
trepo "github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository"
twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
"github.com/tektoncd/pipeline/pkg/names"
"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestGithubPullRerequest(t *testing.T) {
targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns")
ctx := context.Background()
runcnx, opts, ghcnx, err := githubSetup(ctx, false)
assert.NilError(t, err)
entries := map[string]string{
".tekton/info.yaml": fmt.Sprintf(`---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: pipeline
annotations:
pipelinesascode.tekton.dev/target-namespace: "%s"
pipelinesascode.tekton.dev/on-target-branch: "[%s]"
pipelinesascode.tekton.dev/on-event: "[%s]"
spec:
pipelineSpec:
tasks:
- name: task
taskSpec:
steps:
- name: task
image: gcr.io/google-containers/busybox
command: ["/bin/echo", "HELLOMOTO"]
`, targetNS, mainBranch, pullRequestEvent),
}
repoinfo, resp, err := ghcnx.Client.Repositories.Get(ctx, opts.Organization, opts.Repo)
assert.NilError(t, err)
if resp != nil && resp.Response.StatusCode == http.StatusNotFound {
t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo)
}
repository := &pacv1alpha1.Repository{
ObjectMeta: metav1.ObjectMeta{
Name: targetNS,
},
Spec: pacv1alpha1.RepositorySpec{
URL: repoinfo.GetHTMLURL(),
},
}
err = trepo.CreateNS(ctx, targetNS, runcnx)
assert.NilError(t, err)
err = trepo.CreateRepo(ctx, targetNS, runcnx, repository)
assert.NilError(t, err)
targetRefName := fmt.Sprintf("refs/heads/%s",
names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test"))
sha, err := tgithub.PushFilesToRef(ctx, ghcnx.Client,
"TestPullRequest - "+targetRefName, repoinfo.GetDefaultBranch(),
targetRefName,
opts.Organization,
opts.Repo,
entries)
assert.NilError(t, err)
runcnx.Clients.Log.Infof("Commit %s has been created and pushed to %s", sha, targetRefName)
title := "TestPullRerequest on " + targetRefName
number, err := tgithub.PRCreate(ctx, runcnx, ghcnx, opts.Organization, opts.Repo, targetRefName, repoinfo.GetDefaultBranch(), title)
assert.NilError(t, err)
defer ghtearDown(ctx, t, runcnx, ghcnx, number, targetRefName, targetNS, opts)
runcnx.Clients.Log.Infof("Waiting for Repository to be updated")
waitOpts := twait.Opts{
RepoName: targetNS,
Namespace: targetNS,
MinNumberStatus: 0,
PollTimeout: defaultTimeout,
TargetSHA: sha,
}
err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts)
assert.NilError(t, err)
runinfo := info.Event{
BaseBranch: repoinfo.GetDefaultBranch(),
DefaultBranch: repoinfo.GetDefaultBranch(),
HeadBranch: targetRefName,
Organization: opts.Organization,
Repository: opts.Repo,
URL: repoinfo.GetHTMLURL(),
SHA: sha,
Sender: opts.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(number),
},
},
},
},
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,
runcnx,
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)
runcnx.Clients.Log.Infof("Wait for the second repository update to be updated")
waitOpts = twait.Opts{
RepoName: targetNS,
Namespace: targetNS,
MinNumberStatus: 1,
PollTimeout: defaultTimeout,
TargetSHA: sha,
}
err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts)
assert.NilError(t, err)
runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded")
repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{})
assert.NilError(t, err)
assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue)
}