Skip to content

Commit

Permalink
Merge pull request #31 from avi-biton/add_to_results
Browse files Browse the repository at this point in the history
chore(RHTAPWATCH-1236): Add namespace and application to message sent
  • Loading branch information
avi-biton authored Aug 15, 2024
2 parents c7d41e9 + c603125 commit 68de06f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/controller/notificationservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *NotificationServiceReconciler) Reconcile(ctx context.Context, req ctrl.
notificationsFailures.Inc()
return ctrl.Result{}, err
}
logger.Info("SNS Notified", "pipelinerun", pipelineRun.Name, "namespace", pipelineRun.Namespace)
logger.Info("SNS Notified", "message", string(results))
err = AddAnnotationToPipelineRun(ctx, pipelineRun, r, NotificationPipelineRunAnnotation, NotificationPipelineRunAnnotationValue)
if err != nil {
logger.Error(err, "Failed to add annotation", "pipelineRun", pipelineRun.Name)
Expand Down
19 changes: 19 additions & 0 deletions internal/controller/pipelinerun_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const NotificationPipelineRunAnnotation string = "konflux.ci/notified"
const NotificationPipelineRunAnnotationValue string = "true"
const PipelineRunTypeLabel string = "pipelinesascode.tekton.dev/event-type"
const PushPipelineRunTypeValue string = "push"
const AppLabelKey string = "appstudio.openshift.io/application"

// AddFinalizerToPipelineRun adds the finalizer to the PipelineRun.
// If finalizer was not added successfully, a non-nil error is returned.
Expand Down Expand Up @@ -57,6 +58,14 @@ func GetResultsFromPipelineRun(pipelineRun *tektonv1.PipelineRun) ([]byte, error
Name: "PIPELINERUN_NAME",
Value: *tektonv1.NewStructuredValues(pipelineRun.Name),
},
{
Name: "NAMESPACE",
Value: *tektonv1.NewStructuredValues(pipelineRun.Namespace),
},
{
Name: "APPLICATION",
Value: *tektonv1.NewStructuredValues(GetApplicationNameFromPipelineRun(pipelineRun)),
},
}
fetchedResults := pipelineRun.Status.Results
fullResults := append(namedResults, fetchedResults...)
Expand Down Expand Up @@ -136,3 +145,13 @@ func IsPushPipelineRun(object client.Object) bool {

return false
}

// GetApplicationNameFromPipelineRun gets the application name from the application label
// Returns the application name or empty string in case the applicatio name could not retrieved
func GetApplicationNameFromPipelineRun(pipelineRun *tektonv1.PipelineRun) string {
appLabel, err := metadata.GetLabelsWithPrefix(pipelineRun, AppLabelKey)
if err != nil {
return ""
}
return appLabel[AppLabelKey]
}
35 changes: 30 additions & 5 deletions internal/controller/pipelinerun_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ var _ = Describe("Unit testing for pipelinerun_helper", func() {
testPipelineLookupKey := types.NamespacedName{Name: testResourcesPipelineRunName, Namespace: namespace}
testPipelineRun := &tektonv1.PipelineRun{}

Describe("Push Pipelinerun; Ended; with finalizer, annotation and results", func() {
Describe("Push Pipelinerun; Ended; with finalizer, annotation, results and application name", func() {
testTruePipelineRun = &tektonv1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
Name: pushPipelineRunName,
Namespace: namespace,
Labels: map[string]string{
PipelineRunTypeLabel: PushPipelineRunTypeValue,
AppLabelKey: "myapp",
},
Finalizers: []string{
NotificationPipelineRunFinalizer,
Expand Down Expand Up @@ -109,13 +110,25 @@ var _ = Describe("Unit testing for pipelinerun_helper", func() {
got := IsPipelineRunEndedSuccessfully(testTruePipelineRun)
Expect(got).To(BeTrue())
})
It("GetApplicationNameFromPipelineRun should extract the application name", func() {
got := GetApplicationNameFromPipelineRun(testTruePipelineRun)
Expect(got).To(Equal("myapp"))
})
It("GetResultsFromPipelineRun should extract the result and add it the pipelinerunName", func() {
got, err := GetResultsFromPipelineRun(testTruePipelineRun)
Expect(err).ToNot(HaveOccurred())
result := []tektonv1.PipelineRunResult{
{
Name: "PIPELINERUN_NAME",
Value: *tektonv1.NewStructuredValues(testTruePipelineRun.Name),
Value: *tektonv1.NewStructuredValues(pushPipelineRunName),
},
{
Name: "NAMESPACE",
Value: *tektonv1.NewStructuredValues(namespace),
},
{
Name: "APPLICATION",
Value: *tektonv1.NewStructuredValues("myapp"),
},
{
Name: "IMAGE_DIGEST",
Expand All @@ -140,7 +153,7 @@ var _ = Describe("Unit testing for pipelinerun_helper", func() {
})
})
})
Describe("Pull_request Pipelinerun; Not Ended; without finalizer, annotation or results", func() {
Describe("Pull_request Pipelinerun; Not Ended; without finalizer, annotation, results and application name", func() {
testFalsePipelineRun = &tektonv1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
Name: "testPullRequestPipelineRun",
Expand Down Expand Up @@ -196,13 +209,25 @@ var _ = Describe("Unit testing for pipelinerun_helper", func() {
got := IsPipelineRunEndedSuccessfully(testFalsePipelineRun)
Expect(got).To(BeFalse())
})
It("GetResultsFromPipelineRun should add pipelinerun name to empty results", func() {
It("GetApplicationNameFromPipelineRun should return empty string", func() {
got := GetApplicationNameFromPipelineRun(testFalsePipelineRun)
Expect(got).To(Equal(""))
})
It("GetResultsFromPipelineRun should add pipelinerun, namespace and application name to empty results", func() {
got, err := GetResultsFromPipelineRun(testFalsePipelineRun)
Expect(err).ToNot(HaveOccurred())
result := []tektonv1.PipelineRunResult{
{
Name: "PIPELINERUN_NAME",
Value: *tektonv1.NewStructuredValues(testFalsePipelineRun.Name),
Value: *tektonv1.NewStructuredValues("testPullRequestPipelineRun"),
},
{
Name: "NAMESPACE",
Value: *tektonv1.NewStructuredValues(namespace),
},
{
Name: "APPLICATION",
Value: *tektonv1.NewStructuredValues(""),
},
}
expectedResult, err := json.Marshal(result)
Expand Down

0 comments on commit 68de06f

Please sign in to comment.