Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement for requesting DkAPI on EMTEC operation #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,14 @@ type ReleaseTarget struct {
}

type EmtecConfig struct {
EndpointUrl string `json:"endpointUrl"`
EndpointUrl string `json:"endpointUrl"`
Dreamkast EmtecDreamkastConfig `json:"dreamkast"`
}

type EmtecDreamkastConfig struct {
EndpointUrl string `json:"endpointUrl"`
Auth0Domain string `json:"auth0Domain"`
Auth0ClientId string `json:"auth0ClientId"`
Auth0ClientSecret string `json:"auth0ClientSecret"`
Auth0ClientAudience string `json:"auth0ClientAudience" default:"https://event.cloudnativedays.jp/"`
}
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/cloudnativedaysjp/seaman
go 1.19

require (
github.com/cloudnativedaysjp/emtec-ecu v0.1.0
github.com/cloudnativedaysjp/emtec-ecu v0.2.0
github.com/creasty/defaults v1.6.0
github.com/go-logr/logr v1.2.3
github.com/go-logr/zapr v1.2.3
Expand All @@ -21,12 +21,26 @@ require (
)

require (
github.com/avast/retry-go v3.0.0+incompatible // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/labstack/echo/v4 v4.9.1 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_golang v1.13.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
Expand Down
392 changes: 390 additions & 2 deletions go.sum

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions seaman/api/emtec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,35 @@ import (
const (
// Action IDs
ActIdEmtec_SceneNext = "emtec_scenenext"
ActIdEmtec_OnAirNext = "emtec_onairnext"
)

type Track struct {
Id int32
Name string
Id int32
Name string
NextTalkId int32
}

func NewTrack(str string) (Track, error) {
func NewTrack(id int32, name string, nextTalkId int32) Track {
return Track{id, name, nextTalkId}
}

func CastToTrack(str string) (Track, error) {
s := strings.Split(str, "__")
if len(s) != 2 {
if len(s) != 3 {
return Track{}, fmt.Errorf("callbackValue (%s) is not expected", str)
}
trackId, err := strconv.Atoi(s[0])
if err != nil {
return Track{}, fmt.Errorf("callbackValue (%s) is not expected", str)
}
return Track{int32(trackId), s[1]}, nil
nextTalkId, err := strconv.Atoi(s[2])
if err != nil {
return Track{}, fmt.Errorf("callbackValue (%s) is not expected", str)
}
return Track{int32(trackId), s[1], int32(nextTalkId)}, nil
}

func (m Track) String() string {
return fmt.Sprintf("%d__%s", m.Id, m.Name)
return fmt.Sprintf("%d__%s__%d", m.Id, m.Name, m.NextTalkId)
}
73 changes: 70 additions & 3 deletions seaman/controller/emtec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"golang.org/x/xerrors"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/cloudnativedaysjp/emtec-ecu/pkg/infra/dreamkast"
pb "github.com/cloudnativedaysjp/emtec-ecu/pkg/ws-proxy/schema"

"github.com/cloudnativedaysjp/seaman/seaman/api"
Expand All @@ -25,15 +26,17 @@ type EmtecController struct {
slackFactory infra_slack.SlackClientFactory
cndSceneClient pb.SceneServiceClient
cndTrackClient pb.TrackServiceClient
dkClient dreamkast.Client
log logr.Logger
}

func NewEmtecController(
logger logr.Logger,
slackFactory infra_slack.SlackClientFactory,
cndClient *infra_cnd.CndWrapper,
obsClient *infra_cnd.CndWrapper,
dkClient dreamkast.Client,
) *EmtecController {
return &EmtecController{slackFactory, cndClient, cndClient, logger}
return &EmtecController{slackFactory, obsClient, obsClient, dkClient, logger}
}

func (c *EmtecController) ListTrack(evt *socketmode.Event, client *socketmode.Client) {
Expand Down Expand Up @@ -163,9 +166,10 @@ func (c *EmtecController) UpdateSceneToNext(evt *socketmode.Event, client *socke
sc, err := c.slackFactory.New(client.Client)
if err != nil {
logger.Error(xerrors.Errorf("message: %w", err), "failed to initialize Slack client")
return
}

track, err := api.NewTrack(callbackValue)
track, err := api.CastToTrack(callbackValue)
if err != nil {
_ = sc.PostMessage(ctx, channelId, view.InvalidArguments(messageTs,
fmt.Sprintf("invalid format on callbackValue: %s", callbackValue)))
Expand Down Expand Up @@ -205,3 +209,66 @@ func (c *EmtecController) UpdateSceneToNext(evt *socketmode.Event, client *socke
return
}
}

func (c *EmtecController) MakeNextTalkOnAir(evt *socketmode.Event, client *socketmode.Client) {
client.Ack(*evt.Request)

interaction, err := getInteractionCallback(evt)
if err != nil {
c.log.Error(err, "failed to get InteractionCallback")
return
}
channelId := interaction.Container.ChannelID
messageTs := interaction.Container.MessageTs
sentUserId := interaction.User.ID
callbackValue := getCallbackValueOnButton(interaction)

// init logger & context
logger := c.log.WithValues("messageTs", messageTs)
ctx := utils.IntoContext(context.Background(), logger)
// new client from factory
sc, err := c.slackFactory.New(client.Client)
if err != nil {
logger.Error(xerrors.Errorf("message: %w", err), "failed to initialize Slack client")
return
}

track, err := api.CastToTrack(callbackValue)
if err != nil {
_ = sc.PostMessage(ctx, channelId, view.InvalidArguments(messageTs,
fmt.Sprintf("invalid format on callbackValue: %s", callbackValue)))
return
}

if err := c.dkClient.SetSpecifiedTalkOnAir(ctx, track.NextTalkId); err != nil {
msg := "dkClient.SetSpecifiedTalkOnAir was failed"
logger.Info(fmt.Sprintf("%s: %v", msg, err))
_ = sc.PostMessage(ctx, channelId, view.SomethingIsWrong(messageTs))
return
}

msg, err := view.EmtecMadeNextTalkOnAir(interaction.Message.Msg)
if err != nil {
msg := "invalid interactive message"
logger.Info(fmt.Sprintf("%s: %v", msg, err))
_ = sc.PostMessage(ctx, channelId, view.SomethingIsWrong(messageTs))
return
}

if err := sc.UpdateMessage(
ctx, channelId, messageTs, msg,
); err != nil {
logger.Error(xerrors.Errorf("message: %w", err), "failed to post message")
_ = sc.UpdateMessage(ctx, channelId, messageTs, view.SomethingIsWrong(messageTs))
return
}

if err := sc.PostMessageToThread(
ctx, channelId, messageTs, slack.Msg{
Text: fmt.Sprintf("Switching was pushed by <@%s>", sentUserId)},
); err != nil {
logger.Error(xerrors.Errorf("message: %w", err), "failed to post message")
_ = sc.UpdateMessage(ctx, channelId, messageTs, view.SomethingIsWrong(messageTs))
return
}
}
21 changes: 17 additions & 4 deletions seaman/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/cloudnativedaysjp/emtec-ecu/pkg/infra/dreamkast"
pb "github.com/cloudnativedaysjp/emtec-ecu/pkg/ws-proxy/schema"

"github.com/cloudnativedaysjp/seaman/config"
Expand Down Expand Up @@ -61,7 +62,7 @@ func Run(conf *config.Config) error {
slackFactory := infra_slack.NewSlackClientFactory()
githubApiClient := githubapi.NewGitHubApiClientImpl(conf.GitHub.AccessToken)
gitCommandClient := gitcommand.NewGitCommandClientImpl(conf.GitHub.Username, conf.GitHub.AccessToken)
var cndClient *cndoperationserver.CndWrapper
var obsClient *cndoperationserver.CndWrapper
if conf.Emtec.EndpointUrl != "" {
func() {
conn, err := grpc.Dial(conf.Emtec.EndpointUrl,
Expand All @@ -71,11 +72,21 @@ func Run(conf *config.Config) error {
logger.Error(err, "cannot connect to EMTEC-ECU, skipped")
return
}
cndClient = cndoperationserver.NewCndWrapper(
obsClient = cndoperationserver.NewCndWrapper(
pb.NewSceneServiceClient(conn), pb.NewTrackServiceClient(conn),
)
}()
}
dkClient, err := dreamkast.NewClient(conf.Emtec.Dreamkast.EndpointUrl)
if err != nil {
return err
}
dkClient = dkClient.WithCredential(
conf.Emtec.Dreamkast.Auth0Domain,
conf.Emtec.Dreamkast.Auth0ClientId,
conf.Emtec.Dreamkast.Auth0ClientSecret,
conf.Emtec.Dreamkast.Auth0ClientAudience,
)

{ // release
var targets []controller.Target
Expand All @@ -100,8 +111,8 @@ func Run(conf *config.Config) error {
socketmodeHandler.HandleInteractionBlockAction(
api.ActIdRelease_OK, c.CreatePullRequestForRelease)
}
if cndClient != nil { // emtec
c := controller.NewEmtecController(logger, slackFactory, cndClient)
if obsClient != nil { // emtec
c := controller.NewEmtecController(logger, slackFactory, obsClient, dkClient)
socketmodeHandler.HandleEvents(
slackevents.AppMention, middleware.MiddlewareSet(c.ListTrack,
middleware.RegisterCommand("emtec", "list-track").
Expand All @@ -119,6 +130,8 @@ func Run(conf *config.Config) error {
))
socketmodeHandler.HandleInteractionBlockAction(
api.ActIdEmtec_SceneNext, c.UpdateSceneToNext)
socketmodeHandler.HandleInteractionBlockAction(
api.ActIdEmtec_OnAirNext, c.MakeNextTalkOnAir)
}
{ // common
c := controller.NewCommonController(logger,
Expand Down
63 changes: 63 additions & 0 deletions seaman/view/emtec.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,69 @@ func emtecEnabled(trackName string) (slack.Msg, error) {
}

func EmtecMovedToNextScene(msg slack.Msg) (slack.Msg, error) {
// update "Move to Next Scene" Button
var buttonValue string
bs := &msg.Blocks.BlockSet
secBlock, ok := (*bs)[len(*bs)-1].(*slack.SectionBlock)
if !ok {
return slack.Msg{}, fmt.Errorf("msg.Blocks.BlockSet[-1] cannot be cast to *slack.SectionBlock")
}
buttonValue = secBlock.Accessory.ButtonElement.Value
secBlock.Accessory.ButtonElement = &slack.ButtonBlockElement{
Type: slack.METButton,
ActionID: api.ActIdCommon_NothingToDo,
Text: &slack.TextBlockObject{
Type: "plain_text",
Text: ":white_check_mark: Switched",
},
}
// add "Set nextTalk OnAir" Button
accessory := &slack.Accessory{
ButtonElement: &slack.ButtonBlockElement{
Type: slack.METButton,
ActionID: api.ActIdEmtec_OnAirNext,
Value: buttonValue,
Text: &slack.TextBlockObject{
Type: "plain_text",
Text: "Switching",
Emoji: true,
},
Style: slack.StylePrimary,
Confirm: &slack.ConfirmationBlockObject{
Title: &slack.TextBlockObject{
Type: "plain_text",
Text: "Set nextTalk OnAir",
},
Text: &slack.TextBlockObject{
Type: "plain_text",
Text: "Are you sure?",
},
Confirm: &slack.TextBlockObject{
Type: "plain_text",
Text: "OK",
},
Deny: &slack.TextBlockObject{
Type: "plain_text",
Text: "Cancel",
},
},
},
}
msg.Blocks.BlockSet = append(msg.Blocks.BlockSet, slack.NewDividerBlock())
msg.Blocks.BlockSet = append(msg.Blocks.BlockSet, &slack.SectionBlock{
Type: slack.MBTSection,
Text: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Set nextTalk OnAir",
},
Accessory: accessory,
})

return msg, nil
}

func EmtecMadeNextTalkOnAir(msg slack.Msg) (slack.Msg, error) {
// update "Set nextTalk OnAir" Button
bs := &msg.Blocks.BlockSet
secBlock, ok := (*bs)[len(*bs)-1].(*slack.SectionBlock)
if !ok {
Expand Down
41 changes: 40 additions & 1 deletion seaman/view/emtec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func Test_EmtecMovedToNextScene(t *testing.T) {
"accessory": {
"type": "button",
"action_id": "emtec_scenenext",
"value": "1__A",
"value": "1__A__10002",
"text": {
"type": "plain_text",
"text": "Switching"
Expand Down Expand Up @@ -279,6 +279,45 @@ func Test_EmtecMovedToNextScene(t *testing.T) {
"text": ":white_check_mark: Switched"
}
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": "Set nextTalk OnAir"
},
"accessory": {
"type": "button",
"action_id": "emtec_onairnext",
"text": {
"type": "plain_text",
"text": "Switching",
"emoji": true
},
"value": "1__A__10002",
"style": "primary",
"confirm": {
"title": {
"type": "plain_text",
"text": "Set nextTalk OnAir"
},
"text": {
"type": "plain_text",
"text": "Are you sure?"
},
"confirm": {
"type": "plain_text",
"text": "OK"
},
"deny": {
"type": "plain_text",
"text": "Cancel"
}
}
}
}
]
}
Expand Down