-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathreporter_gitlab.go
326 lines (282 loc) · 13.9 KB
/
reporter_gitlab.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
/*
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
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
"github.com/go-logr/logr"
applicationapiv1alpha1 "github.com/konflux-ci/application-api/api/v1alpha1"
"github.com/konflux-ci/operator-toolkit/metadata"
gitlab "github.com/xanzy/go-gitlab"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/konflux-ci/integration-service/gitops"
"github.com/konflux-ci/integration-service/helpers"
intgteststat "github.com/konflux-ci/integration-service/pkg/integrationteststatus"
)
type GitLabReporter struct {
logger *logr.Logger
k8sClient client.Client
client *gitlab.Client
sha string
sourceProjectID int
targetProjectID int
mergeRequest int
snapshot *applicationapiv1alpha1.Snapshot
}
func NewGitLabReporter(logger logr.Logger, k8sClient client.Client) *GitLabReporter {
return &GitLabReporter{
logger: &logger,
k8sClient: k8sClient,
}
}
// check if interface has been correctly implemented
var _ ReporterInterface = (*GitLabReporter)(nil)
var existingCommitStatus *gitlab.CommitStatus
// Detect if snapshot has been created from gitlab provider
func (r *GitLabReporter) Detect(snapshot *applicationapiv1alpha1.Snapshot) bool {
return metadata.HasAnnotationWithValue(snapshot, gitops.PipelineAsCodeGitProviderLabel, gitops.PipelineAsCodeGitLabProviderType) ||
metadata.HasLabelWithValue(snapshot, gitops.PipelineAsCodeGitProviderAnnotation, gitops.PipelineAsCodeGitLabProviderType)
}
// GetReporterName returns the reporter name
func (r *GitLabReporter) GetReporterName() string {
return "GitlabReporter"
}
// Initialize initializes gitlab reporter
func (r *GitLabReporter) Initialize(ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) error {
var unRecoverableError error
token, err := GetPACGitProviderToken(ctx, r.k8sClient, snapshot)
if err != nil {
r.logger.Error(err, "failed to get PAC token from snapshot",
"snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return err
}
annotations := snapshot.GetAnnotations()
repoUrl, ok := annotations[gitops.PipelineAsCodeRepoURLAnnotation]
if !ok {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to get value of %s annotation from the snapshot %s", gitops.PipelineAsCodeRepoURLAnnotation, snapshot.Name))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
burl, err := url.Parse(repoUrl)
if err != nil {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to parse repo-url %s: %s", repoUrl, err.Error()))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
apiURL := fmt.Sprintf("%s://%s", burl.Scheme, burl.Host)
r.client, err = gitlab.NewClient(token, gitlab.WithBaseURL(apiURL))
if err != nil {
r.logger.Error(err, "failed to create gitlab client", "apiURL", apiURL, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return err
}
labels := snapshot.GetLabels()
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
}
r.sha = sha
targetProjectIDstr, found := annotations[gitops.PipelineAsCodeTargetProjectIDAnnotation]
if !found {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("target project ID annotation not found %q", gitops.PipelineAsCodeTargetProjectIDAnnotation))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
r.targetProjectID, err = strconv.Atoi(targetProjectIDstr)
if err != nil {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to convert project ID '%s' to integer: %s", targetProjectIDstr, err.Error()))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
sourceProjectIDstr, found := annotations[gitops.PipelineAsCodeSourceProjectIDAnnotation]
if !found {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("source project ID annotation not found %q", gitops.PipelineAsCodeSourceProjectIDAnnotation))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
r.sourceProjectID, err = strconv.Atoi(sourceProjectIDstr)
if err != nil {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to convert project ID '%s' to integer: %s", sourceProjectIDstr, err.Error()))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
mergeRequestStr, found := annotations[gitops.PipelineAsCodePullRequestAnnotation]
if !found && !gitops.IsSnapshotCreatedByPACPushEvent(snapshot) {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("pull-request annotation not found %q", gitops.PipelineAsCodePullRequestAnnotation))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
if found {
r.mergeRequest, err = strconv.Atoi(mergeRequestStr)
if err != nil && !gitops.IsSnapshotCreatedByPACPushEvent(snapshot) {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to convert merge request number '%s' to integer: %s", mergeRequestStr, err.Error()))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
}
r.snapshot = snapshot
return nil
}
// setCommitStatus sets commit status to be shown as pipeline run in gitlab view
func (r *GitLabReporter) setCommitStatus(report TestReport) error {
glState, err := GenerateGitlabCommitState(report.Status)
if err != nil {
return fmt.Errorf("failed to generate gitlab state: %w", err)
}
opt := gitlab.SetCommitStatusOptions{
State: gitlab.BuildStateValue(glState),
Name: gitlab.Ptr(report.FullName),
Description: gitlab.Ptr(report.Summary),
}
if report.TestPipelineRunName == "" {
r.logger.Info("TestPipelineRunName is not set, cannot add URL to message")
} else {
url := FormatPipelineURL(report.TestPipelineRunName, r.snapshot.Namespace, *r.logger)
opt.TargetURL = gitlab.Ptr(url)
}
// Fetch commit statuses only if necessary
if glState == gitlab.Running || glState == gitlab.Pending {
allCommitStatuses, _, err := r.client.Commits.GetCommitStatuses(r.sourceProjectID, r.sha, nil)
if err != nil {
return fmt.Errorf("error while getting all commitStatuses for sha %s: %w", r.sha, err)
}
existingCommitStatus = r.GetExistingCommitStatus(allCommitStatuses, report.FullName)
// special case, we want to skip updating commit status if the status from running to running, from pending to pending
if existingCommitStatus != nil && existingCommitStatus.Status == string(glState) {
r.logger.Info("Skipping commit status update",
"scenario.name", report.ScenarioName,
"commitStatus.ID", existingCommitStatus.ID,
"current_status", existingCommitStatus.Status,
"new_status", glState)
return nil
}
}
r.logger.Info("creating commit status for scenario test status of snapshot",
"scenarioName", report.ScenarioName)
commitStatus, _, err := r.client.Commits.SetCommitStatus(r.sourceProjectID, r.sha, &opt)
if err != nil {
// when commitStatus is created in multiple thread occasionally, we can still see the transition error, so let's ignore it as a workaround
if strings.Contains(err.Error(), "Cannot transition status via :enqueue from :pending") {
r.logger.Info("Ingoring the error when transition from pending to pending when the commitStatus might be created/updated in multiple threads at the same time occasionally")
return nil
}
return fmt.Errorf("failed to set commit status to %s: %w", string(glState), err)
}
r.logger.Info("Created gitlab commit status", "scenario.name", report.ScenarioName, "commitStatus.ID", commitStatus.ID, "TargetURL", opt.TargetURL)
return nil
}
// updateStatusInComment will create/update a comment in the MR which creates snapshot
func (r *GitLabReporter) updateStatusInComment(report TestReport) error {
comment, err := FormatComment(report.Summary, report.Text)
if err != nil {
unRecoverableError := helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to generate comment for merge-request %d: %s", r.mergeRequest, err.Error()))
r.logger.Error(unRecoverableError, "report.SnapshotName", report.SnapshotName)
return unRecoverableError
}
allNotes, _, err := r.client.Notes.ListMergeRequestNotes(r.targetProjectID, r.mergeRequest, nil)
if err != nil {
r.logger.Error(err, "error while getting all comments for merge-request", "mergeRequest", r.mergeRequest, "report.SnapshotName", report.SnapshotName)
return fmt.Errorf("error while getting all comments for merge-request %d: %w", r.mergeRequest, err)
}
existingCommentId := r.GetExistingNoteID(allNotes, report.ScenarioName, report.SnapshotName)
if existingCommentId == nil {
noteOptions := gitlab.CreateMergeRequestNoteOptions{Body: &comment}
_, _, err := r.client.Notes.CreateMergeRequestNote(r.targetProjectID, r.mergeRequest, ¬eOptions)
if err != nil {
return fmt.Errorf("error while creating comment for merge-request %d: %w", r.mergeRequest, err)
}
} else {
noteOptions := gitlab.UpdateMergeRequestNoteOptions{Body: &comment}
_, _, err := r.client.Notes.UpdateMergeRequestNote(r.targetProjectID, r.mergeRequest, *existingCommentId, ¬eOptions)
if err != nil {
return fmt.Errorf("error while creating comment for merge-request %d: %w", r.mergeRequest, err)
}
}
return nil
}
// GetExistingCommitStatus returns existing GitLab commit status that matches .
func (r *GitLabReporter) GetExistingCommitStatus(commitStatuses []*gitlab.CommitStatus, statusName string) *gitlab.CommitStatus {
for _, commitStatus := range commitStatuses {
if commitStatus.Name == statusName {
r.logger.Info("found matching existing commitStatus",
"commitStatus.Name", commitStatus.Name, "commitStatus.ID", commitStatus.ID)
return commitStatus
}
}
r.logger.Info("found no matching existing commitStatus", "statusName", statusName)
return nil
}
// GetExistingNoteID returns existing GitLab note for the scenario of ref.
func (r *GitLabReporter) GetExistingNoteID(notes []*gitlab.Note, scenarioName, snapshotName string) *int {
for _, note := range notes {
if strings.Contains(note.Body, snapshotName) && strings.Contains(note.Body, scenarioName) {
r.logger.Info("found note ID with a matching scenarioName", "scenarioName", scenarioName, "noteID", ¬e.ID)
return ¬e.ID
}
}
r.logger.Info("found no note with a matching scenarioName", "scenarioName", scenarioName)
return nil
}
// ReportStatus reports test result to gitlab
func (r *GitLabReporter) ReportStatus(ctx context.Context, report TestReport) error {
if r.client == nil {
return fmt.Errorf("gitlab reporter is not initialized")
}
// We only create/update commitStatus when source project and target project are
// the same one due to the access limitation for forked repo
// refer to the same issue in pipelines-as-code https://github.com/openshift-pipelines/pipelines-as-code/blob/2f78eb8fd04d149b266ba93f2bea706b4b026403/pkg/provider/gitlab/gitlab.go#L207
if r.sourceProjectID == r.targetProjectID {
if err := r.setCommitStatus(report); err != nil {
return fmt.Errorf("failed to set gitlab commit status: %w", err)
}
} else {
r.logger.Info("Won't create/update commitStatus due to the access limitation for forked repo", "r.sourceProjectID", r.sourceProjectID, "r.targetProjectID", r.targetProjectID)
}
// Create a note when integration test is neither pending nor inprogress since comment for pending/inprogress is less meaningful
_, isMergeRequest := r.snapshot.GetAnnotations()[gitops.PipelineAsCodePullRequestAnnotation]
if report.Status != intgteststat.IntegrationTestStatusPending && report.Status != intgteststat.IntegrationTestStatusInProgress && report.Status != intgteststat.SnapshotCreationFailed && isMergeRequest {
err := r.updateStatusInComment(report)
if err != nil {
return err
}
}
return nil
}
// GenerateGitlabCommitState transforms internal integration test state into Gitlab state
func GenerateGitlabCommitState(state intgteststat.IntegrationTestStatus) (gitlab.BuildStateValue, error) {
glState := gitlab.Failed
switch state {
case intgteststat.IntegrationTestStatusPending, intgteststat.BuildPLRInProgress:
glState = gitlab.Pending
case intgteststat.IntegrationTestStatusInProgress:
glState = gitlab.Running
case intgteststat.IntegrationTestStatusEnvironmentProvisionError_Deprecated,
intgteststat.IntegrationTestStatusDeploymentError_Deprecated,
intgteststat.IntegrationTestStatusTestInvalid:
glState = gitlab.Failed
case intgteststat.IntegrationTestStatusDeleted,
intgteststat.BuildPLRFailed, intgteststat.SnapshotCreationFailed:
glState = gitlab.Canceled
case intgteststat.IntegrationTestStatusTestPassed:
glState = gitlab.Success
case intgteststat.IntegrationTestStatusTestFail:
glState = gitlab.Failed
default:
return glState, fmt.Errorf("unknown status %s", state)
}
return glState, nil
}