-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathreporter.go
124 lines (106 loc) · 4.45 KB
/
reporter.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
/*
Copyright 2024 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
//go:generate mockgen -destination mock_reporter.go -package status github.com/konflux-ci/integration-service/status ReporterInterface
import (
"context"
"fmt"
"time"
applicationapiv1alpha1 "github.com/konflux-ci/application-api/api/v1alpha1"
pacv1alpha1 "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
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"
"github.com/konflux-ci/integration-service/gitops"
"github.com/konflux-ci/integration-service/helpers"
intgteststat "github.com/konflux-ci/integration-service/pkg/integrationteststatus"
)
type TestReport struct {
// FullName describing the snapshot and integration test
FullName string
// Name of scenario
ScenarioName string
// Name of snapshot
SnapshotName string
// Name of Component that triggered snapshot creation (optional)
ComponentName string
// text with details of test results
Text string
// test status
Status intgteststat.IntegrationTestStatus
// short summary of test results
Summary string
// time when test started
StartTime *time.Time
// time when test completed
CompletionTime *time.Time
// pipelineRun Name
TestPipelineRunName string
}
type ReporterInterface interface {
// Detect if the reporter can be used with the snapshot
Detect(*applicationapiv1alpha1.Snapshot) bool
// Initialize reporter to be able to update statuses (authenticate, fetching metadata)
Initialize(context.Context, *applicationapiv1alpha1.Snapshot) error
// Get plain reporter name
GetReporterName() string
// Update status of the integration test
ReportStatus(context.Context, TestReport) error
}
// GetPACGitProviderToken lookup for configured repo and fetch token from namespace
func GetPACGitProviderToken(ctx context.Context, k8sClient client.Client, snapshot *applicationapiv1alpha1.Snapshot) (string, error) {
log := log.FromContext(ctx)
var err, unRecoverableError error
// List all the Repository CRs in the namespace
repos := pacv1alpha1.RepositoryList{}
if err = k8sClient.List(ctx, &repos, &client.ListOptions{Namespace: snapshot.Namespace}); err != nil {
log.Error(err, fmt.Sprintf("failed to get repo from namespace %s", snapshot.Namespace))
return "", err
}
// Get the full repo URL
url, found := snapshot.GetAnnotations()[gitops.PipelineAsCodeRepoURLAnnotation]
if !found {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("object annotation not found %q", gitops.PipelineAsCodeRepoURLAnnotation))
log.Error(unRecoverableError, fmt.Sprintf("object annotation not found %q", gitops.PipelineAsCodeRepoURLAnnotation))
return "", unRecoverableError
}
// Find a Repository CR with a matching URL and get its secret details
var repoSecret *pacv1alpha1.Secret
for _, repo := range repos.Items {
if url == repo.Spec.URL {
repoSecret = repo.Spec.GitProvider.Secret
break
}
}
if repoSecret == nil {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to find a Repository matching URL: %q", url))
log.Error(unRecoverableError, fmt.Sprintf("failed to find a Repository matching URL: %q", url))
return "", unRecoverableError
}
// Get the pipelines as code secret from the PipelineRun's namespace
pacSecret := v1.Secret{}
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: snapshot.Namespace, Name: repoSecret.Name}, &pacSecret)
if err != nil {
log.Error(err, fmt.Sprintf("failed to get secret %s/%s", snapshot.Namespace, repoSecret.Name))
return "", err
}
// Get the personal access token from the secret
token, found := pacSecret.Data[repoSecret.Key]
if !found {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to find %s secret key", repoSecret.Key))
log.Error(unRecoverableError, fmt.Sprintf("failed to find %s secret key", repoSecret.Key))
return "", unRecoverableError
}
return string(token), nil
}