-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathreporter_github.go
657 lines (569 loc) · 26 KB
/
reporter_github.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
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 status
import (
"context"
"fmt"
"strconv"
"github.com/go-logr/logr"
ghapi "github.com/google/go-github/v45/github"
applicationapiv1alpha1 "github.com/konflux-ci/application-api/api/v1alpha1"
"github.com/konflux-ci/integration-service/git/github"
"github.com/konflux-ci/integration-service/gitops"
"github.com/konflux-ci/integration-service/helpers"
intgteststat "github.com/konflux-ci/integration-service/pkg/integrationteststatus"
"github.com/konflux-ci/operator-toolkit/metadata"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)
// Used by statusReport to get pipelines-as-code-secret under NS integration-service
const (
integrationNS = "integration-service"
PACSecret = "pipelines-as-code-secret"
gitHubApplicationID = "github-application-id"
gitHubPrivateKey = "github-private-key"
)
// StatusUpdater is common interface used by status reporter to update PR status
type StatusUpdater interface {
// Authentication of client
Authenticate(ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) error
// Update status of PR
UpdateStatus(ctx context.Context, report TestReport) error
}
// CheckRunStatusUpdater updates PR status using CheckRuns (when application integration is enabled in repo)
type CheckRunStatusUpdater struct {
ghClient github.ClientInterface
k8sClient client.Client
logger *logr.Logger
owner string
repo string
sha string
snapshot *applicationapiv1alpha1.Snapshot
creds *appCredentials
allCheckRunsCache []*ghapi.CheckRun
}
// NewCheckRunStatusUpdater returns a pointer to initialized CheckRunStatusUpdater
func NewCheckRunStatusUpdater(
ghClient github.ClientInterface,
k8sClient client.Client,
logger *logr.Logger,
owner string,
repo string,
sha string,
snapshot *applicationapiv1alpha1.Snapshot,
) *CheckRunStatusUpdater {
return &CheckRunStatusUpdater{
ghClient: ghClient,
k8sClient: k8sClient,
logger: logger,
owner: owner,
repo: repo,
sha: sha,
snapshot: snapshot,
}
}
func GetAppCredentials(ctx context.Context, k8sclient client.Client, object client.Object) (*appCredentials, error) {
log := log.FromContext(ctx)
var err, unRecoverableError error
var found bool
appInfo := appCredentials{}
appInfo.InstallationID, err = strconv.ParseInt(object.GetAnnotations()[gitops.PipelineAsCodeInstallationIDAnnotation], 10, 64)
if err != nil {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("Error %s when parsing string annotation %s: %s", err.Error(), gitops.PipelineAsCodeInstallationIDAnnotation, object.GetAnnotations()[gitops.PipelineAsCodeInstallationIDAnnotation]))
log.Error(unRecoverableError, fmt.Sprintf("Error %s when parsing string annotation %s: %s", err.Error(), gitops.PipelineAsCodeInstallationIDAnnotation, object.GetAnnotations()[gitops.PipelineAsCodeInstallationIDAnnotation]))
return nil, unRecoverableError
}
// Get the global pipelines as code secret
pacSecret := v1.Secret{}
err = k8sclient.Get(ctx, types.NamespacedName{Namespace: integrationNS, Name: PACSecret}, &pacSecret)
if err != nil {
log.Error(err, fmt.Sprintf("failed to get pac secret %s/%s", integrationNS, PACSecret))
return nil, err
}
// Get the App ID from the secret
ghAppIDBytes, found := pacSecret.Data[gitHubApplicationID]
if !found {
unRecoverableError = helpers.NewUnrecoverableMetadataError("failed to find github-application-id secret key")
log.Error(unRecoverableError, "failed to find github-application-id secret key")
return nil, unRecoverableError
}
appInfo.AppID, err = strconv.ParseInt(string(ghAppIDBytes), 10, 64)
if err != nil {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("Error %s when parsing ghAppIDBytes", err.Error()))
log.Error(unRecoverableError, "failed to parse gitHub App ID")
return nil, unRecoverableError
}
// Get the App's private key from the secret
appInfo.PrivateKey, found = pacSecret.Data[gitHubPrivateKey]
if !found {
unRecoverableError = helpers.NewUnrecoverableMetadataError("failed to find github-private-key secret key")
return nil, unRecoverableError
}
return &appInfo, nil
}
// Authenticate Github Client with application credentials
func (cru *CheckRunStatusUpdater) Authenticate(ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) error {
creds, err := GetAppCredentials(ctx, cru.k8sClient, snapshot)
cru.creds = creds
if err != nil {
cru.logger.Error(err, "failed to get app credentials from Snapshot",
"snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return err
}
token, err := cru.ghClient.CreateAppInstallationToken(ctx, creds.AppID, creds.InstallationID, creds.PrivateKey)
if err != nil {
cru.logger.Error(err, "failed to create app installation token",
"creds.AppID", creds.AppID, "creds.InstallationID", creds.InstallationID)
return err
}
cru.ghClient.SetOAuthToken(ctx, token)
return nil
}
func (cru *CheckRunStatusUpdater) getAllCheckRuns(ctx context.Context) ([]*ghapi.CheckRun, error) {
if len(cru.allCheckRunsCache) == 0 {
allCheckRuns, err := cru.ghClient.GetAllCheckRunsForRef(ctx, cru.owner, cru.repo, cru.sha, cru.creds.AppID)
if err != nil {
cru.logger.Error(err, "failed to get all checkruns for ref",
"owner", cru.owner, "repo", cru.repo, "creds.AppID", cru.creds.AppID)
return nil, err
}
cru.allCheckRunsCache = allCheckRuns
}
return cru.allCheckRunsCache, nil
}
// createCheckRunAdapterForSnapshot create a CheckRunAdapter for given snapshot, integrationTestStatusDetail, owner, repo and sha to create a checkRun
// https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#create-a-check-run
func (cru *CheckRunStatusUpdater) createCheckRunAdapterForSnapshot(report TestReport) (*github.CheckRunAdapter, error) {
snapshot := cru.snapshot
detailsURL := ""
conclusion, err := generateCheckRunConclusion(report.Status)
if err != nil {
cru.logger.Error(err, fmt.Sprintf("failed to generate conclusion for integrationTestScenario %s and snapshot %s/%s", report.ScenarioName, snapshot.Namespace, snapshot.Name))
return nil, fmt.Errorf("unknown status %s for integrationTestScenario %s and snapshot %s/%s", report.Status, report.ScenarioName, snapshot.Namespace, snapshot.Name)
}
title, err := generateCheckRunTitle(report.Status)
if err != nil {
cru.logger.Error(err, fmt.Sprintf("failed to generate title for integrationTestScenario %s and snapshot %s/%s", report.ScenarioName, snapshot.Namespace, snapshot.Name))
return nil, fmt.Errorf("failed to generate title for integrationTestScenario %s and snapshot %s/%s", report.ScenarioName, snapshot.Namespace, snapshot.Name)
}
externalID := report.ScenarioName
if report.ComponentName != "" {
externalID = fmt.Sprintf("%s-%s", report.ScenarioName, report.ComponentName)
}
if report.TestPipelineRunName == "" {
cru.logger.Info("TestPipelineRunName is not set for CheckRun", "ExternalID", externalID)
} else {
detailsURL = FormatPipelineURL(report.TestPipelineRunName, snapshot.Namespace, *cru.logger)
}
cra := &github.CheckRunAdapter{
Owner: cru.owner,
Repository: cru.repo,
Name: report.FullName,
SHA: cru.sha,
ExternalID: externalID,
Conclusion: conclusion,
Title: title,
Summary: report.Summary,
Text: report.Text,
DetailsURL: detailsURL,
}
if start := report.StartTime; start != nil {
cra.StartTime = *start
}
if complete := report.CompletionTime; complete != nil {
cra.CompletionTime = *complete
}
return cra, nil
}
// UpdateStatus updates CheckRun status of PR
func (cru *CheckRunStatusUpdater) UpdateStatus(ctx context.Context, report TestReport) error {
if cru.creds == nil {
panic("authenticate first")
}
allCheckRuns, err := cru.getAllCheckRuns(ctx)
if err != nil {
cru.logger.Error(err, "failed to get all checkruns")
return err
}
checkRunAdapter, err := cru.createCheckRunAdapterForSnapshot(report)
if err != nil {
cru.logger.Error(err, "failed to create checkRunAdapter for scenario, skipping update",
"snapshot.NameSpace", cru.snapshot.Namespace, "snapshot.Name", cru.snapshot.Name,
"scenario.Name", report.ScenarioName,
)
return nil
}
existingCheckrun := cru.ghClient.GetExistingCheckRun(allCheckRuns, checkRunAdapter)
if existingCheckrun == nil {
cru.logger.Info("creating checkrun for scenario test status of snapshot",
"snapshot.NameSpace", cru.snapshot.Namespace, "snapshot.Name", cru.snapshot.Name, "scenarioName", report.ScenarioName, "externalID", checkRunAdapter.ExternalID)
_, err = cru.ghClient.CreateCheckRun(ctx, checkRunAdapter)
if err != nil {
cru.logger.Error(err, "failed to create checkrun",
"checkRunAdapter", checkRunAdapter)
}
return err
}
cru.logger.Info("found existing checkrun", "existingCheckRun", existingCheckrun)
// If pre-existing checkrun is already completed, then create a
// new checkrun with same external ID, rather than updating it
if existingCheckrun.GetStatus() == "completed" {
cru.logger.Info("The existing checkrun is already in completed state, re-creating a new checkrun for scenario test status of snapshot",
"snapshot.NameSpace", cru.snapshot.Namespace, "snapshot.Name", cru.snapshot.Name, "scenarioName", report.ScenarioName, "externalID", checkRunAdapter.ExternalID)
_, err = cru.ghClient.CreateCheckRun(ctx, checkRunAdapter)
if err != nil {
cru.logger.Error(err, "failed to create checkrun",
"checkRunAdapter", checkRunAdapter)
}
return err
}
err = cru.ghClient.UpdateCheckRun(ctx, *existingCheckrun.ID, checkRunAdapter)
if err != nil {
cru.logger.Error(err, "failed to update checkrun",
"checkRunAdapter", checkRunAdapter)
}
return err
}
// CommitStatusUpdater updates PR using Commit/RepoStatus (without application integration enabled)
type CommitStatusUpdater struct {
ghClient github.ClientInterface
k8sClient client.Client
logger *logr.Logger
owner string
repo string
sha string
snapshot *applicationapiv1alpha1.Snapshot
allCommitStatusesCache []*ghapi.RepoStatus
}
// NewCommitStatusUpdater returns a pointer to initialized CommitStatusUpdater
func NewCommitStatusUpdater(
ghClient github.ClientInterface,
k8sClient client.Client,
logger *logr.Logger,
owner string,
repo string,
sha string,
snapshot *applicationapiv1alpha1.Snapshot,
) *CommitStatusUpdater {
return &CommitStatusUpdater{
ghClient: ghClient,
k8sClient: k8sClient,
logger: logger,
owner: owner,
repo: repo,
sha: sha,
snapshot: snapshot,
}
}
func (csu *CommitStatusUpdater) getAllCommitStatuses(ctx context.Context) ([]*ghapi.RepoStatus, error) {
if len(csu.allCommitStatusesCache) == 0 {
allCommitStatuses, err := csu.ghClient.GetAllCommitStatusesForRef(ctx, csu.owner, csu.repo, csu.sha)
if err != nil {
csu.logger.Error(err, "failed to get all commitStatuses for snapshot",
"snapshot.NameSpace", csu.snapshot.Namespace, "snapshot.Name", csu.snapshot.Name)
return nil, err
}
csu.allCommitStatusesCache = allCommitStatuses
}
return csu.allCommitStatusesCache, nil
}
// Authenticate Github Client with token secret ref defined in snapshot
func (csu *CommitStatusUpdater) Authenticate(ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) error {
token, err := GetPACGitProviderToken(ctx, csu.k8sClient, snapshot)
if err != nil {
csu.logger.Error(err, "failed to get token from snapshot",
"snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return err
}
csu.ghClient.SetOAuthToken(ctx, token)
return nil
}
// createCommitStatusAdapterForSnapshot create a commitStatusAdapter used to create commitStatus on GitHub
// https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#create-a-commit-status
func (csu *CommitStatusUpdater) createCommitStatusAdapterForSnapshot(report TestReport) (*github.CommitStatusAdapter, error) {
snapshot := csu.snapshot
targetURL := ""
state, err := generateGithubCommitState(report.Status)
if err != nil {
csu.logger.Error(err, fmt.Sprintf("failed to generate commitStatus for integrationTestScenario %s and snapshot %s/%s", report.ScenarioName, snapshot.Namespace, snapshot.Name))
return nil, fmt.Errorf("unknown status %s for integrationTestScenario %s and snapshot %s/%s", report.Status, report.ScenarioName, snapshot.Namespace, snapshot.Name)
}
if report.TestPipelineRunName == "" {
csu.logger.Info("TestPipelineRunName is not set for CommitStatus")
} else {
targetURL = FormatPipelineURL(report.TestPipelineRunName, snapshot.Namespace, *csu.logger)
}
return &github.CommitStatusAdapter{
Owner: csu.owner,
Repository: csu.repo,
SHA: csu.sha,
State: state,
Description: report.Summary,
Context: report.FullName,
TargetURL: targetURL,
}, nil
}
// updateStatusInComment will create/update a comment in PR which creates snapshot
func (csu *CommitStatusUpdater) updateStatusInComment(ctx context.Context, report TestReport) error {
var unRecoverableError error
issueNumberStr, found := csu.snapshot.GetAnnotations()[gitops.PipelineAsCodePullRequestAnnotation]
if !found {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("pull-request annotation not found %q", gitops.PipelineAsCodePullRequestAnnotation))
csu.logger.Error(unRecoverableError, "snapshot.Name", report.SnapshotName)
return unRecoverableError
}
issueNumber, err := strconv.Atoi(issueNumberStr)
if err != nil {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to convert string issueNumberStr %s to int:%s", issueNumberStr, err.Error()))
csu.logger.Error(unRecoverableError, "snapshot.Name", report.SnapshotName)
return unRecoverableError
}
comment, err := FormatComment(report.Summary, report.Text)
if err != nil {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to format comment for pull-request %d: %s", issueNumber, err.Error()))
csu.logger.Error(unRecoverableError, "snapshot.Name", report.SnapshotName)
return unRecoverableError
}
allComments, err := csu.ghClient.GetAllCommentsForPR(ctx, csu.owner, csu.repo, issueNumber)
if err != nil {
csu.logger.Error(err, fmt.Sprintf("error while getting all comments for pull-request %s", issueNumberStr))
return fmt.Errorf("error while getting all comments for pull-request %s: %w", issueNumberStr, err)
}
existingCommentId := csu.ghClient.GetExistingCommentID(allComments, csu.snapshot.Name, report.ScenarioName)
if existingCommentId == nil {
_, err = csu.ghClient.CreateComment(ctx, csu.owner, csu.repo, issueNumber, comment)
if err != nil {
csu.logger.Error(err, fmt.Sprintf("error while creating comment for pull-request %s", issueNumberStr))
return fmt.Errorf("error while creating comment for pull-request %s: %w", issueNumberStr, err)
}
} else {
_, err = csu.ghClient.EditComment(ctx, csu.owner, csu.repo, *existingCommentId, comment)
if err != nil {
csu.logger.Error(err, fmt.Sprintf("error while updating comment for pull-request %s", issueNumberStr))
return fmt.Errorf("error while updating comment for pull-request %s: %w", issueNumberStr, err)
}
}
return nil
}
// UpdateStatus updates commit status in PR
func (csu *CommitStatusUpdater) UpdateStatus(ctx context.Context, report TestReport) error {
sourceRepoOwner := gitops.GetSourceRepoOwnerFromSnapshot(csu.snapshot)
// we create/update commitStatus only when the source and target repo owner are the same
if csu.owner == sourceRepoOwner {
allCommitStatuses, err := csu.getAllCommitStatuses(ctx)
if err != nil {
csu.logger.Error(err, "failed to get all CommitStatuses for scenario",
"snapshot.NameSpace", csu.snapshot.Namespace, "snapshot.Name", csu.snapshot.Name,
"scenario.Name", report.ScenarioName)
return err
}
commitStatusAdapter, err := csu.createCommitStatusAdapterForSnapshot(report)
if err != nil {
csu.logger.Error(err, "failed to create CommitStatusAdapter for scenario, skipping update",
"snapshot.NameSpace", csu.snapshot.Namespace, "snapshot.Name", csu.snapshot.Name,
"scenario.Name", report.ScenarioName,
)
return nil
}
commitStatusExist, err := csu.ghClient.CommitStatusExists(allCommitStatuses, commitStatusAdapter)
if err != nil {
csu.logger.Error(err, "failed to check existing commitStatus")
return err
}
if !commitStatusExist {
csu.logger.Info("creating commit status for scenario test status of snapshot",
"snapshot.NameSpace", csu.snapshot.Namespace, "snapshot.Name", csu.snapshot.Name, "scenarioName", report.ScenarioName)
_, err = csu.ghClient.CreateCommitStatus(ctx, commitStatusAdapter.Owner, commitStatusAdapter.Repository, commitStatusAdapter.SHA, commitStatusAdapter.State, commitStatusAdapter.Description, commitStatusAdapter.Context, commitStatusAdapter.TargetURL)
if err != nil {
csu.logger.Error(err, "failed to create commitStatus", "snapshot.NameSpace", csu.snapshot.Namespace, "snapshot.Name", csu.snapshot.Name, "scenarioName", report.ScenarioName)
return err
}
} else {
csu.logger.Info("found existing commitStatus for scenario test status of snapshot, no need to create new commit status",
"snapshot.NameSpace", csu.snapshot.Namespace, "snapshot.Name", csu.snapshot.Name, "scenarioName", report.ScenarioName)
}
} else {
csu.logger.Info("Won't create/update commitStatus since there is access limitation for different source and target Repo Owner",
"snapshot.NameSpace", csu.snapshot.Namespace, "snapshot.Name", csu.snapshot.Name, "sourceRepoOwner", sourceRepoOwner, "targetRepoOwner", csu.owner)
}
// Create a comment when integration test is neither pending nor inprogress since comment for pending/inprogress is less meaningful and there is commitStatus for all statuses
_, isPullRequest := csu.snapshot.GetAnnotations()[gitops.PipelineAsCodePullRequestAnnotation]
if report.Status != intgteststat.IntegrationTestStatusPending && report.Status != intgteststat.IntegrationTestStatusInProgress && isPullRequest {
err := csu.updateStatusInComment(ctx, report)
if err != nil {
csu.logger.Error(err, "failed to update comment", "snapshot.NameSpace", csu.snapshot.Namespace, "snapshot.Name", csu.snapshot.Name, "scenarioName", report.ScenarioName)
return err
}
}
return nil
}
// GitHubReporter reports status back to GitHub for a Snapshot.
type GitHubReporter struct {
logger *logr.Logger
k8sClient client.Client
client github.ClientInterface
updater StatusUpdater
}
// check if interface has been correctly implemented
var _ ReporterInterface = (*GitHubReporter)(nil)
// GitHubReporterOption is used to extend GitHubReporter with optional parameters.
type GitHubReporterOption = func(r *GitHubReporter)
func WithGitHubClient(client github.ClientInterface) GitHubReporterOption {
return func(r *GitHubReporter) {
r.client = client
}
}
// NewGitHubReporter returns a struct implementing the Reporter interface for GitHub
func NewGitHubReporter(logger logr.Logger, k8sClient client.Client, opts ...GitHubReporterOption) *GitHubReporter {
reporter := GitHubReporter{
logger: &logger,
k8sClient: k8sClient,
client: github.NewClient(logger),
}
for _, opt := range opts {
opt(&reporter)
}
return &reporter
}
type appCredentials struct {
AppID int64
InstallationID int64
PrivateKey []byte
}
// generateTitle generate a Title of checkRun for the given state
func generateCheckRunTitle(state intgteststat.IntegrationTestStatus) (string, error) {
var title string
switch state {
case intgteststat.IntegrationTestStatusPending, intgteststat.BuildPLRInProgress:
title = "Pending"
case intgteststat.IntegrationTestStatusInProgress:
title = "In Progress"
case intgteststat.IntegrationTestStatusEnvironmentProvisionError_Deprecated,
intgteststat.IntegrationTestStatusDeploymentError_Deprecated,
intgteststat.IntegrationTestStatusTestInvalid:
title = "Errored"
case intgteststat.IntegrationTestStatusDeleted:
title = "Deleted"
case intgteststat.IntegrationTestStatusTestPassed:
title = "Succeeded"
case intgteststat.IntegrationTestStatusTestFail,
intgteststat.SnapshotCreationFailed,
intgteststat.BuildPLRFailed:
title = "Failed"
default:
return title, fmt.Errorf("unknown status")
}
return title, nil
}
// generateCheckRunConclusion generate a conclusion as the conclusion of CheckRun
// Can be one of: action_required, cancelled, failure, neutral, success, skipped, stale, timed_out
// https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#create-a-check-run
func generateCheckRunConclusion(state intgteststat.IntegrationTestStatus) (string, error) {
var conclusion string
switch state {
case intgteststat.IntegrationTestStatusTestFail, intgteststat.IntegrationTestStatusEnvironmentProvisionError_Deprecated,
intgteststat.IntegrationTestStatusDeploymentError_Deprecated, intgteststat.IntegrationTestStatusDeleted,
intgteststat.IntegrationTestStatusTestInvalid:
conclusion = gitops.IntegrationTestStatusFailureGithub
case intgteststat.IntegrationTestStatusTestPassed:
conclusion = gitops.IntegrationTestStatusSuccessGithub
case intgteststat.IntegrationTestStatusPending, intgteststat.IntegrationTestStatusInProgress,
intgteststat.BuildPLRInProgress:
conclusion = ""
case intgteststat.SnapshotCreationFailed, intgteststat.BuildPLRFailed:
conclusion = gitops.IntegrationTestStatusCancelledGithub
default:
return conclusion, fmt.Errorf("unknown status")
}
return conclusion, nil
}
// generateGithubCommitState generate state of CommitStatus
// Can be one of: error, failure, pending, success
// https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#create-a-commit-status
func generateGithubCommitState(state intgteststat.IntegrationTestStatus) (string, error) {
var commitState string
switch state {
case intgteststat.IntegrationTestStatusTestFail, intgteststat.SnapshotCreationFailed,
intgteststat.BuildPLRFailed:
commitState = gitops.IntegrationTestStatusFailureGithub
case intgteststat.IntegrationTestStatusEnvironmentProvisionError_Deprecated, intgteststat.IntegrationTestStatusDeploymentError_Deprecated,
intgteststat.IntegrationTestStatusDeleted, intgteststat.IntegrationTestStatusTestInvalid:
commitState = gitops.IntegrationTestStatusErrorGithub
case intgteststat.IntegrationTestStatusTestPassed:
commitState = gitops.IntegrationTestStatusSuccessGithub
case intgteststat.IntegrationTestStatusPending, intgteststat.IntegrationTestStatusInProgress,
intgteststat.BuildPLRInProgress:
commitState = gitops.IntegrationTestStatusPendingGithub
default:
return commitState, fmt.Errorf("unknown status")
}
return commitState, nil
}
// Detect if GitHubReporter can be used
func (r *GitHubReporter) Detect(snapshot *applicationapiv1alpha1.Snapshot) bool {
return metadata.HasAnnotationWithValue(snapshot, gitops.PipelineAsCodeGitProviderAnnotation, gitops.PipelineAsCodeGitHubProviderType) ||
metadata.HasLabelWithValue(snapshot, gitops.PipelineAsCodeGitProviderLabel, gitops.PipelineAsCodeGitHubProviderType)
}
// Initialize github reporter. Must be called before updating status
func (r *GitHubReporter) Initialize(ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) error {
var unRecoverableError error
labels := snapshot.GetLabels()
owner, found := labels[gitops.PipelineAsCodeURLOrgLabel]
if !found {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("org label not found %q", gitops.PipelineAsCodeURLOrgLabel))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
repo, found := labels[gitops.PipelineAsCodeURLRepositoryLabel]
if !found {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("repository label not found %q", gitops.PipelineAsCodeURLRepositoryLabel))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
sha, found := labels[gitops.PipelineAsCodeSHALabel]
if !found {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("sha label not found %q", gitops.PipelineAsCodeSHALabel))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
// Existence of the Pipelines as Code installation ID annotation signals configuration using GitHub App integration.
// If it doesn't exist, GitHub webhook integration is configured.
if metadata.HasAnnotation(snapshot, gitops.PipelineAsCodeInstallationIDAnnotation) {
r.updater = NewCheckRunStatusUpdater(r.client, r.k8sClient, r.logger, owner, repo, sha, snapshot)
} else {
r.updater = NewCommitStatusUpdater(r.client, r.k8sClient, r.logger, owner, repo, sha, snapshot)
}
if err := r.updater.Authenticate(ctx, snapshot); err != nil {
r.logger.Error(err, fmt.Sprintf("failed to authenticate for snapshot %s/%s", snapshot.Namespace, snapshot.Name))
return err
}
return nil
}
// Return reporter name
func (r *GitHubReporter) GetReporterName() string {
return "GithubReporter"
}
// Update status in Github
func (r *GitHubReporter) ReportStatus(ctx context.Context, report TestReport) error {
if r.updater == nil {
r.logger.Error(nil, fmt.Sprintf("reporter is not initialized for snapshot %s", report.SnapshotName))
return fmt.Errorf("reporter is not initialized")
}
if err := r.updater.UpdateStatus(ctx, report); err != nil {
r.logger.Error(err, fmt.Sprintf("failed to update status for snapshot %s", report.SnapshotName))
return err
}
return nil
}