-
Notifications
You must be signed in to change notification settings - Fork 3
#1 delete dynamodb data with slack slash command #34
#1 delete dynamodb data with slack slash command #34
Conversation
Codecov Report
@@ Coverage Diff @@
## master #34 +/- ##
=======================================
Coverage 87.32% 87.32%
=======================================
Files 4 4
Lines 142 142
=======================================
Hits 124 124
Misses 13 13
Partials 5 5 Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントしました。
database/database.go
Outdated
func DeleteUser(configData config.ConfigData, member WriteBlogEveryWeek) { | ||
table := getTableObject(configData) | ||
if err := table.Delete("user_id", member.UserID).Run(); err != nil { | ||
panic("削除エラー => " + err.Error()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
基本goのアプリではpanic
はしないでちゃんとerrror
を返してその結果を戻したほうがいいですね。今回の場合だと以下の点で不便だと思います。
panic
したら結果が返らないのでcloud watch見れる人じゃないと結果がわからない- cloud watch見れる人でもSlackコマンド使ったらSlack上で結果を見たいと思う
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
勉強になります!エラーメッセージを返すよう修正しました。
main.go
Outdated
@@ -96,3 +99,24 @@ func blogResult() { | |||
} | |||
// fmt.Println(sendText) | |||
} | |||
|
|||
// blogDelete ブログの削除ロジックを実行 | |||
func blogDelete(_ context.Context, rawParams interface{}) (interface{}, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
監査的な機能なくいれちゃってダイジョブですかね?コマンドを実行したIDをログに残しておくとかとか(消したレコードもログに出力しておいたほうが復旧しやすそうです)
これってSlackコマンド知っていたら誰でも消せちゃう感じだと、しれっとDMとかでやってメッセージ消したら完全犯罪になってしまうような。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slack.goでSlackから渡ってきたパラメータを出力しました。
と書いたところで、消したレコードがログに記載がないのに気づいたので追加で修正します 🙇
main.go
Outdated
@@ -96,3 +99,24 @@ func blogResult() { | |||
} | |||
// fmt.Println(sendText) | |||
} | |||
|
|||
// blogDelete ブログの削除ロジックを実行 | |||
func blogDelete(_ context.Context, rawParams interface{}) (interface{}, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
戻り値の interface{}
はstring
ですかね?自分で宣言する関数の引数戻り値でinterface{}
は使わないほうがいいですね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正しました!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なんどもごめんなさい、追記しました 🙇
slack/slack.go
Outdated
// また、ユーザー名が送られてきた場合は、先頭の@をtrimする | ||
Text: strings.TrimRight(strings.TrimLeft(strings.TrimLeft(params["text"][0], "@"), "<"), ">"), | ||
} | ||
fmt.Printf("SlackParams: %v", slackParams) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Token: params["token"][0],
っていうなんかお漏らししたらいけなそうなフィールドがあるのでUserID
(UserName
)、Text
など特定のフィールドのみ出力するだけに絞っておいたほうがいいと思います。
(業務でやるときはloggerとかに特定のフィールド名をマスキングする処理仕込んだりするのですが、そこまではするのはめんどくさいので。。。)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
token以外のユーザーID、ユーザー名、Textを出力するように修正しました
(ユーザー名だけだとSlack側で変更できてしまうので、IDも念の為出力)
@@ -64,11 +65,14 @@ func ParseSlackParams(rawParams interface{}) (result *SlackParams, err error) { | |||
return | |||
} | |||
|
|||
return &SlackParams{ | |||
slackParams := SlackParams{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好みの問題(?)ですが、私はこっちはslackParams := &SlackParams
のままにしておきますね。
(これは変えなくてもいいです。)
理由はポインタのほうにメソッド生やしているときが多いので
// DeleteUser ユーザーデータを削除する | ||
func DeleteUser(configData config.ConfigData, member WriteBlogEveryWeek) error { | ||
table := getTableObject(configData) | ||
return table.Delete("user_id", member.UserID).Run() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DynamoDBのuser_idってなんの文字列だったのかよくわかってなかったんですが、SlackのIDだったんですね!(今更)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最後にひとつだけコメントしました
@@ -96,3 +99,28 @@ func blogResult() { | |||
} | |||
// fmt.Println(sendText) | |||
} | |||
|
|||
// blogDelete ブログの削除ロジックを実行 | |||
func blogDelete(_ context.Context, rawParams interface{}) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error
返すPATHがなくなったのなら戻り値はstring
だけでいいと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
残念ながらLambdaのハンドラ関数の制約で戻り値1つの場合はerrorでないといけないので、戻り値を2個定義する必要があります……
https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/go-programming-model-handler-types.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、今まで勘違いしていたのですが、この関数の戻り値がSlackに通知されるわけではないのですね。
そうするとこの関数で文字列を返す理由はなんでしょうか?
Slackに結果を通知して関数を正常に完了するだけならば、文字列を slack.SendMessage
に渡してこの関数は戻り値なしにすればいいのでは?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あー、slack slash commandに紐づけるLambdaとしてはLambda関数の戻り値として設定した値がSlackに通知される感じですかね?
だからblogResult
やblogReminder
はslack.SendMessage
使っているけど、blogRegister
とblogDelete
は戻り値で結果返しているという理解でよいですかね(今更感)
なんか全然検討違いのコメントをしてしまって && ドメイン理解が甘いままレビューしてしまってごめんなさい 🙇 🙇
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
説明が雑ですみません。。 🙇
ご認識の通り、このQiita記事みたいに、Slack側にレスポンスを返せるようにAPI Gatewayが設定されているはずです。(IAMの権限の都合で見れませんが)
https://qiita.com/G-awa/items/fc803c285cb560dab796
blogRegister関数の動作が同じようになっていて、添付の画像のようにメッセージが表示されます
(main.goの72行目のメッセージが表示されたケースです)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
解説ありがとうございます!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントしました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
昨夜盛大にトンチンカンなコメントをしてしまった気がします。私の不勉強で余計なやりとり発生してしまってごめんなさい 🙇
私の理解が間違っていなければLGTMです
@@ -96,3 +99,28 @@ func blogResult() { | |||
} | |||
// fmt.Println(sendText) | |||
} | |||
|
|||
// blogDelete ブログの削除ロジックを実行 | |||
func blogDelete(_ context.Context, rawParams interface{}) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あー、slack slash commandに紐づけるLambdaとしてはLambda関数の戻り値として設定した値がSlackに通知される感じですかね?
だからblogResult
やblogReminder
はslack.SendMessage
使っているけど、blogRegister
とblogDelete
は戻り値で結果返しているという理解でよいですかね(今更感)
なんか全然検討違いのコメントをしてしまって && ドメイン理解が甘いままレビューしてしまってごめんなさい 🙇 🙇
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎉
🎫 Issue
#1
📝 概要
退会者が出た時に利用するDynamoDBからのデータ削除用のSlackのスラッシュコマンドを実装しました。
Slackで
/blog_delete @kdnakt
のように利用できる予定です。テストコードがないのですが、自分のAWSアカウントで簡単に動作確認済みです。
(対象者がいない場合のメッセージ、いる場合の処理)
🙅♂️ やってないこと
(このPRではやってないことなど書きましょう)
✔️ 動作確認
(レビューアに確認してほしいことを書きましょう)