|
| 1 | +/* |
| 2 | +Copyright 2026 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1 |
| 18 | + |
| 19 | +const ( |
| 20 | + maxControllerNotices = 10 |
| 21 | + maxNoticeMessageLen = 1024 |
| 22 | + maxNoticeFileLen = 256 |
| 23 | +) |
| 24 | + |
| 25 | +// NoticeLevel indicates the severity of a notice. |
| 26 | +type NoticeLevel string |
| 27 | + |
| 28 | +const ( |
| 29 | + // NoticeLevelInfo represents an informational message, no action needed. |
| 30 | + NoticeLevelInfo NoticeLevel = "info" |
| 31 | + // NoticeLevelWarning represents something to address, but not blocking. |
| 32 | + NoticeLevelWarning NoticeLevel = "warning" |
| 33 | +) |
| 34 | + |
| 35 | +// AllNoticeLevels can be used for NoticeLevel validation. |
| 36 | +var AllNoticeLevels = []NoticeLevel{NoticeLevelInfo, NoticeLevelWarning} |
| 37 | + |
| 38 | +// Notice represents a structured message emitted by a controller that does not affect the run's success/failure status. |
| 39 | +type Notice struct { |
| 40 | + // Level indicates the severity of the notice. |
| 41 | + // Valid values: "info", "warning". |
| 42 | + // +kubebuilder:validation:Enum=info;warning |
| 43 | + Level NoticeLevel `json:"level"` |
| 44 | + |
| 45 | + // Message is the human-readable notice text. |
| 46 | + // Maximum length: 1024 characters. |
| 47 | + // +kubebuilder:validation:MaxLength=1024 |
| 48 | + Message string `json:"message"` |
| 49 | + |
| 50 | + // Step is the name of the step that emitted this notice. |
| 51 | + // Empty for controller-emitted notices. |
| 52 | + // +optional |
| 53 | + Step string `json:"step,omitempty"` |
| 54 | + |
| 55 | + // File is the source file path related to this notice. |
| 56 | + // Used by VCS integrations to create inline annotations. |
| 57 | + // Maximum length: 256 characters. |
| 58 | + // +optional |
| 59 | + // +kubebuilder:validation:MaxLength=256 |
| 60 | + File string `json:"file,omitempty"` |
| 61 | + |
| 62 | + // StartLine is the starting line number in the source file (1-based). |
| 63 | + // Pointer type so that absence (nil) is distinguishable from line 0. |
| 64 | + // +optional |
| 65 | + StartLine *int `json:"startLine,omitempty"` |
| 66 | +} |
| 67 | + |
| 68 | +// AddControllerNotice appends a bounded, deduplicated controller notice to TaskRun status. |
| 69 | +func (trs *TaskRunStatus) AddControllerNotice(notice Notice) { |
| 70 | + if notice.Level != NoticeLevelInfo && notice.Level != NoticeLevelWarning { |
| 71 | + return |
| 72 | + } |
| 73 | + notice.Message = truncateString(notice.Message, maxNoticeMessageLen) |
| 74 | + notice.File = truncateString(notice.File, maxNoticeFileLen) |
| 75 | + notice.Step = "" |
| 76 | + |
| 77 | + for _, existing := range trs.Notices { |
| 78 | + if existing.Level == notice.Level && existing.Message == notice.Message { |
| 79 | + return |
| 80 | + } |
| 81 | + } |
| 82 | + if len(trs.Notices) >= maxControllerNotices { |
| 83 | + return |
| 84 | + } |
| 85 | + trs.Notices = append(trs.Notices, notice) |
| 86 | +} |
| 87 | + |
| 88 | +func truncateString(value string, limit int) string { |
| 89 | + runes := []rune(value) |
| 90 | + if len(runes) <= limit { |
| 91 | + return value |
| 92 | + } |
| 93 | + return string(runes[:limit]) |
| 94 | +} |
0 commit comments