Skip to content

Commit

Permalink
Fixed the Source Annotation bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Senjuti256 committed Aug 5, 2024
1 parent 845e92a commit 7579df4
Showing 1 changed file with 104 additions and 13 deletions.
117 changes: 104 additions & 13 deletions internal/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func untar(dst, version string, tektonResources map[string]contract.TektonResour
}
}

func addSourceAnnotationToTask(file, resourcesURI string) error {
/*func addSourceAnnotationToTask(file, resourcesURI string) error {
// Add the new annotation
repoURL := extractRepositoryURL(resourcesURI)
Expand All @@ -205,30 +205,121 @@ func addSourceAnnotationToTask(file, resourcesURI string) error {
annotationsPattern := regexp.MustCompile(`^\s+annotations:\s*$`)
sourceAnnotationPattern := regexp.MustCompile(`^\s+tekton\.dev/source:\s*".*"$`)
// Flag to indicate if the "tekton.dev/source" annotation is already present
var sourceAnnotationExists bool
// Flag to indicate if we are within the annotations block
var inAnnotationsBlock bool
// Buffer to store annotations lines
var annotationsBuffer []string
// Read the file line by line
for scanner.Scan() {
line := scanner.Text()
// Check if the line matches the annotations pattern
if annotationsPattern.MatchString(line) {
// If annotations block is found, initialize sourceAnnotationExists to false
sourceAnnotationExists = false
} else if !sourceAnnotationExists && sourceAnnotationPattern.MatchString(line) {
// If source annotation is found within annotations block, set sourceAnnotationExists to true
sourceAnnotationExists = true
// If annotations block is found, set the inAnnotationsBlock flag to true
inAnnotationsBlock = true
// Append the line to updatedContent slice
updatedContent = append(updatedContent, line)
// Add the source annotation as the first line of the annotations block
updatedContent = append(updatedContent, fmt.Sprintf(" tekton.dev/source: \"%s\"", repoURL))
} else if inAnnotationsBlock {
// If we are within the annotations block
if sourceAnnotationPattern.MatchString(line) {
// Skip the existing source annotation
continue
} else if strings.TrimSpace(line) == "" || !strings.HasPrefix(line, " ") {
// If a non-indented line or an empty line is found, we consider it as the end of the annotations block
inAnnotationsBlock = false
// Append buffered annotations lines
updatedContent = append(updatedContent, annotationsBuffer...)
// Append the current line
updatedContent = append(updatedContent, line)
} else {
// Buffer the annotation lines
annotationsBuffer = append(annotationsBuffer, line)
}
} else {
// Append the line to updatedContent slice
updatedContent = append(updatedContent, line)
}
}
// Check for scanner errors
if err := scanner.Err(); err != nil {
return err
}
// Clear the file content and write the updated content
if err := f.Truncate(0); err != nil {
return err
}
if _, err := f.Seek(0, 0); err != nil {
return err
}
writer := bufio.NewWriter(f)
for _, line := range updatedContent {
fmt.Fprintln(writer, line)
}
return writer.Flush()
}*/

func addSourceAnnotationToTask(file, resourcesURI string) error {
// Add the new annotation
repoURL := extractRepositoryURL(resourcesURI)

// Open the Task YAML file
f, err := os.OpenFile(file, os.O_RDWR, 0o644)
if err != nil {
return err
}
defer f.Close()

// Create a scanner to read the file line by line
scanner := bufio.NewScanner(f)
var updatedContent []string

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

// Check if we are still within the annotations block
if !sourceAnnotationExists && annotationsPattern.MatchString(line) {
// Flag to indicate if we are within the annotations block
var inAnnotationsBlock bool

// Buffer to store annotations lines
var annotationsBuffer []string

// Read the file line by line
for scanner.Scan() {
line := scanner.Text()

switch {
case annotationsPattern.MatchString(line):
// If annotations block is found, set the inAnnotationsBlock flag to true
inAnnotationsBlock = true
// Append the line to updatedContent slice
updatedContent = append(updatedContent, line)
// Add the source annotation as the first line of the annotations block
updatedContent = append(updatedContent, fmt.Sprintf(" tekton.dev/source: \"%s\"", repoURL))
sourceAnnotationExists = true // Set sourceAnnotationExists to true after adding the annotation
case inAnnotationsBlock:
switch {
case sourceAnnotationPattern.MatchString(line):
// Skip the existing source annotation
continue
case strings.TrimSpace(line) == "" || !strings.HasPrefix(line, " "):
// If a non-indented line or an empty line is found, we consider it as the end of the annotations block
inAnnotationsBlock = false
// Append buffered annotations lines
updatedContent = append(updatedContent, annotationsBuffer...)
// Append the current line
updatedContent = append(updatedContent, line)
default:
// Buffer the annotation lines
annotationsBuffer = append(annotationsBuffer, line)
}
default:
// Append the line to updatedContent slice
updatedContent = append(updatedContent, line)
}
}

Expand Down

0 comments on commit 7579df4

Please sign in to comment.