-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add annotation to integrate with Tekton Results
- 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
Showing
3 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters