-
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
- annotation : results.tekton.dev/recordSummaryAnnotations: |- {"repo": "tektoncd/results", "commit": "1a6b908", eventType: "pull_request", "pull_request-id": 6} - This annotation will be utilized in results to capture data. Signed-off-by: Satyam Bhardwaj <[email protected]>
- Loading branch information
Satyam Bhardwaj
committed
Dec 11, 2023
1 parent
a5d0561
commit a6809a3
Showing
7 changed files
with
217 additions
and
3 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
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
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
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,45 @@ | ||
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" | ||
recordSummaryAnnotation = "/recordSummaryAnnotations" | ||
) | ||
|
||
type ResultAnnotation struct { | ||
Repo string `json:"repo"` | ||
Commit string `json:"commit"` | ||
EventType string `json:"eventType"` | ||
PullRequestID int `json:"pull_request-id,omitempty"` | ||
} | ||
|
||
// Add annotation 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 annotation | ||
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,77 @@ | ||
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 annotation is added correctly | ||
assert.Assert(t, pipelineRun.Annotations[resGroupName+recordSummaryAnnotation] == expectedAnnotation, "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
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,65 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/kubeinteraction" | ||
tgitea "github.com/openshift-pipelines/pipelines-as-code/test/pkg/gitea" | ||
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options" | ||
"gotest.tools/v3/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const ( | ||
AnnotationPullRequest = "pipelinesascode.tekton.dev/pull-request" | ||
AnnotationSummary = "results.tekton.dev/recordSummaryAnnotations" | ||
) | ||
|
||
func TestGiteaResultsAnnotations(t *testing.T) { | ||
topts := &tgitea.TestOpts{ | ||
Regexp: successRegexp, | ||
TargetEvent: options.PullRequestEvent, | ||
YAMLFiles: map[string]string{ | ||
".tekton/pipeline.yaml": "testdata/pipelinerun.yaml", | ||
}, | ||
CheckForStatus: "success", | ||
} | ||
defer tgitea.TestPR(t, topts)() | ||
|
||
// assertions for checking results specific annotation in the PipelineRuns manifest here | ||
prs, err := topts.ParamsRun.Clients.Tekton.TektonV1().PipelineRuns(topts.TargetNS).List(context.Background(), metav1.ListOptions{}) | ||
assert.NilError(t, err) | ||
for _, pr := range prs.Items { | ||
annotations := pr.GetAnnotations() | ||
assert.Assert(t, annotations != nil, "Annotations should not be nil") | ||
|
||
val, exists := annotations[AnnotationPullRequest] | ||
if !exists { | ||
t.Fatalf("Annotation %s does not exist", AnnotationPullRequest) | ||
} | ||
|
||
pullRequestNumber, err := strconv.Atoi(val) | ||
assert.NilError(t, err) | ||
|
||
// Assert specific annotation | ||
resultAnnotation := kubeinteraction.ResultAnnotation{ | ||
Repo: topts.TargetNS, | ||
Commit: topts.PullRequest.Head.Sha, | ||
EventType: topts.TargetEvent, | ||
PullRequestID: pullRequestNumber, | ||
} | ||
expectedJSON, err := json.Marshal(resultAnnotation) | ||
assert.NilError(t, err) | ||
expectedResultAnnotation := string(expectedJSON) | ||
|
||
// an example of results annotation format | ||
// results.tekton.dev/recordSummaryAnnotations:{"repo":"pac-demo","commit":"62f8c8b7e4c3fc38cfbe7fcce2660e5b95de2d9a","eventType":"pull_request","pull_request-id":7} | ||
assert.Equal(t, annotations[AnnotationSummary], expectedResultAnnotation, "Unexpected annotation value") | ||
} | ||
} |