Skip to content

Commit

Permalink
add annotation to integrate with Tekton Results
Browse files Browse the repository at this point in the history
    - These annotations will be utilized in results to capture
      data for both summary and record.

Signed-off-by: Satyam Bhardwaj <[email protected]>
  • Loading branch information
Satyam Bhardwaj committed Apr 20, 2023
1 parent 96a8e74 commit 1e21808
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/kubeinteraction/resultsannotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package kubeinteraction

import (
"encoding/json"
"fmt"

"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
)

const (
resGroupName = "results.tekton.dev"
resAnnotation = "/resAnnotations"
recordSummaryAnnotation = "/recordSummaryAnnotations"
)

type ResultAnnotation struct {
Repo string `json:"repo"`
Commit string `json:"commit"`
EventType string `json:"eventType"`
PullRequestID int `json:"pull_request-id"`
}

// Add annotations to PipelineRuns produced by PaC for TektonResults
// to capture data for summary and record.
func AddResultsAnnotation(event *info.Event, pipelineRun *tektonv1.PipelineRun) error {
if event == nil {
return fmt.Errorf("nil event")
}
resultAnnotation := ResultAnnotation{
Repo: event.Repository,
Commit: event.SHA,
EventType: event.EventType,
PullRequestID: event.PullRequestNumber,
}

// convert the `resultAnnotation` sturct into JSON string
resAnnotationJSON, err := json.Marshal(resultAnnotation)
if err != nil {
return err
}

// append the result annotations
pipelineRun.Annotations[resGroupName+resAnnotation] = string(resAnnotationJSON)
pipelineRun.Annotations[resGroupName+recordSummaryAnnotation] = string(resAnnotationJSON)

return nil
}
82 changes: 82 additions & 0 deletions pkg/kubeinteraction/resultsannotation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package kubeinteraction

import (
"encoding/json"
"errors"
"testing"

"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"gotest.tools/v3/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestAddResultsAnnotation(t *testing.T) {
testCases := []struct {
name string
event *info.Event
expectedError error
}{
{
name: "Valid Event",
event: &info.Event{
Repository: "tektoncd/results",
SHA: "8789abb6",
EventType: "PR",
PullRequestNumber: 123,
},
expectedError: nil,
},
{
name: "Empty Event",
event: &info.Event{},
expectedError: nil,
},
{
name: "Nil Event",
event: nil,
expectedError: errors.New("nil event"),
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
// Prepare test data
pipelineRun := &v1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
}
err := AddResultsAnnotation(tt.event, pipelineRun)

// Check if the error matches the expected error
if !errors.Is(err, tt.expectedError) {
assert.Equal(t, err.Error(), "nil event")
}

// If no error, check annotations
if err == nil {
// Expected result annotation
resultAnnotation := ResultAnnotation{
Repo: tt.event.Repository,
Commit: tt.event.SHA,
EventType: tt.event.EventType,
PullRequestID: tt.event.PullRequestNumber,
}
expectedJSON, err := json.Marshal(resultAnnotation)
if err != nil {
t.Fatalf("Failed to marshal expected result annotation: %v", err)
}
expectedAnnotation := string(expectedJSON)

// Check if annotations are added correctly
if pipelineRun.Annotations[resGroupName+resAnnotation] != expectedAnnotation {
t.Fatalf("Unexpected result annotation. Expected: %s, Got: %s", expectedAnnotation, pipelineRun.Annotations[resGroupName+resAnnotation])
}
if pipelineRun.Annotations[resGroupName+recordSummaryAnnotation] != expectedAnnotation {
t.Fatalf("Unexpected record summary annotation. Expected: %s, Got: %s", expectedAnnotation, pipelineRun.Annotations[resGroupName+recordSummaryAnnotation])
}
}
})
}
}
6 changes: 6 additions & 0 deletions pkg/pipelineascode/pipelineascode.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ func (p *PacRun) startPR(ctx context.Context, match matcher.Match) (*tektonv1.Pi
// Add labels and annotations to pipelinerun
kubeinteraction.AddLabelsAndAnnotations(p.event, match.PipelineRun, match.Repo, p.vcx.GetConfig())

// Add annotations to PipelineRuns to integrate with Tekton Results
err := kubeinteraction.AddResultsAnnotation(p.event, match.PipelineRun)
if err != nil {
return nil, err
}

// if concurrency is defined then start the pipelineRun in pending state and
// state as queued
if match.Repo.Spec.ConcurrencyLimit != nil && *match.Repo.Spec.ConcurrencyLimit != 0 {
Expand Down

0 comments on commit 1e21808

Please sign in to comment.