Skip to content

Commit

Permalink
feat(cloudevents-server): add extral message receiver from event cont…
Browse files Browse the repository at this point in the history
…ext (#79)

Signed-off-by: wuhuizuo <wuhuizuo@126.com>
  • Loading branch information
wuhuizuo authored Feb 27, 2024
1 parent e3dbbc8 commit 403acb5
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package tekton

import (
"encoding/json"
"net/http"

cloudevents "github.com/cloudevents/sdk-go/v2"
lark "github.com/larksuite/oapi-sdk-go/v3"
"github.com/rs/zerolog/log"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
tektoncloudevent "github.com/tektoncd/pipeline/pkg/reconciler/events/cloudevent"
)

const (
eventContextAnnotationKey = "tekton.dev/ce-context"
eventContextAnnotationInnerKeyUser = "user"
)

type pipelineRunHandler struct {
LarkClient *lark.Client
RunDetailBaseURL string
Expand All @@ -35,9 +42,28 @@ func (h *pipelineRunHandler) Handle(event cloudevents.Event) cloudevents.Result
case tektoncloudevent.PipelineRunStartedEventV1,
tektoncloudevent.PipelineRunFailedEventV1,
tektoncloudevent.PipelineRunSuccessfulEventV1:
return sendLarkMessages(h.LarkClient, h.Receivers, event, h.RunDetailBaseURL)
receivers := h.Receivers
if receiver := getTriggerUser(data.PipelineRun); receiver != "" {
receivers = append(receivers, receiver)
}

return sendLarkMessages(h.LarkClient, receivers, event, h.RunDetailBaseURL)
default:
log.Debug().Str("ce-type", event.Type()).Msg("skip notifing for the event type.")
return cloudevents.ResultACK
}
}

func getTriggerUser(pr *v1beta1.PipelineRun) string {
eventContext := pr.Annotations[eventContextAnnotationKey]
if eventContext == "" {
return ""
}

contextData := make(map[string]string)
if err := json.Unmarshal([]byte(eventContext), &contextData); err != nil {
return ""
}

return contextData[eventContextAnnotationInnerKeyUser]
}

0 comments on commit 403acb5

Please sign in to comment.