From 252edcaf7d0965f3a21ed207e7803da7af275912 Mon Sep 17 00:00:00 2001 From: SSlime-s <62363188+SSlime-s@users.noreply.github.com> Date: Wed, 27 Oct 2021 09:31:35 +0900 Subject: [PATCH 01/15] =?UTF-8?q?=F0=9F=90=9B=20=E8=AA=AC=E6=98=8E?= =?UTF-8?q?=E3=81=8C=E3=81=AA=E3=81=84=E3=81=A8=E3=81=8D=E3=81=AB=E7=A9=BA?= =?UTF-8?q?=E3=81=AE=E5=BC=95=E7=94=A8=E3=81=8C=20webhook=20=E3=81=AB?= =?UTF-8?q?=E5=90=AB=E3=81=BE=E3=82=8C=E3=81=A6=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/middleware.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/router/middleware.go b/router/middleware.go index 0147e302..87bf8d2a 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -215,7 +215,11 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) content += "\n\n\n" } - content += "> " + strings.ReplaceAll(e.Description, "\n", "\n> ") + if strings.TrimSpace(e.Description) != "" { + content += "> " + strings.ReplaceAll(e.Description, "\n", "\n> ") + } else { + content = strings.TrimRight(content, "\n") + } _ = utils.RequestWebhook(content, h.WebhookSecret, h.ActivityChannelID, h.WebhookID, 1) } From 0f04b708a00db6f5f13a31ea218171c7939d4a61 Mon Sep 17 00:00:00 2001 From: fuji Date: Sat, 6 Nov 2021 21:14:42 +0900 Subject: [PATCH 02/15] remove bot users from traP group --- domain/user.go | 2 +- router/middleware.go | 2 +- router/users.go | 2 +- usecase/production/event.go | 4 ++-- usecase/production/group.go | 4 ++-- usecase/production/user.go | 5 ++++- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/domain/user.go b/domain/user.go index 5cc109bd..55c70803 100644 --- a/domain/user.go +++ b/domain/user.go @@ -18,7 +18,7 @@ type UserRepository interface { GetUser(userID uuid.UUID, info *ConInfo) (*User, error) GetUserMe(info *ConInfo) (*User, error) - GetAllUsers(includeSuspend bool, info *ConInfo) ([]*User, error) + GetAllUsers(includeSuspend, includeBot bool, info *ConInfo) ([]*User, error) //ReplaceToken(userID uuid.UUID, token string, info *ConInfo) error //GetToken(info *ConInfo) (string, error) ReNewMyiCalSecret(info *ConInfo) (string, error) diff --git a/router/middleware.go b/router/middleware.go index 0147e302..86b24899 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -182,7 +182,7 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) return } - users, err := h.Repo.GetAllUsers(false, getConinfo(c)) + users, err := h.Repo.GetAllUsers(false, true, getConinfo(c)) if err != nil { return } diff --git a/router/users.go b/router/users.go index fe0fe277..5c1334be 100644 --- a/router/users.go +++ b/router/users.go @@ -29,7 +29,7 @@ func (h *Handlers) HandleGetUserMe(c echo.Context) error { func (h *Handlers) HandleGetUsers(c echo.Context) error { includeSuspend, _ := strconv.ParseBool(c.QueryParam("include-suspended")) - users, err := h.Repo.GetAllUsers(includeSuspend, getConinfo(c)) + users, err := h.Repo.GetAllUsers(includeSuspend, true, getConinfo(c)) if err != nil { if errors.Is(domain.ErrInvalidToken, err) { return forbidden(err, message("token is invalid."), needAuthorization(true)) diff --git a/usecase/production/event.go b/usecase/production/event.go index 88f86d21..a8a019e4 100644 --- a/usecase/production/event.go +++ b/usecase/production/event.go @@ -108,7 +108,7 @@ func (repo *Repository) GetEvent(eventID uuid.UUID, info *domain.ConInfo) (*doma return nil, defaultErrorHandling(err) } event.Group = *g - users, err := repo.GetAllUsers(false, info) + users, err := repo.GetAllUsers(false, true, info) if err != nil { return &event, err } @@ -159,7 +159,7 @@ func (repo *Repository) GetEvents(expr filter.Expr, info *domain.ConInfo) ([]*do return events, nil } groupMap := createGroupMap(groups) - users, err := repo.GetAllUsers(false, info) + users, err := repo.GetAllUsers(false, true, info) if err != nil { return events, err } diff --git a/usecase/production/group.go b/usecase/production/group.go index f7a9617a..c3b4c4be 100644 --- a/usecase/production/group.go +++ b/usecase/production/group.go @@ -179,7 +179,7 @@ func (repo *Repository) IsGroupMember(userID, groupID uuid.UUID, info *domain.Co } func (repo *Repository) getTraPGroup(info *domain.ConInfo) *domain.Group { - users, err := repo.GetAllUsers(false, info) + members, err := repo.GetAllUsers(false, false, info) if err != nil { return nil } @@ -189,7 +189,7 @@ func (repo *Repository) getTraPGroup(info *domain.ConInfo) *domain.Group { Name: "traP", Description: "traP全体グループ", JoinFreely: false, - Members: convSPdomainUserToSdomainUser(users), + Members: convSPdomainUserToSdomainUser(members), Admins: []domain.User{}, IsTraQGroup: true, CreatedBy: domain.User{}, diff --git a/usecase/production/user.go b/usecase/production/user.go index 3e85fcbe..c9191fa1 100644 --- a/usecase/production/user.go +++ b/usecase/production/user.go @@ -116,7 +116,7 @@ func (repo *Repository) GetUserMe(info *domain.ConInfo) (*domain.User, error) { return repo.GetUser(info.ReqUserID, info) } -func (repo *Repository) GetAllUsers(includeSuspend bool, info *domain.ConInfo) ([]*domain.User, error) { +func (repo *Repository) GetAllUsers(includeSuspend, includeBot bool, info *domain.ConInfo) ([]*domain.User, error) { t, err := repo.GormRepo.GetToken(info.ReqUserID) if err != nil { return nil, defaultErrorHandling(err) @@ -138,6 +138,9 @@ func (repo *Repository) GetAllUsers(includeSuspend bool, info *domain.ConInfo) ( if !ok { continue } + if !includeBot && userBody.Bot { + continue + } user, err := repo.mergeUser(userMeta, userBody) if err != nil { continue From 8ffb7ee866d977a16e57c628f31d1c6fd1251683 Mon Sep 17 00:00:00 2001 From: fuji Date: Sat, 6 Nov 2021 21:38:15 +0900 Subject: [PATCH 03/15] fix mention for traP group --- router/middleware.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/router/middleware.go b/router/middleware.go index 86b24899..79b802e5 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -202,15 +202,27 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) content += fmt.Sprintf("- 場所: %s", e.Room.Place) + "\n" content += "\n" + // TODO fix: IDを環境変数などで定義すべき + traPGroupID := uuid.Must(uuid.FromString("11111111-1111-1111-1111-111111111111")) + if e.TimeStart.After(time.Now()) { content += "以下の方は参加予定の入力をお願いします:pray:" + "\n" - for _, attendee := range e.Attendees { - if attendee.Schedule == presentation.Pending { - user, ok := usersMap[attendee.ID] - if ok { - content += "@" + user.Name + " " + prefix := "@" + if e.Group.ID == traPGroupID { + gradeGroups := []string{"21B", "20B", "19B", "18B", "17B", "16B", "21M", "20M"} + for _, gg := range gradeGroups { + content += prefix + gg + " " + } + } else { + for _, attendee := range e.Attendees { + if attendee.Schedule == presentation.Pending { + user, ok := usersMap[attendee.ID] + if ok { + content += prefix + user.Name + " " + } } } + } content += "\n\n\n" } From 3bebaabd02ab343daab67490c3e7f897afc7aaaa Mon Sep 17 00:00:00 2001 From: fuji Date: Sat, 6 Nov 2021 21:58:43 +0900 Subject: [PATCH 04/15] dynamic definition of grade groups --- router/middleware.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/router/middleware.go b/router/middleware.go index 79b802e5..dc8eb104 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -209,9 +209,19 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) content += "以下の方は参加予定の入力をお願いします:pray:" + "\n" prefix := "@" if e.Group.ID == traPGroupID { - gradeGroups := []string{"21B", "20B", "19B", "18B", "17B", "16B", "21M", "20M"} - for _, gg := range gradeGroups { - content += prefix + gg + " " + repo, ok := h.Repo.(*production.Repository) + if !ok { + return + } + t, err := repo.GormRepo.GetToken(getConinfo(c).ReqUserID) + if err != nil { + return + } + groups, _ := repo.TraQRepo.GetAllGroups(t) + for _, g := range groups { + if g.Type == "grade" { + content += prefix + g.Name + " " + } } } else { for _, attendee := range e.Attendees { From 9944310f98b5879bd50abf0401552145b4746850 Mon Sep 17 00:00:00 2001 From: fuji Date: Tue, 9 Nov 2021 22:43:54 +0900 Subject: [PATCH 05/15] add environment value DEVELOPMENT --- README.md | 1 + development/.env.example | 1 + development/docker-compose.yml | 1 + main.go | 7 +++++++ 4 files changed, 10 insertions(+) diff --git a/README.md b/README.md index aa02b77a..cda18a67 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ knoQ の全ての機能を動作させるためには、追加の情報が必要 | WEBHOOK_SECRET | 環境変数 | | Bot 情報 | | CHANNEL_ID | 環境変数 | | Bot の送信先チャンネル | | TOKEN_KEY | 環境変数 | `random32wordsXXXXXXXXXXXXXXXXXXX` | Token を暗号化する。長さ 32 文字のランダム文字列。存在しない場合はエラー。 | +| DEVELOPMENT | 環境変数 | | 開発時かどうか | | service.json | ファイル | 空のファイル | google calendar api に必要(権限は必要なし) | ### テスト diff --git a/development/.env.example b/development/.env.example index 1f3dc84f..a2c0758e 100644 --- a/development/.env.example +++ b/development/.env.example @@ -5,3 +5,4 @@ WEBHOOK_ID= WEBHOOK_SECRET= CHANNEL_ID= TOKEN_KEY= +DEVELOPMENT= diff --git a/development/docker-compose.yml b/development/docker-compose.yml index 76f01331..5b82abd2 100755 --- a/development/docker-compose.yml +++ b/development/docker-compose.yml @@ -23,6 +23,7 @@ services: WEBHOOK_SECRET: ${WEBHOOK_SECRET} CHANNEL_ID: ${CHANNEL_ID} TOKEN_KEY: ${TOKEN_KEY:-random32wordsXXXXXXXXXXXXXXXXXXX} + DEVELOPMENT: true volumes: - ../main.go:/srv/knoq/main.go - ../logging:/srv/knoq/logging diff --git a/main.go b/main.go index af8a4084..6d48b8b6 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "net/http" "os" "os/signal" + "strconv" "time" "github.com/traPtitech/knoQ/infra/db" @@ -20,8 +21,14 @@ import ( "go.uber.org/zap" ) +var ( + DEVELOPMENT bool +) + func main() { logger, _ := zap.NewDevelopment() + DEVELOPMENT, _ = strconv.ParseBool(os.Getenv("DEVELOPMENT")) + gormRepo := db.GormRepository{} err := gormRepo.Setup(os.Getenv("MARIADB_HOSTNAME"), os.Getenv("MARIADB_USERNAME"), os.Getenv("MARIADB_PASSWORD"), os.Getenv("MARIADB_DATABASE"), os.Getenv("TOKEN_KEY")) From b0388380c58f7a4e53b9020a73db6dbff3af6428 Mon Sep 17 00:00:00 2001 From: fuji Date: Tue, 9 Nov 2021 22:44:07 +0900 Subject: [PATCH 06/15] wip --- presentation/event.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/presentation/event.go b/presentation/event.go index 53b68652..3dc70ec5 100644 --- a/presentation/event.go +++ b/presentation/event.go @@ -139,3 +139,7 @@ func ICalFormat(events []*domain.Event, host string) *ical.Calendar { } return c } + +func GenerateEventWebhookContent(isMention bool) { + +} From b8a2a7f40dbc6374f08a47e7d438b880a020de0e Mon Sep 17 00:00:00 2001 From: fuji Date: Sat, 13 Nov 2021 20:33:01 +0900 Subject: [PATCH 07/15] fix: Sort notification targets --- router/middleware.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/router/middleware.go b/router/middleware.go index dc8eb104..57e81813 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -5,12 +5,14 @@ import ( "errors" "fmt" "net/http" + "sort" "strconv" "strings" "time" log "github.com/traPtitech/knoQ/logging" "github.com/traPtitech/knoQ/presentation" + "github.com/traPtitech/knoQ/usecase/production" "github.com/traPtitech/knoQ/utils" "github.com/gofrs/uuid" @@ -208,6 +210,8 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) if e.TimeStart.After(time.Now()) { content += "以下の方は参加予定の入力をお願いします:pray:" + "\n" prefix := "@" + + nofiticationTargets := make([]string, 0) if e.Group.ID == traPGroupID { repo, ok := h.Repo.(*production.Repository) if !ok { @@ -220,7 +224,7 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) groups, _ := repo.TraQRepo.GetAllGroups(t) for _, g := range groups { if g.Type == "grade" { - content += prefix + g.Name + " " + nofiticationTargets = append(nofiticationTargets, g.Name) } } } else { @@ -228,11 +232,14 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) if attendee.Schedule == presentation.Pending { user, ok := usersMap[attendee.ID] if ok { - content += prefix + user.Name + " " + nofiticationTargets = append(nofiticationTargets, user.Name) } } } - + } + sort.Strings(nofiticationTargets) + for _, nt := range nofiticationTargets { + content += prefix + nt + " " } content += "\n\n\n" } From 7f9b08864f8f173160e65c3c0fd96b1ccfb6812c Mon Sep 17 00:00:00 2001 From: fuji Date: Sat, 13 Nov 2021 21:09:12 +0900 Subject: [PATCH 08/15] fix: Move webhook content to presentation --- presentation/event.go | 36 +++++++++++++++++++++++++++++++++++- router/middleware.go | 34 ++++------------------------------ 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/presentation/event.go b/presentation/event.go index 3dc70ec5..79028f1e 100644 --- a/presentation/event.go +++ b/presentation/event.go @@ -2,6 +2,9 @@ package presentation import ( "fmt" + "net/http" + "sort" + "strings" "time" "github.com/traPtitech/knoQ/domain" @@ -140,6 +143,37 @@ func ICalFormat(events []*domain.Event, host string) *ical.Calendar { return c } -func GenerateEventWebhookContent(isMention bool) { +func GenerateEventWebhookContent(method string, e *EventDetailRes, nofiticationTargets []string, origin string, isMention bool) string { + jst, _ := time.LoadLocation("Asia/Tokyo") + timeFormat := "01/02(Mon) 15:04" + var content string + switch method { + case http.MethodPost: + content = "## イベントが作成されました" + "\n" + case http.MethodPut: + content = "## イベントが更新されました" + "\n" + } + content += fmt.Sprintf("### [%s](%s/events/%s)", e.Name, origin, e.ID) + "\n" + content += fmt.Sprintf("- 主催: [%s](%s/groups/%s)", e.GroupName, origin, e.Group.ID) + "\n" + content += fmt.Sprintf("- 日時: %s ~ %s", e.TimeStart.In(jst).Format(timeFormat), e.TimeEnd.In(jst).Format(timeFormat)) + "\n" + content += fmt.Sprintf("- 場所: %s", e.Room.Place) + "\n" + content += "\n" + + if e.TimeStart.After(time.Now()) { + content += "以下の方は参加予定の入力をお願いします:pray:" + "\n" + prefix := "@" + if !isMention { + prefix = "@." + } + + sort.Strings(nofiticationTargets) + for _, nt := range nofiticationTargets { + content += prefix + nt + " " + } + content += "\n\n\n" + } + + content += "> " + strings.ReplaceAll(e.Description, "\n", "\n> ") + return "" } diff --git a/router/middleware.go b/router/middleware.go index 57e81813..8f467b9c 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -3,11 +3,8 @@ package router import ( "encoding/json" "errors" - "fmt" "net/http" - "sort" "strconv" - "strings" "time" log "github.com/traPtitech/knoQ/logging" @@ -189,29 +186,11 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) return } usersMap := createUserMap(users) - - jst, _ := time.LoadLocation("Asia/Tokyo") - timeFormat := "01/02(Mon) 15:04" - var content string - if c.Request().Method == http.MethodPost { - content = "## イベントが作成されました" + "\n" - } else if c.Request().Method == http.MethodPut { - content = "## イベントが更新されました" + "\n" - } - content += fmt.Sprintf("### [%s](%s/events/%s)", e.Name, h.Origin, e.ID) + "\n" - content += fmt.Sprintf("- 主催: [%s](%s/groups/%s)", e.GroupName, h.Origin, e.Group.ID) + "\n" - content += fmt.Sprintf("- 日時: %s ~ %s", e.TimeStart.In(jst).Format(timeFormat), e.TimeEnd.In(jst).Format(timeFormat)) + "\n" - content += fmt.Sprintf("- 場所: %s", e.Room.Place) + "\n" - content += "\n" - - // TODO fix: IDを環境変数などで定義すべき - traPGroupID := uuid.Must(uuid.FromString("11111111-1111-1111-1111-111111111111")) + nofiticationTargets := make([]string, 0) if e.TimeStart.After(time.Now()) { - content += "以下の方は参加予定の入力をお願いします:pray:" + "\n" - prefix := "@" - - nofiticationTargets := make([]string, 0) + // TODO fix: IDを環境変数などで定義すべき + traPGroupID := uuid.Must(uuid.FromString("11111111-1111-1111-1111-111111111111")) if e.Group.ID == traPGroupID { repo, ok := h.Repo.(*production.Repository) if !ok { @@ -237,14 +216,9 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) } } } - sort.Strings(nofiticationTargets) - for _, nt := range nofiticationTargets { - content += prefix + nt + " " - } - content += "\n\n\n" } - content += "> " + strings.ReplaceAll(e.Description, "\n", "\n> ") + content := presentation.GenerateEventWebhookContent(c.Request().Method, e, nofiticationTargets, h.Origin, false) _ = utils.RequestWebhook(content, h.WebhookSecret, h.ActivityChannelID, h.WebhookID, 1) } From 8c4f2bf372e96ce08e512f9888981c09c4225fb7 Mon Sep 17 00:00:00 2001 From: fuji Date: Sat, 13 Nov 2021 21:14:29 +0900 Subject: [PATCH 09/15] fix: Move DEVELOPMENT to domain --- domain/domain.go | 4 ++++ main.go | 7 ++----- router/middleware.go | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/domain/domain.go b/domain/domain.go index 0c81729d..556c0d1b 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -6,6 +6,10 @@ import ( "github.com/gofrs/uuid" ) +var ( + DEVELOPMENT bool +) + type Model struct { CreatedAt time.Time UpdatedAt time.Time diff --git a/main.go b/main.go index 6d48b8b6..7d7d80b1 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "strconv" "time" + "github.com/traPtitech/knoQ/domain" "github.com/traPtitech/knoQ/infra/db" "github.com/traPtitech/knoQ/infra/traq" "github.com/traPtitech/knoQ/usecase/production" @@ -21,13 +22,9 @@ import ( "go.uber.org/zap" ) -var ( - DEVELOPMENT bool -) - func main() { logger, _ := zap.NewDevelopment() - DEVELOPMENT, _ = strconv.ParseBool(os.Getenv("DEVELOPMENT")) + domain.DEVELOPMENT, _ = strconv.ParseBool(os.Getenv("DEVELOPMENT")) gormRepo := db.GormRepository{} err := gormRepo.Setup(os.Getenv("MARIADB_HOSTNAME"), os.Getenv("MARIADB_USERNAME"), diff --git a/router/middleware.go b/router/middleware.go index 8f467b9c..4d857b56 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -7,6 +7,7 @@ import ( "strconv" "time" + "github.com/traPtitech/knoQ/domain" log "github.com/traPtitech/knoQ/logging" "github.com/traPtitech/knoQ/presentation" "github.com/traPtitech/knoQ/usecase/production" @@ -218,7 +219,7 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) } } - content := presentation.GenerateEventWebhookContent(c.Request().Method, e, nofiticationTargets, h.Origin, false) + content := presentation.GenerateEventWebhookContent(c.Request().Method, e, nofiticationTargets, h.Origin, !domain.DEVELOPMENT) _ = utils.RequestWebhook(content, h.WebhookSecret, h.ActivityChannelID, h.WebhookID, 1) } From 0de5ed1f550d3c3a87af161909ed95395a0d1206 Mon Sep 17 00:00:00 2001 From: fuji Date: Sat, 13 Nov 2021 21:19:39 +0900 Subject: [PATCH 10/15] fix --- presentation/event.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/presentation/event.go b/presentation/event.go index 79028f1e..0ccbb664 100644 --- a/presentation/event.go +++ b/presentation/event.go @@ -175,5 +175,5 @@ func GenerateEventWebhookContent(method string, e *EventDetailRes, nofiticationT content += "> " + strings.ReplaceAll(e.Description, "\n", "\n> ") - return "" + return content } From b803450a6fb1f877e2daab250b83cccddc16bb54 Mon Sep 17 00:00:00 2001 From: fuji Date: Sat, 13 Nov 2021 21:39:34 +0900 Subject: [PATCH 11/15] trim space --- development/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/development/docker-compose.yml b/development/docker-compose.yml index 5b82abd2..b6eb1e24 100755 --- a/development/docker-compose.yml +++ b/development/docker-compose.yml @@ -78,7 +78,7 @@ services: - PMA_HOST=mysql - PMA_USER=root - PMA_PASSWORD=password - depends_on: + depends_on: - mysql networks: - default From 8310379dd63f92dae6d46fb57dd31c72f2fd64da Mon Sep 17 00:00:00 2001 From: fuji Date: Tue, 7 Dec 2021 21:54:47 +0900 Subject: [PATCH 12/15] add environment value --- README.md | 2 ++ development/.env.example | 2 ++ development/docker-compose.yml | 1 + domain/domain.go | 2 ++ main.go | 2 ++ 5 files changed, 9 insertions(+) diff --git a/README.md b/README.md index cda18a67..c1d21517 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ knoQ の全ての機能を動作させるためには、追加の情報が必要 | WEBHOOK_SECRET | 環境変数 | | Bot 情報 | | CHANNEL_ID | 環境変数 | | Bot の送信先チャンネル | | TOKEN_KEY | 環境変数 | `random32wordsXXXXXXXXXXXXXXXXXXX` | Token を暗号化する。長さ 32 文字のランダム文字列。存在しない場合はエラー。 | +| KNOQ_VERSION | 環境変数 | UNKNOWN | knoQ のバージョン (github actions でイメージ作成時に指定) | +| KNOQ_REVISION | 環境変数 | UNKNOWN | git の sha1 (github actions でイメージ作成時に指定) | | DEVELOPMENT | 環境変数 | | 開発時かどうか | | service.json | ファイル | 空のファイル | google calendar api に必要(権限は必要なし) | diff --git a/development/.env.example b/development/.env.example index a2c0758e..e685fc05 100644 --- a/development/.env.example +++ b/development/.env.example @@ -5,4 +5,6 @@ WEBHOOK_ID= WEBHOOK_SECRET= CHANNEL_ID= TOKEN_KEY= +KNOQ_VERSION= +KNOQ_REVISION= DEVELOPMENT= diff --git a/development/docker-compose.yml b/development/docker-compose.yml index b6eb1e24..96e4c62e 100755 --- a/development/docker-compose.yml +++ b/development/docker-compose.yml @@ -23,6 +23,7 @@ services: WEBHOOK_SECRET: ${WEBHOOK_SECRET} CHANNEL_ID: ${CHANNEL_ID} TOKEN_KEY: ${TOKEN_KEY:-random32wordsXXXXXXXXXXXXXXXXXXX} + KNOQ_VERSION: ${KNOQ_VERSION:-dev} DEVELOPMENT: true volumes: - ../main.go:/srv/knoq/main.go diff --git a/domain/domain.go b/domain/domain.go index 556c0d1b..80806cab 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -7,6 +7,8 @@ import ( ) var ( + VERSION string = "UNKNOWN" + REVISION string = "UNKNOWN" DEVELOPMENT bool ) diff --git a/main.go b/main.go index 7d7d80b1..19e9e911 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,8 @@ import ( func main() { logger, _ := zap.NewDevelopment() + domain.VERSION = os.Getenv("KNOQ_VERSION") + domain.REVISION = os.Getenv("KNOQ_REVISION") domain.DEVELOPMENT, _ = strconv.ParseBool(os.Getenv("DEVELOPMENT")) gormRepo := db.GormRepository{} From b6baebbb6f62e9daaffb9c15cbea36f57cfc765a Mon Sep 17 00:00:00 2001 From: fuji Date: Tue, 7 Dec 2021 22:22:54 +0900 Subject: [PATCH 13/15] add get version to api --- docs/swagger.yaml | 25 ++++++++++++++++++++++++- presentation/presentation.go | 5 +++++ router/router.go | 1 + router/version.go | 16 ++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 router/version.go diff --git a/docs/swagger.yaml b/docs/swagger.yaml index b91e2c55..d560074b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -4,7 +4,7 @@ info: title: traP knoQ description: | This is a sample knoQ server. - version: 2.1.0 + version: 2.1.3 servers: - url: http://knoq.trap.jp/api @@ -29,6 +29,8 @@ tags: description: 認証 - name: iCal description: ics出力 + - name: public + description: 外部公開API paths: /rooms: @@ -525,6 +527,7 @@ paths: post: tags: - authentication + - public operationId: getAuthParams description: リクエストに必要な情報を返す responses: @@ -572,6 +575,26 @@ paths: type: string example: "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:github.com/lestrrat-go/ical\nBEGIN:VTIMEZONE\nTZID:Asia/Tokyo\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZNAME:JST\nTZOFFSETFROM:+9000\nTZOFFSETTO:+9000\nEND:STANDARD\nEND:VTIMEZONE\nBEGIN:VEVENT\nCREATED:20'200'827T09'400'8Z\nDESCRIPTION:asd¥n-----------------------------------¥nイベント詳細ページ¥nhttp://loc\nalhost:'400'0/events/855cff6d-aef6-4de2-8ba5-bc0c3e76a617\nDTEND:20'200'828T1'400'00Z\nDTSTAMP:20'200'904T145144Z\nDTSTART:20'200'828T050000Z\nLAST-MODIFIED:20'200'827T09'400'8Z\nLOCATION:dfaaa\nORGANIZER:18984a38-4dc7-41ab-9c8d-f5469a8e30a9\nSUMMARY:sadfwe\nUID:855cff6d-aef6-4de2-8ba5-bc0c3e76a617\nEND:VEVENT\nBEGIN:VEVENT\nCREATED:20'200'827T09'400'8Z\nDESCRIPTION:asd¥n-----------------------------------¥nイベント詳細ページ¥nhttp://loc\nalhost:'400'0/events/855cff6d-aef6-4de2-8ba5-bc0c3e76a617\nDTEND:20'200'828T1'400'00Z\nDTSTAMP:20'200'904T145144Z\nDTSTART:20'200'828T050000Z\nLAST-MODIFIED:20'200'827T09'400'8Z\nLOCATION:dfaaa\nORGANIZER:18984a38-4dc7-41ab-9c8d-f5469a8e30a9\nSUMMARY:sadfwe\nUID:855cff6d-aef6-4de2-8ba5-bc0c3e76a617\nEND:VEVENT\nEND:VCALENDAR" + /version: + get: + tags: + - public + operationId: getVersion + description: version情報を取得 + responses: + '200': + description: versionを出力 + content: + application/json: + schema: + type: object + properties: + version: + type: string + example: v2.1.3 + revision: + type: string + example: 587c185 components: schemas: # will delete diff --git a/presentation/presentation.go b/presentation/presentation.go index afa44ce5..9f473708 100644 --- a/presentation/presentation.go +++ b/presentation/presentation.go @@ -7,3 +7,8 @@ type Model struct { UpdatedAt time.Time `json:"updatedAt"` DeletedAt *time.Time `json:"deletedAt,omitempty"` } + +type Version struct { + Version string `json:"version"` + Revision string `json:"revision"` +} diff --git a/router/router.go b/router/router.go index bd1c36ca..57f0c600 100644 --- a/router/router.go +++ b/router/router.go @@ -142,6 +142,7 @@ func (h *Handlers) SetupRoute() *echo.Echo { e.POST("/api/authParams", h.HandlePostAuthParams) e.GET("/api/callback", h.HandleCallback) e.GET("/api/ical/v1/:userIDsecret", h.HandleGetiCalByPrivateID) + e.GET("/api/version", h.HandleGetVersion) e.Use(middleware.StaticWithConfig(middleware.StaticConfig{ Skipper: func(c echo.Context) bool { diff --git a/router/version.go b/router/version.go new file mode 100644 index 00000000..a85793c7 --- /dev/null +++ b/router/version.go @@ -0,0 +1,16 @@ +package router + +import ( + "net/http" + + "github.com/labstack/echo/v4" + "github.com/traPtitech/knoQ/domain" + "github.com/traPtitech/knoQ/presentation" +) + +func (h *Handlers) HandleGetVersion(c echo.Context) error { + return c.JSON(http.StatusOK, presentation.Version{ + Version: domain.VERSION, + Revision: domain.REVISION, + }) +} From a3b08f1980954b784726a375bcb439d825a5ee80 Mon Sep 17 00:00:00 2001 From: fuji Date: Tue, 7 Dec 2021 22:29:28 +0900 Subject: [PATCH 14/15] add environment value to github actions --- .github/workflows/develop.yml | 7 +++++++ .github/workflows/main.yml | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml index 97177c16..b8f37508 100644 --- a/.github/workflows/develop.yml +++ b/.github/workflows/develop.yml @@ -12,6 +12,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - name: Set KNOQ_VERSION env + run: echo "KNOQ_VERSION=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV + - name: Set KNOQ_REVISION env + run: echo "KNOQ_REVISION=$(git rev-parse --short HEAD)" >> $GITHUB_ENV - name: Set up QEMU uses: docker/setup-qemu-action@v1 - name: Set up Docker Buildx @@ -28,5 +32,8 @@ jobs: context: . push: true platforms: linux/amd64,linux/arm64 + build-args: | + KNOQ_VERSION=${{ env.KNOQ_VERSION }} + KNOQ_REVISION=${{ env.KNOQ_REVISION }} tags: | ghcr.io/traptitech/${{ env.IMAGE_NAME }}:beta diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 04e3a17c..0c567ffe 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,6 +14,10 @@ jobs: - name: Set IMAGE_TAG env run: echo "IMAGE_TAG=$(echo ${GITHUB_REF:11})" >> $GITHUB_ENV - uses: actions/checkout@v2 + - name: Set KNOQ_VERSION env + run: echo "KNOQ_VERSION=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV + - name: Set KNOQ_REVISION env + run: echo "KNOQ_REVISION=$(git rev-parse --short HEAD)" >> $GITHUB_ENV - name: Set up QEMU uses: docker/setup-qemu-action@v1 - name: Set up Docker Buildx @@ -30,6 +34,9 @@ jobs: context: . push: true platforms: linux/amd64,linux/arm64 + build-args: | + KNOQ_VERSION=${{ env.KNOQ_VERSION }} + KNOQ_REVISION=${{ env.KNOQ_REVISION }} tags: | ghcr.io/traptitech/${{ env.IMAGE_NAME }}:latest ghcr.io/traptitech/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} From 628f231dad173ac65cbaa95c8d34cbfcf092dd89 Mon Sep 17 00:00:00 2001 From: fuji Date: Tue, 7 Dec 2021 22:44:33 +0900 Subject: [PATCH 15/15] add x-knoq-version header --- router/middleware.go | 10 ++++++++++ router/router.go | 2 ++ 2 files changed, 12 insertions(+) diff --git a/router/middleware.go b/router/middleware.go index 4d857b56..56b0e88d 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -74,6 +74,16 @@ func AccessLoggingMiddleware(logger *zap.Logger) echo.MiddlewareFunc { } } +// ServerVersionMiddleware X-KNOQ-VERSIONをレスポンスヘッダーを追加するミドルウェア +func ServerVersionMiddleware(version string) echo.MiddlewareFunc { + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + c.Response().Header().Set("X-KNOQ-VERSION", version) + return next(c) + } + } +} + // TraQUserMiddleware traQユーザーか判定するミドルウェア // TODO funcname fix func (h *Handlers) TraQUserMiddleware(next echo.HandlerFunc) echo.HandlerFunc { diff --git a/router/router.go b/router/router.go index 57f0c600..7d327c07 100644 --- a/router/router.go +++ b/router/router.go @@ -46,6 +46,8 @@ func (h *Handlers) SetupRoute() *echo.Echo { } e.Use(session.Middleware(sessions.NewCookieStore(h.SessionKey))) + e.Use(ServerVersionMiddleware(domain.VERSION)) + // TODO fix "portal origin" e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ AllowOrigins: []string{"https://portal.trap.jp", "http://localhost:8080"},