-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathgithub_test.go
363 lines (312 loc) · 12.9 KB
/
github_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
Copyright 2022 Red Hat Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package github_test
import (
"context"
"time"
"github.com/go-logr/logr"
ghapi "github.com/google/go-github/v45/github"
"github.com/konflux-ci/integration-service/git/github"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
const samplePrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDFuZgVJy0ZUPMa8WerKv/bY9uyBfIJVAdJHf8S1tT2hhLd0hTr
gRTwPdfQOPTpoBZwzDDDjaeztgGCHxDtc4MI06mRJ/bdKKbWOseybONHMRRAC02X
Wql/QhOT49n77FrculoKSZ9P7M9mHOiwWZgqcZCHNfvE0zzDtazbAz71FwIDAQAB
AoGANkwCHIT2mIYnTFXQnyueuVORyDBjx/YATi7jlfvS3aPx2GJRhl6bLmH9Otv5
PZLNtsoL9heXXv1zKpV3Y42eBLkAsIIyD7H2o74KSRCj8w1mOpvzLgS8fjy7Cve8
NUVmhaNfDvrDck16yXkYQ3tF68DFRbs0an7lrstqBf4Bp6kCQQD+j1qhqoxz00Jo
nES3Ba8PVGBvOuXK7IKD/ul7QZdalC0bChM5WM3tKcNNXQvlcffjqUyv55jabzax
I/MstVIbAkEAxtfu7xF/hOYor5bZ51pOPozg0PLm7u+n2NGoa5gbt3XDAw88+uAl
jdWzmPDmKefD7x0eUuWImRVGj9pg2KI4tQJBAIwUlusfzupt9r1aQPX2Sr9Ez0xm
PM3OGIRKYnFRRtixcaDPioSkOa2orbCE42b/sEm3gFvMNnD9gjs4bTjNDsECQQCq
3D1pnAWRMxxe1Sbkj0qQoQlFQpOBWNlHi9BGs+uNF1m8tUlW4Wgjmi+7CbUc6RQJ
0WGPJcDcmdWKIsH5JFvJAkEAyNNOsbLp1DjZE0rMS5W8YHPR5USzlOSprsrsN8F/
WGhfkh9h9Db8xSx4boFcEqwyyHd2E1Hjbp5q3fzp06XecA==
-----END RSA PRIVATE KEY-----`
type MockAppsService struct{}
// CreateInstallationToken implements github.AppsService
func (MockAppsService) CreateInstallationToken(
ctx context.Context, id int64, opts *ghapi.InstallationTokenOptions,
) (*ghapi.InstallationToken, *ghapi.Response, error) {
token := "example-token"
return &ghapi.InstallationToken{Token: &token}, nil, nil
}
type MockChecksService struct {
ListCheckRunsForRefResult []*ghapi.CheckRun
}
// CreateCheckRun implements github.ChecksService
func (MockChecksService) CreateCheckRun(
ctx context.Context, owner string, repo string, opts ghapi.CreateCheckRunOptions,
) (*ghapi.CheckRun, *ghapi.Response, error) {
var id int64 = 10
return &ghapi.CheckRun{ID: &id}, nil, nil
}
// ListCheckRunsForRef implements github.ChecksService
func (MockChecksService) ListCheckRunsForRef(
ctx context.Context, owner string, repo string, ref string, opts *ghapi.ListCheckRunsOptions,
) (*ghapi.ListCheckRunsResults, *ghapi.Response, error) {
var id int64 = 20
var externalID string = "example-external-id"
var text string = "example-text-update"
var checkRunOutput = ghapi.CheckRunOutput{Text: &text}
conclusion := "failure"
checkRuns := []*ghapi.CheckRun{{ID: &id, ExternalID: &externalID, Conclusion: &conclusion, Output: &checkRunOutput}}
total := len(checkRuns)
return &ghapi.ListCheckRunsResults{Total: &total, CheckRuns: checkRuns}, nil, nil
}
// GetAllCheckRunsForRef implements github.ChecksService
func (MockChecksService) GetAllCheckRunsForRef(
ctx context.Context, owner string, repo string, ref string, appID int64,
) ([]*ghapi.CheckRun, error) {
var id int64 = 20
var externalID string = "example-external-id"
var text string = "example-text-update"
var checkRunOutput = ghapi.CheckRunOutput{Text: &text}
conclusion := "failure"
checkRuns := []*ghapi.CheckRun{{ID: &id, ExternalID: &externalID, Conclusion: &conclusion, Output: &checkRunOutput}}
return checkRuns, nil
}
// UpdateCheckRun implements github.ChecksService
func (MockChecksService) UpdateCheckRun(
ctx context.Context, owner string, repo string, checkRunID int64, opts ghapi.UpdateCheckRunOptions,
) (*ghapi.CheckRun, *ghapi.Response, error) {
var id int64 = 30
return &ghapi.CheckRun{ID: &id}, nil, nil
}
type MockIssuesService struct{}
// CreateComment implements github.IssuesService
func (MockIssuesService) CreateComment(
ctx context.Context, owner string, repo string, number int, comment *ghapi.IssueComment,
) (*ghapi.IssueComment, *ghapi.Response, error) {
var id int64 = 40
return &ghapi.IssueComment{ID: &id}, nil, nil
}
// ListComments implements github.IssuesService
func (MockIssuesService) ListComments(ctx context.Context, owner string, repo string,
number int, opts *ghapi.IssueListCommentsOptions) ([]*ghapi.IssueComment, *ghapi.Response, error) {
var id int64 = 40
var body string = "Integration test for snapshot snapshotName and scenario scenarioName"
issueComments := []*ghapi.IssueComment{{ID: &id, Body: &body}}
return issueComments, nil, nil
}
// EditComment implements github.IssuesService
func (MockIssuesService) EditComment(ctx context.Context, owner string, repo string, number int64, comment *ghapi.IssueComment,
) (*ghapi.IssueComment, *ghapi.Response, error) {
return &ghapi.IssueComment{ID: &number}, nil, nil
}
type MockRepositoriesService struct{}
// CreateStatus implements github.RepositoriesService
func (MockRepositoriesService) CreateStatus(
ctx context.Context, owner string, repo string, ref string, status *ghapi.RepoStatus,
) (*ghapi.RepoStatus, *ghapi.Response, error) {
var id int64 = 50
var state = "success"
return &ghapi.RepoStatus{ID: &id, State: &state}, nil, nil
}
// ListStatuses implements github.RepositoriesService
func (MockRepositoriesService) ListStatuses(
ctx context.Context, owner string, repo string, ref string, opts *ghapi.ListOptions,
) ([]*ghapi.RepoStatus, *ghapi.Response, error) {
var id int64 = 60
var state = "success"
var description = "example-description"
var context = "example-context"
var target_url = "https://example.com"
repoStatus := &ghapi.RepoStatus{ID: &id, State: &state, Description: &description, Context: &context, TargetURL: &target_url}
return []*ghapi.RepoStatus{repoStatus}, nil, nil
}
type MockPullRequestsService struct {
GetPullRequestResult *ghapi.PullRequest
}
// MockPullRequestsService implements github.PullRequestsService
func (MockPullRequestsService) Get(
ctx context.Context, owner string, repo string, prID int,
) (*ghapi.PullRequest, *ghapi.Response, error) {
var id int64 = 60
var state = "opened"
GetPullRequestResult := &ghapi.PullRequest{ID: &id, State: &state}
return GetPullRequestResult, nil, nil
}
var _ = Describe("CheckRunAdapter", func() {
It("can compute status", func() {
adapter := &github.CheckRunAdapter{Conclusion: "success", StartTime: time.Time{}}
Expect(adapter.GetStatus()).To(Equal("completed"))
adapter.Conclusion = "failure"
Expect(adapter.GetStatus()).To(Equal("completed"))
adapter.Conclusion = ""
Expect(adapter.GetStatus()).To(Equal("queued"))
adapter.StartTime = time.Now()
Expect(adapter.GetStatus()).To(Equal("in_progress"))
})
})
var _ = Describe("Client", func() {
var (
client *github.Client
mockAppsSvc MockAppsService
mockChecksSvc MockChecksService
mockIssuesSvc MockIssuesService
mockReposSvc MockRepositoriesService
mockPullRequestsSvc MockPullRequestsService
)
var checkRunAdapter = &github.CheckRunAdapter{
Name: "example-name",
Owner: "example-owner",
Repository: "example-repo",
SHA: "abcdef1",
ExternalID: "example-external-id",
DetailsURL: "https://example.com",
Conclusion: "Passed",
Title: "example-title",
Summary: "example-summary",
Text: "example-text",
StartTime: time.Now(),
CompletionTime: time.Now(),
}
var commitStatusAdapter = &github.CommitStatusAdapter{
Owner: "example-owner",
Repository: "example-repo",
SHA: "abcdef1",
State: "success",
Description: "example-description",
Context: "example-context",
TargetURL: "https://example.com",
}
BeforeEach(func() {
mockAppsSvc = MockAppsService{}
mockChecksSvc = MockChecksService{}
mockIssuesSvc = MockIssuesService{}
mockReposSvc = MockRepositoriesService{}
mockPullRequestsSvc = MockPullRequestsService{}
client = github.NewClient(
logr.Discard(),
github.WithAppsService(mockAppsSvc),
github.WithChecksService(mockChecksSvc),
github.WithIssuesService(mockIssuesSvc),
github.WithRepositoriesService(mockReposSvc),
github.WithPullRequestsService(mockPullRequestsSvc),
)
})
It("can create app installation tokens", func() {
token, err := client.CreateAppInstallationToken(context.TODO(), 1, 1, []byte(samplePrivateKey))
Expect(err).ToNot(HaveOccurred())
Expect(token).To(Equal("example-token"))
})
It("accepts an OAuth token", func() {
client.SetOAuthToken(context.TODO(), "example-token")
Expect(client.GetAppsService()).To(Equal(mockAppsSvc))
Expect(client.GetChecksService()).To(Equal(mockChecksSvc))
Expect(client.GetIssuesService()).To(Equal(mockIssuesSvc))
Expect(client.GetRepositoriesService()).To(Equal(mockReposSvc))
Expect(client.GetPullRequestsService()).To(Equal(mockPullRequestsSvc))
client = github.NewClient(logr.Discard())
client.SetOAuthToken(context.TODO(), "example-token")
Expect(client.GetAppsService()).ToNot(Equal(mockAppsSvc))
Expect(client.GetChecksService()).ToNot(Equal(mockChecksSvc))
Expect(client.GetIssuesService()).ToNot(Equal(mockIssuesSvc))
Expect(client.GetRepositoriesService()).ToNot(Equal(mockReposSvc))
Expect(client.GetPullRequestsService()).ToNot(Equal(mockPullRequestsSvc))
})
It("can create comments", func() {
id, err := client.CreateComment(context.TODO(), "", "", 1, "example-comment")
Expect(err).ToNot(HaveOccurred())
Expect(id).To(Equal(int64(40)))
})
It("can create commit statuses", func() {
id, err := client.CreateCommitStatus(context.TODO(), "", "", "", "", "", "", "")
Expect(err).ToNot(HaveOccurred())
Expect(id).To(Equal(int64(50)))
})
It("can set and get details URL", func() {
detilsURL := "https://example.com/details"
checkRunAdapter.DetailsURL = detilsURL
Expect(checkRunAdapter.DetailsURL).To(Equal(detilsURL))
})
It("can create check runs", func() {
checkRunID, err := client.CreateCheckRun(context.TODO(), checkRunAdapter)
Expect(err).ToNot(HaveOccurred())
Expect(checkRunID).ToNot(BeNil())
Expect(*checkRunID).To(Equal(int64(10)))
})
It("can update check runs", func() {
err := client.UpdateCheckRun(context.TODO(), 1, checkRunAdapter)
Expect(err).ToNot(HaveOccurred())
})
It("can get a check run ID", func() {
checkRunID, err := client.GetCheckRunID(context.TODO(), "", "", "", "example-external-id", 1)
Expect(err).ToNot(HaveOccurred())
Expect(checkRunID).ToNot(BeNil())
Expect(*checkRunID).To(Equal(int64(20)))
checkRunID, err = client.GetCheckRunID(context.TODO(), "", "", "", "unknown-external-id", 1)
Expect(err).ToNot(HaveOccurred())
Expect(checkRunID).To(BeNil())
})
It("can check if check run updated is needed", func() {
var checkRunAdapter = &github.CheckRunAdapter{
Name: "example-name",
Owner: "example-owner",
Repository: "example-repo",
SHA: "abcdef1",
ExternalID: "example-external-id",
Conclusion: "success",
Title: "example-title",
Summary: "example-summary",
Text: "example-text",
StartTime: time.Now(),
CompletionTime: time.Now(),
}
allCheckRuns, err := client.GetAllCheckRunsForRef(context.TODO(), "", "", "", 1)
Expect(err).ToNot(HaveOccurred())
Expect(allCheckRuns).NotTo(BeEmpty())
existingCheckRun := client.GetExistingCheckRun(allCheckRuns, checkRunAdapter)
Expect(existingCheckRun).NotTo(BeNil())
})
It("can check if creating a new commit status is needed", func() {
commitStatuses, err := client.GetAllCommitStatusesForRef(context.TODO(), "", "", "")
Expect(err).ToNot(HaveOccurred())
Expect(commitStatuses).NotTo(BeEmpty())
commitStatusExist, err := client.CommitStatusExists(commitStatuses, commitStatusAdapter)
Expect(commitStatusExist).To(BeTrue())
Expect(err).ToNot(HaveOccurred())
commitStatusAdapter = &github.CommitStatusAdapter{
Owner: "example-owner",
Repository: "example-repo",
SHA: "abcdef1",
State: "failure",
Description: "example-description",
Context: "example-context",
TargetURL: "https://example.com",
}
commitStatusExist, err = client.CommitStatusExists(commitStatuses, commitStatusAdapter)
Expect(commitStatusExist).To(BeFalse())
Expect(err).ToNot(HaveOccurred())
})
It("can get existing comment id", func() {
comments, err := client.GetAllCommentsForPR(context.TODO(), "", "", 1)
Expect(err).ToNot(HaveOccurred())
Expect(comments).NotTo(BeEmpty())
commentID := client.GetExistingCommentID(comments, "snapshotName", "scenarioName")
Expect(*commentID).To(Equal(int64(40)))
})
It("can edit comments", func() {
id, err := client.EditComment(context.TODO(), "", "", 1, "example-comment")
Expect(err).ToNot(HaveOccurred())
Expect(id).To(Equal(int64(1)))
})
It("can get pull request", func() {
pullRequest, err := client.GetPullRequest(context.TODO(), "", "", 60)
Expect(err).ToNot(HaveOccurred())
Expect(*pullRequest.State).To(Equal("opened"))
})
})