-
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 29, 2023
1 parent
5872e4f
commit 2519df1
Showing
7 changed files
with
190 additions
and
6 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,37 @@ | ||
package kubeinteraction | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys" | ||
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" | ||
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" | ||
) | ||
|
||
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 capturing additional | ||
// data specific for TektonResults. | ||
func AddResultsAnnotation(event *info.Event, pipelineRun *tektonv1.PipelineRun) error { | ||
resultAnnotation := ResultAnnotation{ | ||
Repo: event.Repository, | ||
Commit: event.SHA, | ||
EventType: event.EventType, | ||
PullRequestID: event.PullRequestNumber, | ||
} | ||
|
||
resAnnotationJSON, err := json.Marshal(resultAnnotation) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// append the result annotation | ||
pipelineRun.Annotations[keys.ResultsRecordSummary] = 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,68 @@ | ||
package kubeinteraction | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys" | ||
"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, | ||
}, | ||
} | ||
|
||
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) | ||
assert.NilError(t, err) | ||
|
||
// 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[keys.ResultsRecordSummary] == expectedAnnotation, "Unexpected record summary annotation. Expected: %s, Got: %s", expectedAnnotation, pipelineRun.Annotations[keys.ResultsRecordSummary]) | ||
} | ||
}) | ||
} | ||
} |
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,61 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys" | ||
"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" | ||
) | ||
|
||
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[keys.PullRequest] | ||
if !exists { | ||
t.Fatalf("Annotation %s does not exist", keys.PullRequest) | ||
} | ||
|
||
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[keys.ResultsRecordSummary], expectedResultAnnotation, "Unexpected annotation value") | ||
} | ||
} |