Skip to content

Commit 7579df4

Browse files
committed
Fixed the Source Annotation bug.
1 parent 845e92a commit 7579df4

File tree

1 file changed

+104
-13
lines changed

1 file changed

+104
-13
lines changed

internal/catalog/catalog.go

+104-13
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func untar(dst, version string, tektonResources map[string]contract.TektonResour
186186
}
187187
}
188188

189-
func addSourceAnnotationToTask(file, resourcesURI string) error {
189+
/*func addSourceAnnotationToTask(file, resourcesURI string) error {
190190
// Add the new annotation
191191
repoURL := extractRepositoryURL(resourcesURI)
192192
@@ -205,30 +205,121 @@ func addSourceAnnotationToTask(file, resourcesURI string) error {
205205
annotationsPattern := regexp.MustCompile(`^\s+annotations:\s*$`)
206206
sourceAnnotationPattern := regexp.MustCompile(`^\s+tekton\.dev/source:\s*".*"$`)
207207
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
210213
211214
// Read the file line by line
212215
for scanner.Scan() {
213216
line := scanner.Text()
214217
215218
// Check if the line matches the annotations pattern
216219
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
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)
222245
}
246+
}
247+
248+
// Check for scanner errors
249+
if err := scanner.Err(); err != nil {
250+
return err
251+
}
252+
253+
// Clear the file content and write the updated content
254+
if err := f.Truncate(0); err != nil {
255+
return err
256+
}
257+
if _, err := f.Seek(0, 0); err != nil {
258+
return err
259+
}
260+
writer := bufio.NewWriter(f)
261+
for _, line := range updatedContent {
262+
fmt.Fprintln(writer, line)
263+
}
264+
return writer.Flush()
265+
}*/
266+
267+
func addSourceAnnotationToTask(file, resourcesURI string) error {
268+
// Add the new annotation
269+
repoURL := extractRepositoryURL(resourcesURI)
270+
271+
// Open the Task YAML file
272+
f, err := os.OpenFile(file, os.O_RDWR, 0o644)
273+
if err != nil {
274+
return err
275+
}
276+
defer f.Close()
277+
278+
// Create a scanner to read the file line by line
279+
scanner := bufio.NewScanner(f)
280+
var updatedContent []string
223281

224-
// Append the line to updatedContent slice
225-
updatedContent = append(updatedContent, line)
282+
// Regular expression pattern to match the annotations in Task metadata
283+
annotationsPattern := regexp.MustCompile(`^\s+annotations:\s*$`)
284+
sourceAnnotationPattern := regexp.MustCompile(`^\s+tekton\.dev/source:\s*".*"$`)
226285

227-
// Check if we are still within the annotations block
228-
if !sourceAnnotationExists && annotationsPattern.MatchString(line) {
286+
// Flag to indicate if we are within the annotations block
287+
var inAnnotationsBlock bool
288+
289+
// Buffer to store annotations lines
290+
var annotationsBuffer []string
291+
292+
// Read the file line by line
293+
for scanner.Scan() {
294+
line := scanner.Text()
295+
296+
switch {
297+
case annotationsPattern.MatchString(line):
298+
// If annotations block is found, set the inAnnotationsBlock flag to true
299+
inAnnotationsBlock = true
300+
// Append the line to updatedContent slice
301+
updatedContent = append(updatedContent, line)
229302
// Add the source annotation as the first line of the annotations block
230303
updatedContent = append(updatedContent, fmt.Sprintf(" tekton.dev/source: \"%s\"", repoURL))
231-
sourceAnnotationExists = true // Set sourceAnnotationExists to true after adding the annotation
304+
case inAnnotationsBlock:
305+
switch {
306+
case sourceAnnotationPattern.MatchString(line):
307+
// Skip the existing source annotation
308+
continue
309+
case strings.TrimSpace(line) == "" || !strings.HasPrefix(line, " "):
310+
// If a non-indented line or an empty line is found, we consider it as the end of the annotations block
311+
inAnnotationsBlock = false
312+
// Append buffered annotations lines
313+
updatedContent = append(updatedContent, annotationsBuffer...)
314+
// Append the current line
315+
updatedContent = append(updatedContent, line)
316+
default:
317+
// Buffer the annotation lines
318+
annotationsBuffer = append(annotationsBuffer, line)
319+
}
320+
default:
321+
// Append the line to updatedContent slice
322+
updatedContent = append(updatedContent, line)
232323
}
233324
}
234325

0 commit comments

Comments
 (0)