@@ -193,7 +193,7 @@ func addSourceAnnotationToTask(file, resourcesURI string) error {
193
193
// Open the Task YAML file
194
194
f , err := os .OpenFile (file , os .O_RDWR , 0o644 )
195
195
if err != nil {
196
- return err
196
+ return err
197
197
}
198
198
defer f .Close ()
199
199
@@ -205,48 +205,61 @@ func addSourceAnnotationToTask(file, resourcesURI string) error {
205
205
annotationsPattern := regexp .MustCompile (`^\s+annotations:\s*$` )
206
206
sourceAnnotationPattern := regexp .MustCompile (`^\s+tekton\.dev/source:\s*".*"$` )
207
207
208
- // Flag to indicate if the "tekton.dev/source" annotation is already present
209
- var sourceAnnotationExists bool
208
+ // Flag to indicate if we are within the annotations block
209
+ var inAnnotationsBlock bool
210
+
211
+ // Buffer to store annotations lines
212
+ var annotationsBuffer []string
210
213
211
214
// Read the file line by line
212
215
for scanner .Scan () {
213
- line := scanner .Text ()
214
-
215
- // Check if the line matches the annotations pattern
216
- if annotationsPattern .MatchString (line ) {
217
- // If annotations block is found, initialize sourceAnnotationExists to false
218
- sourceAnnotationExists = false
219
- } else if ! sourceAnnotationExists && sourceAnnotationPattern .MatchString (line ) {
220
- // If source annotation is found within annotations block, set sourceAnnotationExists to true
221
- sourceAnnotationExists = true
222
- }
223
-
224
- // Append the line to updatedContent slice
225
- updatedContent = append (updatedContent , line )
226
-
227
- // Check if we are still within the annotations block
228
- if ! sourceAnnotationExists && annotationsPattern .MatchString (line ) {
229
- // Add the source annotation as the first line of the annotations block
230
- updatedContent = append (updatedContent , fmt .Sprintf (" tekton.dev/source: \" %s\" " , repoURL ))
231
- sourceAnnotationExists = true // Set sourceAnnotationExists to true after adding the annotation
232
- }
216
+ line := scanner .Text ()
217
+
218
+ // Check if the line matches the annotations pattern
219
+ if annotationsPattern .MatchString (line ) {
220
+ // If annotations block is found, set the inAnnotationsBlock flag to true
221
+ inAnnotationsBlock = true
222
+ // Append the line to updatedContent slice
223
+ updatedContent = append (updatedContent , line )
224
+ // Add the source annotation as the first line of the annotations block
225
+ updatedContent = append (updatedContent , fmt .Sprintf (" tekton.dev/source: \" %s\" " , repoURL ))
226
+ } else if inAnnotationsBlock {
227
+ // If we are within the annotations block
228
+ if sourceAnnotationPattern .MatchString (line ) {
229
+ // Skip the existing source annotation
230
+ continue
231
+ } else if strings .TrimSpace (line ) == "" || ! strings .HasPrefix (line , " " ) {
232
+ // If a non-indented line or an empty line is found, we consider it as the end of the annotations block
233
+ inAnnotationsBlock = false
234
+ // Append buffered annotations lines
235
+ updatedContent = append (updatedContent , annotationsBuffer ... )
236
+ // Append the current line
237
+ updatedContent = append (updatedContent , line )
238
+ } else {
239
+ // Buffer the annotation lines
240
+ annotationsBuffer = append (annotationsBuffer , line )
241
+ }
242
+ } else {
243
+ // Append the line to updatedContent slice
244
+ updatedContent = append (updatedContent , line )
245
+ }
233
246
}
234
247
235
248
// Check for scanner errors
236
249
if err := scanner .Err (); err != nil {
237
- return err
250
+ return err
238
251
}
239
252
240
253
// Clear the file content and write the updated content
241
254
if err := f .Truncate (0 ); err != nil {
242
- return err
255
+ return err
243
256
}
244
257
if _ , err := f .Seek (0 , 0 ); err != nil {
245
- return err
258
+ return err
246
259
}
247
260
writer := bufio .NewWriter (f )
248
261
for _ , line := range updatedContent {
249
- fmt .Fprintln (writer , line )
262
+ fmt .Fprintln (writer , line )
250
263
}
251
264
return writer .Flush ()
252
265
}
0 commit comments