From 4fd820a25b96a149e3316211a26d4484d5aae14f Mon Sep 17 00:00:00 2001 From: David May <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:53:00 -0500 Subject: [PATCH] fix: logging (#1145) --- api/admin/build.go | 28 +- api/admin/clean.go | 15 +- api/admin/hook.go | 14 +- api/admin/repo.go | 18 +- api/admin/rotate_keys.go | 4 +- api/admin/secret.go | 24 +- api/admin/service.go | 15 +- api/admin/settings.go | 49 ++- api/admin/step.go | 18 +- api/admin/user.go | 19 +- api/admin/worker.go | 11 +- api/auth/get_token.go | 18 +- api/auth/login.go | 7 +- api/auth/logout.go | 14 +- api/auth/post_token.go | 15 +- api/auth/redirect.go | 3 +- api/auth/refresh.go | 5 + api/auth/validate.go | 4 + api/auth/validate_oauth.go | 4 + api/badge.go | 11 +- api/build/approve.go | 18 +- api/build/auto_cancel.go | 26 ++ api/build/cancel.go | 23 +- api/build/clean.go | 28 +- api/build/compile_publish.go | 37 ++- api/build/create.go | 21 +- api/build/delete.go | 15 +- api/build/enqueue.go | 22 +- api/build/executable.go | 23 +- api/build/get.go | 15 +- api/build/get_id.go | 11 +- api/build/graph.go | 26 +- api/build/id_request_token.go | 15 +- api/build/id_token.go | 11 +- api/build/list_org.go | 11 +- api/build/list_repo.go | 14 +- api/build/plan.go | 7 + api/build/restart.go | 21 +- api/build/token.go | 13 +- api/build/update.go | 38 ++- api/dashboard/create.go | 15 +- api/dashboard/delete.go | 9 +- api/dashboard/get.go | 11 +- api/dashboard/list_user.go | 13 +- api/dashboard/update.go | 10 +- api/deployment/create.go | 16 +- api/deployment/get.go | 12 +- api/deployment/list.go | 16 +- api/hook/create.go | 19 +- api/hook/delete.go | 44 +-- api/hook/get.go | 44 +-- api/hook/list.go | 14 +- api/hook/redeliver.go | 42 +-- api/hook/update.go | 40 +-- api/jwks.go | 5 + api/log/create_service.go | 21 +- api/log/create_step.go | 21 +- api/log/delete_service.go | 20 +- api/log/delete_step.go | 20 +- api/log/get_service.go | 20 +- api/log/get_step.go | 20 +- api/log/list_build.go | 19 +- api/log/update_service.go | 22 +- api/log/update_step.go | 22 +- api/oi_config.go | 5 + api/pipeline/compile.go | 13 +- api/pipeline/create.go | 21 +- api/pipeline/delete.go | 15 +- api/pipeline/expand.go | 13 +- api/pipeline/get.go | 15 +- api/pipeline/list.go | 14 +- api/pipeline/template.go | 11 +- api/pipeline/update.go | 15 +- api/pipeline/validate.go | 13 +- api/queue/queue.go | 7 +- api/repo/chown.go | 14 +- api/repo/create.go | 28 +- api/repo/delete.go | 12 +- api/repo/get.go | 14 +- api/repo/list.go | 8 +- api/repo/list_org.go | 11 +- api/repo/repair.go | 21 +- api/repo/update.go | 18 +- api/schedule/create.go | 20 +- api/schedule/delete.go | 16 +- api/schedule/get.go | 17 +- api/schedule/list.go | 9 +- api/schedule/update.go | 14 +- api/scm/sync.go | 18 +- api/scm/sync_org.go | 17 +- api/secret/create.go | 50 +-- api/secret/delete.go | 31 +- api/secret/get.go | 31 +- api/secret/list.go | 26 +- api/secret/update.go | 26 +- api/service/create.go | 20 +- api/service/delete.go | 16 +- api/service/get.go | 16 +- api/service/list.go | 15 +- api/service/plan.go | 19 ++ api/service/update.go | 16 +- api/step/create.go | 20 +- api/step/delete.go | 16 +- api/step/get.go | 16 +- api/step/list.go | 15 +- api/step/plan.go | 17 + api/step/update.go | 18 +- api/types/dashboard.go | 2 + api/user/create.go | 10 +- api/user/create_token.go | 10 +- api/user/delete.go | 10 +- api/user/delete_token.go | 10 +- api/user/get.go | 10 +- api/user/get_current.go | 8 +- api/user/get_source.go | 8 +- api/user/list.go | 10 +- api/user/update.go | 12 +- api/user/update_current.go | 8 +- api/webhook/post.go | 151 +++++++-- api/worker/create.go | 19 +- api/worker/delete.go | 11 +- api/worker/get.go | 11 +- api/worker/list.go | 10 +- api/worker/refresh.go | 16 +- api/worker/update.go | 11 +- cmd/vela-server/metadata.go | 10 +- cmd/vela-server/schedule.go | 15 + cmd/vela-server/scm.go | 2 +- cmd/vela-server/secret.go | 2 +- cmd/vela-server/server.go | 2 + cmd/vela-server/token.go | 2 +- cmd/vela-server/validate.go | 6 +- compiler/native/native.go | 6 +- database/build/build.go | 2 +- database/build/clean.go | 2 +- database/build/count.go | 2 +- database/build/count_deployment.go | 2 +- database/build/count_org.go | 2 +- database/build/count_repo.go | 2 +- database/build/count_status.go | 2 +- database/build/create.go | 2 +- database/build/delete.go | 2 +- database/build/get.go | 2 +- database/build/get_repo.go | 2 +- database/build/index.go | 2 +- database/build/last_repo.go | 2 +- database/build/list.go | 2 +- database/build/list_dashboard.go | 2 +- database/build/list_org.go | 2 +- database/build/list_pending_running.go | 2 +- database/build/list_pending_running_repo.go | 2 +- database/build/list_repo.go | 2 +- database/build/table.go | 2 +- database/build/update.go | 2 +- database/dashboard/create.go | 2 +- database/dashboard/dashboard.go | 2 +- database/dashboard/delete.go | 2 +- database/dashboard/get.go | 2 +- database/dashboard/table.go | 2 +- database/dashboard/update.go | 2 +- database/deployment/count.go | 2 +- database/deployment/count_repo.go | 2 +- database/deployment/create.go | 2 +- database/deployment/delete.go | 2 +- database/deployment/deployment.go | 2 +- database/deployment/get.go | 2 +- database/deployment/get_repo.go | 2 +- database/deployment/index.go | 2 +- database/deployment/list.go | 2 +- database/deployment/list_repo.go | 2 +- database/deployment/table.go | 2 +- database/deployment/update.go | 2 +- database/executable/pop.go | 3 +- database/executable/table.go | 2 +- database/hook/count.go | 2 +- database/hook/count_repo.go | 2 +- database/hook/create.go | 2 +- database/hook/delete.go | 2 +- database/hook/get.go | 2 +- database/hook/get_repo.go | 2 +- database/hook/get_webhook.go | 2 +- database/hook/hook.go | 2 +- database/hook/index.go | 2 +- database/hook/last_repo.go | 2 +- database/hook/list.go | 2 +- database/hook/list_repo.go | 2 +- database/hook/table.go | 2 +- database/hook/update.go | 2 +- database/jwk/create.go | 2 +- database/jwk/get.go | 2 +- database/jwk/jwk.go | 2 +- database/jwk/list.go | 2 +- database/jwk/rotate.go | 2 +- database/jwk/table.go | 2 +- database/log/count.go | 2 +- database/log/count_build.go | 2 +- database/log/create.go | 4 +- database/log/delete.go | 4 +- database/log/get.go | 2 +- database/log/get_service.go | 2 +- database/log/get_step.go | 2 +- database/log/index.go | 2 +- database/log/list.go | 2 +- database/log/list_build.go | 2 +- database/log/log.go | 2 +- database/log/table.go | 2 +- database/log/update.go | 4 +- database/pipeline/count.go | 2 +- database/pipeline/count_repo.go | 2 +- database/pipeline/delete.go | 2 +- database/pipeline/get.go | 2 +- database/pipeline/get_repo.go | 2 +- database/pipeline/list.go | 2 +- database/pipeline/list_repo.go | 2 +- database/repo/count.go | 2 +- database/repo/count_org.go | 2 +- database/repo/count_user.go | 2 +- database/repo/create.go | 2 +- database/repo/delete.go | 2 +- database/repo/get.go | 2 +- database/repo/get_org.go | 2 +- database/repo/index.go | 2 +- database/repo/list.go | 2 +- database/repo/list_org.go | 2 +- database/repo/list_user.go | 2 +- database/repo/repo.go | 2 +- database/repo/table.go | 2 +- database/repo/update.go | 2 +- database/schedule/count.go | 2 +- database/schedule/count_active.go | 2 +- database/schedule/count_repo.go | 2 +- database/schedule/get.go | 2 +- database/schedule/get_repo.go | 2 +- database/schedule/list.go | 2 +- database/schedule/list_active.go | 2 +- database/schedule/list_repo.go | 2 +- database/secret/count.go | 2 +- database/secret/count_org.go | 2 +- database/secret/count_repo.go | 2 +- database/secret/count_team.go | 4 +- database/secret/create.go | 4 +- database/secret/delete.go | 4 +- database/secret/get.go | 2 +- database/secret/get_org.go | 2 +- database/secret/get_repo.go | 2 +- database/secret/get_team.go | 2 +- database/secret/index.go | 2 +- database/secret/list.go | 2 +- database/secret/list_org.go | 2 +- database/secret/list_repo.go | 2 +- database/secret/list_team.go | 4 +- database/secret/secret.go | 2 +- database/secret/table.go | 2 +- database/secret/update.go | 4 +- database/service/count.go | 2 +- database/service/count_build.go | 2 +- database/service/delete.go | 2 +- database/service/get.go | 2 +- database/service/get_build.go | 2 +- database/service/list.go | 2 +- database/service/list_build.go | 2 +- database/service/list_image.go | 2 +- database/service/list_status.go | 2 +- database/service/table.go | 2 +- database/service/update.go | 2 +- database/settings/create.go | 2 +- database/settings/get.go | 2 +- database/settings/settings.go | 2 +- database/settings/table.go | 2 +- database/step/count.go | 2 +- database/step/count_build.go | 2 +- database/step/delete.go | 2 +- database/step/get.go | 2 +- database/step/get_build.go | 2 +- database/step/list.go | 2 +- database/step/list_build.go | 2 +- database/step/list_image.go | 2 +- database/step/list_status.go | 2 +- database/user/count.go | 2 +- database/user/create.go | 2 +- database/user/delete.go | 2 +- database/user/get.go | 2 +- database/user/get_name.go | 2 +- database/user/index.go | 2 +- database/user/list.go | 2 +- database/user/list_lite.go | 2 +- database/user/table.go | 2 +- database/user/update.go | 2 +- database/user/user.go | 2 +- database/worker/count.go | 2 +- database/worker/create.go | 2 +- database/worker/delete.go | 2 +- database/worker/get.go | 2 +- database/worker/get_hostname.go | 2 +- database/worker/index.go | 2 +- database/worker/list.go | 2 +- database/worker/table.go | 2 +- database/worker/update.go | 2 +- database/worker/worker.go | 2 +- queue/queue.go | 2 +- router/admin.go | 1 - router/dashboard.go | 11 +- router/hook.go | 13 +- router/middleware/build/build.go | 23 +- router/middleware/build/build_test.go | 6 + router/middleware/claims/claims.go | 10 + router/middleware/claims/claims_test.go | 4 + router/middleware/dashboard/dashboard.go | 16 +- router/middleware/dashboard/dashboard_test.go | 4 + router/middleware/dashboard/doc.go | 10 + router/middleware/header.go | 2 + router/middleware/hook/context.go | 37 +++ router/middleware/hook/context_test.go | 88 +++++ router/middleware/hook/doc.go | 10 + router/middleware/hook/hook.go | 76 +++++ router/middleware/hook/hook_test.go | 302 ++++++++++++++++++ router/middleware/logger.go | 69 +++- router/middleware/org/org.go | 12 +- router/middleware/org/org_test.go | 3 + router/middleware/perm/perm.go | 148 +++------ router/middleware/perm/perm_test.go | 30 ++ router/middleware/pipeline/pipeline.go | 22 +- router/middleware/pipeline/pipeline_test.go | 5 + router/middleware/repo/repo.go | 20 +- router/middleware/repo/repo_test.go | 5 + router/middleware/schedule/schedule.go | 20 +- router/middleware/service/service.go | 22 +- router/middleware/service/service_test.go | 7 + router/middleware/step/step.go | 22 +- router/middleware/step/step_test.go | 7 + router/middleware/user/user.go | 11 +- router/middleware/user/user_test.go | 6 + router/middleware/worker/worker.go | 11 +- router/middleware/worker/worker_test.go | 3 + router/queue.go | 2 +- scm/github/deployment.go | 7 +- scm/github/repo.go | 2 +- 337 files changed, 2014 insertions(+), 1689 deletions(-) create mode 100644 router/middleware/dashboard/doc.go create mode 100644 router/middleware/hook/context.go create mode 100644 router/middleware/hook/context_test.go create mode 100644 router/middleware/hook/doc.go create mode 100644 router/middleware/hook/hook.go create mode 100644 router/middleware/hook/hook_test.go diff --git a/api/admin/build.go b/api/admin/build.go index aab5f4fca..525179256 100644 --- a/api/admin/build.go +++ b/api/admin/build.go @@ -1,6 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -//nolint:dupl // ignore similar code package admin import ( @@ -50,11 +49,13 @@ import ( // AllBuildsQueue represents the API handler to get running and pending builds. func AllBuildsQueue(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) + + l.Debug("platform admin: reading running and pending builds") + // capture middleware values ctx := c.Request.Context() - logrus.Info("Admin: reading running and pending builds") - // default timestamp to 24 hours ago if user did not provide it as query parameter after := c.DefaultQuery("after", strconv.FormatInt(time.Now().UTC().Add(-24*time.Hour).Unix(), 10)) @@ -103,11 +104,12 @@ func AllBuildsQueue(c *gin.Context) { // UpdateBuild represents the API handler to update a build. func UpdateBuild(c *gin.Context) { - logrus.Info("Admin: updating build in database") - // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() + l.Debug("platform admin: updating build") + // capture body from API request input := new(types.Build) @@ -120,6 +122,14 @@ func UpdateBuild(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "build": input.GetNumber(), + "build_id": input.GetID(), + "repo": util.EscapeValue(input.GetRepo().GetName()), + "repo_id": input.GetRepo().GetID(), + "org": util.EscapeValue(input.GetRepo().GetOrg()), + }).Debug("platform admin: attempting to update build") + // send API call to update the build b, err := database.FromContext(c).UpdateBuild(ctx, input) if err != nil { @@ -130,5 +140,13 @@ func UpdateBuild(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), + "org": b.GetRepo().GetOrg(), + }).Info("platform admin: updated build") + c.JSON(http.StatusOK, b) } diff --git a/api/admin/clean.go b/api/admin/clean.go index d6ecbde4d..da3949568 100644 --- a/api/admin/clean.go +++ b/api/admin/clean.go @@ -12,7 +12,6 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types" "github.com/go-vela/types/constants" @@ -60,10 +59,10 @@ import ( // CleanResources represents the API handler to update stale resources. func CleanResources(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - logrus.Infof("platform admin %s: updating pending resources in database", u.GetName()) + l.Debug("platform admin: cleaning resources") // default error message msg := "build cleaned by platform admin" @@ -82,7 +81,7 @@ func CleanResources(c *gin.Context) { // if a message is provided, set the error message to that if input.Message != nil { - msg = *input.Message + msg = util.EscapeValue(*input.Message) } // capture before query parameter, default to max build timeout @@ -105,7 +104,7 @@ func CleanResources(c *gin.Context) { return } - logrus.Infof("platform admin %s: cleaned %d builds in database", u.GetName(), builds) + l.Debugf("platform admin: cleaned %d builds", builds) // clean executables executables, err := database.FromContext(c).CleanBuildExecutables(ctx) @@ -117,6 +116,8 @@ func CleanResources(c *gin.Context) { return } + l.Debugf("platform admin: cleaned %d executables", executables) + // clean services services, err := database.FromContext(c).CleanServices(ctx, msg, before) if err != nil { @@ -127,7 +128,7 @@ func CleanResources(c *gin.Context) { return } - logrus.Infof("platform admin %s: cleaned %d services in database", u.GetName(), services) + l.Debugf("platform admin: cleaned %d services", services) // clean steps steps, err := database.FromContext(c).CleanSteps(ctx, msg, before) @@ -139,7 +140,7 @@ func CleanResources(c *gin.Context) { return } - logrus.Infof("platform admin %s: cleaned %d steps in database", u.GetName(), steps) + l.Debugf("platform admin: cleaned %d steps", steps) c.JSON(http.StatusOK, fmt.Sprintf("%d builds cleaned. %d executables cleaned. %d services cleaned. %d steps cleaned.", builds, executables, services, steps)) } diff --git a/api/admin/hook.go b/api/admin/hook.go index d9d7b4b17..a7d0ef303 100644 --- a/api/admin/hook.go +++ b/api/admin/hook.go @@ -1,6 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -//nolint:dupl // ignore similar code package admin import ( @@ -51,11 +50,12 @@ import ( // UpdateHook represents the API handler to update a hook. func UpdateHook(c *gin.Context) { - logrus.Info("Admin: updating hook in database") - // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() + l.Debug("platform admin: updating hook") + // capture body from API request input := new(library.Hook) @@ -68,6 +68,10 @@ func UpdateHook(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "hook_id": input.GetID(), + }).Debug("platform admin: attempting to update hook") + // send API call to update the hook h, err := database.FromContext(c).UpdateHook(ctx, input) if err != nil { @@ -78,5 +82,9 @@ func UpdateHook(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "hook_id": h.GetID(), + }).Info("platform admin: hook updated") + c.JSON(http.StatusOK, h) } diff --git a/api/admin/repo.go b/api/admin/repo.go index 6ce1bac37..16a26fef0 100644 --- a/api/admin/repo.go +++ b/api/admin/repo.go @@ -1,6 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -//nolint:dupl // ignore similar code package admin import ( @@ -51,11 +50,12 @@ import ( // UpdateRepo represents the API handler to update a repo. func UpdateRepo(c *gin.Context) { - logrus.Info("Admin: updating repo in database") - // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() + l.Debug("platform admin: updating repo") + // capture body from API request input := new(types.Repo) @@ -68,6 +68,12 @@ func UpdateRepo(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "org": util.EscapeValue(input.GetOrg()), + "repo": util.EscapeValue(input.GetName()), + "repo_id": input.GetID(), + }).Debug("platform admin: attempting to update repo") + // send API call to update the repo r, err := database.FromContext(c).UpdateRepo(ctx, input) if err != nil { @@ -78,5 +84,11 @@ func UpdateRepo(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "org": r.GetOrg(), + "repo": r.GetName(), + "repo_id": r.GetID(), + }).Info("platform admin: repo updated") + c.JSON(http.StatusOK, r) } diff --git a/api/admin/rotate_keys.go b/api/admin/rotate_keys.go index 0186c0c29..4fba00616 100644 --- a/api/admin/rotate_keys.go +++ b/api/admin/rotate_keys.go @@ -40,7 +40,9 @@ import ( // RotateOIDCKeys represents the API handler to // rotate RSA keys in the OIDC provider service. func RotateOIDCKeys(c *gin.Context) { - logrus.Info("Admin: rotating keys for OIDC provider") + l := c.MustGet("logger").(*logrus.Entry) + + l.Info("platform admin: rotating keys for OIDC provider") // capture middleware values ctx := c.Request.Context() diff --git a/api/admin/secret.go b/api/admin/secret.go index 673134ba6..f287e4227 100644 --- a/api/admin/secret.go +++ b/api/admin/secret.go @@ -1,6 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -//nolint:dupl // ignore similar code package admin import ( @@ -51,11 +50,12 @@ import ( // UpdateSecret represents the API handler to update a secret. func UpdateSecret(c *gin.Context) { - logrus.Info("Admin: updating secret in database") - // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() + l.Debug("platform admin: updating secret") + // capture body from API request input := new(library.Secret) @@ -68,6 +68,15 @@ func UpdateSecret(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "secret_id": input.GetID(), + "secret_org": util.EscapeValue(input.GetOrg()), + "secret_repo": util.EscapeValue(input.GetRepo()), + "secret_type": util.EscapeValue(input.GetType()), + "secret_name": util.EscapeValue(input.GetName()), + "secret_team": util.EscapeValue(input.GetTeam()), + }).Debug("platform admin: attempting to update secret") + // send API call to update the secret s, err := database.FromContext(c).UpdateSecret(ctx, input) if err != nil { @@ -78,5 +87,14 @@ func UpdateSecret(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "secret_id": s.GetID(), + "secret_org": s.GetOrg(), + "secret_repo": s.GetRepo(), + "secret_type": s.GetType(), + "secret_name": s.GetName(), + "secret_team": s.GetTeam(), + }).Info("platform admin: secret updated") + c.JSON(http.StatusOK, s) } diff --git a/api/admin/service.go b/api/admin/service.go index 61153ad5f..13d33d199 100644 --- a/api/admin/service.go +++ b/api/admin/service.go @@ -52,11 +52,12 @@ import ( // UpdateService represents the API handler to update a service. func UpdateService(c *gin.Context) { - logrus.Info("Admin: updating service in database") - // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() + l.Debug("platform admin: updating service") + // capture body from API request input := new(library.Service) @@ -69,6 +70,11 @@ func UpdateService(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "service_id": input.GetID(), + "service": util.EscapeValue(input.GetName()), + }).Debug("platform admin: attempting to update service") + // send API call to update the service s, err := database.FromContext(c).UpdateService(ctx, input) if err != nil { @@ -79,5 +85,10 @@ func UpdateService(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "service_id": s.GetID(), + "service": s.GetName(), + }).Info("platform admin: updated service") + c.JSON(http.StatusOK, s) } diff --git a/api/admin/settings.go b/api/admin/settings.go index 8b5c525d8..dfb8cb338 100644 --- a/api/admin/settings.go +++ b/api/admin/settings.go @@ -47,15 +47,17 @@ import ( // GetSettings represents the API handler to get platform settings. func GetSettings(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) + + l.Debug("platform admin: reading platform settings") + // capture middleware values s := sMiddleware.FromContext(c) - logrus.Info("Admin: reading settings") - // check captured value because we aren't retrieving settings from the database // instead we are retrieving the auto-refreshed middleware value if s == nil { - retErr := fmt.Errorf("settings not found") + retErr := fmt.Errorf("platform settings not found") util.HandleError(c, http.StatusNotFound, retErr) @@ -108,16 +110,17 @@ func GetSettings(c *gin.Context) { // platform settings singleton. func UpdateSettings(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) s := sMiddleware.FromContext(c) u := uMiddleware.FromContext(c) ctx := c.Request.Context() - logrus.Info("Admin: updating settings") + l.Debug("platform admin: updating platform settings") // check captured value because we aren't retrieving settings from the database // instead we are retrieving the auto-refreshed middleware value if s == nil { - retErr := fmt.Errorf("settings not found") + retErr := fmt.Errorf("platform settings not found") util.HandleError(c, http.StatusNotFound, retErr) @@ -136,7 +139,7 @@ func UpdateSettings(c *gin.Context) { err := c.Bind(input) if err != nil { - retErr := fmt.Errorf("unable to decode JSON for settings: %w", err) + retErr := fmt.Errorf("unable to decode JSON for platform settings: %w", err) util.HandleError(c, http.StatusBadRequest, retErr) @@ -150,7 +153,7 @@ func UpdateSettings(c *gin.Context) { _, err = image.ParseWithError(cloneImage) if err != nil { - retErr := fmt.Errorf("invalid clone image %s: %w", cloneImage, err) + retErr := fmt.Errorf("invalid clone image %s for platform settings: %w", cloneImage, err) util.HandleError(c, http.StatusBadRequest, retErr) @@ -158,14 +161,20 @@ func UpdateSettings(c *gin.Context) { } _s.SetCloneImage(cloneImage) + + l.Infof("platform admin: updating clone image to %s", cloneImage) } if input.TemplateDepth != nil { _s.SetTemplateDepth(*input.TemplateDepth) + + l.Infof("platform admin: updating template depth to %d", *input.TemplateDepth) } if input.StarlarkExecLimit != nil { _s.SetStarlarkExecLimit(*input.StarlarkExecLimit) + + l.Infof("platform admin: updating starlark exec limit to %d", *input.StarlarkExecLimit) } } @@ -173,14 +182,20 @@ func UpdateSettings(c *gin.Context) { if input.Queue.Routes != nil { _s.SetRoutes(input.GetRoutes()) } + + l.Infof("platform admin: updating queue routes to: %s", input.GetRoutes()) } if input.RepoAllowlist != nil { _s.SetRepoAllowlist(input.GetRepoAllowlist()) + + l.Infof("platform admin: updating repo allowlist to: %s", input.GetRepoAllowlist()) } if input.ScheduleAllowlist != nil { _s.SetScheduleAllowlist(input.GetScheduleAllowlist()) + + l.Infof("platform admin: updating schedule allowlist to: %s", input.GetScheduleAllowlist()) } _s.SetUpdatedBy(u.GetName()) @@ -188,7 +203,7 @@ func UpdateSettings(c *gin.Context) { // send API call to update the settings _s, err = database.FromContext(c).UpdateSettings(ctx, _s) if err != nil { - retErr := fmt.Errorf("unable to update settings: %w", err) + retErr := fmt.Errorf("unable to update platform settings: %w", err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -229,18 +244,20 @@ func UpdateSettings(c *gin.Context) { // RestoreSettings represents the API handler to // restore platform settings to the environment defaults. func RestoreSettings(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) + + l.Debug("platform admin: restoring platform settings") + // capture middleware values + ctx := c.Request.Context() + cliCtx := cliMiddleware.FromContext(c) s := sMiddleware.FromContext(c) u := uMiddleware.FromContext(c) - cliCtx := cliMiddleware.FromContext(c) - ctx := c.Request.Context() - - logrus.Info("Admin: restoring settings") // check captured value because we aren't retrieving settings from the database // instead we are retrieving the auto-refreshed middleware value if s == nil { - retErr := fmt.Errorf("settings not found") + retErr := fmt.Errorf("platform settings not found") util.HandleError(c, http.StatusNotFound, retErr) @@ -249,7 +266,7 @@ func RestoreSettings(c *gin.Context) { compiler, err := native.FromCLIContext(cliCtx) if err != nil { - retErr := fmt.Errorf("unable to restore settings: %w", err) + retErr := fmt.Errorf("unable to restore platform settings: %w", err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -258,7 +275,7 @@ func RestoreSettings(c *gin.Context) { queue, err := queue.FromCLIContext(cliCtx) if err != nil { - retErr := fmt.Errorf("unable to restore settings: %w", err) + retErr := fmt.Errorf("unable to restore platform settings: %w", err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -281,7 +298,7 @@ func RestoreSettings(c *gin.Context) { // send API call to update the settings s, err = database.FromContext(c).UpdateSettings(ctx, _s) if err != nil { - retErr := fmt.Errorf("unable to update (restore) settings: %w", err) + retErr := fmt.Errorf("unable to update (restore) platform settings: %w", err) util.HandleError(c, http.StatusInternalServerError, retErr) diff --git a/api/admin/step.go b/api/admin/step.go index b2250b9ec..b29fc57e0 100644 --- a/api/admin/step.go +++ b/api/admin/step.go @@ -1,5 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 +//nolint:dupl // ignore similar code with user.go package admin import ( @@ -50,11 +51,14 @@ import ( // UpdateStep represents the API handler to update a step. func UpdateStep(c *gin.Context) { - logrus.Info("Admin: updating step in database") + // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) + ctx := c.Request.Context() + + l.Debug("platform admin: updating step") // capture body from API request input := new(library.Step) - ctx := c.Request.Context() err := c.Bind(input) if err != nil { @@ -65,6 +69,11 @@ func UpdateStep(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "step_id": input.GetID(), + "step": util.EscapeValue(input.GetName()), + }).Debug("platform admin: attempting to update step") + // send API call to update the step s, err := database.FromContext(c).UpdateStep(ctx, input) if err != nil { @@ -75,5 +84,10 @@ func UpdateStep(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "step_id": s.GetID(), + "step": s.GetName(), + }).Info("platform admin: updated step") + c.JSON(http.StatusOK, s) } diff --git a/api/admin/user.go b/api/admin/user.go index c8b0482a3..3f6d1b1ca 100644 --- a/api/admin/user.go +++ b/api/admin/user.go @@ -51,11 +51,12 @@ import ( // UpdateUser represents the API handler to update a user. func UpdateUser(c *gin.Context) { - logrus.Info("Admin: updating user in database") - // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() + l.Debug("platform admin: updating user") + // capture body from API request input := new(types.User) @@ -68,8 +69,13 @@ func UpdateUser(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "target_user_id": input.GetID(), + "target_user": util.EscapeValue(input.GetName()), + }).Debug("platform admin: attempting to update user") + // send API call to update the user - u, err := database.FromContext(c).UpdateUser(ctx, input) + tu, err := database.FromContext(c).UpdateUser(ctx, input) if err != nil { retErr := fmt.Errorf("unable to update user %d: %w", input.GetID(), err) @@ -78,5 +84,10 @@ func UpdateUser(c *gin.Context) { return } - c.JSON(http.StatusOK, u) + l.WithFields(logrus.Fields{ + "target_user_id": tu.GetID(), + "target_user": tu.GetName(), + }).Info("platform admin: updated user") + + c.JSON(http.StatusOK, tu) } diff --git a/api/admin/worker.go b/api/admin/worker.go index 36be236ac..59ed71a5e 100644 --- a/api/admin/worker.go +++ b/api/admin/worker.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/internal/token" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" @@ -48,11 +47,9 @@ import ( // RegisterToken represents the API handler to // generate a registration token for onboarding a worker. func RegisterToken(c *gin.Context) { - // retrieve user from context - u := user.Retrieve(c) - - logrus.Infof("Platform admin %s: generating registration token", u.GetName()) + l := c.MustGet("logger").(*logrus.Entry) + // capture middleware values host := util.PathParameter(c, "worker") tm := c.MustGet("token-manager").(*token.Manager) @@ -62,6 +59,8 @@ func RegisterToken(c *gin.Context) { TokenDuration: tm.WorkerRegisterTokenDuration, } + l.Debug("platform admin: generating worker registration token") + rt, err := tm.MintToken(rmto) if err != nil { retErr := fmt.Errorf("unable to generate registration token: %w", err) @@ -71,5 +70,7 @@ func RegisterToken(c *gin.Context) { return } + l.Infof("platform admin: generated worker registration token for %s", host) + c.JSON(http.StatusOK, library.Token{Token: &rt}) } diff --git a/api/auth/get_token.go b/api/auth/get_token.go index 83adef396..07df70b0c 100644 --- a/api/auth/get_token.go +++ b/api/auth/get_token.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -61,8 +62,9 @@ import ( func GetAuthToken(c *gin.Context) { var err error - tm := c.MustGet("token-manager").(*token.Manager) // capture middleware values + tm := c.MustGet("token-manager").(*token.Manager) + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() // capture the OAuth state if present @@ -123,7 +125,7 @@ func GetAuthToken(c *gin.Context) { u.SetRefreshToken(rt) // send API call to create the user in the database - _, err = database.FromContext(c).CreateUser(ctx, u) + ur, err := database.FromContext(c).CreateUser(ctx, u) if err != nil { retErr := fmt.Errorf("unable to create user %s: %w", u.GetName(), err) @@ -132,6 +134,11 @@ func GetAuthToken(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "user": ur.GetName(), + "user_id": ur.GetID(), + }).Info("new user created") + // return the jwt access token c.JSON(http.StatusOK, library.Token{Token: &at}) @@ -156,7 +163,7 @@ func GetAuthToken(c *gin.Context) { u.SetRefreshToken(rt) // send API call to update the user in the database - _, err = database.FromContext(c).UpdateUser(ctx, u) + ur, err := database.FromContext(c).UpdateUser(ctx, u) if err != nil { retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err) @@ -165,6 +172,11 @@ func GetAuthToken(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "user": ur.GetName(), + "user_id": ur.GetID(), + }).Info("user updated - new token") + // return the user with their jwt access token c.JSON(http.StatusOK, library.Token{Token: &at}) } diff --git a/api/auth/login.go b/api/auth/login.go index 05b975f8d..f2bf5adc5 100644 --- a/api/auth/login.go +++ b/api/auth/login.go @@ -40,6 +40,7 @@ import ( func Login(c *gin.Context) { // load the metadata m := c.MustGet("metadata").(*internal.Metadata) + l := c.MustGet("logger").(*logrus.Entry) // capture query params t := util.FormParameter(c, "type") @@ -51,18 +52,20 @@ func Login(c *gin.Context) { // default path (headless mode) path := "/authenticate" + l.Info("logging in user") + // handle web and cli logins switch t { case "web": r = fmt.Sprintf("%s/authenticate/%s", m.Vela.Address, t) - logrus.Debugf("web login request, setting redirect to: %s", r) + l.Infof("web login request, setting redirect to: %s", r) case "cli": // port must be supplied if len(p) > 0 { r = fmt.Sprintf("%s/authenticate/%s/%s", m.Vela.Address, t, p) - logrus.Debugf("cli login request, setting redirect to: %s", r) + l.Infof("cli login request, setting redirect to: %s", r) } } diff --git a/api/auth/logout.go b/api/auth/logout.go index 8c0af25fa..9acb575cd 100644 --- a/api/auth/logout.go +++ b/api/auth/logout.go @@ -45,25 +45,19 @@ import ( func Logout(c *gin.Context) { // grab the metadata to help deal with the cookie m := c.MustGet("metadata").(*internal.Metadata) + l := c.MustGet("logger").(*logrus.Entry) // capture middleware values u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }) - - logger.Infof("logging out user %s", u.GetName()) + l.Debug("logging out user") // parse the address for the backend server // so we can set it for the cookie domain addr, err := url.Parse(m.Vela.Address) if err != nil { // silently fail - logger.Error("unable to parse Vela address during logout") + l.Error("unable to parse Vela address during logout") } // set the same samesite attribute we used to create the cookie @@ -86,6 +80,8 @@ func Logout(c *gin.Context) { return } + l.Info("updated user - logged out") + // return 200 for successful logout c.JSON(http.StatusOK, "ok") } diff --git a/api/auth/post_token.go b/api/auth/post_token.go index 929ef17ca..d5fc345c9 100644 --- a/api/auth/post_token.go +++ b/api/auth/post_token.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/go-vela/server/database" "github.com/go-vela/server/internal/token" @@ -48,6 +49,7 @@ import ( // a user logging in using PAT to Vela from the API. func PostAuthToken(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() // attempt to get user from source @@ -60,16 +62,23 @@ func PostAuthToken(c *gin.Context) { return } + l.Infof("SCM user %s authenticated using PAT", u.GetName()) + // check if the user exists u, err = database.FromContext(c).GetUserForName(ctx, u.GetName()) if err != nil { - retErr := fmt.Errorf("user %s not found", u.GetName()) + retErr := fmt.Errorf("unable to authenticate: user %s not found", u.GetName()) util.HandleError(c, http.StatusUnauthorized, retErr) return } + l.WithFields(logrus.Fields{ + "user": u.GetName(), + "user_id": u.GetID(), + }).Info("user successfully authenticated via SCM PAT") + // We don't need refresh token for this scenario // We only need access token and are configured based on the config defined tm := c.MustGet("token-manager").(*token.Manager) @@ -80,14 +89,14 @@ func PostAuthToken(c *gin.Context) { TokenType: constants.UserAccessTokenType, TokenDuration: tm.UserAccessTokenDuration, } - at, err := tm.MintToken(amto) + at, err := tm.MintToken(amto) if err != nil { retErr := fmt.Errorf("unable to compose token for user %s: %w", u.GetName(), err) util.HandleError(c, http.StatusServiceUnavailable, retErr) } - // return the user with their jwt access token + // return jwt access token c.JSON(http.StatusOK, library.Token{Token: &at}) } diff --git a/api/auth/redirect.go b/api/auth/redirect.go index 7837ca07e..83022abc5 100644 --- a/api/auth/redirect.go +++ b/api/auth/redirect.go @@ -70,8 +70,9 @@ import ( func GetAuthRedirect(c *gin.Context) { // load the metadata m := c.MustGet("metadata").(*internal.Metadata) + l := c.MustGet("logger").(*logrus.Entry) - logrus.Info("redirecting for final auth flow destination") + l.Debug("redirecting for final auth flow destination") // capture the path elements t := util.PathParameter(c, "type") diff --git a/api/auth/refresh.go b/api/auth/refresh.go index 0dc10b7e7..69d61bb1e 100644 --- a/api/auth/refresh.go +++ b/api/auth/refresh.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/go-vela/server/internal/token" "github.com/go-vela/server/router/middleware/auth" @@ -36,6 +37,10 @@ import ( // RefreshAccessToken will return a new access token if the provided // refresh token via cookie is valid. func RefreshAccessToken(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) + + l.Info("refreshing access token") + // capture the refresh token // TODO: move this into token package and do it internally // since we are already passsing context diff --git a/api/auth/validate.go b/api/auth/validate.go index 3282baafb..40bab2c38 100644 --- a/api/auth/validate.go +++ b/api/auth/validate.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/go-vela/server/router/middleware/claims" "github.com/go-vela/server/util" @@ -35,8 +36,11 @@ import ( // ValidateServerToken will validate if a token was issued by the server // if it is provided in the auth header. func ValidateServerToken(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) + l.Info("validating server token") + if !strings.EqualFold(cl.Subject, "vela-server") { retErr := fmt.Errorf("token is not a valid server token") diff --git a/api/auth/validate_oauth.go b/api/auth/validate_oauth.go index b96918325..25325a353 100644 --- a/api/auth/validate_oauth.go +++ b/api/auth/validate_oauth.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" @@ -40,8 +41,11 @@ import ( // validate that a user OAuth token was created by Vela. func ValidateOAuthToken(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() + l.Info("validating oauth token") + token := c.Request.Header.Get("Token") if len(token) == 0 { retErr := fmt.Errorf("unable to validate oauth token: no token provided in header") diff --git a/api/badge.go b/api/badge.go index c926c64f3..9aed49600 100644 --- a/api/badge.go +++ b/api/badge.go @@ -9,7 +9,6 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" @@ -51,19 +50,13 @@ import ( // return a build status badge. func GetBadge(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) ctx := c.Request.Context() branch := util.QueryParameter(c, "branch", r.GetBranch()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - }).Infof("creating latest build badge for repo %s on branch %s", r.GetFullName(), branch) + l.Debugf("creating latest build badge for repo %s on branch %s", r.GetFullName(), branch) // send API call to capture the last build for the repo and branch b, err := database.FromContext(c).LastBuildForRepo(ctx, r, branch) diff --git a/api/build/approve.go b/api/build/approve.go index cc7159d53..86c491c04 100644 --- a/api/build/approve.go +++ b/api/build/approve.go @@ -15,7 +15,6 @@ import ( "github.com/go-vela/server/queue" "github.com/go-vela/server/queue/models" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" @@ -72,20 +71,13 @@ import ( // ApproveBuild represents the API handler to approve a build to run. func ApproveBuild(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) + l.Debugf("approving build %d", b.GetID()) // verify build is in correct status if !strings.EqualFold(b.GetStatus(), constants.StatusPendingApproval) { @@ -103,8 +95,6 @@ func ApproveBuild(c *gin.Context) { return } - logger.Debugf("user %s approved build %s/%d for execution", u.GetName(), r.GetFullName(), b.GetNumber()) - // set fields b.SetStatus(constants.StatusPending) b.SetApprovedAt(time.Now().Unix()) @@ -113,9 +103,11 @@ func ApproveBuild(c *gin.Context) { // update the build in the db _, err := database.FromContext(c).UpdateBuild(ctx, b) if err != nil { - logrus.Errorf("Failed to update build %d during publish to queue for %s: %v", b.GetNumber(), r.GetFullName(), err) + l.Errorf("failed to update build during publish to queue: %v", err) } + l.Info("build updated - user approved build execution") + // publish the build to the queue go Enqueue( ctx, diff --git a/api/build/auto_cancel.go b/api/build/auto_cancel.go index 62b2922a1..d1ba091fe 100644 --- a/api/build/auto_cancel.go +++ b/api/build/auto_cancel.go @@ -12,6 +12,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -23,6 +24,17 @@ import ( // AutoCancel is a helper function that checks to see if any pending or running // builds for the repo can be replaced by the current build. func AutoCancel(c *gin.Context, b *types.Build, rB *types.Build, cancelOpts *pipeline.CancelOptions) (bool, error) { + l := c.MustGet("logger").(*logrus.Entry) + + // in this path, the middleware doesn't inject build, + // so we need to set it manually + l = l.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + }) + + l.Debug("checking if builds should be auto canceled") + // if build is the current build, continue if rB.GetID() == b.GetID() { return false, nil @@ -44,6 +56,11 @@ func AutoCancel(c *gin.Context, b *types.Build, rB *types.Build, cancelOpts *pip return false, err } + l.WithFields(logrus.Fields{ + "build": rB.GetNumber(), + "build_id": rB.GetID(), + }).Info("build updated - build canceled") + // remove executable from table _, err = database.FromContext(c).PopBuildExecutable(c, rB.GetID()) if err != nil { @@ -67,6 +84,11 @@ func AutoCancel(c *gin.Context, b *types.Build, rB *types.Build, cancelOpts *pip // if this call fails, we still canceled the build, so return true return true, err } + + l.WithFields(logrus.Fields{ + "build": rB.GetNumber(), + "build_id": rB.GetID(), + }).Info("build updated - build canceled") } return true, nil @@ -75,6 +97,8 @@ func AutoCancel(c *gin.Context, b *types.Build, rB *types.Build, cancelOpts *pip // cancelRunning is a helper function that determines the executor currently running a build and sends an API call // to that executor's worker to cancel the build. func cancelRunning(c *gin.Context, b *types.Build) error { + l := c.MustGet("logger").(*logrus.Entry) + e := new([]types.Executor) // retrieve the worker w, err := database.FromContext(c).GetWorkerForHostname(c, b.GetHost()) @@ -170,6 +194,8 @@ func cancelRunning(c *gin.Context, b *types.Build) error { } defer resp.Body.Close() + l.Debugf("sent cancel request to worker %s (executor %d) for build %d", w.GetHostname(), executor.GetID(), b.GetID()) + // Read Response Body respBody, err := io.ReadAll(resp.Body) if err != nil { diff --git a/api/build/cancel.go b/api/build/cancel.go index e4ee0b84e..79d186799 100644 --- a/api/build/cancel.go +++ b/api/build/cancel.go @@ -17,7 +17,6 @@ import ( "github.com/go-vela/server/internal/token" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/executors" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" @@ -76,24 +75,16 @@ import ( //nolint:funlen // ignore statement count func CancelBuild(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) e := executors.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) user := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": user.GetName(), - }).Infof("canceling build %s", entry) + l.Debugf("canceling build %s", entry) switch b.GetStatus() { case constants.StatusRunning: @@ -182,6 +173,11 @@ func CancelBuild(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + }).Info("build updated - build canceled") + c.JSON(resp.StatusCode, b) return @@ -211,6 +207,11 @@ func CancelBuild(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + }).Info("build updated - build canceled") + // remove build executable for clean up _, err = database.FromContext(c).PopBuildExecutable(ctx, b.GetID()) if err != nil { diff --git a/api/build/clean.go b/api/build/clean.go index 8473c25e1..5c70b5164 100644 --- a/api/build/clean.go +++ b/api/build/clean.go @@ -19,6 +19,16 @@ import ( // without execution. This will kill all resources, // like steps and services, for the build. func CleanBuild(ctx context.Context, database database.Interface, b *types.Build, services []*library.Service, steps []*library.Step, e error) { + l := logrus.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + "org": b.GetRepo().GetOrg(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), + }) + + l.Debug("cleaning build") + // update fields in build object b.SetError(fmt.Sprintf("unable to publish to queue: %s", e.Error())) b.SetStatus(constants.StatusError) @@ -27,9 +37,11 @@ func CleanBuild(ctx context.Context, database database.Interface, b *types.Build // send API call to update the build b, err := database.UpdateBuild(ctx, b) if err != nil { - logrus.Errorf("unable to kill build %d: %v", b.GetNumber(), err) + l.Errorf("unable to kill build %d: %v", b.GetNumber(), err) } + l.Info("build updated - build cleaned") + for _, s := range services { // update fields in service object s.SetStatus(constants.StatusKilled) @@ -38,8 +50,13 @@ func CleanBuild(ctx context.Context, database database.Interface, b *types.Build // send API call to update the service _, err := database.UpdateService(ctx, s) if err != nil { - logrus.Errorf("unable to kill service %s for build %d: %v", s.GetName(), b.GetNumber(), err) + l.Errorf("unable to kill service %s for build %d: %v", s.GetName(), b.GetNumber(), err) } + + l.WithFields(logrus.Fields{ + "service": s.GetName(), + "service_id": s.GetID(), + }).Info("service updated - service cleaned") } for _, s := range steps { @@ -50,7 +67,12 @@ func CleanBuild(ctx context.Context, database database.Interface, b *types.Build // send API call to update the step _, err := database.UpdateStep(ctx, s) if err != nil { - logrus.Errorf("unable to kill step %s for build %d: %v", s.GetName(), b.GetNumber(), err) + l.Errorf("unable to kill step %s for build %d: %v", s.GetName(), b.GetNumber(), err) } + + l.WithFields(logrus.Fields{ + "step": s.GetName(), + "step_id": s.GetID(), + }).Info("step updated - step cleaned") } } diff --git a/api/build/compile_publish.go b/api/build/compile_publish.go index 29f2cdac2..a4e458c7a 100644 --- a/api/build/compile_publish.go +++ b/api/build/compile_publish.go @@ -49,7 +49,15 @@ func CompileAndPublish( compiler compiler.Engine, queue queue.Service, ) (*pipeline.Build, *models.Item, int, error) { - logrus.Debugf("generating queue items for build %s/%d", cfg.Build.GetRepo().GetFullName(), cfg.Build.GetNumber()) + logger := logrus.WithFields(logrus.Fields{ + "org": cfg.Build.GetRepo().GetOrg(), + "repo": cfg.Build.GetRepo().GetName(), + "repo_id": cfg.Build.GetRepo().GetID(), + "build": cfg.Build.GetNumber(), + "build_id": cfg.Build.GetID(), + }) + + logger.Debug("generating queue items") // assign variables from form for readibility r := cfg.Build.GetRepo() @@ -118,7 +126,7 @@ func CompileAndPublish( return nil, nil, http.StatusInternalServerError, retErr } - logrus.Debugf("currently %d builds running on repo %s", builds, r.GetFullName()) + logger.Debugf("currently %d builds running on repo %s", builds, r.GetFullName()) // check if the number of pending and running builds exceeds the limit for the repo if builds >= r.GetBuildLimit() { @@ -186,7 +194,7 @@ func CompileAndPublish( // failing to successfully process the request. This logic ensures we attempt our // best efforts to handle these cases gracefully. for i := 0; i < cfg.Retries; i++ { - logrus.Debugf("compilation loop - attempt %d", i+1) + logger.Debugf("compilation loop - attempt %d", i+1) // check if we're on the first iteration of the loop if i > 0 { // incrementally sleep in between retries @@ -214,7 +222,7 @@ func CompileAndPublish( // check if the retry limit has been exceeded if i < cfg.Retries-1 { - logrus.WithError(retErr).Warningf("retrying #%d", i+1) + logger.WithError(retErr).Warningf("retrying #%d", i+1) // continue to the next iteration of the loop continue @@ -267,7 +275,7 @@ func CompileAndPublish( err = fmt.Errorf("unable to compile pipeline configuration for %s: %w", repo.GetFullName(), err) // log the error for traceability - logrus.Error(err.Error()) + logger.Error(err.Error()) return nil, nil, http.StatusInternalServerError, fmt.Errorf("%s: %w", baseErr, err) } @@ -289,7 +297,7 @@ func CompileAndPublish( // send API call to set the status on the commit err = scm.Status(c, u, b, repo.GetOrg(), repo.GetName()) if err != nil { - logrus.Errorf("unable to set commit status for %s/%d: %v", repo.GetFullName(), b.GetNumber(), err) + logger.Errorf("unable to set commit status for %s/%d: %v", repo.GetFullName(), b.GetNumber(), err) } return nil, @@ -314,7 +322,7 @@ func CompileAndPublish( // check if the retry limit has been exceeded if i < cfg.Retries-1 { - logrus.WithError(retErr).Warningf("retrying #%d", i+1) + logger.WithError(retErr).Warningf("retrying #%d", i+1) // continue to the next iteration of the loop continue @@ -322,6 +330,13 @@ func CompileAndPublish( return nil, nil, http.StatusInternalServerError, retErr } + + logger.WithFields(logrus.Fields{ + "pipeline": pipeline.GetID(), + "org": repo.GetOrg(), + "repo": repo.GetName(), + "repo_id": repo.GetID(), + }).Info("pipeline created") } b.SetPipelineID(pipeline.GetID()) @@ -339,7 +354,7 @@ func CompileAndPublish( // check if the retry limit has been exceeded if i < cfg.Retries-1 { - logrus.WithError(retErr).Warningf("retrying #%d", i+1) + logger.WithError(retErr).Warningf("retrying #%d", i+1) // reset fields set by cleanBuild for retry b.SetError("") @@ -365,6 +380,12 @@ func CompileAndPublish( return nil, nil, http.StatusInternalServerError, retErr } + logger.WithFields(logrus.Fields{ + "org": repo.GetOrg(), + "repo": repo.GetName(), + "repo_id": repo.GetID(), + }).Info("repo updated - counter incremented") + // return error if pipeline didn't get populated if p == nil { retErr := fmt.Errorf("%s: failed to set pipeline for %s: %w", baseErr, repo.GetFullName(), err) diff --git a/api/build/create.go b/api/build/create.go index e3745cfeb..8fef779c7 100644 --- a/api/build/create.go +++ b/api/build/create.go @@ -14,9 +14,7 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/internal" "github.com/go-vela/server/queue" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" ) @@ -82,21 +80,11 @@ import ( func CreateBuild(c *gin.Context) { // capture middleware values m := c.MustGet("metadata").(*internal.Metadata) - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) - - logger.Infof("creating new build for repo %s", r.GetFullName()) + l.Debugf("creating new build for repo %s", r.GetFullName()) // capture body from API request input := new(types.Build) @@ -152,6 +140,11 @@ func CreateBuild(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "build": item.Build.GetNumber(), + "build_id": item.Build.GetID(), + }).Info("build created") + c.JSON(http.StatusCreated, item.Build) // publish the build to the queue diff --git a/api/build/delete.go b/api/build/delete.go index e4330ecc2..c7824e3f7 100644 --- a/api/build/delete.go +++ b/api/build/delete.go @@ -11,9 +11,7 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -68,23 +66,14 @@ import ( // a build for a repo. func DeleteBuild(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("deleting build %s", entry) + l.Debugf("deleting build %s", entry) // send API call to remove the build err := database.FromContext(c).DeleteBuild(ctx, b) diff --git a/api/build/enqueue.go b/api/build/enqueue.go index 297a974f5..aa87f4473 100644 --- a/api/build/enqueue.go +++ b/api/build/enqueue.go @@ -16,11 +16,19 @@ import ( // Enqueue is a helper function that pushes a queue item (build, repo, user) to the queue. func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, item *models.Item, route string) { - logrus.Infof("Converting queue item to json for build %d for %s", item.Build.GetNumber(), item.Build.GetRepo().GetFullName()) + l := logrus.WithFields(logrus.Fields{ + "build": item.Build.GetNumber(), + "build_id": item.Build.GetID(), + "org": item.Build.GetRepo().GetOrg(), + "repo": item.Build.GetRepo().GetName(), + "repo_id": item.Build.GetRepo().GetID(), + }) + + l.Debug("adding item to queue") byteItem, err := json.Marshal(item) if err != nil { - logrus.Errorf("Failed to convert item to json for build %d for %s: %v", item.Build.GetNumber(), item.Build.GetRepo().GetFullName(), err) + l.Errorf("failed to convert item to json: %v", err) // error out the build CleanBuild(ctx, db, item.Build, nil, nil, err) @@ -28,16 +36,16 @@ func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, it return } - logrus.Infof("Pushing item for build %d for %s to queue route %s", item.Build.GetNumber(), item.Build.GetRepo().GetFullName(), route) + l.Debugf("pushing item for build to queue route %s", route) // push item on to the queue err = queue.Push(context.Background(), route, byteItem) if err != nil { - logrus.Errorf("Retrying; Failed to publish build %d for %s: %v", item.Build.GetNumber(), item.Build.GetRepo().GetFullName(), err) + l.Errorf("retrying; failed to publish build: %v", err) err = queue.Push(context.Background(), route, byteItem) if err != nil { - logrus.Errorf("Failed to publish build %d for %s: %v", item.Build.GetNumber(), item.Build.GetRepo().GetFullName(), err) + l.Errorf("failed to publish build: %v", err) // error out the build CleanBuild(ctx, db, item.Build, nil, nil, err) @@ -52,6 +60,8 @@ func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, it // update the build in the db to reflect the time it was enqueued _, err = db.UpdateBuild(ctx, item.Build) if err != nil { - logrus.Errorf("Failed to update build %d during publish to queue for %s: %v", item.Build.GetNumber(), item.Build.GetRepo().GetFullName(), err) + l.Errorf("failed to update build during publish to queue: %v", err) } + + l.Info("updated build as enqueued") } diff --git a/api/build/executable.go b/api/build/executable.go index a5d250cbc..19f66c3a6 100644 --- a/api/build/executable.go +++ b/api/build/executable.go @@ -14,8 +14,6 @@ import ( "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/claims" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/util" "github.com/go-vela/types/library" @@ -74,21 +72,12 @@ import ( // a build executable for a repository. func GetBuildExecutable(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) - cl := claims.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "subject": cl.Subject, - }).Infof("reading build executable %s/%d", r.GetFullName(), b.GetNumber()) + l.Debugf("reading build executable %s/%d", r.GetFullName(), b.GetNumber()) // send database call to pop the requested build executable from the table bExecutable, err := database.FromContext(c).PopBuildExecutable(ctx, b.GetID()) @@ -108,7 +97,7 @@ func PublishBuildExecutable(ctx context.Context, db database.Interface, p *pipel // marshal pipeline build into byte data to add to the build executable object byteExecutable, err := json.Marshal(p) if err != nil { - logrus.Errorf("Failed to marshal build executable: %v", err) + logrus.Errorf("failed to marshal build executable: %v", err) // error out the build CleanBuild(ctx, db, b, nil, nil, err) @@ -124,7 +113,7 @@ func PublishBuildExecutable(ctx context.Context, db database.Interface, p *pipel // send database call to create a build executable err = db.CreateBuildExecutable(ctx, bExecutable) if err != nil { - logrus.Errorf("Failed to publish build executable to database: %v", err) + logrus.Errorf("failed to publish build executable to database: %v", err) // error out the build CleanBuild(ctx, db, b, nil, nil, err) @@ -132,5 +121,9 @@ func PublishBuildExecutable(ctx context.Context, db database.Interface, p *pipel return err } + logrus.WithFields(logrus.Fields{ + "build_executable_id": bExecutable.GetBuildID(), + }).Info("created build executable") + return nil } diff --git a/api/build/get.go b/api/build/get.go index df8c87bfd..773cec777 100644 --- a/api/build/get.go +++ b/api/build/get.go @@ -9,9 +9,7 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" ) // swagger:operation GET /api/v1/repos/{org}/{repo}/builds/{build} builds GetBuild @@ -62,20 +60,11 @@ import ( // a build for a repository. func GetBuild(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("reading build %s/%d", r.GetFullName(), b.GetNumber()) + l.Debugf("reading build %s/%d", r.GetFullName(), b.GetNumber()) c.JSON(http.StatusOK, b) } diff --git a/api/build/get_id.go b/api/build/get_id.go index f27fde5f2..2a9e4201e 100644 --- a/api/build/get_id.go +++ b/api/build/get_id.go @@ -53,6 +53,7 @@ import ( // build by its id. func GetBuildByID(c *gin.Context) { // Capture user from middleware + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) ctx := c.Request.Context() @@ -66,13 +67,7 @@ func GetBuildByID(c *gin.Context) { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": id, - "user": u.GetName(), - }).Infof("reading build %d", id) + l.Debugf("reading build %d", id) // Get build from database b, err := database.FromContext(c).GetBuild(ctx, id) @@ -88,7 +83,7 @@ func GetBuildByID(c *gin.Context) { // just retrieving any build using a random id number. perm, err := scm.FromContext(c).RepoAccess(ctx, u.GetName(), u.GetToken(), b.GetRepo().GetOrg(), b.GetRepo().GetName()) if err != nil { - logrus.Errorf("unable to get user %s access level for repo %s", u.GetName(), b.GetRepo().GetFullName()) + l.Errorf("unable to get user %s access level for repo %s", u.GetName(), b.GetRepo().GetFullName()) } // Ensure that user has at least read access to repo to return the build diff --git a/api/build/graph.go b/api/build/graph.go index e7207625c..5fb3ea1ee 100644 --- a/api/build/graph.go +++ b/api/build/graph.go @@ -15,7 +15,6 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/internal" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -143,29 +142,18 @@ const ( //nolint:funlen,goconst,gocyclo // ignore function length and constants func GetBuildGraph(c *gin.Context) { // capture middleware values + m := c.MustGet("metadata").(*internal.Metadata) + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) u := user.Retrieve(c) - m := c.MustGet("metadata").(*internal.Metadata) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber()) - logger := logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) baseErr := "unable to retrieve graph" - logger.Infof("constructing graph for build %s", entry) - - logger.Info("retrieving pipeline configuration") + l.Debugf("constructing graph for build %s and retrieving pipeline configuration", entry) var config []byte @@ -210,7 +198,7 @@ func GetBuildGraph(c *gin.Context) { } } - logger.Info("compiling pipeline configuration") + l.Debug("compiling pipeline configuration") // parse and compile the pipeline configuration file p, _, err := compiler.FromContext(c). @@ -226,7 +214,7 @@ func GetBuildGraph(c *gin.Context) { // format the error message with extra information err = fmt.Errorf("unable to compile pipeline configuration for %s: %w", r.GetFullName(), err) - logger.Error(err.Error()) + l.Error(err.Error()) retErr := fmt.Errorf("%s: %w", baseErr, err) @@ -238,7 +226,7 @@ func GetBuildGraph(c *gin.Context) { if p == nil { retErr := fmt.Errorf("unable to compile pipeline configuration for %s: pipeline is nil", r.GetFullName()) - logger.Error(retErr) + l.Error(retErr) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -331,7 +319,7 @@ func GetBuildGraph(c *gin.Context) { return } - logger.Info("generating build graph") + l.Debug("generating build graph") // create nodes from pipeline stages nodes := make(map[int]*node) diff --git a/api/build/id_request_token.go b/api/build/id_request_token.go index 22e4d117c..77869a44c 100644 --- a/api/build/id_request_token.go +++ b/api/build/id_request_token.go @@ -15,7 +15,6 @@ import ( "github.com/go-vela/server/constants" "github.com/go-vela/server/internal/token" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/claims" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -82,18 +81,10 @@ import ( // GetIDRequestToken represents the API handler to generate and return an ID request token. func GetIDRequestToken(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - cl := claims.Retrieve(c) - - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": b.GetRepo().GetOrg(), - "repo": b.GetRepo().GetName(), - "user": cl.Subject, - }).Infof("generating ID request token for build %s/%d", b.GetRepo().GetFullName(), b.GetNumber()) + + l.Infof("generating ID request token for build %s/%d", b.GetRepo().GetFullName(), b.GetNumber()) image := c.Query("image") if len(image) == 0 { diff --git a/api/build/id_token.go b/api/build/id_token.go index 54744e983..e049dcebd 100644 --- a/api/build/id_token.go +++ b/api/build/id_token.go @@ -75,19 +75,12 @@ import ( // GetIDToken represents the API handler to generate a id token. func GetIDToken(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) cl := claims.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": b.GetRepo().GetOrg(), - "repo": b.GetRepo().GetName(), - "subject": cl.Subject, - }).Infof("generating ID token for build %s/%d", b.GetRepo().GetFullName(), b.GetNumber()) + l.Infof("generating ID token for build %s/%d", b.GetRepo().GetFullName(), b.GetNumber()) // retrieve token manager from context tm := c.MustGet("token-manager").(*token.Manager) diff --git a/api/build/list_org.go b/api/build/list_org.go index ff516c39c..339baa0df 100644 --- a/api/build/list_org.go +++ b/api/build/list_org.go @@ -111,17 +111,12 @@ func ListBuildsForOrg(c *gin.Context) { ) // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) o := org.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "user": u.GetName(), - }).Infof("listing builds for org %s", o) + l.Debugf("listing builds for org %s", o) // capture the branch name parameter branch := c.Query("branch") @@ -196,7 +191,7 @@ func ListBuildsForOrg(c *gin.Context) { // See if the user is an org admin to bypass individual permission checks perm, err := scm.FromContext(c).OrgAccess(ctx, u, o) if err != nil { - logrus.Errorf("unable to get user %s access level for org %s", u.GetName(), o) + l.Errorf("unable to get user %s access level for org %s", u.GetName(), o) } // Only show public repos to non-admins if perm != "admin" { diff --git a/api/build/list_repo.go b/api/build/list_repo.go index f631e68ef..b4771eefe 100644 --- a/api/build/list_repo.go +++ b/api/build/list_repo.go @@ -14,9 +14,7 @@ import ( "github.com/go-vela/server/api" "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" ) @@ -135,19 +133,11 @@ func ListBuildsForRepo(c *gin.Context) { ) // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("listing builds for repo %s", r.GetFullName()) + l.Debugf("listing builds for repo %s", r.GetFullName()) // capture the branch name parameter branch := c.Query("branch") diff --git a/api/build/plan.go b/api/build/plan.go index f7c23b971..fcdcd2dff 100644 --- a/api/build/plan.go +++ b/api/build/plan.go @@ -7,6 +7,8 @@ import ( "fmt" "time" + "github.com/sirupsen/logrus" + "github.com/go-vela/server/api/service" "github.com/go-vela/server/api/step" "github.com/go-vela/server/api/types" @@ -40,6 +42,11 @@ func PlanBuild(ctx context.Context, database database.Interface, scm scm.Service return fmt.Errorf("unable to create new build for %s: %w", r.GetFullName(), err) } + logrus.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + }).Info("build created") + // plan all services for the build services, err := service.PlanServices(ctx, database, p, b) if err != nil { diff --git a/api/build/restart.go b/api/build/restart.go index 4cf0a21e0..aed5fccd4 100644 --- a/api/build/restart.go +++ b/api/build/restart.go @@ -16,7 +16,6 @@ import ( "github.com/go-vela/server/queue" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/claims" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -84,9 +83,9 @@ import ( func RestartBuild(c *gin.Context) { // capture middleware values m := c.MustGet("metadata").(*internal.Metadata) + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) u := user.Retrieve(c) scm := scm.FromContext(c) @@ -94,15 +93,7 @@ func RestartBuild(c *gin.Context) { entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) + l.Debugf("restarting build %d", b.GetNumber()) // a build that is in a pending approval state cannot be restarted if strings.EqualFold(b.GetStatus(), constants.StatusPendingApproval) { @@ -131,7 +122,7 @@ func RestartBuild(c *gin.Context) { // parent to the previous build b.SetParent(b.GetNumber()) - logger.Debugf("Generating queue items for build %s", entry) + l.Debugf("generating queue items for build %s", entry) // restart form config := CompileAndPublishConfig{ @@ -151,13 +142,17 @@ func RestartBuild(c *gin.Context) { compiler.FromContext(c), queue.FromContext(c), ) - if err != nil { util.HandleError(c, code, err) return } + l.WithFields(logrus.Fields{ + "new_build": item.Build.GetNumber(), + "new_build_id": item.Build.GetID(), + }).Info("build created via restart") + c.JSON(http.StatusCreated, item.Build) // publish the build to the queue diff --git a/api/build/token.go b/api/build/token.go index d58a60bc5..b2126bb84 100644 --- a/api/build/token.go +++ b/api/build/token.go @@ -14,7 +14,6 @@ import ( "github.com/go-vela/server/internal/token" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/claims" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" @@ -71,20 +70,12 @@ import ( // GetBuildToken represents the API handler to generate a build token. func GetBuildToken(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) cl := claims.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": cl.Subject, - }).Infof("generating build token for build %s/%d", r.GetFullName(), b.GetNumber()) + l.Infof("generating build token for build %s/%d", r.GetFullName(), b.GetNumber()) // if build is not in a pending state, then a build token should not be needed - conflict if !strings.EqualFold(b.GetStatus(), constants.StatusPending) { diff --git a/api/build/update.go b/api/build/update.go index e5e3385a0..eabe94981 100644 --- a/api/build/update.go +++ b/api/build/update.go @@ -12,8 +12,6 @@ import ( "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/claims" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" @@ -78,23 +76,14 @@ import ( // a build for a repo. func UpdateBuild(c *gin.Context) { // capture middleware values - cl := claims.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": cl.Subject, - }).Infof("updating build %s", entry) + l.Debugf("updating build %s", entry) // capture body from API request input := new(types.Build) @@ -181,15 +170,26 @@ func UpdateBuild(c *gin.Context) { // send API call to set the status on the commit err = scm.FromContext(c).Status(ctx, r.GetOwner(), b, r.GetOrg(), r.GetName()) if err != nil { - logrus.Errorf("unable to set commit status for build %s: %v", entry, err) + l.Errorf("unable to set commit status for build %s: %v", entry, err) } } } // UpdateComponentStatuses updates all components (steps and services) for a build to a given status. func UpdateComponentStatuses(c *gin.Context, b *types.Build, status string) error { + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() + l = l.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + "org": b.GetRepo().GetOrg(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), + }) + + l.Debug("updating component statuses") + // retrieve the steps for the build from the step table steps := []*library.Step{} page := 1 @@ -222,6 +222,11 @@ func UpdateComponentStatuses(c *gin.Context, b *types.Build, status string) erro if err != nil { return err } + + l.WithFields(logrus.Fields{ + "step": step.GetNumber(), + "step_id": step.GetID(), + }).Infof("step status updated") } // retrieve the services for the build from the service table @@ -255,6 +260,11 @@ func UpdateComponentStatuses(c *gin.Context, b *types.Build, status string) erro if err != nil { return err } + + l.WithFields(logrus.Fields{ + "service": service.GetNumber(), + "service_id": service.GetID(), + }).Info("service status updated") } return nil diff --git a/api/dashboard/create.go b/api/dashboard/create.go index 38cb95115..104a59074 100644 --- a/api/dashboard/create.go +++ b/api/dashboard/create.go @@ -56,6 +56,7 @@ import ( // create a dashboard. func CreateDashboard(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) // capture body from API request @@ -77,12 +78,7 @@ func CreateDashboard(c *gin.Context) { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("creating new dashboard %s", input.GetName()) + l.Debugf("creating new dashboard %s", input.GetName()) d := new(types.Dashboard) @@ -123,6 +119,11 @@ func CreateDashboard(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "dashboard": d.GetName(), + "dashboard_id": d.GetID(), + }).Info("dashboard created") + // add dashboard to claims' user's dashboard set u.SetDashboards(append(u.GetDashboards(), d.GetID())) @@ -136,6 +137,8 @@ func CreateDashboard(c *gin.Context) { return } + l.Infof("user updated with new dashboard %s", d.GetName()) + c.JSON(http.StatusCreated, d) } diff --git a/api/dashboard/delete.go b/api/dashboard/delete.go index bf921abe1..05b14e86b 100644 --- a/api/dashboard/delete.go +++ b/api/dashboard/delete.go @@ -56,16 +56,11 @@ import ( // DeleteDashboard represents the API handler to remove a dashboard. func DeleteDashboard(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) d := dashboard.Retrieve(c) u := user.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "dashboard": d.GetID(), - "user": u.GetName(), - }).Infof("deleting dashboard %s", d.GetID()) + l.Debugf("deleting dashboard %s", d.GetID()) if !isAdmin(d, u) { retErr := fmt.Errorf("unable to delete dashboard %s: user is not an admin", d.GetID()) diff --git a/api/dashboard/get.go b/api/dashboard/get.go index 4cc34e93d..c009e4127 100644 --- a/api/dashboard/get.go +++ b/api/dashboard/get.go @@ -13,7 +13,6 @@ import ( "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/dashboard" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -59,18 +58,12 @@ import ( // a dashboard for a repository. func GetDashboard(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) d := dashboard.Retrieve(c) - u := user.Retrieve(c) var err error - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "dashboard": d.GetID(), - "user": u.GetName(), - }).Infof("reading dashboard %s", d.GetID()) + l.Debugf("reading dashboard %s", d.GetID()) // initialize DashCard and set dashboard to the dashboard info pulled from database dashboard := new(types.DashCard) diff --git a/api/dashboard/list_user.go b/api/dashboard/list_user.go index c43a89f05..d2e8e300a 100644 --- a/api/dashboard/list_user.go +++ b/api/dashboard/list_user.go @@ -29,9 +29,10 @@ import ( // responses: // '200': // description: Successfully retrieved user dashboards -// type: json // schema: -// "$ref": "#/definitions/Dashboard" +// type: array +// items: +// "$ref": "#/definitions/DashCard" // '400': // description: Invalid request payload // schema: @@ -49,14 +50,10 @@ import ( // of dashboards for a user. func ListUserDashboards(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("listing dashboards for user %s", u.GetName()) + l.Debugf("listing dashboards for user %s", u.GetName()) var dashCards []types.DashCard diff --git a/api/dashboard/update.go b/api/dashboard/update.go index db11d4355..aec6affa4 100644 --- a/api/dashboard/update.go +++ b/api/dashboard/update.go @@ -62,9 +62,12 @@ import ( // UpdateDashboard represents the API handler to update a dashboard. func UpdateDashboard(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) d := dashboard.Retrieve(c) u := user.Retrieve(c) + l.Debugf("updating dashboard %s", d.GetID()) + if !isAdmin(d, u) { retErr := fmt.Errorf("unable to update dashboard %s: user is not an admin", d.GetID()) @@ -73,13 +76,6 @@ func UpdateDashboard(c *gin.Context) { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "dashboard": d.GetID(), - }).Infof("updating dashboard %s", d.GetID()) - // capture body from API request input := new(types.Dashboard) diff --git a/api/deployment/create.go b/api/deployment/create.go index b596f75be..fa980e5e1 100644 --- a/api/deployment/create.go +++ b/api/deployment/create.go @@ -11,7 +11,6 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -65,19 +64,12 @@ import ( // create a deployment. func CreateDeployment(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("creating new deployment for repo %s", r.GetFullName()) + l.Debugf("creating new deployment for repo %s", r.GetFullName()) // capture body from API request input := new(library.Deployment) @@ -129,5 +121,9 @@ func CreateDeployment(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "deployment_id": d.GetID(), + }).Info("deployment created") + c.JSON(http.StatusCreated, d) } diff --git a/api/deployment/get.go b/api/deployment/get.go index 8f890ab77..5c978cf64 100644 --- a/api/deployment/get.go +++ b/api/deployment/get.go @@ -11,7 +11,6 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -68,7 +67,7 @@ import ( // GetDeployment represents the API handler to get a deployment. func GetDeployment(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) u := user.Retrieve(c) deployment := util.PathParameter(c, "deployment") @@ -76,14 +75,7 @@ func GetDeployment(c *gin.Context) { entry := fmt.Sprintf("%s/%s", r.GetFullName(), deployment) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("reading deployment %s", entry) + l.Debugf("reading deployment %s", entry) number, err := strconv.Atoi(deployment) if err != nil { diff --git a/api/deployment/list.go b/api/deployment/list.go index b07e3ba0a..34f4c74a2 100644 --- a/api/deployment/list.go +++ b/api/deployment/list.go @@ -12,9 +12,7 @@ import ( "github.com/go-vela/server/api" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -83,18 +81,10 @@ import ( // ListDeployments represents the API handler to get a list of deployments. func ListDeployments(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) - - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("reading deployments for repo %s", r.GetFullName()) + + l.Debugf("reading deployments for repo %s", r.GetFullName()) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/hook/create.go b/api/hook/create.go index bb8fbbaa0..67eb86789 100644 --- a/api/hook/create.go +++ b/api/hook/create.go @@ -11,9 +11,7 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -69,19 +67,11 @@ import ( // CreateHook represents the API handler to create a webhook. func CreateHook(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("creating new hook for repo %s", r.GetFullName()) + l.Debugf("creating new hook for repo %s", r.GetFullName()) // capture body from API request input := new(library.Hook) @@ -129,5 +119,10 @@ func CreateHook(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "hook": h.GetNumber(), + "hook_id": h.GetID(), + }).Info("hook created") + c.JSON(http.StatusCreated, h) } diff --git a/api/hook/delete.go b/api/hook/delete.go index f15f122df..a67f35263 100644 --- a/api/hook/delete.go +++ b/api/hook/delete.go @@ -5,15 +5,13 @@ package hook import ( "fmt" "net/http" - "strconv" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" + "github.com/go-vela/server/router/middleware/hook" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -67,47 +65,19 @@ import ( // DeleteHook represents the API handler to remove a webhook. func DeleteHook(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) - hook := util.PathParameter(c, "hook") + h := hook.Retrieve(c) ctx := c.Request.Context() - entry := fmt.Sprintf("%s/%s", r.GetFullName(), hook) + entry := fmt.Sprintf("%s/%d", r.GetFullName(), h.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "hook": hook, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("deleting hook %s", entry) - - number, err := strconv.Atoi(hook) - if err != nil { - retErr := fmt.Errorf("invalid hook parameter provided: %s", hook) - - util.HandleError(c, http.StatusBadRequest, retErr) - - return - } - - // send API call to capture the webhook - h, err := database.FromContext(c).GetHookForRepo(ctx, r, number) - if err != nil { - retErr := fmt.Errorf("unable to get hook %s: %w", hook, err) - - util.HandleError(c, http.StatusNotFound, retErr) - - return - } + l.Debugf("deleting hook %s", entry) // send API call to remove the webhook - err = database.FromContext(c).DeleteHook(ctx, h) + err := database.FromContext(c).DeleteHook(ctx, h) if err != nil { - retErr := fmt.Errorf("unable to delete hook %s: %w", hook, err) + retErr := fmt.Errorf("unable to delete hook %s: %w", entry, err) util.HandleError(c, http.StatusInternalServerError, retErr) diff --git a/api/hook/get.go b/api/hook/get.go index 792a76a30..d31e6e659 100644 --- a/api/hook/get.go +++ b/api/hook/get.go @@ -3,18 +3,13 @@ package hook import ( - "fmt" "net/http" - "strconv" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" - "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" + "github.com/go-vela/server/router/middleware/hook" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" - "github.com/go-vela/server/util" ) // swagger:operation GET /api/v1/hooks/{org}/{repo}/{hook} webhook GetHook @@ -67,42 +62,11 @@ import ( // GetHook represents the API handler to get a hook. func GetHook(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) - hook := util.PathParameter(c, "hook") - ctx := c.Request.Context() + h := hook.Retrieve(c) - entry := fmt.Sprintf("%s/%s", r.GetFullName(), hook) - - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "hook": hook, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("reading hook %s", entry) - - number, err := strconv.Atoi(hook) - if err != nil { - retErr := fmt.Errorf("invalid hook parameter provided: %s", hook) - - util.HandleError(c, http.StatusBadRequest, retErr) - - return - } - - // send API call to capture the webhook - h, err := database.FromContext(c).GetHookForRepo(ctx, r, number) - if err != nil { - retErr := fmt.Errorf("unable to get hook %s: %w", entry, err) - - util.HandleError(c, http.StatusInternalServerError, retErr) - - return - } + l.Debugf("reading hook %s/%d", r.GetFullName(), h.GetNumber()) c.JSON(http.StatusOK, h) } diff --git a/api/hook/list.go b/api/hook/list.go index 3f1b47851..d2e65d1ac 100644 --- a/api/hook/list.go +++ b/api/hook/list.go @@ -12,9 +12,7 @@ import ( "github.com/go-vela/server/api" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -84,19 +82,11 @@ import ( // for a repository. func ListHooks(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("reading hooks for repo %s", r.GetFullName()) + l.Debugf("reading hooks for repo %s", r.GetFullName()) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/hook/redeliver.go b/api/hook/redeliver.go index f95c598b5..13ec3f57a 100644 --- a/api/hook/redeliver.go +++ b/api/hook/redeliver.go @@ -5,13 +5,11 @@ package hook import ( "fmt" "net/http" - "strconv" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" - "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" + "github.com/go-vela/server/router/middleware/hook" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -69,44 +67,16 @@ import ( // a webhook from the SCM. func RedeliverHook(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) u := user.Retrieve(c) - hook := util.PathParameter(c, "hook") - ctx := c.Request.Context() + h := hook.Retrieve(c) - entry := fmt.Sprintf("%s/%s", r.GetFullName(), hook) + entry := fmt.Sprintf("%s/%d", r.GetFullName(), h.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "hook": hook, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("redelivering hook %s", entry) + l.Debugf("redelivering hook %s", entry) - number, err := strconv.Atoi(hook) - if err != nil { - retErr := fmt.Errorf("invalid hook parameter provided: %s", hook) - - util.HandleError(c, http.StatusBadRequest, retErr) - - return - } - - // send API call to capture the webhook - h, err := database.FromContext(c).GetHookForRepo(ctx, r, number) - if err != nil { - retErr := fmt.Errorf("unable to get hook %s: %w", entry, err) - - util.HandleError(c, http.StatusNotFound, retErr) - - return - } - - err = scm.FromContext(c).RedeliverWebhook(c, u, r, h) + err := scm.FromContext(c).RedeliverWebhook(c, u, r, h) if err != nil { retErr := fmt.Errorf("unable to redeliver hook %s: %w", entry, err) diff --git a/api/hook/update.go b/api/hook/update.go index 35b2617a2..75492e45a 100644 --- a/api/hook/update.go +++ b/api/hook/update.go @@ -5,15 +5,13 @@ package hook import ( "fmt" "net/http" - "strconv" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" + "github.com/go-vela/server/router/middleware/hook" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -74,23 +72,14 @@ import ( // UpdateHook represents the API handler to update a hook. func UpdateHook(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) - hook := util.PathParameter(c, "hook") + h := hook.Retrieve(c) ctx := c.Request.Context() - entry := fmt.Sprintf("%s/%s", r.GetFullName(), hook) + entry := fmt.Sprintf("%s/%d", r.GetFullName(), h.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "hook": hook, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("updating hook %s", entry) + l.Debugf("updating hook %s", entry) // capture body from API request input := new(library.Hook) @@ -104,25 +93,6 @@ func UpdateHook(c *gin.Context) { return } - number, err := strconv.Atoi(hook) - if err != nil { - retErr := fmt.Errorf("invalid hook parameter provided: %s", hook) - - util.HandleError(c, http.StatusBadRequest, retErr) - - return - } - - // send API call to capture the webhook - h, err := database.FromContext(c).GetHookForRepo(ctx, r, number) - if err != nil { - retErr := fmt.Errorf("unable to get hook %s: %w", entry, err) - - util.HandleError(c, http.StatusNotFound, retErr) - - return - } - // update webhook fields if provided if input.GetCreated() > 0 { // update created if set diff --git a/api/jwks.go b/api/jwks.go index 4b0be0153..166ccc15d 100644 --- a/api/jwks.go +++ b/api/jwks.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/go-vela/server/database" "github.com/go-vela/server/util" @@ -34,6 +35,10 @@ import ( // GetJWKS represents the API handler for requests to public keys in the Vela OpenID service. func GetJWKS(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) + + l.Debug("reading JWKS") + // retrieve JWKs from the database keys, err := database.FromContext(c).ListJWKs(c) if err != nil { diff --git a/api/log/create_service.go b/api/log/create_service.go index 275b86b64..736f1fca5 100644 --- a/api/log/create_service.go +++ b/api/log/create_service.go @@ -12,10 +12,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/service" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -81,25 +79,15 @@ import ( // the logs for a service. func CreateServiceLog(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := service.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "service": s.GetNumber(), - "user": u.GetName(), - }).Infof("creating logs for service %s", entry) + l.Debugf("creating logs for service %s", entry) // capture body from API request input := new(library.Log) @@ -128,5 +116,10 @@ func CreateServiceLog(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "service": s.GetName(), + "service_id": s.GetID(), + }).Info("logs created for service") + c.JSON(http.StatusCreated, nil) } diff --git a/api/log/create_step.go b/api/log/create_step.go index 42465299f..2a92abab7 100644 --- a/api/log/create_step.go +++ b/api/log/create_step.go @@ -12,10 +12,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/step" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -81,25 +79,15 @@ import ( // the logs for a step. func CreateStepLog(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := step.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "step": s.GetNumber(), - "user": u.GetName(), - }).Infof("creating logs for step %s", entry) + l.Debugf("creating logs for step %s", entry) // capture body from API request input := new(library.Log) @@ -128,5 +116,10 @@ func CreateStepLog(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "step": s.GetName(), + "step_id": s.GetID(), + }).Info("logs created for step") + c.JSON(http.StatusCreated, nil) } diff --git a/api/log/delete_service.go b/api/log/delete_service.go index 6c8b63c42..598874def 100644 --- a/api/log/delete_service.go +++ b/api/log/delete_service.go @@ -12,10 +12,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/service" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -75,28 +73,18 @@ import ( // the logs for a service. func DeleteServiceLog(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := service.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "service": s.GetNumber(), - "user": u.GetName(), - }).Infof("deleting logs for service %s", entry) + l.Debugf("deleting logs for service %s", entry) // send API call to capture the service logs - l, err := database.FromContext(c).GetLogForService(ctx, s) + sl, err := database.FromContext(c).GetLogForService(ctx, s) if err != nil { retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err) @@ -106,7 +94,7 @@ func DeleteServiceLog(c *gin.Context) { } // send API call to remove the log - err = database.FromContext(c).DeleteLog(ctx, l) + err = database.FromContext(c).DeleteLog(ctx, sl) if err != nil { retErr := fmt.Errorf("unable to delete logs for service %s: %w", entry, err) diff --git a/api/log/delete_step.go b/api/log/delete_step.go index 36ebfacff..28b8b6cd5 100644 --- a/api/log/delete_step.go +++ b/api/log/delete_step.go @@ -12,10 +12,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/step" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -75,28 +73,18 @@ import ( // the logs for a step. func DeleteStepLog(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := step.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "step": s.GetNumber(), - "user": u.GetName(), - }).Infof("deleting logs for step %s", entry) + l.Debugf("deleting logs for step %s", entry) // send API call to capture the step logs - l, err := database.FromContext(c).GetLogForStep(ctx, s) + sl, err := database.FromContext(c).GetLogForStep(ctx, s) if err != nil { retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err) @@ -106,7 +94,7 @@ func DeleteStepLog(c *gin.Context) { } // send API call to remove the log - err = database.FromContext(c).DeleteLog(ctx, l) + err = database.FromContext(c).DeleteLog(ctx, sl) if err != nil { retErr := fmt.Errorf("unable to delete logs for step %s: %w", entry, err) diff --git a/api/log/get_service.go b/api/log/get_service.go index c7c72fc73..de223da08 100644 --- a/api/log/get_service.go +++ b/api/log/get_service.go @@ -12,10 +12,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/service" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -74,28 +72,18 @@ import ( // GetServiceLog represents the API handler to get the logs for a service. func GetServiceLog(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := service.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "service": s.GetNumber(), - "user": u.GetName(), - }).Infof("reading logs for service %s", entry) + l.Debugf("reading logs for service %s", entry) // send API call to capture the service logs - l, err := database.FromContext(c).GetLogForService(ctx, s) + sl, err := database.FromContext(c).GetLogForService(ctx, s) if err != nil { retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err) @@ -104,5 +92,5 @@ func GetServiceLog(c *gin.Context) { return } - c.JSON(http.StatusOK, l) + c.JSON(http.StatusOK, sl) } diff --git a/api/log/get_step.go b/api/log/get_step.go index 8e0a30223..1bf6c4191 100644 --- a/api/log/get_step.go +++ b/api/log/get_step.go @@ -12,10 +12,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/step" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -75,28 +73,18 @@ import ( // GetStepLog represents the API handler to get the logs for a step. func GetStepLog(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := step.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "step": s.GetNumber(), - "user": u.GetName(), - }).Infof("reading logs for step %s", entry) + l.Debugf("reading logs for step %s", entry) // send API call to capture the step logs - l, err := database.FromContext(c).GetLogForStep(ctx, s) + sl, err := database.FromContext(c).GetLogForStep(ctx, s) if err != nil { retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err) @@ -105,5 +93,5 @@ func GetStepLog(c *gin.Context) { return } - c.JSON(http.StatusOK, l) + c.JSON(http.StatusOK, sl) } diff --git a/api/log/list_build.go b/api/log/list_build.go index a33fd2c32..72d4ce7ab 100644 --- a/api/log/list_build.go +++ b/api/log/list_build.go @@ -13,9 +13,7 @@ import ( "github.com/go-vela/server/api" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -82,23 +80,14 @@ import ( // ListLogsForBuild represents the API handler to get a list of logs for a build. func ListLogsForBuild(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("listing logs for build %s", entry) + l.Debugf("listing logs for build %s", entry) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) @@ -123,7 +112,7 @@ func ListLogsForBuild(c *gin.Context) { perPage = util.MaxInt(1, util.MinInt(100, perPage)) // send API call to capture the list of logs for the build - l, t, err := database.FromContext(c).ListLogsForBuild(ctx, b, page, perPage) + bl, t, err := database.FromContext(c).ListLogsForBuild(ctx, b, page, perPage) if err != nil { retErr := fmt.Errorf("unable to list logs for build %s: %w", entry, err) @@ -141,5 +130,5 @@ func ListLogsForBuild(c *gin.Context) { // set pagination headers pagination.SetHeaderLink(c) - c.JSON(http.StatusOK, l) + c.JSON(http.StatusOK, bl) } diff --git a/api/log/update_service.go b/api/log/update_service.go index 54e1fade8..dd8b8329d 100644 --- a/api/log/update_service.go +++ b/api/log/update_service.go @@ -12,10 +12,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/service" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -81,28 +79,18 @@ import ( // the logs for a service. func UpdateServiceLog(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := service.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "service": s.GetNumber(), - "user": u.GetName(), - }).Infof("updating logs for service %s", entry) + l.Debugf("updating logs for service %s", entry) // send API call to capture the service logs - l, err := database.FromContext(c).GetLogForService(ctx, s) + sl, err := database.FromContext(c).GetLogForService(ctx, s) if err != nil { retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err) @@ -126,11 +114,11 @@ func UpdateServiceLog(c *gin.Context) { // update log fields if provided if len(input.GetData()) > 0 { // update data if set - l.SetData(input.GetData()) + sl.SetData(input.GetData()) } // send API call to update the log - err = database.FromContext(c).UpdateLog(ctx, l) + err = database.FromContext(c).UpdateLog(ctx, sl) if err != nil { retErr := fmt.Errorf("unable to update logs for service %s: %w", entry, err) diff --git a/api/log/update_step.go b/api/log/update_step.go index 4c27dd0ad..ff19da7da 100644 --- a/api/log/update_step.go +++ b/api/log/update_step.go @@ -12,10 +12,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/step" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -81,28 +79,18 @@ import ( // the logs for a step. func UpdateStepLog(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := step.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "step": s.GetNumber(), - "user": u.GetName(), - }).Infof("updating logs for step %s", entry) + l.Debugf("updating logs for step %s", entry) // send API call to capture the step logs - l, err := database.FromContext(c).GetLogForStep(ctx, s) + sl, err := database.FromContext(c).GetLogForStep(ctx, s) if err != nil { retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err) @@ -126,11 +114,11 @@ func UpdateStepLog(c *gin.Context) { // update log fields if provided if len(input.GetData()) > 0 { // update data if set - l.SetData(input.GetData()) + sl.SetData(input.GetData()) } // send API call to update the log - err = database.FromContext(c).UpdateLog(ctx, l) + err = database.FromContext(c).UpdateLog(ctx, sl) if err != nil { retErr := fmt.Errorf("unable to update logs for step %s: %w", entry, err) diff --git a/api/oi_config.go b/api/oi_config.go index 1111e352f..2e600577d 100644 --- a/api/oi_config.go +++ b/api/oi_config.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v5" + "github.com/sirupsen/logrus" "github.com/go-vela/server/api/types" "github.com/go-vela/server/internal" @@ -32,6 +33,10 @@ import ( // GetOpenIDConfig represents the API handler for requests for configurations in the Vela OpenID service. func GetOpenIDConfig(c *gin.Context) { m := c.MustGet("metadata").(*internal.Metadata) + l := c.MustGet("logger").(*logrus.Entry) + + l.Debug("reading OpenID configuration") + config := types.OpenIDConfig{ Issuer: fmt.Sprintf("%s/_services/token", m.Vela.Address), JWKSAddress: fmt.Sprintf("%s/%s", m.Vela.Address, "_services/token/.well-known/jwks"), diff --git a/api/pipeline/compile.go b/api/pipeline/compile.go index 67419d91d..a7c24af80 100644 --- a/api/pipeline/compile.go +++ b/api/pipeline/compile.go @@ -12,7 +12,6 @@ import ( "github.com/go-vela/server/compiler" "github.com/go-vela/server/internal" - "github.com/go-vela/server/router/middleware/org" pMiddleware "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" @@ -81,22 +80,14 @@ import ( func CompilePipeline(c *gin.Context) { // capture middleware values m := c.MustGet("metadata").(*internal.Metadata) - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) p := pMiddleware.Retrieve(c) r := repo.Retrieve(c) u := user.Retrieve(c) entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "pipeline": p.GetCommit(), - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("compiling pipeline %s", entry) + l.Debugf("compiling pipeline %s", entry) // ensure we use the expected pipeline type when compiling r.SetPipelineType(p.GetType()) diff --git a/api/pipeline/create.go b/api/pipeline/create.go index 1420fdc67..2dc5d5067 100644 --- a/api/pipeline/create.go +++ b/api/pipeline/create.go @@ -10,9 +10,7 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -70,21 +68,11 @@ import ( // create a pipeline. func CreatePipeline(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) - - logger.Infof("creating new pipeline for repo %s", r.GetFullName()) + l.Debugf("creating new pipeline for repo %s", r.GetFullName()) // capture body from API request input := new(library.Pipeline) @@ -111,5 +99,10 @@ func CreatePipeline(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "pipeline": p.GetCommit(), + "pipeline_id": p.GetID(), + }).Info("pipeline created for repo") + c.JSON(http.StatusCreated, p) } diff --git a/api/pipeline/delete.go b/api/pipeline/delete.go index 89c0f362c..0af419b78 100644 --- a/api/pipeline/delete.go +++ b/api/pipeline/delete.go @@ -10,10 +10,8 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -67,23 +65,14 @@ import ( // DeletePipeline represents the API handler to remove a pipeline for a repository. func DeletePipeline(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) p := pipeline.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "pipeline": p.GetCommit(), - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("deleting pipeline %s", entry) + l.Debugf("deleting pipeline %s", entry) // send API call to remove the build err := database.FromContext(c).DeletePipeline(ctx, p) diff --git a/api/pipeline/expand.go b/api/pipeline/expand.go index 062992a81..be77aba50 100644 --- a/api/pipeline/expand.go +++ b/api/pipeline/expand.go @@ -12,7 +12,6 @@ import ( "github.com/go-vela/server/compiler" "github.com/go-vela/server/internal" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" @@ -81,22 +80,14 @@ import ( func ExpandPipeline(c *gin.Context) { // capture middleware values m := c.MustGet("metadata").(*internal.Metadata) - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) p := pipeline.Retrieve(c) r := repo.Retrieve(c) u := user.Retrieve(c) entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "pipeline": p.GetCommit(), - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("expanding templates for pipeline %s", entry) + l.Debugf("expanding templates for pipeline %s", entry) // ensure we use the expected pipeline type when compiling r.SetPipelineType(p.GetType()) diff --git a/api/pipeline/get.go b/api/pipeline/get.go index d6fdcbeed..e0c5b17af 100644 --- a/api/pipeline/get.go +++ b/api/pipeline/get.go @@ -8,10 +8,8 @@ import ( "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" ) // swagger:operation GET /api/v1/pipelines/{org}/{repo}/{pipeline} pipelines GetPipeline @@ -65,20 +63,11 @@ import ( // GetPipeline represents the API handler to get a pipeline for a repo. func GetPipeline(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) p := pipeline.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "pipeline": p.GetCommit(), - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("reading pipeline %s/%s", r.GetFullName(), p.GetCommit()) + l.Debugf("reading pipeline %s/%s", r.GetFullName(), p.GetCommit()) c.JSON(http.StatusOK, p) } diff --git a/api/pipeline/list.go b/api/pipeline/list.go index c2fd7f0b6..4203c987f 100644 --- a/api/pipeline/list.go +++ b/api/pipeline/list.go @@ -12,9 +12,7 @@ import ( "github.com/go-vela/server/api" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -84,19 +82,11 @@ import ( // of pipelines for a repository. func ListPipelines(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("listing pipelines for repo %s", r.GetFullName()) + l.Debugf("listing pipelines for repo %s", r.GetFullName()) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/pipeline/template.go b/api/pipeline/template.go index 8271732b6..8dc9742b8 100644 --- a/api/pipeline/template.go +++ b/api/pipeline/template.go @@ -86,6 +86,7 @@ import ( func GetTemplates(c *gin.Context) { // capture middleware values m := c.MustGet("metadata").(*internal.Metadata) + l := c.MustGet("logger").(*logrus.Entry) o := org.Retrieve(c) p := pipeline.Retrieve(c) r := repo.Retrieve(c) @@ -94,15 +95,7 @@ func GetTemplates(c *gin.Context) { entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "pipeline": p.GetCommit(), - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("reading templates from pipeline %s", entry) + l.Debugf("reading templates from pipeline %s", entry) // create the compiler object compiler := compiler.FromContext(c).Duplicate().WithCommit(p.GetCommit()).WithMetadata(m).WithRepo(r).WithUser(u) diff --git a/api/pipeline/update.go b/api/pipeline/update.go index bc70d53c4..babd29494 100644 --- a/api/pipeline/update.go +++ b/api/pipeline/update.go @@ -10,10 +10,8 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -75,23 +73,14 @@ import ( // a pipeline for a repo. func UpdatePipeline(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) p := pipeline.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "pipeline": p.GetCommit(), - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("updating pipeline %s", entry) + l.Debugf("updating pipeline %s", entry) // capture body from API request input := new(library.Pipeline) diff --git a/api/pipeline/validate.go b/api/pipeline/validate.go index 335f61850..6ff069347 100644 --- a/api/pipeline/validate.go +++ b/api/pipeline/validate.go @@ -11,7 +11,6 @@ import ( "github.com/go-vela/server/compiler" "github.com/go-vela/server/internal" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" @@ -79,22 +78,14 @@ import ( func ValidatePipeline(c *gin.Context) { // capture middleware values m := c.MustGet("metadata").(*internal.Metadata) - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) p := pipeline.Retrieve(c) r := repo.Retrieve(c) u := user.Retrieve(c) entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "pipeline": p.GetCommit(), - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("validating pipeline %s", entry) + l.Debugf("validating pipeline %s", entry) // ensure we use the expected pipeline type when compiling r.SetPipelineType(p.GetType()) diff --git a/api/queue/queue.go b/api/queue/queue.go index 5c861572e..2c5698bb6 100644 --- a/api/queue/queue.go +++ b/api/queue/queue.go @@ -8,7 +8,6 @@ import ( "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" - "github.com/go-vela/server/router/middleware/claims" "github.com/go-vela/types/library" ) @@ -34,11 +33,9 @@ import ( // Info represents the API handler to // retrieve queue credentials as part of worker onboarding. func Info(c *gin.Context) { - cl := claims.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) - logrus.WithFields(logrus.Fields{ - "user": cl.Subject, - }).Info("requesting queue credentials with registration token") + l.Info("requesting queue credentials with registration token") // extract the public key that was packed into gin context k := c.MustGet("public-key").(string) diff --git a/api/repo/chown.go b/api/repo/chown.go index 30442c683..a30eab938 100644 --- a/api/repo/chown.go +++ b/api/repo/chown.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" @@ -62,19 +61,12 @@ import ( // the owner of a repo. func ChownRepo(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("changing owner of repo %s to %s", r.GetFullName(), u.GetName()) + l.Debugf("changing owner of repo %s to %s", r.GetFullName(), u.GetName()) // update repo owner r.SetOwner(u) @@ -89,5 +81,7 @@ func ChownRepo(c *gin.Context) { return } + l.Infof("updated repo - changed owner to %s", u.GetName()) + c.JSON(http.StatusOK, fmt.Sprintf("repo %s changed owner to %s", r.GetFullName(), u.GetName())) } diff --git a/api/repo/create.go b/api/repo/create.go index 7516b9682..25719f2b4 100644 --- a/api/repo/create.go +++ b/api/repo/create.go @@ -74,6 +74,7 @@ import ( //nolint:funlen,gocyclo // ignore function length and cyclomatic complexity func CreateRepo(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) s := settings.FromContext(c) @@ -98,14 +99,7 @@ func CreateRepo(c *gin.Context) { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": input.GetOrg(), - "repo": input.GetName(), - "user": u.GetName(), - }).Infof("creating new repo %s", input.GetFullName()) + l.Debugf("creating new repo %s", input.GetFullName()) // get repo information from the source r, _, err := scm.FromContext(c).GetRepo(ctx, u, input) @@ -290,6 +284,8 @@ func CreateRepo(c *gin.Context) { dbRepo.SetActive(true) // send API call to update the repo + // NOTE: not logging modification out separately + // although we are CREATING a repo in this path r, err = database.FromContext(c).UpdateRepo(ctx, dbRepo) if err != nil { retErr := fmt.Errorf("unable to set repo %s to active: %w", dbRepo.GetFullName(), err) @@ -298,6 +294,12 @@ func CreateRepo(c *gin.Context) { return } + + l.WithFields(logrus.Fields{ + "org": r.GetOrg(), + "repo": r.GetName(), + "repo_id": r.GetID(), + }).Infof("repo %s activated", r.GetFullName()) } else { // send API call to create the repo r, err = database.FromContext(c).CreateRepo(ctx, r) @@ -308,6 +310,12 @@ func CreateRepo(c *gin.Context) { return } + + l.WithFields(logrus.Fields{ + "org": r.GetOrg(), + "repo": r.GetName(), + "repo_id": r.GetID(), + }).Infof("repo %s created", r.GetFullName()) } // create init hook in the DB after repo has been added in order to capture its ID @@ -323,6 +331,10 @@ func CreateRepo(c *gin.Context) { return } + + l.WithFields(logrus.Fields{ + "hook": h.GetID(), + }).Infof("hook %d created for repo %s", h.GetID(), r.GetFullName()) } c.JSON(http.StatusCreated, r) diff --git a/api/repo/delete.go b/api/repo/delete.go index c75d2fc51..0d8be5d84 100644 --- a/api/repo/delete.go +++ b/api/repo/delete.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -62,19 +61,12 @@ import ( // DeleteRepo represents the API handler to remove a repository. func DeleteRepo(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("deleting repo %s", r.GetFullName()) + l.Debugf("deleting repo %s", r.GetFullName()) // send API call to remove the webhook err := scm.FromContext(c).Disable(ctx, u, r.GetOrg(), r.GetName()) diff --git a/api/repo/get.go b/api/repo/get.go index ac988093c..ab0254f7e 100644 --- a/api/repo/get.go +++ b/api/repo/get.go @@ -8,9 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" ) // swagger:operation GET /api/v1/repos/{org}/{repo} repos GetRepo @@ -54,18 +52,10 @@ import ( // GetRepo represents the API handler to get a repository. func GetRepo(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("reading repo %s", r.GetFullName()) + l.Debug("reading repo") c.JSON(http.StatusOK, r) } diff --git a/api/repo/list.go b/api/repo/list.go index 306214605..0e408e41d 100644 --- a/api/repo/list.go +++ b/api/repo/list.go @@ -68,15 +68,11 @@ import ( // of repositories for a user. func ListRepos(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("listing repos for user %s", u.GetName()) + l.Debugf("listing repos for user %s", u.GetName()) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/repo/list_org.go b/api/repo/list_org.go index 2466efce1..2152006c9 100644 --- a/api/repo/list_org.go +++ b/api/repo/list_org.go @@ -89,17 +89,12 @@ import ( // of repositories for an organization. func ListReposForOrg(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) o := org.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "user": u.GetName(), - }).Infof("listing repos for org %s", o) + l.Debugf("listing repos for org %s", o) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) @@ -142,7 +137,7 @@ func ListReposForOrg(c *gin.Context) { // See if the user is an org admin to bypass individual permission checks perm, err := scm.FromContext(c).OrgAccess(ctx, u, o) if err != nil { - logrus.Errorf("unable to get user %s access level for org %s", u.GetName(), o) + l.Errorf("unable to get user %s access level for org %s", u.GetName(), o) } // Only show public repos to non-admins if perm != "admin" { diff --git a/api/repo/repair.go b/api/repo/repair.go index af43148e7..27e720c37 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -12,7 +12,6 @@ import ( wh "github.com/go-vela/server/api/webhook" "github.com/go-vela/server/database" "github.com/go-vela/server/internal" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -65,21 +64,13 @@ import ( // and then create a webhook for a repo. func RepairRepo(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + m := c.MustGet("metadata").(*internal.Metadata) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // capture middleware values - m := c.MustGet("metadata").(*internal.Metadata) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("repairing repo %s", r.GetFullName()) + l.Debugf("repairing repo %s", r.GetFullName()) // check if we should create the webhook if c.Value("webhookvalidation").(bool) { @@ -131,6 +122,10 @@ func RepairRepo(c *gin.Context) { return } + + l.WithFields(logrus.Fields{ + "hook": hook.GetID(), + }).Info("new webhook created") } // get repo information from the source @@ -181,6 +176,8 @@ func RepairRepo(c *gin.Context) { return } + + l.Infof("repo %s updated - set to active", r.GetFullName()) } c.JSON(http.StatusOK, fmt.Sprintf("repo %s repaired", r.GetFullName())) diff --git a/api/repo/update.go b/api/repo/update.go index 4626dd72b..db3342326 100644 --- a/api/repo/update.go +++ b/api/repo/update.go @@ -14,7 +14,6 @@ import ( "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -75,7 +74,7 @@ import ( //nolint:funlen,gocyclo // ignore function length func UpdateRepo(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) u := user.Retrieve(c) maxBuildLimit := c.Value("maxBuildLimit").(int64) @@ -83,14 +82,7 @@ func UpdateRepo(c *gin.Context) { defaultRepoEventsMask := c.Value("defaultRepoEventsMask").(int64) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("updating repo %s", r.GetFullName()) + l.Debug("updating repo") // capture body from API request input := new(types.Repo) @@ -259,11 +251,7 @@ func UpdateRepo(c *gin.Context) { admn := u.GetName() // log admin override update repo hook - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("platform admin %s updating repo webhook events for repo %s", admn, r.GetFullName()) + l.Debugf("platform admin %s updating repo webhook events for repo %s", admn, r.GetFullName()) u = r.GetOwner() } diff --git a/api/schedule/create.go b/api/schedule/create.go index 938c31e1f..36741832b 100644 --- a/api/schedule/create.go +++ b/api/schedule/create.go @@ -83,6 +83,7 @@ import ( // create a schedule. func CreateSchedule(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) r := repo.Retrieve(c) ctx := c.Request.Context() @@ -119,14 +120,7 @@ func CreateSchedule(c *gin.Context) { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": r.GetOrg(), - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("creating new schedule %s", input.GetName()) + l.Debugf("creating new schedule %s", input.GetName()) // ensure repo is allowed to create new schedules if !util.CheckAllowlist(r, s.GetScheduleAllowlist()) { @@ -196,6 +190,11 @@ func CreateSchedule(c *gin.Context) { return } + + l.WithFields(logrus.Fields{ + "schedule": schedule.GetName(), + "schedule_id": schedule.GetID(), + }).Infof("schedule %s updated - activated", schedule.GetName()) } else { // send API call to create the schedule schedule, err = database.FromContext(c).CreateSchedule(ctx, schedule) @@ -206,6 +205,11 @@ func CreateSchedule(c *gin.Context) { return } + + l.WithFields(logrus.Fields{ + "schedule": schedule.GetName(), + "schedule_id": schedule.GetID(), + }).Infof("schedule %s created", schedule.GetName()) } c.JSON(http.StatusCreated, schedule) diff --git a/api/schedule/delete.go b/api/schedule/delete.go index 19c448aa1..1cf2da000 100644 --- a/api/schedule/delete.go +++ b/api/schedule/delete.go @@ -10,10 +10,7 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" - "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/schedule" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -67,20 +64,11 @@ import ( // DeleteSchedule represents the API handler to remove a schedule. func DeleteSchedule(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) - r := repo.Retrieve(c) - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) s := schedule.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("deleting schedule %s", s.GetName()) + l.Debugf("deleting schedule %s", s.GetName()) err := database.FromContext(c).DeleteSchedule(ctx, s) if err != nil { diff --git a/api/schedule/get.go b/api/schedule/get.go index 2978860b9..287debf42 100644 --- a/api/schedule/get.go +++ b/api/schedule/get.go @@ -8,10 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" - "github.com/go-vela/server/router/middleware/org" - "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/schedule" - "github.com/go-vela/server/router/middleware/user" ) // swagger:operation GET /api/v1/schedules/{org}/{repo}/{schedule} schedules GetSchedule @@ -60,20 +57,10 @@ import ( // GetSchedule represents the API handler to get a schedule. func GetSchedule(c *gin.Context) { // capture middleware values - o := org.Retrieve(c) - r := repo.Retrieve(c) - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) s := schedule.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - "schedule": s.GetName(), - }).Infof("reading schedule %s", s.GetName()) + l.Debugf("reading schedule %s", s.GetName()) c.JSON(http.StatusOK, s) } diff --git a/api/schedule/list.go b/api/schedule/list.go index a42569ba0..22b1fd43a 100644 --- a/api/schedule/list.go +++ b/api/schedule/list.go @@ -81,16 +81,11 @@ import ( // ListSchedules represents the API handler to get a list of schedules for a repository. func ListSchedules(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "repo": r.GetName(), - "org": r.GetOrg(), - }).Infof("listing schedules for repo %s", r.GetFullName()) + l.Debugf("listing schedules for repo %s", r.GetFullName()) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/schedule/update.go b/api/schedule/update.go index 149cd3e74..3aedde1a1 100644 --- a/api/schedule/update.go +++ b/api/schedule/update.go @@ -12,7 +12,6 @@ import ( api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/schedule" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" @@ -74,21 +73,14 @@ import ( // UpdateSchedule represents the API handler to update a schedule. func UpdateSchedule(c *gin.Context) { // capture middleware values - r := repo.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) s := schedule.Retrieve(c) - ctx := c.Request.Context() u := user.Retrieve(c) + ctx := c.Request.Context() scheduleName := util.PathParameter(c, "schedule") minimumFrequency := c.Value("scheduleminimumfrequency").(time.Duration) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "schedule": scheduleName, - "repo": r.GetName(), - "org": r.GetOrg(), - }).Infof("updating schedule %s", scheduleName) + l.Debugf("updating schedule %s", scheduleName) // capture body from API request input := new(api.Schedule) diff --git a/api/scm/sync.go b/api/scm/sync.go index 66e2e6fd2..66522510a 100644 --- a/api/scm/sync.go +++ b/api/scm/sync.go @@ -73,21 +73,13 @@ import ( // subscribed events with allowed events. func SyncRepo(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) o := org.Retrieve(c) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) - - logger.Infof("syncing repo %s", r.GetFullName()) + l.Debugf("syncing repo %s", r.GetFullName()) // retrieve repo from source code manager service _, respCode, err := scm.FromContext(c).GetRepo(ctx, u, r) @@ -107,6 +99,8 @@ func SyncRepo(c *gin.Context) { return } + l.Infof("repo %s has been updated - set to inactive", r.GetFullName()) + // exit with success as hook sync will be unnecessary c.JSON(http.StatusOK, r) @@ -124,7 +118,7 @@ func SyncRepo(c *gin.Context) { // we cannot use our normal permissions check due to the possibility the repo was deleted perm, err := scm.FromContext(c).RepoAccess(ctx, u.GetName(), u.GetToken(), o, r.GetName()) if err != nil { - logger.Errorf("unable to get user %s access level for org %s", u.GetName(), o) + l.Errorf("unable to get user %s access level for org %s", u.GetName(), o) } if !strings.EqualFold(perm, "admin") { @@ -165,6 +159,8 @@ func SyncRepo(c *gin.Context) { return } + l.Infof("repo %s has been updated - set to inactive", r.GetFullName()) + c.JSON(http.StatusOK, r) return diff --git a/api/scm/sync_org.go b/api/scm/sync_org.go index 8809e7b4f..e8efee734 100644 --- a/api/scm/sync_org.go +++ b/api/scm/sync_org.go @@ -69,24 +69,17 @@ import ( // subscribed events with allowed events. func SyncReposForOrg(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) o := org.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(logrus.Fields{ - "org": o, - "user": u.GetName(), - }) - - logger.Infof("syncing repos for org %s", o) + l.Debugf("syncing repos for org %s", o) // see if the user is an org admin perm, err := scm.FromContext(c).OrgAccess(ctx, u, o) if err != nil { - logger.Errorf("unable to get user %s access level for org %s", u.GetName(), o) + l.Errorf("unable to get user %s access level for org %s", u.GetName(), o) } // only allow org-wide syncing if user is admin of org @@ -143,6 +136,8 @@ func SyncReposForOrg(c *gin.Context) { return } + l.Infof("repo %s has been updated - set to inactive", repo.GetFullName()) + results = append(results, repo) } else { retErr := fmt.Errorf("error while retrieving repo %s from %s: %w", repo.GetFullName(), scm.FromContext(c).Driver(), err) @@ -183,6 +178,8 @@ func SyncReposForOrg(c *gin.Context) { return } + l.Infof("repo %s has been updated - set to inactive", repo.GetFullName()) + results = append(results, repo) continue diff --git a/api/secret/create.go b/api/secret/create.go index e2d3e2c50..6c4c502b1 100644 --- a/api/secret/create.go +++ b/api/secret/create.go @@ -20,7 +20,6 @@ import ( "github.com/go-vela/types/library/actions" ) -// // swagger:operation POST /api/v1/secrets/{engine}/{type}/{org}/{name} secrets CreateSecret // // Create a secret @@ -85,6 +84,7 @@ import ( //nolint:funlen // suppress long function error func CreateSecret(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) e := util.PathParameter(c, "engine") t := util.PathParameter(c, "type") @@ -96,25 +96,24 @@ func CreateSecret(c *gin.Context) { // create log fields from API metadata fields := logrus.Fields{ - "engine": e, - "org": o, - "repo": n, - "type": t, - "user": u.GetName(), + "secret_engine": e, + "secret_org": o, + "secret_repo": n, + "secret_type": t, } // check if secret is a shared secret if strings.EqualFold(t, constants.SecretShared) { - // update log fields from API metadata - fields = logrus.Fields{ - "engine": e, - "org": o, - "team": n, - "type": t, - "user": u.GetName(), - } + // update log fields for shared secret + delete(fields, "secret_repo") + fields["secret_team"] = n } + // update engine logger with API metadata + // + // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields + logger := l.WithFields(fields) + if strings.EqualFold(t, constants.SecretOrg) { // retrieve org name from SCM // @@ -173,10 +172,7 @@ func CreateSecret(c *gin.Context) { } } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(fields).Infof("creating new secret %s for %s service", entry, e) + logger.Debugf("creating new secret %s for %s service", entry, e) // capture body from API request input := new(library.Secret) @@ -259,5 +255,23 @@ func CreateSecret(c *gin.Context) { return } + // update log fields from create response + fields = logrus.Fields{ + "secret_id": s.GetID(), + "secret_name": s.GetName(), + "secret_org": s.GetOrg(), + "secret_repo": s.GetRepo(), + "secret_type": s.GetType(), + } + + // check if secret is a shared secret + if strings.EqualFold(t, constants.SecretShared) { + // update log fields for shared secret + delete(fields, "secret_repo") + fields["secret_team"] = s.GetTeam() + } + + l.WithFields(fields).Infof("created secret %s for %s service", entry, e) + c.JSON(http.StatusOK, s.Sanitize()) } diff --git a/api/secret/delete.go b/api/secret/delete.go index 188016155..c038c1a08 100644 --- a/api/secret/delete.go +++ b/api/secret/delete.go @@ -10,13 +10,11 @@ import ( "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/secret" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" ) -// // swagger:operation DELETE /api/v1/secrets/{engine}/{type}/{org}/{name}/{secret} secrets DeleteSecret // // Delete a secret @@ -73,7 +71,7 @@ import ( // DeleteSecret deletes a secret from the provided secrets service. func DeleteSecret(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) e := util.PathParameter(c, "engine") t := util.PathParameter(c, "type") o := util.PathParameter(c, "org") @@ -85,31 +83,26 @@ func DeleteSecret(c *gin.Context) { // create log fields from API metadata fields := logrus.Fields{ - "engine": e, - "org": o, - "repo": n, - "secret": s, - "type": t, - "user": u.GetName(), + "secret_engine": e, + "secret_org": o, + "secret_repo": n, + "secret_name": s, + "secret_type": t, } // check if secret is a shared secret if strings.EqualFold(t, constants.SecretShared) { // update log fields from API metadata - fields = logrus.Fields{ - "engine": e, - "org": o, - "secret": s, - "team": n, - "type": t, - "user": u.GetName(), - } + delete(fields, "secret_repo") + fields["secret_team"] = n } // update engine logger with API metadata // // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(fields).Infof("deleting secret %s from %s service", entry, e) + logger := l.WithFields(fields) + + logger.Debugf("deleting secret %s from %s service", entry, e) // send API call to remove the secret err := secret.FromContext(c, e).Delete(ctx, t, o, n, s) @@ -121,5 +114,7 @@ func DeleteSecret(c *gin.Context) { return } + logger.Infof("secret %s deleted from %s service", entry, e) + c.JSON(http.StatusOK, fmt.Sprintf("secret %s deleted from %s service", entry, e)) } diff --git a/api/secret/get.go b/api/secret/get.go index dc4d4da1f..72a84fc18 100644 --- a/api/secret/get.go +++ b/api/secret/get.go @@ -11,13 +11,11 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/router/middleware/claims" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/secret" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" ) -// // swagger:operation GET /api/v1/secrets/{engine}/{type}/{org}/{name}/{secret} secrets GetSecret // // Get a secret @@ -74,8 +72,8 @@ import ( // GetSecret gets a secret from the provided secrets service. func GetSecret(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) - u := user.Retrieve(c) e := util.PathParameter(c, "engine") t := util.PathParameter(c, "type") o := util.PathParameter(c, "org") @@ -87,31 +85,26 @@ func GetSecret(c *gin.Context) { // create log fields from API metadata fields := logrus.Fields{ - "engine": e, - "org": o, - "repo": n, - "secret": s, - "type": t, - "user": u.GetName(), + "secret_engine": e, + "secret_org": o, + "secret_repo": n, + "secret_name": s, + "secret_type": t, } // check if secret is a shared secret if strings.EqualFold(t, constants.SecretShared) { // update log fields from API metadata - fields = logrus.Fields{ - "engine": e, - "org": o, - "secret": s, - "team": n, - "type": t, - "user": u.GetName(), - } + delete(fields, "secret_repo") + fields["secret_team"] = n } // update engine logger with API metadata // // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(fields).Infof("reading secret %s from %s service", entry, e) + logger := l.WithFields(fields) + + logger.Debugf("reading secret %s from %s service", entry, e) // send API call to capture the secret secret, err := secret.FromContext(c, e).Get(ctx, t, o, n, s) @@ -130,5 +123,7 @@ func GetSecret(c *gin.Context) { return } + logger.Infof("retrieved secret %s from %s service", entry, e) + c.JSON(http.StatusOK, secret.Sanitize()) } diff --git a/api/secret/list.go b/api/secret/list.go index a6699ef34..793b594f7 100644 --- a/api/secret/list.go +++ b/api/secret/list.go @@ -20,7 +20,6 @@ import ( "github.com/go-vela/types/library" ) -// // swagger:operation GET /api/v1/secrets/{engine}/{type}/{org}/{name} secrets ListSecrets // // Get all organization or shared secrets @@ -96,6 +95,7 @@ import ( // ListSecrets represents the API handler to get a list of secrets. func ListSecrets(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) e := util.PathParameter(c, "engine") t := util.PathParameter(c, "type") @@ -122,29 +122,25 @@ func ListSecrets(c *gin.Context) { // create log fields from API metadata fields := logrus.Fields{ - "engine": e, - "org": o, - "repo": n, - "type": t, - "user": u.GetName(), + "secret_engine": e, + "secret_org": o, + "secret_repo": n, + "secret_type": t, } // check if secret is a shared secret if strings.EqualFold(t, constants.SecretShared) { // update log fields from API metadata - fields = logrus.Fields{ - "engine": e, - "org": o, - "team": n, - "type": t, - "user": u.GetName(), - } + delete(fields, "secret_repo") + fields["secret_team"] = n } // update engine logger with API metadata // // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(fields).Infof("listing secrets %s from %s service", entry, e) + logger := l.WithFields(fields) + + logger.Debugf("listing secrets %s from %s service", entry, e) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) @@ -209,5 +205,7 @@ func ListSecrets(c *gin.Context) { secrets = append(secrets, tmp.Sanitize()) } + logger.Infof("successfully listed secrets %s from %s service", entry, e) + c.JSON(http.StatusOK, secrets) } diff --git a/api/secret/update.go b/api/secret/update.go index dc66df7ce..0d7d62072 100644 --- a/api/secret/update.go +++ b/api/secret/update.go @@ -85,6 +85,7 @@ import ( // UpdateSecret updates a secret for the provided secrets service. func UpdateSecret(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) e := util.PathParameter(c, "engine") t := util.PathParameter(c, "type") @@ -97,31 +98,24 @@ func UpdateSecret(c *gin.Context) { // create log fields from API metadata fields := logrus.Fields{ - "engine": e, - "org": o, - "repo": n, - "secret": s, - "type": t, - "user": u.GetName(), + "secret_engine": e, + "secret_org": o, + "secret_repo": n, + "secret_name": s, + "secret_type": t, } // check if secret is a shared secret if strings.EqualFold(t, constants.SecretShared) { // update log fields from API metadata - fields = logrus.Fields{ - "engine": e, - "org": o, - "secret": s, - "team": n, - "type": t, - "user": u.GetName(), - } + delete(fields, "secret_repo") + fields["secret_team"] = n } // update engine logger with API metadata // // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(fields).Infof("updating secret %s for %s service", entry, e) + l.WithFields(fields).Debugf("updating secret %s for %s service", entry, e) // capture body from API request input := new(library.Secret) @@ -174,5 +168,7 @@ func UpdateSecret(c *gin.Context) { return } + l.WithFields(fields).Info("secret updated") + c.JSON(http.StatusOK, secret.Sanitize()) } diff --git a/api/service/create.go b/api/service/create.go index e4289cc0b..4e3ee47c7 100644 --- a/api/service/create.go +++ b/api/service/create.go @@ -12,9 +12,7 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" @@ -77,23 +75,14 @@ import ( // a service for a build. func CreateService(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("creating new service for build %s", entry) + l.Debugf("creating new service for build %s", entry) // capture body from API request input := new(library.Service) @@ -129,5 +118,10 @@ func CreateService(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "service": s.GetName(), + "service_id": s.GetID(), + }).Infof("service %s created for build %s", s.GetName(), entry) + c.JSON(http.StatusCreated, s) } diff --git a/api/service/delete.go b/api/service/delete.go index 90ca9b9b6..5bd8269fd 100644 --- a/api/service/delete.go +++ b/api/service/delete.go @@ -11,10 +11,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/service" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -74,25 +72,15 @@ import ( // DeleteService represents the API handler to remove a service for a build. func DeleteService(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := service.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "service": s.GetNumber(), - "user": u.GetName(), - }).Infof("deleting service %s", entry) + l.Debugf("deleting service %s", entry) // send API call to remove the service err := database.FromContext(c).DeleteService(ctx, s) diff --git a/api/service/get.go b/api/service/get.go index c54d1427d..9538d3300 100644 --- a/api/service/get.go +++ b/api/service/get.go @@ -9,10 +9,8 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/service" - "github.com/go-vela/server/router/middleware/user" ) // @@ -71,22 +69,12 @@ import ( // GetService represents the API handler to get a service for a build. func GetService(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := service.Retrieve(c) - u := user.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "service": s.GetNumber(), - "user": u.GetName(), - }).Infof("reading service %s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) + l.Debugf("reading service %s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) c.JSON(http.StatusOK, s) } diff --git a/api/service/list.go b/api/service/list.go index 2862a28d4..efc4aa5a0 100644 --- a/api/service/list.go +++ b/api/service/list.go @@ -13,9 +13,7 @@ import ( "github.com/go-vela/server/api" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -89,23 +87,14 @@ import ( // ListServices represents the API handler to get a list of services for a build. func ListServices(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("reading services for build %s", entry) + l.Debugf("reading services for build %s", entry) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/service/plan.go b/api/service/plan.go index faed795cb..bc8408ddb 100644 --- a/api/service/plan.go +++ b/api/service/plan.go @@ -7,6 +7,8 @@ import ( "fmt" "time" + "github.com/sirupsen/logrus" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/types/constants" @@ -39,6 +41,14 @@ func PlanServices(ctx context.Context, database database.Interface, p *pipeline. return services, fmt.Errorf("unable to create service %s: %w", s.GetName(), err) } + logrus.WithFields(logrus.Fields{ + "service": s.GetName(), + "service_id": s.GetID(), + "org": b.GetRepo().GetOrg(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), + }).Info("service created") + // populate environment variables from service library // // https://pkg.go.dev/github.com/go-vela/types/library#Service.Environment @@ -59,6 +69,15 @@ func PlanServices(ctx context.Context, database database.Interface, p *pipeline. if err != nil { return services, fmt.Errorf("unable to create service logs for service %s: %w", s.GetName(), err) } + + logrus.WithFields(logrus.Fields{ + "service": s.GetName(), + "service_id": s.GetID(), + "log_id": l.GetID(), // it won't have an ID here, because CreateLog doesn't return the created log + "org": b.GetRepo().GetOrg(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), + }).Info("log for service created") } return services, nil diff --git a/api/service/update.go b/api/service/update.go index 0b2825997..a7e047a60 100644 --- a/api/service/update.go +++ b/api/service/update.go @@ -11,10 +11,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/service" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -82,25 +80,15 @@ import ( // a service for a build. func UpdateService(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := service.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "service": s.GetNumber(), - "user": u.GetName(), - }).Infof("updating service %s", entry) + l.Debugf("updating service %s", entry) // capture body from API request input := new(library.Service) diff --git a/api/step/create.go b/api/step/create.go index 33225a93f..b9a113de4 100644 --- a/api/step/create.go +++ b/api/step/create.go @@ -12,9 +12,7 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" @@ -77,23 +75,14 @@ import ( // a step for a build. func CreateStep(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("creating new step for build %s", entry) + l.Debugf("creating new step for build %s", entry) // capture body from API request input := new(library.Step) @@ -129,5 +118,10 @@ func CreateStep(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "step": s.GetName(), + "step_id": s.GetID(), + }).Infof("step %s created for build %s", s.GetName(), entry) + c.JSON(http.StatusCreated, s) } diff --git a/api/step/delete.go b/api/step/delete.go index 27a3eff1d..b59f9b1d0 100644 --- a/api/step/delete.go +++ b/api/step/delete.go @@ -11,10 +11,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/step" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -73,25 +71,15 @@ import ( // DeleteStep represents the API handler to remove a step for a build. func DeleteStep(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := step.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "step": s.GetNumber(), - "user": u.GetName(), - }).Infof("deleting step %s", entry) + l.Debugf("deleting step %s", entry) // send API call to remove the step err := database.FromContext(c).DeleteStep(ctx, s) diff --git a/api/step/get.go b/api/step/get.go index 09267ffb3..627937371 100644 --- a/api/step/get.go +++ b/api/step/get.go @@ -9,10 +9,8 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/step" - "github.com/go-vela/server/router/middleware/user" ) // swagger:operation GET /api/v1/repos/{org}/{repo}/builds/{build}/steps/{step} steps GetStep @@ -66,22 +64,12 @@ import ( // GetStep represents the API handler to get a step for a build. func GetStep(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := step.Retrieve(c) - u := user.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "step": s.GetNumber(), - "user": u.GetName(), - }).Infof("reading step %s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) + l.Debugf("reading step %s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) c.JSON(http.StatusOK, s) } diff --git a/api/step/list.go b/api/step/list.go index 67617b2b6..194235f5c 100644 --- a/api/step/list.go +++ b/api/step/list.go @@ -13,9 +13,7 @@ import ( "github.com/go-vela/server/api" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -89,23 +87,14 @@ import ( // ListSteps represents the API handler to get a list of steps for a build. func ListSteps(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Infof("listing steps for build %s", entry) + l.Debugf("listing steps for build %s", entry) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/step/plan.go b/api/step/plan.go index 6b3faac06..cfb55652e 100644 --- a/api/step/plan.go +++ b/api/step/plan.go @@ -70,6 +70,14 @@ func planStep(ctx context.Context, database database.Interface, scm scm.Service, return nil, fmt.Errorf("unable to create step %s: %w", s.GetName(), err) } + logrus.WithFields(logrus.Fields{ + "step": s.GetName(), + "step_id": s.GetID(), + "org": b.GetRepo().GetOrg(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), + }).Info("step created") + // populate environment variables from step library // // https://pkg.go.dev/github.com/go-vela/types/library#step.Environment @@ -91,6 +99,15 @@ func planStep(ctx context.Context, database database.Interface, scm scm.Service, return nil, fmt.Errorf("unable to create logs for step %s: %w", s.GetName(), err) } + logrus.WithFields(logrus.Fields{ + "step": s.GetName(), + "step_id": s.GetID(), + "log_id": l.GetID(), // it won't have an ID here + "org": b.GetRepo().GetOrg(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), + }).Info("log for step created") + if len(s.GetReportAs()) > 0 { // send API call to set the status on the commit err = scm.StepStatus(ctx, b.GetRepo().GetOwner(), b, s, b.GetRepo().GetOrg(), b.GetRepo().GetName()) diff --git a/api/step/update.go b/api/step/update.go index 87e2fc6dd..c543de2c8 100644 --- a/api/step/update.go +++ b/api/step/update.go @@ -11,10 +11,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/step" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" @@ -83,25 +81,15 @@ import ( // a step for a build. func UpdateStep(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) s := step.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "repo": r.GetName(), - "step": s.GetNumber(), - "user": u.GetName(), - }).Infof("updating step %s", entry) + l.Debugf("updating step %s", entry) // capture body from API request input := new(library.Step) @@ -180,7 +168,7 @@ func UpdateStep(c *gin.Context) { // send API call to set the status on the commit err = scm.FromContext(c).StepStatus(ctx, r.GetOwner(), b, s, r.GetOrg(), r.GetName()) if err != nil { - logrus.Errorf("unable to set commit status for build %s: %v", entry, err) + l.Errorf("unable to set commit status for build %s: %v", entry, err) } } } diff --git a/api/types/dashboard.go b/api/types/dashboard.go index 11c3883ee..08954350f 100644 --- a/api/types/dashboard.go +++ b/api/types/dashboard.go @@ -31,6 +31,8 @@ type BuildPartial struct { // DashCard is an API type that holds the dashboard information as // well as a list of RepoPartials attached to the dashboard. +// +// swagger:model DashCard type DashCard struct { Dashboard *Dashboard `json:"dashboard,omitempty"` Repos []RepoPartial `json:"repos,omitempty"` diff --git a/api/user/create.go b/api/user/create.go index bf37a8472..e856a8736 100644 --- a/api/user/create.go +++ b/api/user/create.go @@ -52,6 +52,7 @@ import ( // CreateUser represents the API handler to create a user. func CreateUser(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) ctx := c.Request.Context() @@ -70,9 +71,9 @@ func CreateUser(c *gin.Context) { // update engine logger with API metadata // // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "user": u.GetName(), - }).Infof("creating new user %s", input.GetName()) + }).Debugf("creating new user %s", input.GetName()) // send API call to create the user user, err := database.FromContext(c).CreateUser(ctx, input) @@ -84,5 +85,10 @@ func CreateUser(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "user": user.GetName(), + "user_id": user.GetID(), + }).Info("user created") + c.JSON(http.StatusCreated, user) } diff --git a/api/user/create_token.go b/api/user/create_token.go index 6a3bb2678..2eb0c9028 100644 --- a/api/user/create_token.go +++ b/api/user/create_token.go @@ -44,15 +44,11 @@ import ( // a user token. func CreateToken(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("composing token for user %s", u.GetName()) + l.Debugf("composing token for user %s", u.GetName()) tm := c.MustGet("token-manager").(*token.Manager) @@ -78,5 +74,7 @@ func CreateToken(c *gin.Context) { return } + l.Info("user updated - token created") + c.JSON(http.StatusOK, library.Token{Token: &at}) } diff --git a/api/user/delete.go b/api/user/delete.go index 35c026736..e3cd1e184 100644 --- a/api/user/delete.go +++ b/api/user/delete.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -50,16 +49,11 @@ import ( // DeleteUser represents the API handler to remove a user. func DeleteUser(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) user := util.PathParameter(c, "user") ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("deleting user %s", user) + l.Debugf("deleting user %s", user) // send API call to capture the user u, err := database.FromContext(c).GetUserForName(ctx, user) diff --git a/api/user/delete_token.go b/api/user/delete_token.go index 1f21a7a24..8ee9f27da 100644 --- a/api/user/delete_token.go +++ b/api/user/delete_token.go @@ -44,15 +44,11 @@ import ( // and recreate a user token. func DeleteToken(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("revoking token for user %s", u.GetName()) + l.Debugf("revoking token for user %s", u.GetName()) tm := c.MustGet("token-manager").(*token.Manager) @@ -78,5 +74,7 @@ func DeleteToken(c *gin.Context) { return } + l.Info("user updated - token rotated") + c.JSON(http.StatusOK, library.Token{Token: &at}) } diff --git a/api/user/get.go b/api/user/get.go index 708eb56bb..5d39b9518 100644 --- a/api/user/get.go +++ b/api/user/get.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -46,16 +45,11 @@ import ( // GetUser represents the API handler to get a user. func GetUser(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) user := util.PathParameter(c, "user") ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("reading user %s", user) + l.Debugf("reading user %s", user) // send API call to capture the user u, err := database.FromContext(c).GetUserForName(ctx, user) diff --git a/api/user/get_current.go b/api/user/get_current.go index 1cd21526d..a33fab4d2 100644 --- a/api/user/get_current.go +++ b/api/user/get_current.go @@ -34,14 +34,10 @@ import ( // currently authenticated user. func GetCurrentUser(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("reading current user %s", u.GetName()) + l.Debugf("reading current user %s", u.GetName()) c.JSON(http.StatusOK, u) } diff --git a/api/user/get_source.go b/api/user/get_source.go index b74639f96..19b75eea3 100644 --- a/api/user/get_source.go +++ b/api/user/get_source.go @@ -42,15 +42,11 @@ import ( // GetSourceRepos represents the API handler to get a list of repos for a user. func GetSourceRepos(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("reading available SCM repos for user %s", u.GetName()) + l.Debugf("reading available SCM repos for user %s", u.GetName()) // variables to capture requested data dbRepos := []*types.Repo{} diff --git a/api/user/list.go b/api/user/list.go index d4f5ad0ff..12dde536c 100644 --- a/api/user/list.go +++ b/api/user/list.go @@ -12,7 +12,6 @@ import ( "github.com/go-vela/server/api" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -67,15 +66,10 @@ import ( // ListUsers represents the API handler to get a list of users. func ListUsers(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Info("reading lite users") + l.Debug("reading lite users") // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/user/update.go b/api/user/update.go index 9ebd37c36..f356bd956 100644 --- a/api/user/update.go +++ b/api/user/update.go @@ -11,7 +11,6 @@ import ( "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -61,16 +60,11 @@ import ( // UpdateUser represents the API handler to update a user. func UpdateUser(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) user := util.PathParameter(c, "user") ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("updating user %s", user) + l.Debugf("updating user %s", user) // capture body from API request input := new(types.User) @@ -85,7 +79,7 @@ func UpdateUser(c *gin.Context) { } // send API call to capture the user - u, err = database.FromContext(c).GetUserForName(ctx, user) + u, err := database.FromContext(c).GetUserForName(ctx, user) if err != nil { retErr := fmt.Errorf("unable to get user %s: %w", user, err) diff --git a/api/user/update_current.go b/api/user/update_current.go index 3853611ec..d73bb445d 100644 --- a/api/user/update_current.go +++ b/api/user/update_current.go @@ -53,15 +53,11 @@ import ( // update the currently authenticated user. func UpdateCurrentUser(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Infof("updating current user %s", u.GetName()) + l.Debugf("updating current user %s", u.GetName()) // capture body from API request input := new(types.User) diff --git a/api/webhook/post.go b/api/webhook/post.go index ddc0a047e..e4ac32c3d 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -82,12 +82,13 @@ var baseErr = "unable to process webhook" // //nolint:funlen,gocyclo // ignore function length and cyclomatic complexity func PostWebhook(c *gin.Context) { - logrus.Info("webhook received") - // capture middleware values m := c.MustGet("metadata").(*internal.Metadata) + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() + l.Debug("webhook received") + // duplicate request so we can perform operations on the request body // // https://golang.org/pkg/net/http/#Request.Clone @@ -142,8 +143,8 @@ func PostWebhook(c *gin.Context) { h, r, b := webhook.Hook, webhook.Repo, webhook.Build - logrus.Debugf("hook generated from SCM: %v", h) - logrus.Debugf("repo generated from SCM: %v", r) + l.Debugf("hook generated from SCM: %v", h) + l.Debugf("repo generated from SCM: %v", r) // if event is repository event, handle separately and return if strings.EqualFold(h.GetEvent(), constants.EventRepository) { @@ -173,7 +174,7 @@ func PostWebhook(c *gin.Context) { return } - logrus.Debugf(`build author: %s, + l.Debugf(`build author: %s, build branch: %s, build commit: %s, build ref: %s`, @@ -191,8 +192,16 @@ func PostWebhook(c *gin.Context) { // send API call to update the webhook _, err = database.FromContext(c).UpdateHook(ctx, h) if err != nil { - logrus.Errorf("unable to update webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err) + l.Errorf("unable to update webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err) } + + l.WithFields(logrus.Fields{ + "hook": h.GetNumber(), + "hook_id": h.GetID(), + "org": r.GetOrg(), + "repo": r.GetName(), + "repo_id": r.GetID(), + }).Info("hook updated") }() // send API call to capture parsed repo from webhook @@ -261,6 +270,13 @@ func PostWebhook(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "hook": h.GetNumber(), + "hook_id": h.GetID(), + "org": repo.GetOrg(), + "repo": repo.GetName(), + }).Info("hook created") + // verify the webhook from the source control provider if c.Value("webhookvalidation").(bool) { err = scm.FromContext(c).VerifyWebhook(ctx, dupRequest, repo) @@ -371,7 +387,7 @@ func PostWebhook(c *gin.Context) { deployment.SetRepoID(repo.GetID()) deployment.SetBuilds([]*library.Build{b.ToLibrary()}) - _, err := database.FromContext(c).CreateDeployment(c, deployment) + dr, err := database.FromContext(c).CreateDeployment(c, deployment) if err != nil { retErr := fmt.Errorf("%s: failed to create deployment %s/%d: %w", baseErr, repo.GetFullName(), deployment.GetNumber(), err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -381,6 +397,14 @@ func PostWebhook(c *gin.Context) { return } + + l.WithFields(logrus.Fields{ + "deployment": dr.GetNumber(), + "deployment_id": dr.GetID(), + "org": repo.GetOrg(), + "repo": repo.GetName(), + "repo_id": repo.GetID(), + }).Info("deployment created") } else { retErr := fmt.Errorf("%s: failed to get deployment %s/%d: %w", baseErr, repo.GetFullName(), webhook.Deployment.GetNumber(), err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -405,6 +429,14 @@ func PostWebhook(c *gin.Context) { return } + + l.WithFields(logrus.Fields{ + "deployment": d.GetNumber(), + "deployment_id": d.GetID(), + "org": repo.GetOrg(), + "repo": repo.GetName(), + "repo_id": repo.GetID(), + }).Info("deployment updated") } } @@ -416,27 +448,43 @@ func PostWebhook(c *gin.Context) { // fetch pending and running builds rBs, err := database.FromContext(c).ListPendingAndRunningBuildsForRepo(c, repo) if err != nil { - logrus.Errorf("unable to fetch pending and running builds for %s: %v", repo.GetFullName(), err) + l.Errorf("unable to fetch pending and running builds for %s: %v", repo.GetFullName(), err) } + l.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + "org": repo.GetOrg(), + "repo": repo.GetName(), + "repo_id": repo.GetID(), + }).Debugf("found %d pending/running builds", len(rBs)) + for _, rB := range rBs { // call auto cancel routine canceled, err := build.AutoCancel(c, b, rB, p.Metadata.AutoCancel) if err != nil { // continue cancel loop if error, but log based on type of error if canceled { - logrus.Errorf("unable to update canceled build error message: %v", err) + l.Errorf("unable to update canceled build error message: %v", err) } else { - logrus.Errorf("unable to cancel running build: %v", err) + l.Errorf("unable to cancel running build: %v", err) } } + + l.WithFields(logrus.Fields{ + "build": rB.GetNumber(), + "build_id": rB.GetID(), + "org": repo.GetOrg(), + "repo": repo.GetName(), + "repo_id": repo.GetID(), + }).Debug("auto-canceled build") } } }() // if the webhook was from a Pull event from a forked repository, verify it is allowed to run if webhook.PullRequest.IsFromFork { - logrus.Tracef("inside %s workflow for fork PR build %s/%d", repo.GetApproveBuild(), r.GetFullName(), b.GetNumber()) + l.Tracef("inside %s workflow for fork PR build %s/%d", repo.GetApproveBuild(), r.GetFullName(), b.GetNumber()) switch repo.GetApproveBuild() { case constants.ApproveForkAlways: @@ -458,7 +506,7 @@ func PostWebhook(c *gin.Context) { return } - logrus.Debugf("fork PR build %s/%d automatically running without approval", repo.GetFullName(), b.GetNumber()) + l.Debugf("fork PR build %s/%d automatically running without approval", repo.GetFullName(), b.GetNumber()) case constants.ApproveOnce: // determine if build sender is in the contributors list for the repo // @@ -482,14 +530,14 @@ func PostWebhook(c *gin.Context) { case constants.ApproveNever: fallthrough default: - logrus.Debugf("fork PR build %s/%d automatically running without approval", repo.GetFullName(), b.GetNumber()) + l.Debugf("fork PR build %s/%d automatically running without approval", repo.GetFullName(), b.GetNumber()) } } // send API call to set the status on the commit err = scm.FromContext(c).Status(ctx, repo.GetOwner(), b, repo.GetOrg(), repo.GetName()) if err != nil { - logrus.Errorf("unable to set commit status for %s/%d: %v", repo.GetFullName(), b.GetNumber(), err) + l.Errorf("unable to set commit status for %s/%d: %v", repo.GetFullName(), b.GetNumber(), err) } // publish the build to the queue @@ -505,14 +553,28 @@ func PostWebhook(c *gin.Context) { // handleRepositoryEvent is a helper function that processes repository events from the SCM and updates // the database resources with any relevant changes resulting from the event, such as name changes, transfers, etc. func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *internal.Metadata, h *library.Hook, r *types.Repo) (*types.Repo, error) { - logrus.Debugf("webhook is repository event, making necessary updates to repo %s", r.GetFullName()) + l := c.MustGet("logger").(*logrus.Entry) + + l = l.WithFields(logrus.Fields{ + "event_type": h.GetEvent(), + }) + + l.Debugf("webhook is repository event, making necessary updates to repo %s", r.GetFullName()) defer func() { // send API call to update the webhook - _, err := database.FromContext(c).CreateHook(ctx, h) + hr, err := database.FromContext(c).CreateHook(ctx, h) if err != nil { - logrus.Errorf("unable to create webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err) + l.Errorf("unable to create webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err) } + + l.WithFields(logrus.Fields{ + "hook": hr.GetNumber(), + "hook_id": hr.GetID(), + "org": r.GetOrg(), + "repo": r.GetName(), + "repo_id": r.GetID(), + }).Info("hook created") }() switch h.GetEventAction() { @@ -529,7 +591,7 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *internal.Meta return r, nil // if action is archived, unarchived, or edited, perform edits to relevant repo fields case "archived", "unarchived", constants.ActionEdited: - logrus.Debugf("repository action %s for %s", h.GetEventAction(), r.GetFullName()) + l.Debugf("repository action %s for %s", h.GetEventAction(), r.GetFullName()) // send call to get repository from database dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName()) if err != nil { @@ -585,6 +647,12 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *internal.Meta return nil, err } + l.WithFields(logrus.Fields{ + "org": dbRepo.GetOrg(), + "repo": dbRepo.GetName(), + "repo_id": dbRepo.GetID(), + }).Info("repo updated") + return dbRepo, nil // all other repo event actions are skippable default: @@ -597,7 +665,13 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *internal.Meta // that repo to its new name in order to preserve it. It also updates the secrets // associated with that repo as well as build links for the UI. func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gin.Context, m *internal.Metadata) (*types.Repo, error) { - logrus.Infof("renaming repository from %s to %s", r.GetPreviousName(), r.GetName()) + l := c.MustGet("logger").(*logrus.Entry) + + l = l.WithFields(logrus.Fields{ + "event_type": h.GetEvent(), + }) + + l.Debugf("renaming repository from %s to %s", r.GetPreviousName(), r.GetName()) // get any matching hook with the repo's unique webhook ID in the SCM hook, err := database.FromContext(c).GetHookByWebhookID(ctx, h.GetWebhookID()) @@ -662,6 +736,12 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi if err != nil { return nil, fmt.Errorf("unable to update secret for repo %s/%s: %w", dbR.GetOrg(), dbR.GetName(), err) } + + l.WithFields(logrus.Fields{ + "secret_id": secret.GetID(), + "repo": secret.GetRepo(), + "org": secret.GetOrg(), + }).Info("secret updated") } // get total number of builds associated with repository @@ -694,6 +774,14 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi if err != nil { return nil, fmt.Errorf("unable to update build for repo %s: %w", dbR.GetFullName(), err) } + + l.WithFields(logrus.Fields{ + "build_id": build.GetID(), + "build": build.GetNumber(), + "org": dbR.GetOrg(), + "repo": dbR.GetName(), + "repo_id": dbR.GetID(), + }).Info("build updated") } // update the repo name information @@ -707,7 +795,7 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi // update the repo in the database dbR, err = database.FromContext(c).UpdateRepo(ctx, dbR) if err != nil { - retErr := fmt.Errorf("%s: failed to update repo %s/%s in database", baseErr, dbR.GetOrg(), dbR.GetName()) + retErr := fmt.Errorf("%s: failed to update repo %s/%s", baseErr, dbR.GetOrg(), dbR.GetName()) util.HandleError(c, http.StatusBadRequest, retErr) h.SetStatus(constants.StatusFailure) @@ -716,13 +804,30 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi return nil, retErr } + l.WithFields(logrus.Fields{ + "org": dbR.GetOrg(), + "repo": dbR.GetName(), + "repo_id": dbR.GetID(), + }).Infof("repo updated in database (previous name: %s)", r.GetPreviousName()) + return dbR, nil } // gatekeepBuild is a helper function that will set the status of a build to 'pending approval' and // send a status update to the SCM. func gatekeepBuild(c *gin.Context, b *types.Build, r *types.Repo) error { - logrus.Debugf("fork PR build %s/%d waiting for approval", r.GetFullName(), b.GetNumber()) + l := c.MustGet("logger").(*logrus.Entry) + + l = l.WithFields(logrus.Fields{ + "org": r.GetOrg(), + "repo": r.GetName(), + "repo_id": r.GetID(), + "build": b.GetNumber(), + "build_id": b.GetID(), + }) + + l.Debug("fork PR build waiting for approval") + b.SetStatus(constants.StatusPendingApproval) _, err := database.FromContext(c).UpdateBuild(c, b) @@ -730,6 +835,8 @@ func gatekeepBuild(c *gin.Context, b *types.Build, r *types.Repo) error { return fmt.Errorf("unable to update build for %s/%d: %w", r.GetFullName(), b.GetNumber(), err) } + l.Info("build updated") + // update the build components to pending approval status err = build.UpdateComponentStatuses(c, b, constants.StatusPendingApproval) if err != nil { @@ -739,7 +846,7 @@ func gatekeepBuild(c *gin.Context, b *types.Build, r *types.Repo) error { // send API call to set the status on the commit err = scm.FromContext(c).Status(c, r.GetOwner(), b, r.GetOrg(), r.GetName()) if err != nil { - logrus.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), b.GetNumber(), err) + l.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), b.GetNumber(), err) } return nil diff --git a/api/worker/create.go b/api/worker/create.go index c8af50b5e..cfac76e54 100644 --- a/api/worker/create.go +++ b/api/worker/create.go @@ -15,7 +15,6 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/internal/token" "github.com/go-vela/server/router/middleware/claims" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" @@ -59,7 +58,7 @@ import ( // create a worker. func CreateWorker(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) ctx := c.Request.Context() @@ -86,15 +85,9 @@ func CreateWorker(c *gin.Context) { input.SetLastCheckedIn(time.Now().Unix()) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - "worker": input.GetHostname(), - }).Infof("creating new worker %s", input.GetHostname()) + l.Debugf("creating new worker %s", input.GetHostname()) - _, err = database.FromContext(c).CreateWorker(ctx, input) + w, err := database.FromContext(c).CreateWorker(ctx, input) if err != nil { retErr := fmt.Errorf("unable to create worker: %w", err) @@ -103,12 +96,18 @@ func CreateWorker(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "worker": w.GetHostname(), + "worker_id": w.GetID(), + }).Info("worker created") + switch cl.TokenType { // if symmetric token configured, send back symmetric token case constants.ServerWorkerTokenType: if secret, ok := c.Value("secret").(string); ok { tkn := new(library.Token) tkn.SetToken(secret) + c.JSON(http.StatusCreated, tkn) return diff --git a/api/worker/delete.go b/api/worker/delete.go index 84f1442c0..a31ed9d44 100644 --- a/api/worker/delete.go +++ b/api/worker/delete.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/router/middleware/worker" "github.com/go-vela/server/util" ) @@ -55,17 +54,11 @@ import ( // DeleteWorker represents the API handler to remove a worker. func DeleteWorker(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) w := worker.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - "worker": w.GetHostname(), - }).Infof("deleting worker %s", w.GetHostname()) + l.Debugf("deleting worker %s", w.GetHostname()) // send API call to remove the step err := database.FromContext(c).DeleteWorker(ctx, w) diff --git a/api/worker/get.go b/api/worker/get.go index c7d3f5227..23af82124 100644 --- a/api/worker/get.go +++ b/api/worker/get.go @@ -11,7 +11,6 @@ import ( "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/router/middleware/worker" "github.com/go-vela/server/util" ) @@ -56,17 +55,11 @@ import ( // GetWorker represents the API handler to get a worker. func GetWorker(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) w := worker.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - "worker": w.GetHostname(), - }).Infof("reading worker %s", w.GetHostname()) + l.Debugf("reading worker %s", w.GetHostname()) rBs := []*types.Build{} diff --git a/api/worker/list.go b/api/worker/list.go index eb20d58f8..5c0584809 100644 --- a/api/worker/list.go +++ b/api/worker/list.go @@ -13,7 +13,6 @@ import ( "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -63,15 +62,10 @@ import ( // ListWorkers represents the API handler to get a list of workers. func ListWorkers(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - }).Info("reading workers") + l.Debug("reading workers") active := c.Query("active") diff --git a/api/worker/refresh.go b/api/worker/refresh.go index 5fca4b884..51098efe2 100644 --- a/api/worker/refresh.go +++ b/api/worker/refresh.go @@ -61,6 +61,7 @@ import ( // refresh the auth token for a worker. func Refresh(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) w := worker.Retrieve(c) cl := claims.Retrieve(c) ctx := c.Request.Context() @@ -69,10 +70,7 @@ func Refresh(c *gin.Context) { if !strings.EqualFold(cl.TokenType, constants.ServerWorkerTokenType) && !strings.EqualFold(cl.Subject, w.GetHostname()) { retErr := fmt.Errorf("unable to refresh worker auth: claims subject %s does not match worker hostname %s", cl.Subject, w.GetHostname()) - logrus.WithFields(logrus.Fields{ - "subject": cl.Subject, - "worker": w.GetHostname(), - }).Warnf("attempted refresh of worker %s using token from worker %s", w.GetHostname(), cl.Subject) + l.Warnf("attempted refresh of worker %s using token from worker %s", w.GetHostname(), cl.Subject) util.HandleError(c, http.StatusBadRequest, retErr) @@ -92,12 +90,9 @@ func Refresh(c *gin.Context) { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "worker": w.GetHostname(), - }).Infof("refreshing worker %s authentication", w.GetHostname()) + l.Info("worker updated - check-in time updated") + + l.Debugf("refreshing worker %s authentication", w.GetHostname()) switch cl.TokenType { // if symmetric token configured, send back symmetric token @@ -105,6 +100,7 @@ func Refresh(c *gin.Context) { if secret, ok := c.Value("secret").(string); ok { tkn := new(library.Token) tkn.SetToken(secret) + c.JSON(http.StatusOK, tkn) return diff --git a/api/worker/update.go b/api/worker/update.go index b717f9838..199928349 100644 --- a/api/worker/update.go +++ b/api/worker/update.go @@ -11,7 +11,6 @@ import ( "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/router/middleware/worker" "github.com/go-vela/server/util" ) @@ -63,17 +62,11 @@ import ( // update a worker. func UpdateWorker(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) w := worker.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": u.GetName(), - "worker": w.GetHostname(), - }).Infof("updating worker %s", w.GetHostname()) + l.Debugf("updating worker %s", w.GetHostname()) // capture body from API request input := new(types.Worker) diff --git a/cmd/vela-server/metadata.go b/cmd/vela-server/metadata.go index f14d9ee12..11da44af4 100644 --- a/cmd/vela-server/metadata.go +++ b/cmd/vela-server/metadata.go @@ -13,7 +13,7 @@ import ( // helper function to setup the metadata from the CLI arguments. func setupMetadata(c *cli.Context) (*internal.Metadata, error) { - logrus.Debug("Creating metadata from CLI configuration") + logrus.Debug("creating metadata from CLI configuration") m := new(internal.Metadata) @@ -50,7 +50,7 @@ func setupMetadata(c *cli.Context) (*internal.Metadata, error) { // helper function to capture the database metadata from the CLI arguments. func metadataDatabase(c *cli.Context) (*internal.Database, error) { - logrus.Trace("Creating database metadata from CLI configuration") + logrus.Trace("creating database metadata from CLI configuration") u, err := url.Parse(c.String("database.addr")) if err != nil { @@ -65,7 +65,7 @@ func metadataDatabase(c *cli.Context) (*internal.Database, error) { // helper function to capture the queue metadata from the CLI arguments. func metadataQueue(c *cli.Context) (*internal.Queue, error) { - logrus.Trace("Creating queue metadata from CLI configuration") + logrus.Trace("creating queue metadata from CLI configuration") u, err := url.Parse(c.String("queue.addr")) if err != nil { @@ -80,7 +80,7 @@ func metadataQueue(c *cli.Context) (*internal.Queue, error) { // helper function to capture the source metadata from the CLI arguments. func metadataSource(c *cli.Context) (*internal.Source, error) { - logrus.Trace("Creating source metadata from CLI configuration") + logrus.Trace("creating source metadata from CLI configuration") u, err := url.Parse(c.String("scm.addr")) if err != nil { @@ -97,7 +97,7 @@ func metadataSource(c *cli.Context) (*internal.Source, error) { // //nolint:unparam // ignore unparam for now func metadataVela(c *cli.Context) (*internal.Vela, error) { - logrus.Trace("Creating Vela metadata from CLI configuration") + logrus.Trace("creating Vela metadata from CLI configuration") vela := new(internal.Vela) diff --git a/cmd/vela-server/schedule.go b/cmd/vela-server/schedule.go index 3709df9a8..422dcd710 100644 --- a/cmd/vela-server/schedule.go +++ b/cmd/vela-server/schedule.go @@ -122,6 +122,11 @@ func processSchedules(ctx context.Context, start time.Time, settings *settings.P continue } + logrus.WithFields(logrus.Fields{ + "schedule": schedule.GetName(), + "schedule_id": schedule.GetID(), + }).Info("schedule updated - scheduled at set") + // process the schedule and trigger a new build err = processSchedule(ctx, schedule, settings, compiler, database, metadata, queue, scm) if err != nil { @@ -141,6 +146,11 @@ func processSchedules(ctx context.Context, start time.Time, settings *settings.P continue } + + logrus.WithFields(logrus.Fields{ + "schedule": schedule.GetName(), + "schedule_id": schedule.GetID(), + }).Info("schedule updated - error message cleared") } } @@ -241,4 +251,9 @@ func handleError(ctx context.Context, database database.Interface, err error, sc if err != nil { logrus.WithError(err).Warnf("%s %s: %s", scheduleErr, schedule.GetName(), err.Error()) } + + logrus.WithFields(logrus.Fields{ + "schedule": schedule.GetName(), + "schedule_id": schedule.GetID(), + }).Info("schedule updated - error message set") } diff --git a/cmd/vela-server/scm.go b/cmd/vela-server/scm.go index 4cf76a740..c8c441442 100644 --- a/cmd/vela-server/scm.go +++ b/cmd/vela-server/scm.go @@ -11,7 +11,7 @@ import ( // helper function to setup the scm from the CLI arguments. func setupSCM(c *cli.Context) (scm.Service, error) { - logrus.Debug("Creating scm client from CLI configuration") + logrus.Debug("creating scm client from CLI configuration") // scm configuration _setup := &scm.Setup{ diff --git a/cmd/vela-server/secret.go b/cmd/vela-server/secret.go index 88312bd6d..2ca65615a 100644 --- a/cmd/vela-server/secret.go +++ b/cmd/vela-server/secret.go @@ -13,7 +13,7 @@ import ( // helper function to setup the secrets engines from the CLI arguments. func setupSecrets(c *cli.Context, d database.Interface) (map[string]secret.Service, error) { - logrus.Debug("Creating secret clients from CLI configuration") + logrus.Debug("creating secret clients from CLI configuration") secrets := make(map[string]secret.Service) diff --git a/cmd/vela-server/server.go b/cmd/vela-server/server.go index 9472e9c54..43b7a8f3c 100644 --- a/cmd/vela-server/server.go +++ b/cmd/vela-server/server.go @@ -145,6 +145,8 @@ func server(c *cli.Context) error { if err != nil { return err } + + logrus.Info("initial platform settings created") } // update any internal settings, this occurs in middleware diff --git a/cmd/vela-server/token.go b/cmd/vela-server/token.go index 525ce1518..5636d0535 100644 --- a/cmd/vela-server/token.go +++ b/cmd/vela-server/token.go @@ -14,7 +14,7 @@ import ( // helper function to setup the tokenmanager from the CLI arguments. func setupTokenManager(c *cli.Context, db database.Interface) (*token.Manager, error) { - logrus.Debug("Creating token manager from CLI configuration") + logrus.Debug("creating token manager from CLI configuration") tm := &token.Manager{ PrivateKeyHMAC: c.String("vela-server-private-key"), diff --git a/cmd/vela-server/validate.go b/cmd/vela-server/validate.go index 3ceb2b330..605e0707c 100644 --- a/cmd/vela-server/validate.go +++ b/cmd/vela-server/validate.go @@ -13,7 +13,7 @@ import ( ) func validate(c *cli.Context) error { - logrus.Debug("Validating CLI configuration") + logrus.Debug("validating CLI configuration") // validate core configuration err := validateCore(c) @@ -32,7 +32,7 @@ func validate(c *cli.Context) error { // helper function to validate the core CLI configuration. func validateCore(c *cli.Context) error { - logrus.Trace("Validating core CLI configuration") + logrus.Trace("validating core CLI configuration") if len(c.String("server-addr")) == 0 { return fmt.Errorf("server-addr (VELA_ADDR or VELA_HOST) flag is not properly configured") @@ -110,7 +110,7 @@ func validateCore(c *cli.Context) error { // helper function to validate the compiler CLI configuration. func validateCompiler(c *cli.Context) error { - logrus.Trace("Validating compiler CLI configuration") + logrus.Trace("validating compiler CLI configuration") if c.Bool("github-driver") { if len(c.String("github-url")) == 0 { diff --git a/compiler/native/native.go b/compiler/native/native.go index 93d93c5ea..63e526f2e 100644 --- a/compiler/native/native.go +++ b/compiler/native/native.go @@ -49,7 +49,7 @@ type client struct { // //nolint:revive // ignore returning unexported client func FromCLIContext(ctx *cli.Context) (*client, error) { - logrus.Debug("Creating registry clients from CLI configuration") + logrus.Debug("creating registry clients from CLI configuration") c := new(client) @@ -107,14 +107,14 @@ func FromCLIContext(ctx *cli.Context) (*client, error) { // setupGithub is a helper function to setup the // Github registry service from the CLI arguments. func setupGithub() (registry.Service, error) { - logrus.Tracef("Creating %s registry client from CLI configuration", "github") + logrus.Tracef("creating %s registry client from CLI configuration", "github") return github.New("", "") } // setupPrivateGithub is a helper function to setup the // Github registry service from the CLI arguments. func setupPrivateGithub(addr, token string) (registry.Service, error) { - logrus.Tracef("Creating private %s registry client from CLI configuration", "github") + logrus.Tracef("creating private %s registry client from CLI configuration", "github") return github.New(addr, token) } diff --git a/database/build/build.go b/database/build/build.go index 0ff9871bd..b425731f3 100644 --- a/database/build/build.go +++ b/database/build/build.go @@ -62,7 +62,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating build database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of builds table and indexes in the database") + e.logger.Warning("skipping creation of builds table and indexes") return e, nil } diff --git a/database/build/clean.go b/database/build/clean.go index 5c239a57e..b7d876d16 100644 --- a/database/build/clean.go +++ b/database/build/clean.go @@ -15,7 +15,7 @@ import ( // CleanBuilds updates builds to an error with a provided message with a created timestamp prior to a defined moment. func (e *engine) CleanBuilds(ctx context.Context, msg string, before int64) (int64, error) { - logrus.Tracef("cleaning pending or running builds in the database created prior to %d", before) + logrus.Tracef("cleaning pending or running builds created prior to %d", before) b := new(api.Build) b.SetStatus(constants.StatusError) diff --git a/database/build/count.go b/database/build/count.go index 02960a50d..3e86611d9 100644 --- a/database/build/count.go +++ b/database/build/count.go @@ -10,7 +10,7 @@ import ( // CountBuilds gets the count of all builds from the database. func (e *engine) CountBuilds(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all builds from the database") + e.logger.Tracef("getting count of all builds") // variable to store query results var b int64 diff --git a/database/build/count_deployment.go b/database/build/count_deployment.go index 08cf80951..46c8170fc 100644 --- a/database/build/count_deployment.go +++ b/database/build/count_deployment.go @@ -15,7 +15,7 @@ import ( func (e *engine) CountBuildsForDeployment(ctx context.Context, d *library.Deployment, filters map[string]interface{}) (int64, error) { e.logger.WithFields(logrus.Fields{ "deployment": d.GetURL(), - }).Tracef("getting count of builds for deployment %s from the database", d.GetURL()) + }).Tracef("getting count of builds for deployment %s", d.GetURL()) // variable to store query results var b int64 diff --git a/database/build/count_org.go b/database/build/count_org.go index 2f2a93119..d9f4105ed 100644 --- a/database/build/count_org.go +++ b/database/build/count_org.go @@ -14,7 +14,7 @@ import ( func (e *engine) CountBuildsForOrg(ctx context.Context, org string, filters map[string]interface{}) (int64, error) { e.logger.WithFields(logrus.Fields{ "org": org, - }).Tracef("getting count of builds for org %s from the database", org) + }).Tracef("getting count of builds for org %s", org) // variable to store query results var b int64 diff --git a/database/build/count_repo.go b/database/build/count_repo.go index cb12e4fdc..298420caa 100644 --- a/database/build/count_repo.go +++ b/database/build/count_repo.go @@ -16,7 +16,7 @@ func (e *engine) CountBuildsForRepo(ctx context.Context, r *api.Repo, filters ma e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("getting count of builds for repo %s from the database", r.GetFullName()) + }).Tracef("getting count of builds for repo %s", r.GetFullName()) // variable to store query results var b int64 diff --git a/database/build/count_status.go b/database/build/count_status.go index e95ec8e1e..524d27d76 100644 --- a/database/build/count_status.go +++ b/database/build/count_status.go @@ -10,7 +10,7 @@ import ( // CountBuildsForStatus gets the count of builds by status from the database. func (e *engine) CountBuildsForStatus(ctx context.Context, status string, filters map[string]interface{}) (int64, error) { - e.logger.Tracef("getting count of builds for status %s from the database", status) + e.logger.Tracef("getting count of builds for status %s", status) // variable to store query results var b int64 diff --git a/database/build/create.go b/database/build/create.go index 0ba65aad5..3a3684102 100644 --- a/database/build/create.go +++ b/database/build/create.go @@ -17,7 +17,7 @@ import ( func (e *engine) CreateBuild(ctx context.Context, b *api.Build) (*api.Build, error) { e.logger.WithFields(logrus.Fields{ "build": b.GetNumber(), - }).Tracef("creating build %d in the database", b.GetNumber()) + }).Tracef("creating build %d", b.GetNumber()) build := types.BuildFromAPI(b) diff --git a/database/build/delete.go b/database/build/delete.go index b2bff7795..2bcb282e4 100644 --- a/database/build/delete.go +++ b/database/build/delete.go @@ -16,7 +16,7 @@ import ( func (e *engine) DeleteBuild(ctx context.Context, b *api.Build) error { e.logger.WithFields(logrus.Fields{ "build": b.GetNumber(), - }).Tracef("deleting build %d from the database", b.GetNumber()) + }).Tracef("deleting build %d", b.GetNumber()) build := types.BuildFromAPI(b) diff --git a/database/build/get.go b/database/build/get.go index a780b1721..caaf1fe0c 100644 --- a/database/build/get.go +++ b/database/build/get.go @@ -12,7 +12,7 @@ import ( // GetBuild gets a build by ID from the database. func (e *engine) GetBuild(ctx context.Context, id int64) (*api.Build, error) { - e.logger.Tracef("getting build %d from the database", id) + e.logger.Tracef("getting build %d", id) // variable to store query results b := new(types.Build) diff --git a/database/build/get_repo.go b/database/build/get_repo.go index 8a13009c2..b144cf432 100644 --- a/database/build/get_repo.go +++ b/database/build/get_repo.go @@ -18,7 +18,7 @@ func (e *engine) GetBuildForRepo(ctx context.Context, r *api.Repo, number int) ( "build": number, "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("getting build %s/%d from the database", r.GetFullName(), number) + }).Tracef("getting build %s/%d", r.GetFullName(), number) // variable to store query results b := new(types.Build) diff --git a/database/build/index.go b/database/build/index.go index f5bea2898..446395cc9 100644 --- a/database/build/index.go +++ b/database/build/index.go @@ -44,7 +44,7 @@ ON builds (status); // CreateBuildIndexes creates the indexes for the builds table in the database. func (e *engine) CreateBuildIndexes(ctx context.Context) error { - e.logger.Tracef("creating indexes for builds table in the database") + e.logger.Tracef("creating indexes for builds table") // create the created column index for the builds table err := e.client.Exec(CreateCreatedIndex).Error diff --git a/database/build/last_repo.go b/database/build/last_repo.go index 2795d2f47..fe04af158 100644 --- a/database/build/last_repo.go +++ b/database/build/last_repo.go @@ -19,7 +19,7 @@ func (e *engine) LastBuildForRepo(ctx context.Context, r *api.Repo, branch strin e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("getting last build for repo %s from the database", r.GetFullName()) + }).Tracef("getting last build for repo %s", r.GetFullName()) // variable to store query results b := new(types.Build) diff --git a/database/build/list.go b/database/build/list.go index 2b5176d30..9e3e45c8c 100644 --- a/database/build/list.go +++ b/database/build/list.go @@ -12,7 +12,7 @@ import ( // ListBuilds gets a list of all builds from the database. func (e *engine) ListBuilds(ctx context.Context) ([]*api.Build, error) { - e.logger.Trace("listing all builds from the database") + e.logger.Trace("listing all builds") // variables to store query results and return value count := int64(0) diff --git a/database/build/list_dashboard.go b/database/build/list_dashboard.go index 10d2c378d..8ddc6fa57 100644 --- a/database/build/list_dashboard.go +++ b/database/build/list_dashboard.go @@ -17,7 +17,7 @@ func (e *engine) ListBuildsForDashboardRepo(ctx context.Context, r *api.Repo, br e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("listing builds for repo %s from the database", r.GetFullName()) + }).Tracef("listing builds for repo %s", r.GetFullName()) // variables to store query results and return values b := new([]types.Build) diff --git a/database/build/list_org.go b/database/build/list_org.go index 0799635b4..cfaa1992d 100644 --- a/database/build/list_org.go +++ b/database/build/list_org.go @@ -18,7 +18,7 @@ import ( func (e *engine) ListBuildsForOrg(ctx context.Context, org string, filters map[string]interface{}, page, perPage int) ([]*api.Build, int64, error) { e.logger.WithFields(logrus.Fields{ "org": org, - }).Tracef("listing builds for org %s from the database", org) + }).Tracef("listing builds for org %s", org) // variables to store query results and return values count := int64(0) diff --git a/database/build/list_pending_running.go b/database/build/list_pending_running.go index ae40ece51..858b9c08e 100644 --- a/database/build/list_pending_running.go +++ b/database/build/list_pending_running.go @@ -12,7 +12,7 @@ import ( // ListPendingAndRunningBuilds gets a list of all pending and running builds in the provided timeframe from the database. func (e *engine) ListPendingAndRunningBuilds(ctx context.Context, after string) ([]*api.QueueBuild, error) { - e.logger.Trace("listing all pending and running builds from the database") + e.logger.Trace("listing all pending and running builds") // variables to store query results and return value b := new([]types.QueueBuild) diff --git a/database/build/list_pending_running_repo.go b/database/build/list_pending_running_repo.go index 6213bab29..cc8984265 100644 --- a/database/build/list_pending_running_repo.go +++ b/database/build/list_pending_running_repo.go @@ -12,7 +12,7 @@ import ( // ListPendingAndRunningBuilds gets a list of all pending and running builds in the provided timeframe from the database. func (e *engine) ListPendingAndRunningBuildsForRepo(ctx context.Context, repo *api.Repo) ([]*api.Build, error) { - e.logger.Trace("listing all pending and running builds from the database") + e.logger.Trace("listing all pending and running builds") // variables to store query results and return value b := new([]types.Build) diff --git a/database/build/list_repo.go b/database/build/list_repo.go index 753c941c2..2552c2ea3 100644 --- a/database/build/list_repo.go +++ b/database/build/list_repo.go @@ -19,7 +19,7 @@ func (e *engine) ListBuildsForRepo(ctx context.Context, r *api.Repo, filters map e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("listing builds for repo %s from the database", r.GetFullName()) + }).Tracef("listing builds for repo %s", r.GetFullName()) // variables to store query results and return values count := int64(0) diff --git a/database/build/table.go b/database/build/table.go index 8d1ca461e..8a7ae172c 100644 --- a/database/build/table.go +++ b/database/build/table.go @@ -102,7 +102,7 @@ builds ( // CreateBuildTable creates the builds table in the database. func (e *engine) CreateBuildTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating builds table in the database") + e.logger.Tracef("creating builds table") // handle the driver provided to create the table switch driver { diff --git a/database/build/update.go b/database/build/update.go index 0657470aa..e8ff1f274 100644 --- a/database/build/update.go +++ b/database/build/update.go @@ -17,7 +17,7 @@ import ( func (e *engine) UpdateBuild(ctx context.Context, b *api.Build) (*api.Build, error) { e.logger.WithFields(logrus.Fields{ "build": b.GetNumber(), - }).Tracef("updating build %d in the database", b.GetNumber()) + }).Tracef("updating build %d", b.GetNumber()) build := types.BuildFromAPI(b) diff --git a/database/dashboard/create.go b/database/dashboard/create.go index 5d179e11e..1af56728a 100644 --- a/database/dashboard/create.go +++ b/database/dashboard/create.go @@ -16,7 +16,7 @@ import ( func (e *engine) CreateDashboard(ctx context.Context, d *api.Dashboard) (*api.Dashboard, error) { e.logger.WithFields(logrus.Fields{ "dashboard": d.GetName(), - }).Tracef("creating dashboard %s in the database", d.GetName()) + }).Tracef("creating dashboard %s", d.GetName()) dashboard := types.DashboardFromAPI(d) diff --git a/database/dashboard/dashboard.go b/database/dashboard/dashboard.go index 8cd70c080..b21471114 100644 --- a/database/dashboard/dashboard.go +++ b/database/dashboard/dashboard.go @@ -73,7 +73,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating dashboard database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of dashboards table and indexes in the database") + e.logger.Warning("skipping creation of dashboards table and indexes") return e, nil } diff --git a/database/dashboard/delete.go b/database/dashboard/delete.go index c0bf658ee..2f49cca9f 100644 --- a/database/dashboard/delete.go +++ b/database/dashboard/delete.go @@ -16,7 +16,7 @@ import ( func (e *engine) DeleteDashboard(ctx context.Context, d *api.Dashboard) error { e.logger.WithFields(logrus.Fields{ "dashboard": d.GetID(), - }).Tracef("deleting dashboard %s from the database", d.GetID()) + }).Tracef("deleting dashboard %s", d.GetID()) dashboard := types.DashboardFromAPI(d) diff --git a/database/dashboard/get.go b/database/dashboard/get.go index 1e08391f9..7c29c987f 100644 --- a/database/dashboard/get.go +++ b/database/dashboard/get.go @@ -12,7 +12,7 @@ import ( // GetDashboard gets a dashboard by UUID from the database. func (e *engine) GetDashboard(ctx context.Context, id string) (*api.Dashboard, error) { - e.logger.Tracef("getting dashboard %s from the database", id) + e.logger.Tracef("getting dashboard %s", id) // variable to store query results d := new(types.Dashboard) diff --git a/database/dashboard/table.go b/database/dashboard/table.go index 51d470a99..1cedb30c3 100644 --- a/database/dashboard/table.go +++ b/database/dashboard/table.go @@ -44,7 +44,7 @@ dashboards ( // CreateDashboardTable creates the dashboards table in the database. func (e *engine) CreateDashboardTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating dashboards table in the database") + e.logger.Tracef("creating dashboards table") // handle the driver provided to create the table switch driver { diff --git a/database/dashboard/update.go b/database/dashboard/update.go index f05fe3e28..e13387cb3 100644 --- a/database/dashboard/update.go +++ b/database/dashboard/update.go @@ -16,7 +16,7 @@ import ( func (e *engine) UpdateDashboard(ctx context.Context, d *api.Dashboard) (*api.Dashboard, error) { e.logger.WithFields(logrus.Fields{ "dashboard": d.GetID(), - }).Tracef("creating dashboard %s in the database", d.GetID()) + }).Tracef("creating dashboard %s", d.GetID()) dashboard := types.DashboardFromAPI(d) diff --git a/database/deployment/count.go b/database/deployment/count.go index c83f739c5..26189cf10 100644 --- a/database/deployment/count.go +++ b/database/deployment/count.go @@ -10,7 +10,7 @@ import ( // CountDeployments gets the count of all deployments from the database. func (e *engine) CountDeployments(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all deployments from the database") + e.logger.Tracef("getting count of all deployments") // variable to store query results var d int64 diff --git a/database/deployment/count_repo.go b/database/deployment/count_repo.go index 6b70fff8f..94ed86558 100644 --- a/database/deployment/count_repo.go +++ b/database/deployment/count_repo.go @@ -16,7 +16,7 @@ func (e *engine) CountDeploymentsForRepo(ctx context.Context, r *api.Repo) (int6 e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("getting count of deployments for repo %s from the database", r.GetFullName()) + }).Tracef("getting count of deployments for repo %s", r.GetFullName()) // variable to store query results var d int64 diff --git a/database/deployment/create.go b/database/deployment/create.go index 6ce17e9a8..c766596e8 100644 --- a/database/deployment/create.go +++ b/database/deployment/create.go @@ -16,7 +16,7 @@ import ( func (e *engine) CreateDeployment(ctx context.Context, d *library.Deployment) (*library.Deployment, error) { e.logger.WithFields(logrus.Fields{ "deployment": d.GetID(), - }).Tracef("creating deployment %d in the database", d.GetID()) + }).Tracef("creating deployment %d", d.GetID()) // cast the library type to database type deployment := database.DeploymentFromLibrary(d) diff --git a/database/deployment/delete.go b/database/deployment/delete.go index f50cb9033..606ef8262 100644 --- a/database/deployment/delete.go +++ b/database/deployment/delete.go @@ -16,7 +16,7 @@ import ( func (e *engine) DeleteDeployment(ctx context.Context, d *library.Deployment) error { e.logger.WithFields(logrus.Fields{ "deployment": d.GetID(), - }).Tracef("deleting deployment %d in the database", d.GetID()) + }).Tracef("deleting deployment %d", d.GetID()) // cast the library type to database type deployment := database.DeploymentFromLibrary(d) diff --git a/database/deployment/deployment.go b/database/deployment/deployment.go index 63f661452..cd9192438 100644 --- a/database/deployment/deployment.go +++ b/database/deployment/deployment.go @@ -60,7 +60,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating deployment database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of deployment table and indexes in the database") + e.logger.Warning("skipping creation of deployment table and indexes") return e, nil } diff --git a/database/deployment/get.go b/database/deployment/get.go index 9d84b958e..214dcf57f 100644 --- a/database/deployment/get.go +++ b/database/deployment/get.go @@ -13,7 +13,7 @@ import ( // GetDeployment gets a deployment by ID from the database. func (e *engine) GetDeployment(ctx context.Context, id int64) (*library.Deployment, error) { - e.logger.Tracef("getting deployment %d from the database", id) + e.logger.Tracef("getting deployment %d", id) // variable to store query results d := new(database.Deployment) diff --git a/database/deployment/get_repo.go b/database/deployment/get_repo.go index f000afcda..1558b0428 100644 --- a/database/deployment/get_repo.go +++ b/database/deployment/get_repo.go @@ -20,7 +20,7 @@ func (e *engine) GetDeploymentForRepo(ctx context.Context, r *api.Repo, number i "deployment": number, "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("getting deployment %s/%d from the database", r.GetFullName(), number) + }).Tracef("getting deployment %s/%d", r.GetFullName(), number) // variable to store query results d := new(database.Deployment) diff --git a/database/deployment/index.go b/database/deployment/index.go index 0fa2fac4c..f9b6a3419 100644 --- a/database/deployment/index.go +++ b/database/deployment/index.go @@ -17,7 +17,7 @@ ON deployments (repo_id); // CreateDeploymetsIndexes creates the indexes for the deployments table in the database. func (e *engine) CreateDeploymentIndexes(ctx context.Context) error { - e.logger.Tracef("creating indexes for deployments table in the database") + e.logger.Tracef("creating indexes for deployments table") // create the repo_id column index for the deployments table return e.client.Exec(CreateRepoIDIndex).Error diff --git a/database/deployment/list.go b/database/deployment/list.go index b3f4da509..f395f668b 100644 --- a/database/deployment/list.go +++ b/database/deployment/list.go @@ -13,7 +13,7 @@ import ( // ListDeployments gets a list of all deployments from the database. func (e *engine) ListDeployments(ctx context.Context) ([]*library.Deployment, error) { - e.logger.Trace("listing all deployments from the database") + e.logger.Trace("listing all deployments") // variables to store query results and return value d := new([]database.Deployment) diff --git a/database/deployment/list_repo.go b/database/deployment/list_repo.go index 71fe1ebe5..8f9b40eb4 100644 --- a/database/deployment/list_repo.go +++ b/database/deployment/list_repo.go @@ -19,7 +19,7 @@ func (e *engine) ListDeploymentsForRepo(ctx context.Context, r *api.Repo, page, e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("listing deployments for repo %s from the database", r.GetFullName()) + }).Tracef("listing deployments for repo %s", r.GetFullName()) // variables to store query results and return value d := new([]database.Deployment) diff --git a/database/deployment/table.go b/database/deployment/table.go index 25ac28c78..b519c8abf 100644 --- a/database/deployment/table.go +++ b/database/deployment/table.go @@ -56,7 +56,7 @@ deployments ( // CreateDeploymentTable creates the deployments table in the database. func (e *engine) CreateDeploymentTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating deployments table in the database") + e.logger.Tracef("creating deployments table") // handle the driver provided to create the table switch driver { diff --git a/database/deployment/update.go b/database/deployment/update.go index 191753cbf..c2bbecff0 100644 --- a/database/deployment/update.go +++ b/database/deployment/update.go @@ -16,7 +16,7 @@ import ( func (e *engine) UpdateDeployment(ctx context.Context, d *library.Deployment) (*library.Deployment, error) { e.logger.WithFields(logrus.Fields{ "deployment": d.GetID(), - }).Tracef("updating deployment %d in the database", d.GetID()) + }).Tracef("updating deployment %d", d.GetID()) // cast the library type to database type deployment := database.DeploymentFromLibrary(d) diff --git a/database/executable/pop.go b/database/executable/pop.go index 2696e565e..62dfde193 100644 --- a/database/executable/pop.go +++ b/database/executable/pop.go @@ -14,7 +14,7 @@ import ( // PopBuildExecutable pops a build executable by build_id from the database. func (e *engine) PopBuildExecutable(ctx context.Context, id int64) (*library.BuildExecutable, error) { - e.logger.Tracef("popping build executable for build %d from the database", id) + e.logger.Tracef("popping build executable for build %d", id) // variable to store query results b := new(database.BuildExecutable) @@ -30,7 +30,6 @@ func (e *engine) PopBuildExecutable(ctx context.Context, id int64) (*library.Bui Where("build_id = ?", id). Delete(b). Error - if err != nil { return nil, err } diff --git a/database/executable/table.go b/database/executable/table.go index 82fd1002f..9a683f245 100644 --- a/database/executable/table.go +++ b/database/executable/table.go @@ -36,7 +36,7 @@ build_executables ( // CreateBuildExecutableTable creates the build executables table in the database. func (e *engine) CreateBuildExecutableTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating build_executables table in the database") + e.logger.Tracef("creating build_executables table") // handle the driver provided to create the table switch driver { diff --git a/database/hook/count.go b/database/hook/count.go index 7f6d2449b..ce02e6fa0 100644 --- a/database/hook/count.go +++ b/database/hook/count.go @@ -10,7 +10,7 @@ import ( // CountHooks gets the count of all hooks from the database. func (e *engine) CountHooks(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all hooks from the database") + e.logger.Tracef("getting count of all hooks") // variable to store query results var h int64 diff --git a/database/hook/count_repo.go b/database/hook/count_repo.go index 62bee12a4..aa794b5f1 100644 --- a/database/hook/count_repo.go +++ b/database/hook/count_repo.go @@ -16,7 +16,7 @@ func (e *engine) CountHooksForRepo(ctx context.Context, r *api.Repo) (int64, err e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("getting count of hooks for repo %s from the database", r.GetFullName()) + }).Tracef("getting count of hooks for repo %s", r.GetFullName()) // variable to store query results var h int64 diff --git a/database/hook/create.go b/database/hook/create.go index d71e89e51..c260eb2b5 100644 --- a/database/hook/create.go +++ b/database/hook/create.go @@ -16,7 +16,7 @@ import ( func (e *engine) CreateHook(ctx context.Context, h *library.Hook) (*library.Hook, error) { e.logger.WithFields(logrus.Fields{ "hook": h.GetNumber(), - }).Tracef("creating hook %d in the database", h.GetNumber()) + }).Tracef("creating hook %d", h.GetNumber()) // cast the library type to database type // diff --git a/database/hook/delete.go b/database/hook/delete.go index dca1f900e..0245477af 100644 --- a/database/hook/delete.go +++ b/database/hook/delete.go @@ -16,7 +16,7 @@ import ( func (e *engine) DeleteHook(ctx context.Context, h *library.Hook) error { e.logger.WithFields(logrus.Fields{ "hook": h.GetNumber(), - }).Tracef("deleting hook %d in the database", h.GetNumber()) + }).Tracef("deleting hook %d", h.GetNumber()) // cast the library type to database type // diff --git a/database/hook/get.go b/database/hook/get.go index 6119c0770..82e421dda 100644 --- a/database/hook/get.go +++ b/database/hook/get.go @@ -12,7 +12,7 @@ import ( // GetHook gets a hook by ID from the database. func (e *engine) GetHook(ctx context.Context, id int64) (*library.Hook, error) { - e.logger.Tracef("getting hook %d from the database", id) + e.logger.Tracef("getting hook %d", id) // variable to store query results h := new(database.Hook) diff --git a/database/hook/get_repo.go b/database/hook/get_repo.go index 26fe2c0fb..a88d593df 100644 --- a/database/hook/get_repo.go +++ b/database/hook/get_repo.go @@ -19,7 +19,7 @@ func (e *engine) GetHookForRepo(ctx context.Context, r *api.Repo, number int) (* "hook": number, "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("getting hook %s/%d from the database", r.GetFullName(), number) + }).Tracef("getting hook %s/%d", r.GetFullName(), number) // variable to store query results h := new(database.Hook) diff --git a/database/hook/get_webhook.go b/database/hook/get_webhook.go index d7108ff4c..919092fd7 100644 --- a/database/hook/get_webhook.go +++ b/database/hook/get_webhook.go @@ -12,7 +12,7 @@ import ( // GetHookByWebhookID gets a single hook with a matching webhook id in the database. func (e *engine) GetHookByWebhookID(ctx context.Context, webhookID int64) (*library.Hook, error) { - e.logger.Tracef("getting a hook with webhook id %d from the database", webhookID) + e.logger.Tracef("getting a hook with webhook id %d", webhookID) // variable to store query results h := new(database.Hook) diff --git a/database/hook/hook.go b/database/hook/hook.go index 22816ac83..283dad00a 100644 --- a/database/hook/hook.go +++ b/database/hook/hook.go @@ -60,7 +60,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating hook database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of hooks table and indexes in the database") + e.logger.Warning("skipping creation of hooks table and indexes") return e, nil } diff --git a/database/hook/index.go b/database/hook/index.go index 2b8d4e682..b86c9ac39 100644 --- a/database/hook/index.go +++ b/database/hook/index.go @@ -17,7 +17,7 @@ ON hooks (repo_id); // CreateHookIndexes creates the indexes for the hooks table in the database. func (e *engine) CreateHookIndexes(ctx context.Context) error { - e.logger.Tracef("creating indexes for hooks table in the database") + e.logger.Tracef("creating indexes for hooks table") // create the repo_id column index for the hooks table return e.client.Exec(CreateRepoIDIndex).Error diff --git a/database/hook/last_repo.go b/database/hook/last_repo.go index 388e8e464..710053ad8 100644 --- a/database/hook/last_repo.go +++ b/database/hook/last_repo.go @@ -20,7 +20,7 @@ func (e *engine) LastHookForRepo(ctx context.Context, r *api.Repo) (*library.Hoo e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("getting last hook for repo %s from the database", r.GetFullName()) + }).Tracef("getting last hook for repo %s", r.GetFullName()) // variable to store query results h := new(database.Hook) diff --git a/database/hook/list.go b/database/hook/list.go index 8e0949be4..fcfba1e28 100644 --- a/database/hook/list.go +++ b/database/hook/list.go @@ -12,7 +12,7 @@ import ( // ListHooks gets a list of all hooks from the database. func (e *engine) ListHooks(ctx context.Context) ([]*library.Hook, error) { - e.logger.Trace("listing all hooks from the database") + e.logger.Trace("listing all hooks") // variables to store query results and return value count := int64(0) diff --git a/database/hook/list_repo.go b/database/hook/list_repo.go index b8afd447f..28e80c03e 100644 --- a/database/hook/list_repo.go +++ b/database/hook/list_repo.go @@ -18,7 +18,7 @@ func (e *engine) ListHooksForRepo(ctx context.Context, r *api.Repo, page, perPag e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("listing hooks for repo %s from the database", r.GetFullName()) + }).Tracef("listing hooks for repo %s", r.GetFullName()) // variables to store query results and return value count := int64(0) diff --git a/database/hook/table.go b/database/hook/table.go index c9221e9f4..81ef7157f 100644 --- a/database/hook/table.go +++ b/database/hook/table.go @@ -58,7 +58,7 @@ hooks ( // CreateHookTable creates the hooks table in the database. func (e *engine) CreateHookTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating hooks table in the database") + e.logger.Tracef("creating hooks table") // handle the driver provided to create the table switch driver { diff --git a/database/hook/update.go b/database/hook/update.go index 6c24bfd24..16222a6d1 100644 --- a/database/hook/update.go +++ b/database/hook/update.go @@ -16,7 +16,7 @@ import ( func (e *engine) UpdateHook(ctx context.Context, h *library.Hook) (*library.Hook, error) { e.logger.WithFields(logrus.Fields{ "hook": h.GetNumber(), - }).Tracef("updating hook %d in the database", h.GetNumber()) + }).Tracef("updating hook %d", h.GetNumber()) // cast the library type to database type // diff --git a/database/jwk/create.go b/database/jwk/create.go index 161b3ef4a..0ef8330b9 100644 --- a/database/jwk/create.go +++ b/database/jwk/create.go @@ -17,7 +17,7 @@ import ( func (e *engine) CreateJWK(_ context.Context, j jwk.RSAPublicKey) error { e.logger.WithFields(logrus.Fields{ "jwk": j.KeyID(), - }).Tracef("creating key %s in the database", j.KeyID()) + }).Tracef("creating key %s", j.KeyID()) key := types.JWKFromAPI(j) key.Active = sql.NullBool{Bool: true, Valid: true} diff --git a/database/jwk/get.go b/database/jwk/get.go index 0313b68a5..b324ae3cc 100644 --- a/database/jwk/get.go +++ b/database/jwk/get.go @@ -13,7 +13,7 @@ import ( // GetActiveJWK gets a JWK by UUID (kid) from the database if active. func (e *engine) GetActiveJWK(_ context.Context, id string) (jwk.RSAPublicKey, error) { - e.logger.Tracef("getting key %s from the database", id) + e.logger.Tracef("getting JWK key %s", id) // variable to store query results j := new(types.JWK) diff --git a/database/jwk/jwk.go b/database/jwk/jwk.go index 6b3acc33f..bcce18eb8 100644 --- a/database/jwk/jwk.go +++ b/database/jwk/jwk.go @@ -62,7 +62,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating key set database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of key sets table and indexes in the database") + e.logger.Warning("skipping creation of key sets table and indexes") return e, nil } diff --git a/database/jwk/list.go b/database/jwk/list.go index ed58fa36c..4c29a4c5c 100644 --- a/database/jwk/list.go +++ b/database/jwk/list.go @@ -13,7 +13,7 @@ import ( // ListJWKs gets a list of all configured JWKs from the database. func (e *engine) ListJWKs(_ context.Context) (jwk.Set, error) { - e.logger.Trace("listing all jwks from the database") + e.logger.Trace("listing all JWKs") k := new([]types.JWK) keySet := jwk.NewSet() diff --git a/database/jwk/rotate.go b/database/jwk/rotate.go index 82ff1c422..793584037 100644 --- a/database/jwk/rotate.go +++ b/database/jwk/rotate.go @@ -12,7 +12,7 @@ import ( // RotateKeys removes all inactive keys and sets active keys to inactive. func (e *engine) RotateKeys(_ context.Context) error { - e.logger.Trace("rotating jwks in the database") + e.logger.Trace("rotating jwks") k := types.JWK{} diff --git a/database/jwk/table.go b/database/jwk/table.go index 6e2108de9..b2a40c844 100644 --- a/database/jwk/table.go +++ b/database/jwk/table.go @@ -34,7 +34,7 @@ jwks ( // CreateJWKTable creates the jwks table in the database. func (e *engine) CreateJWKTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating jwks table in the database") + e.logger.Tracef("creating jwks table") // handle the driver provided to create the table switch driver { diff --git a/database/log/count.go b/database/log/count.go index b26066b94..e3b38b316 100644 --- a/database/log/count.go +++ b/database/log/count.go @@ -10,7 +10,7 @@ import ( // CountLogs gets the count of all logs from the database. func (e *engine) CountLogs(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all logs from the database") + e.logger.Tracef("getting count of all logs") // variable to store query results var l int64 diff --git a/database/log/count_build.go b/database/log/count_build.go index 75145d5e3..524ff2960 100644 --- a/database/log/count_build.go +++ b/database/log/count_build.go @@ -11,7 +11,7 @@ import ( // CountLogsForBuild gets the count of logs by build ID from the database. func (e *engine) CountLogsForBuild(ctx context.Context, b *api.Build) (int64, error) { - e.logger.Tracef("getting count of logs for build %d from the database", b.GetID()) + e.logger.Tracef("getting count of logs for build %d", b.GetID()) // variable to store query results var l int64 diff --git a/database/log/create.go b/database/log/create.go index e1c3a9212..fdabab63c 100644 --- a/database/log/create.go +++ b/database/log/create.go @@ -17,9 +17,9 @@ func (e *engine) CreateLog(ctx context.Context, l *library.Log) error { // check what the log entry is for switch { case l.GetServiceID() > 0: - e.logger.Tracef("creating log for service %d for build %d in the database", l.GetServiceID(), l.GetBuildID()) + e.logger.Tracef("creating log for service %d for build %d", l.GetServiceID(), l.GetBuildID()) case l.GetStepID() > 0: - e.logger.Tracef("creating log for step %d for build %d in the database", l.GetStepID(), l.GetBuildID()) + e.logger.Tracef("creating log for step %d for build %d", l.GetStepID(), l.GetBuildID()) } // cast the library type to database type diff --git a/database/log/delete.go b/database/log/delete.go index 7018396de..31f4ea31e 100644 --- a/database/log/delete.go +++ b/database/log/delete.go @@ -15,9 +15,9 @@ func (e *engine) DeleteLog(ctx context.Context, l *library.Log) error { // check what the log entry is for switch { case l.GetServiceID() > 0: - e.logger.Tracef("deleting log for service %d for build %d in the database", l.GetServiceID(), l.GetBuildID()) + e.logger.Tracef("deleting log for service %d for build %d", l.GetServiceID(), l.GetBuildID()) case l.GetStepID() > 0: - e.logger.Tracef("deleting log for step %d for build %d in the database", l.GetStepID(), l.GetBuildID()) + e.logger.Tracef("deleting log for step %d for build %d", l.GetStepID(), l.GetBuildID()) } // cast the library type to database type diff --git a/database/log/get.go b/database/log/get.go index be02ee968..2a825a6da 100644 --- a/database/log/get.go +++ b/database/log/get.go @@ -12,7 +12,7 @@ import ( // GetLog gets a log by ID from the database. func (e *engine) GetLog(ctx context.Context, id int64) (*library.Log, error) { - e.logger.Tracef("getting log %d from the database", id) + e.logger.Tracef("getting log %d", id) // variable to store query results l := new(database.Log) diff --git a/database/log/get_service.go b/database/log/get_service.go index 83d5f2f45..aac501801 100644 --- a/database/log/get_service.go +++ b/database/log/get_service.go @@ -13,7 +13,7 @@ import ( // GetLogForService gets a log by service ID from the database. func (e *engine) GetLogForService(ctx context.Context, s *library.Service) (*library.Log, error) { - e.logger.Tracef("getting log for service %d for build %d from the database", s.GetID(), s.GetBuildID()) + e.logger.Tracef("getting log for service %d for build %d", s.GetID(), s.GetBuildID()) // variable to store query results l := new(database.Log) diff --git a/database/log/get_step.go b/database/log/get_step.go index da6d70cf7..f2ca4a3a1 100644 --- a/database/log/get_step.go +++ b/database/log/get_step.go @@ -13,7 +13,7 @@ import ( // GetLogForStep gets a log by step ID from the database. func (e *engine) GetLogForStep(ctx context.Context, s *library.Step) (*library.Log, error) { - e.logger.Tracef("getting log for step %d for build %d from the database", s.GetID(), s.GetBuildID()) + e.logger.Tracef("getting log for step %d for build %d", s.GetID(), s.GetBuildID()) // variable to store query results l := new(database.Log) diff --git a/database/log/index.go b/database/log/index.go index c167f3825..230a10d8b 100644 --- a/database/log/index.go +++ b/database/log/index.go @@ -17,7 +17,7 @@ ON logs (build_id); // CreateLogIndexes creates the indexes for the logs table in the database. func (e *engine) CreateLogIndexes(ctx context.Context) error { - e.logger.Tracef("creating indexes for logs table in the database") + e.logger.Tracef("creating indexes for logs table") // create the build_id column index for the logs table return e.client.Exec(CreateBuildIDIndex).Error diff --git a/database/log/list.go b/database/log/list.go index ae85a451e..367d8c15e 100644 --- a/database/log/list.go +++ b/database/log/list.go @@ -12,7 +12,7 @@ import ( // ListLogs gets a list of all logs from the database. func (e *engine) ListLogs(ctx context.Context) ([]*library.Log, error) { - e.logger.Trace("listing all logs from the database") + e.logger.Trace("listing all logs") // variables to store query results and return value count := int64(0) diff --git a/database/log/list_build.go b/database/log/list_build.go index f5619d40b..43c117916 100644 --- a/database/log/list_build.go +++ b/database/log/list_build.go @@ -13,7 +13,7 @@ import ( // ListLogsForBuild gets a list of logs by build ID from the database. func (e *engine) ListLogsForBuild(ctx context.Context, b *api.Build, page, perPage int) ([]*library.Log, int64, error) { - e.logger.Tracef("listing logs for build %d from the database", b.GetID()) + e.logger.Tracef("listing logs for build %d", b.GetID()) // variables to store query results and return value count := int64(0) diff --git a/database/log/log.go b/database/log/log.go index a0fa23e5f..e95dc6d04 100644 --- a/database/log/log.go +++ b/database/log/log.go @@ -62,7 +62,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating log database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of logs table and indexes in the database") + e.logger.Warning("skipping creation of logs table and indexes") return e, nil } diff --git a/database/log/table.go b/database/log/table.go index aa8c87cfe..94ec398ff 100644 --- a/database/log/table.go +++ b/database/log/table.go @@ -44,7 +44,7 @@ logs ( // CreateLogTable creates the logs table in the database. func (e *engine) CreateLogTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating logs table in the database") + e.logger.Tracef("creating logs table") // handle the driver provided to create the table switch driver { diff --git a/database/log/update.go b/database/log/update.go index 7929ab0fa..f7a148aae 100644 --- a/database/log/update.go +++ b/database/log/update.go @@ -17,9 +17,9 @@ func (e *engine) UpdateLog(ctx context.Context, l *library.Log) error { // check what the log entry is for switch { case l.GetServiceID() > 0: - e.logger.Tracef("updating log for service %d for build %d in the database", l.GetServiceID(), l.GetBuildID()) + e.logger.Tracef("updating log for service %d for build %d", l.GetServiceID(), l.GetBuildID()) case l.GetStepID() > 0: - e.logger.Tracef("updating log for step %d for build %d in the database", l.GetStepID(), l.GetBuildID()) + e.logger.Tracef("updating log for step %d for build %d", l.GetStepID(), l.GetBuildID()) } // cast the library type to database type diff --git a/database/pipeline/count.go b/database/pipeline/count.go index a251f571b..3252f5edd 100644 --- a/database/pipeline/count.go +++ b/database/pipeline/count.go @@ -10,7 +10,7 @@ import ( // CountPipelines gets the count of all pipelines from the database. func (e *engine) CountPipelines(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all pipelines from the database") + e.logger.Tracef("getting count of all pipelines") // variable to store query results var p int64 diff --git a/database/pipeline/count_repo.go b/database/pipeline/count_repo.go index e2d5a2285..6adf46984 100644 --- a/database/pipeline/count_repo.go +++ b/database/pipeline/count_repo.go @@ -16,7 +16,7 @@ func (e *engine) CountPipelinesForRepo(ctx context.Context, r *api.Repo) (int64, e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("getting count of pipelines for repo %s from the database", r.GetFullName()) + }).Tracef("getting count of pipelines for repo %s", r.GetFullName()) // variable to store query results var p int64 diff --git a/database/pipeline/delete.go b/database/pipeline/delete.go index bba60b2bd..b35cdd7d3 100644 --- a/database/pipeline/delete.go +++ b/database/pipeline/delete.go @@ -16,7 +16,7 @@ import ( func (e *engine) DeletePipeline(ctx context.Context, p *library.Pipeline) error { e.logger.WithFields(logrus.Fields{ "pipeline": p.GetCommit(), - }).Tracef("deleting pipeline %s from the database", p.GetCommit()) + }).Tracef("deleting pipeline %s", p.GetCommit()) // cast the library type to database type // diff --git a/database/pipeline/get.go b/database/pipeline/get.go index 30a7f5d39..8e1c54b13 100644 --- a/database/pipeline/get.go +++ b/database/pipeline/get.go @@ -12,7 +12,7 @@ import ( // GetPipeline gets a pipeline by ID from the database. func (e *engine) GetPipeline(ctx context.Context, id int64) (*library.Pipeline, error) { - e.logger.Tracef("getting pipeline %d from the database", id) + e.logger.Tracef("getting pipeline %d", id) // variable to store query results p := new(database.Pipeline) diff --git a/database/pipeline/get_repo.go b/database/pipeline/get_repo.go index 428e1d84a..0fc493003 100644 --- a/database/pipeline/get_repo.go +++ b/database/pipeline/get_repo.go @@ -19,7 +19,7 @@ func (e *engine) GetPipelineForRepo(ctx context.Context, commit string, r *api.R "org": r.GetOrg(), "pipeline": commit, "repo": r.GetName(), - }).Tracef("getting pipeline %s/%s from the database", r.GetFullName(), commit) + }).Tracef("getting pipeline %s/%s", r.GetFullName(), commit) // variable to store query results p := new(database.Pipeline) diff --git a/database/pipeline/list.go b/database/pipeline/list.go index 2299c8f6f..fd9e077e0 100644 --- a/database/pipeline/list.go +++ b/database/pipeline/list.go @@ -12,7 +12,7 @@ import ( // ListPipelines gets a list of all pipelines from the database. func (e *engine) ListPipelines(ctx context.Context) ([]*library.Pipeline, error) { - e.logger.Trace("listing all pipelines from the database") + e.logger.Trace("listing all pipelines") // variables to store query results and return value count := int64(0) diff --git a/database/pipeline/list_repo.go b/database/pipeline/list_repo.go index 9893ee499..4f855c746 100644 --- a/database/pipeline/list_repo.go +++ b/database/pipeline/list_repo.go @@ -20,7 +20,7 @@ func (e *engine) ListPipelinesForRepo(ctx context.Context, r *api.Repo, page, pe e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("listing pipelines for repo %s from the database", r.GetFullName()) + }).Tracef("listing pipelines for repo %s", r.GetFullName()) // variables to store query results and return values count := int64(0) diff --git a/database/repo/count.go b/database/repo/count.go index c00001b9f..165bf0f0b 100644 --- a/database/repo/count.go +++ b/database/repo/count.go @@ -10,7 +10,7 @@ import ( // CountRepos gets the count of all repos from the database. func (e *engine) CountRepos(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all repos from the database") + e.logger.Tracef("getting count of all repos") // variable to store query results var r int64 diff --git a/database/repo/count_org.go b/database/repo/count_org.go index 339dfd664..6e0ce6cb4 100644 --- a/database/repo/count_org.go +++ b/database/repo/count_org.go @@ -14,7 +14,7 @@ import ( func (e *engine) CountReposForOrg(ctx context.Context, org string, filters map[string]interface{}) (int64, error) { e.logger.WithFields(logrus.Fields{ "org": org, - }).Tracef("getting count of repos for org %s from the database", org) + }).Tracef("getting count of repos for org %s", org) // variable to store query results var r int64 diff --git a/database/repo/count_user.go b/database/repo/count_user.go index b8ba89a12..dfc002d9b 100644 --- a/database/repo/count_user.go +++ b/database/repo/count_user.go @@ -15,7 +15,7 @@ import ( func (e *engine) CountReposForUser(ctx context.Context, u *api.User, filters map[string]interface{}) (int64, error) { e.logger.WithFields(logrus.Fields{ "user": u.GetName(), - }).Tracef("getting count of repos for user %s from the database", u.GetName()) + }).Tracef("getting count of repos for user %s", u.GetName()) // variable to store query results var r int64 diff --git a/database/repo/create.go b/database/repo/create.go index 3a604348e..166362de9 100644 --- a/database/repo/create.go +++ b/database/repo/create.go @@ -19,7 +19,7 @@ func (e *engine) CreateRepo(ctx context.Context, r *api.Repo) (*api.Repo, error) e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("creating repo %s in the database", r.GetFullName()) + }).Tracef("creating repo %s", r.GetFullName()) // cast the library type to database type repo := types.RepoFromAPI(r) diff --git a/database/repo/delete.go b/database/repo/delete.go index 115e131c8..a12e2ec91 100644 --- a/database/repo/delete.go +++ b/database/repo/delete.go @@ -17,7 +17,7 @@ func (e *engine) DeleteRepo(ctx context.Context, r *api.Repo) error { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("deleting repo %s from the database", r.GetFullName()) + }).Tracef("deleting repo %s", r.GetFullName()) // cast the library type to database type repo := types.RepoFromAPI(r) diff --git a/database/repo/get.go b/database/repo/get.go index 9d3fb7412..d29ba16c8 100644 --- a/database/repo/get.go +++ b/database/repo/get.go @@ -12,7 +12,7 @@ import ( // GetRepo gets a repo by ID from the database. func (e *engine) GetRepo(ctx context.Context, id int64) (*api.Repo, error) { - e.logger.Tracef("getting repo %d from the database", id) + e.logger.Tracef("getting repo %d", id) // variable to store query results r := new(types.Repo) diff --git a/database/repo/get_org.go b/database/repo/get_org.go index ecca4530f..e66b97b37 100644 --- a/database/repo/get_org.go +++ b/database/repo/get_org.go @@ -17,7 +17,7 @@ func (e *engine) GetRepoForOrg(ctx context.Context, org, name string) (*api.Repo e.logger.WithFields(logrus.Fields{ "org": org, "repo": name, - }).Tracef("getting repo %s/%s from the database", org, name) + }).Tracef("getting repo %s/%s", org, name) // variable to store query results r := new(types.Repo) diff --git a/database/repo/index.go b/database/repo/index.go index c2446bda9..9c97ad962 100644 --- a/database/repo/index.go +++ b/database/repo/index.go @@ -17,7 +17,7 @@ ON repos (org, name); // CreateRepoIndexes creates the indexes for the repos table in the database. func (e *engine) CreateRepoIndexes(ctx context.Context) error { - e.logger.Tracef("creating indexes for repos table in the database") + e.logger.Tracef("creating indexes for repos table") // create the org and name columns index for the repos table return e.client.Exec(CreateOrgNameIndex).Error diff --git a/database/repo/list.go b/database/repo/list.go index e50de9a21..abe27a434 100644 --- a/database/repo/list.go +++ b/database/repo/list.go @@ -12,7 +12,7 @@ import ( // ListRepos gets a list of all repos from the database. func (e *engine) ListRepos(ctx context.Context) ([]*api.Repo, error) { - e.logger.Trace("listing all repos from the database") + e.logger.Trace("listing all repos") // variables to store query results and return value count := int64(0) diff --git a/database/repo/list_org.go b/database/repo/list_org.go index bc1ee5597..016044744 100644 --- a/database/repo/list_org.go +++ b/database/repo/list_org.go @@ -18,7 +18,7 @@ import ( func (e *engine) ListReposForOrg(ctx context.Context, org, sortBy string, filters map[string]interface{}, page, perPage int) ([]*api.Repo, int64, error) { e.logger.WithFields(logrus.Fields{ "org": org, - }).Tracef("listing repos for org %s from the database", org) + }).Tracef("listing repos for org %s", org) // variables to store query results and return values count := int64(0) diff --git a/database/repo/list_user.go b/database/repo/list_user.go index 37665769a..f50374de1 100644 --- a/database/repo/list_user.go +++ b/database/repo/list_user.go @@ -18,7 +18,7 @@ import ( func (e *engine) ListReposForUser(ctx context.Context, u *api.User, sortBy string, filters map[string]interface{}, page, perPage int) ([]*api.Repo, int64, error) { e.logger.WithFields(logrus.Fields{ "user": u.GetName(), - }).Tracef("listing repos for user %s from the database", u.GetName()) + }).Tracef("listing repos for user %s", u.GetName()) // variables to store query results and return values count := int64(0) diff --git a/database/repo/repo.go b/database/repo/repo.go index 6f9edeb19..2a915192f 100644 --- a/database/repo/repo.go +++ b/database/repo/repo.go @@ -62,7 +62,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating repo database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of repos table and indexes in the database") + e.logger.Warning("skipping creation of repos table and indexes") return e, nil } diff --git a/database/repo/table.go b/database/repo/table.go index a7877d280..65bd21fb2 100644 --- a/database/repo/table.go +++ b/database/repo/table.go @@ -72,7 +72,7 @@ repos ( // CreateRepoTable creates the repos table in the database. func (e *engine) CreateRepoTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating repos table in the database") + e.logger.Tracef("creating repos table") // handle the driver provided to create the table switch driver { diff --git a/database/repo/update.go b/database/repo/update.go index 31922068f..9a11b8010 100644 --- a/database/repo/update.go +++ b/database/repo/update.go @@ -19,7 +19,7 @@ func (e *engine) UpdateRepo(ctx context.Context, r *api.Repo) (*api.Repo, error) e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("creating repo %s in the database", r.GetFullName()) + }).Tracef("creating repo %s", r.GetFullName()) // cast the library type to database type repo := types.RepoFromAPI(r) diff --git a/database/schedule/count.go b/database/schedule/count.go index b61b6d028..8da93e69f 100644 --- a/database/schedule/count.go +++ b/database/schedule/count.go @@ -10,7 +10,7 @@ import ( // CountSchedules gets the count of all schedules from the database. func (e *engine) CountSchedules(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all schedules from the database") + e.logger.Tracef("getting count of all schedules") // variable to store query results var s int64 diff --git a/database/schedule/count_active.go b/database/schedule/count_active.go index 6e46ee253..be2dd9b5e 100644 --- a/database/schedule/count_active.go +++ b/database/schedule/count_active.go @@ -10,7 +10,7 @@ import ( // CountActiveSchedules gets the count of all active schedules from the database. func (e *engine) CountActiveSchedules(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all active schedules from the database") + e.logger.Tracef("getting count of all active schedules") // variable to store query results var s int64 diff --git a/database/schedule/count_repo.go b/database/schedule/count_repo.go index 80c1cd47a..724ce19b3 100644 --- a/database/schedule/count_repo.go +++ b/database/schedule/count_repo.go @@ -16,7 +16,7 @@ func (e *engine) CountSchedulesForRepo(ctx context.Context, r *api.Repo) (int64, e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("getting count of schedules for repo %s from the database", r.GetFullName()) + }).Tracef("getting count of schedules for repo %s", r.GetFullName()) // variable to store query results var s int64 diff --git a/database/schedule/get.go b/database/schedule/get.go index e8097bc51..1bcdc37cb 100644 --- a/database/schedule/get.go +++ b/database/schedule/get.go @@ -12,7 +12,7 @@ import ( // GetSchedule gets a schedule by ID from the database. func (e *engine) GetSchedule(ctx context.Context, id int64) (*api.Schedule, error) { - e.logger.Tracef("getting schedule %d from the database", id) + e.logger.Tracef("getting schedule %d", id) // variable to store query results s := new(types.Schedule) diff --git a/database/schedule/get_repo.go b/database/schedule/get_repo.go index 0af4d1f7d..e940694a4 100644 --- a/database/schedule/get_repo.go +++ b/database/schedule/get_repo.go @@ -18,7 +18,7 @@ func (e *engine) GetScheduleForRepo(ctx context.Context, r *api.Repo, name strin "org": r.GetOrg(), "repo": r.GetName(), "schedule": name, - }).Tracef("getting schedule %s/%s from the database", r.GetFullName(), name) + }).Tracef("getting schedule %s/%s", r.GetFullName(), name) // variable to store query results s := new(types.Schedule) diff --git a/database/schedule/list.go b/database/schedule/list.go index 7df0c2248..4282f554a 100644 --- a/database/schedule/list.go +++ b/database/schedule/list.go @@ -12,7 +12,7 @@ import ( // ListSchedules gets a list of all schedules from the database. func (e *engine) ListSchedules(ctx context.Context) ([]*api.Schedule, error) { - e.logger.Trace("listing all schedules from the database") + e.logger.Trace("listing all schedules") // variables to store query results and return value count := int64(0) diff --git a/database/schedule/list_active.go b/database/schedule/list_active.go index d53b8201d..84e575b1a 100644 --- a/database/schedule/list_active.go +++ b/database/schedule/list_active.go @@ -12,7 +12,7 @@ import ( // ListActiveSchedules gets a list of all active schedules from the database. func (e *engine) ListActiveSchedules(ctx context.Context) ([]*api.Schedule, error) { - e.logger.Trace("listing all active schedules from the database") + e.logger.Trace("listing all active schedules") // variables to store query results and return value count := int64(0) diff --git a/database/schedule/list_repo.go b/database/schedule/list_repo.go index d26f6a198..9ec086589 100644 --- a/database/schedule/list_repo.go +++ b/database/schedule/list_repo.go @@ -17,7 +17,7 @@ func (e *engine) ListSchedulesForRepo(ctx context.Context, r *api.Repo, page, pe e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - }).Tracef("listing schedules for repo %s from the database", r.GetFullName()) + }).Tracef("listing schedules for repo %s", r.GetFullName()) // variables to store query results and return value count := int64(0) diff --git a/database/secret/count.go b/database/secret/count.go index fd4158d8a..bf91525fa 100644 --- a/database/secret/count.go +++ b/database/secret/count.go @@ -10,7 +10,7 @@ import ( // CountSecrets gets the count of all secrets from the database. func (e *engine) CountSecrets(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all secrets from the database") + e.logger.Tracef("getting count of all secrets") // variable to store query results var s int64 diff --git a/database/secret/count_org.go b/database/secret/count_org.go index e6fdc7fcb..9b4a8c263 100644 --- a/database/secret/count_org.go +++ b/database/secret/count_org.go @@ -15,7 +15,7 @@ func (e *engine) CountSecretsForOrg(ctx context.Context, org string, filters map e.logger.WithFields(logrus.Fields{ "org": org, "type": constants.SecretOrg, - }).Tracef("getting count of secrets for org %s from the database", org) + }).Tracef("getting count of secrets for org %s", org) // variable to store query results var s int64 diff --git a/database/secret/count_repo.go b/database/secret/count_repo.go index f734054a5..b8edddcb0 100644 --- a/database/secret/count_repo.go +++ b/database/secret/count_repo.go @@ -17,7 +17,7 @@ func (e *engine) CountSecretsForRepo(ctx context.Context, r *api.Repo, filters m "org": r.GetOrg(), "repo": r.GetName(), "type": constants.SecretRepo, - }).Tracef("getting count of secrets for repo %s from the database", r.GetFullName()) + }).Tracef("getting count of secrets for repo %s", r.GetFullName()) // variable to store query results var s int64 diff --git a/database/secret/count_team.go b/database/secret/count_team.go index 55445b504..1b5fc8361 100644 --- a/database/secret/count_team.go +++ b/database/secret/count_team.go @@ -17,7 +17,7 @@ func (e *engine) CountSecretsForTeam(ctx context.Context, org, team string, filt "org": org, "team": team, "type": constants.SecretShared, - }).Tracef("getting count of secrets for team %s/%s from the database", org, team) + }).Tracef("getting count of secrets for team %s/%s", org, team) // variable to store query results var s int64 @@ -49,7 +49,7 @@ func (e *engine) CountSecretsForTeams(ctx context.Context, org string, teams []s "org": org, "teams": teams, "type": constants.SecretShared, - }).Tracef("getting count of secrets for teams %s in org %s from the database", teams, org) + }).Tracef("getting count of secrets for teams %s in org %s", teams, org) // variable to store query results var s int64 diff --git a/database/secret/create.go b/database/secret/create.go index c8b0400d1..be409d304 100644 --- a/database/secret/create.go +++ b/database/secret/create.go @@ -24,14 +24,14 @@ func (e *engine) CreateSecret(ctx context.Context, s *library.Secret) (*library. "team": s.GetTeam(), "secret": s.GetName(), "type": s.GetType(), - }).Tracef("creating secret %s/%s/%s/%s in the database", s.GetType(), s.GetOrg(), s.GetTeam(), s.GetName()) + }).Tracef("creating secret %s/%s/%s/%s", s.GetType(), s.GetOrg(), s.GetTeam(), s.GetName()) default: e.logger.WithFields(logrus.Fields{ "org": s.GetOrg(), "repo": s.GetRepo(), "secret": s.GetName(), "type": s.GetType(), - }).Tracef("creating secret %s/%s/%s/%s in the database", s.GetType(), s.GetOrg(), s.GetRepo(), s.GetName()) + }).Tracef("creating secret %s/%s/%s/%s", s.GetType(), s.GetOrg(), s.GetRepo(), s.GetName()) } // cast the library type to database type diff --git a/database/secret/delete.go b/database/secret/delete.go index 5b3653ba1..bc3cba36f 100644 --- a/database/secret/delete.go +++ b/database/secret/delete.go @@ -24,14 +24,14 @@ func (e *engine) DeleteSecret(ctx context.Context, s *library.Secret) error { "team": s.GetTeam(), "secret": s.GetName(), "type": s.GetType(), - }).Tracef("deleting secret %s/%s/%s/%s from the database", s.GetType(), s.GetOrg(), s.GetTeam(), s.GetName()) + }).Tracef("deleting secret %s/%s/%s/%s", s.GetType(), s.GetOrg(), s.GetTeam(), s.GetName()) default: e.logger.WithFields(logrus.Fields{ "org": s.GetOrg(), "repo": s.GetRepo(), "secret": s.GetName(), "type": s.GetType(), - }).Tracef("deleting secret %s/%s/%s/%s from the database", s.GetType(), s.GetOrg(), s.GetRepo(), s.GetName()) + }).Tracef("deleting secret %s/%s/%s/%s", s.GetType(), s.GetOrg(), s.GetRepo(), s.GetName()) } // cast the library type to database type diff --git a/database/secret/get.go b/database/secret/get.go index c8cafa2df..6f5bc7bf9 100644 --- a/database/secret/get.go +++ b/database/secret/get.go @@ -12,7 +12,7 @@ import ( // GetSecret gets a secret by ID from the database. func (e *engine) GetSecret(ctx context.Context, id int64) (*library.Secret, error) { - e.logger.Tracef("getting secret %d from the database", id) + e.logger.Tracef("getting secret %d", id) // variable to store query results s := new(database.Secret) diff --git a/database/secret/get_org.go b/database/secret/get_org.go index fec7ff1ef..7d1766c88 100644 --- a/database/secret/get_org.go +++ b/database/secret/get_org.go @@ -18,7 +18,7 @@ func (e *engine) GetSecretForOrg(ctx context.Context, org, name string) (*librar "org": org, "secret": name, "type": constants.SecretOrg, - }).Tracef("getting org secret %s/%s from the database", org, name) + }).Tracef("getting org secret %s/%s", org, name) // variable to store query results s := new(database.Secret) diff --git a/database/secret/get_repo.go b/database/secret/get_repo.go index 6edacafd1..690823c08 100644 --- a/database/secret/get_repo.go +++ b/database/secret/get_repo.go @@ -20,7 +20,7 @@ func (e *engine) GetSecretForRepo(ctx context.Context, name string, r *api.Repo) "repo": r.GetName(), "secret": name, "type": constants.SecretRepo, - }).Tracef("getting repo secret %s/%s from the database", r.GetFullName(), name) + }).Tracef("getting repo secret %s/%s", r.GetFullName(), name) // variable to store query results s := new(database.Secret) diff --git a/database/secret/get_team.go b/database/secret/get_team.go index 74e4a57f9..e03845947 100644 --- a/database/secret/get_team.go +++ b/database/secret/get_team.go @@ -19,7 +19,7 @@ func (e *engine) GetSecretForTeam(ctx context.Context, org, team, name string) ( "team": team, "secret": name, "type": constants.SecretShared, - }).Tracef("getting shared secret %s/%s/%s from the database", org, team, name) + }).Tracef("getting shared secret %s/%s/%s", org, team, name) // variable to store query results s := new(database.Secret) diff --git a/database/secret/index.go b/database/secret/index.go index f0cd1837d..e045031bb 100644 --- a/database/secret/index.go +++ b/database/secret/index.go @@ -33,7 +33,7 @@ ON secrets (type, org); // CreateSecretIndexes creates the indexes for the secrets table in the database. func (e *engine) CreateSecretIndexes(ctx context.Context) error { - e.logger.Tracef("creating indexes for secrets table in the database") + e.logger.Tracef("creating indexes for secrets table") // create the type, org and repo columns index for the secrets table err := e.client.Exec(CreateTypeOrgRepo).Error diff --git a/database/secret/list.go b/database/secret/list.go index 08746332c..a61e2088f 100644 --- a/database/secret/list.go +++ b/database/secret/list.go @@ -12,7 +12,7 @@ import ( // ListSecrets gets a list of all secrets from the database. func (e *engine) ListSecrets(ctx context.Context) ([]*library.Secret, error) { - e.logger.Trace("listing all secrets from the database") + e.logger.Trace("listing all secrets") // variables to store query results and return value count := int64(0) diff --git a/database/secret/list_org.go b/database/secret/list_org.go index 63cf5f899..d8c7ced5a 100644 --- a/database/secret/list_org.go +++ b/database/secret/list_org.go @@ -19,7 +19,7 @@ func (e *engine) ListSecretsForOrg(ctx context.Context, org string, filters map[ e.logger.WithFields(logrus.Fields{ "org": org, "type": constants.SecretOrg, - }).Tracef("listing secrets for org %s from the database", org) + }).Tracef("listing secrets for org %s", org) // variables to store query results and return values count := int64(0) diff --git a/database/secret/list_repo.go b/database/secret/list_repo.go index 0be77454c..ef2fe546c 100644 --- a/database/secret/list_repo.go +++ b/database/secret/list_repo.go @@ -21,7 +21,7 @@ func (e *engine) ListSecretsForRepo(ctx context.Context, r *api.Repo, filters ma "org": r.GetOrg(), "repo": r.GetName(), "type": constants.SecretRepo, - }).Tracef("listing secrets for repo %s from the database", r.GetFullName()) + }).Tracef("listing secrets for repo %s", r.GetFullName()) // variables to store query results and return values count := int64(0) diff --git a/database/secret/list_team.go b/database/secret/list_team.go index f9a17c2fd..93a5d6db1 100644 --- a/database/secret/list_team.go +++ b/database/secret/list_team.go @@ -21,7 +21,7 @@ func (e *engine) ListSecretsForTeam(ctx context.Context, org, team string, filte "org": org, "team": team, "type": constants.SecretShared, - }).Tracef("listing secrets for team %s/%s from the database", org, team) + }).Tracef("listing secrets for team %s/%s", org, team) // variables to store query results and return values count := int64(0) @@ -97,7 +97,7 @@ func (e *engine) ListSecretsForTeams(ctx context.Context, org string, teams []st "org": org, "teams": teams, "type": constants.SecretShared, - }).Tracef("listing secrets for teams %s in org %s from the database", teams, org) + }).Tracef("listing secrets for teams %s in org %s", teams, org) // variables to store query results and return values count := int64(0) diff --git a/database/secret/secret.go b/database/secret/secret.go index 55e20b59f..4a7c772e8 100644 --- a/database/secret/secret.go +++ b/database/secret/secret.go @@ -62,7 +62,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating secret database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of secrets table and indexes in the database") + e.logger.Warning("skipping creation of secrets table and indexes") return e, nil } diff --git a/database/secret/table.go b/database/secret/table.go index 07b84fee8..e696409c2 100644 --- a/database/secret/table.go +++ b/database/secret/table.go @@ -62,7 +62,7 @@ secrets ( // CreateSecretTable creates the secrets table in the database. func (e *engine) CreateSecretTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating secrets table in the database") + e.logger.Tracef("creating secrets table") // handle the driver provided to create the table switch driver { diff --git a/database/secret/update.go b/database/secret/update.go index 641a8a3e3..0bb713e47 100644 --- a/database/secret/update.go +++ b/database/secret/update.go @@ -24,14 +24,14 @@ func (e *engine) UpdateSecret(ctx context.Context, s *library.Secret) (*library. "team": s.GetTeam(), "secret": s.GetName(), "type": s.GetType(), - }).Tracef("updating secret %s/%s/%s/%s in the database", s.GetType(), s.GetOrg(), s.GetTeam(), s.GetName()) + }).Tracef("updating secret %s/%s/%s/%s", s.GetType(), s.GetOrg(), s.GetTeam(), s.GetName()) default: e.logger.WithFields(logrus.Fields{ "org": s.GetOrg(), "repo": s.GetRepo(), "secret": s.GetName(), "type": s.GetType(), - }).Tracef("updating secret %s/%s/%s/%s in the database", s.GetType(), s.GetOrg(), s.GetRepo(), s.GetName()) + }).Tracef("updating secret %s/%s/%s/%s", s.GetType(), s.GetOrg(), s.GetRepo(), s.GetName()) } // cast the library type to database type diff --git a/database/service/count.go b/database/service/count.go index dd1f63188..f8bee25b1 100644 --- a/database/service/count.go +++ b/database/service/count.go @@ -10,7 +10,7 @@ import ( // CountServices gets the count of all services from the database. func (e *engine) CountServices(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all services from the database") + e.logger.Tracef("getting count of all services") // variable to store query results var s int64 diff --git a/database/service/count_build.go b/database/service/count_build.go index a15c061ff..1e84819a2 100644 --- a/database/service/count_build.go +++ b/database/service/count_build.go @@ -15,7 +15,7 @@ import ( func (e *engine) CountServicesForBuild(ctx context.Context, b *api.Build, filters map[string]interface{}) (int64, error) { e.logger.WithFields(logrus.Fields{ "build": b.GetNumber(), - }).Tracef("getting count of services for build %d from the database", b.GetNumber()) + }).Tracef("getting count of services for build %d", b.GetNumber()) // variable to store query results var s int64 diff --git a/database/service/delete.go b/database/service/delete.go index 5762fbb8d..3f0bd331e 100644 --- a/database/service/delete.go +++ b/database/service/delete.go @@ -16,7 +16,7 @@ import ( func (e *engine) DeleteService(ctx context.Context, s *library.Service) error { e.logger.WithFields(logrus.Fields{ "service": s.GetNumber(), - }).Tracef("deleting service %s from the database", s.GetName()) + }).Tracef("deleting service %s", s.GetName()) // cast the library type to database type // diff --git a/database/service/get.go b/database/service/get.go index aacda2904..eee921123 100644 --- a/database/service/get.go +++ b/database/service/get.go @@ -12,7 +12,7 @@ import ( // GetService gets a service by ID from the database. func (e *engine) GetService(ctx context.Context, id int64) (*library.Service, error) { - e.logger.Tracef("getting service %d from the database", id) + e.logger.Tracef("getting service %d", id) // variable to store query results s := new(database.Service) diff --git a/database/service/get_build.go b/database/service/get_build.go index 26b5632c8..81a95e0bc 100644 --- a/database/service/get_build.go +++ b/database/service/get_build.go @@ -18,7 +18,7 @@ func (e *engine) GetServiceForBuild(ctx context.Context, b *api.Build, number in e.logger.WithFields(logrus.Fields{ "build": b.GetNumber(), "service": number, - }).Tracef("getting service %d from the database", number) + }).Tracef("getting service %d", number) // variable to store query results s := new(database.Service) diff --git a/database/service/list.go b/database/service/list.go index 5da3a06ed..656beee10 100644 --- a/database/service/list.go +++ b/database/service/list.go @@ -12,7 +12,7 @@ import ( // ListServices gets a list of all services from the database. func (e *engine) ListServices(ctx context.Context) ([]*library.Service, error) { - e.logger.Trace("listing all services from the database") + e.logger.Trace("listing all services") // variables to store query results and return value count := int64(0) diff --git a/database/service/list_build.go b/database/service/list_build.go index 98cb70294..047aba669 100644 --- a/database/service/list_build.go +++ b/database/service/list_build.go @@ -17,7 +17,7 @@ import ( func (e *engine) ListServicesForBuild(ctx context.Context, b *api.Build, filters map[string]interface{}, page int, perPage int) ([]*library.Service, int64, error) { e.logger.WithFields(logrus.Fields{ "build": b.GetNumber(), - }).Tracef("listing services for build %d from the database", b.GetNumber()) + }).Tracef("listing services for build %d", b.GetNumber()) // variables to store query results and return value count := int64(0) diff --git a/database/service/list_image.go b/database/service/list_image.go index f5267d5da..a94447e6c 100644 --- a/database/service/list_image.go +++ b/database/service/list_image.go @@ -11,7 +11,7 @@ import ( // ListServiceImageCount gets a list of all service images and the count of their occurrence from the database. func (e *engine) ListServiceImageCount(ctx context.Context) (map[string]float64, error) { - e.logger.Tracef("getting count of all images for services from the database") + e.logger.Tracef("getting count of all images for services") // variables to store query results and return value s := []struct { diff --git a/database/service/list_status.go b/database/service/list_status.go index dde89795a..9e5f106fd 100644 --- a/database/service/list_status.go +++ b/database/service/list_status.go @@ -11,7 +11,7 @@ import ( // ListServiceStatusCount gets a list of all service statuses and the count of their occurrence from the database. func (e *engine) ListServiceStatusCount(ctx context.Context) (map[string]float64, error) { - e.logger.Tracef("getting count of all statuses for services from the database") + e.logger.Tracef("getting count of all statuses for services") // variables to store query results and return value s := []struct { diff --git a/database/service/table.go b/database/service/table.go index 7cf7561c3..56e78b249 100644 --- a/database/service/table.go +++ b/database/service/table.go @@ -60,7 +60,7 @@ services ( // CreateServiceTable creates the services table in the database. func (e *engine) CreateServiceTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating services table in the database") + e.logger.Tracef("creating services table") // handle the driver provided to create the table switch driver { diff --git a/database/service/update.go b/database/service/update.go index da6417aab..6eb43a7e2 100644 --- a/database/service/update.go +++ b/database/service/update.go @@ -16,7 +16,7 @@ import ( func (e *engine) UpdateService(ctx context.Context, s *library.Service) (*library.Service, error) { e.logger.WithFields(logrus.Fields{ "service": s.GetNumber(), - }).Tracef("updating service %s in the database", s.GetName()) + }).Tracef("updating service %s", s.GetName()) // cast the library type to database type // diff --git a/database/settings/create.go b/database/settings/create.go index 7775fb4c9..8c5747320 100644 --- a/database/settings/create.go +++ b/database/settings/create.go @@ -11,7 +11,7 @@ import ( // CreateSettings creates a platform settings record in the database. func (e *engine) CreateSettings(_ context.Context, s *settings.Platform) (*settings.Platform, error) { - e.logger.Tracef("creating platform settings in the database with %v", s.String()) + e.logger.Tracef("creating platform settings with %v", s.String()) // cast the api type to database type settings := types.SettingsFromAPI(s) diff --git a/database/settings/get.go b/database/settings/get.go index 0bba2e0f4..96bc19ef1 100644 --- a/database/settings/get.go +++ b/database/settings/get.go @@ -11,7 +11,7 @@ import ( // GetSettings gets platform settings from the database. func (e *engine) GetSettings(ctx context.Context) (*settings.Platform, error) { - e.logger.Trace("getting platform settings from the database") + e.logger.Trace("getting platform settings") // variable to store query results s := new(types.Platform) diff --git a/database/settings/settings.go b/database/settings/settings.go index a673ed49d..e86ea23b1 100644 --- a/database/settings/settings.go +++ b/database/settings/settings.go @@ -62,7 +62,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of settings table and indexes in the database") + e.logger.Warning("skipping creation of settings table and indexes") return e, nil } diff --git a/database/settings/table.go b/database/settings/table.go index a7268bc76..18a4207ab 100644 --- a/database/settings/table.go +++ b/database/settings/table.go @@ -44,7 +44,7 @@ settings ( // CreateSettingsTable creates the settings table in the database. func (e *engine) CreateSettingsTable(_ context.Context, driver string) error { - e.logger.Tracef("creating settings table in the database") + e.logger.Tracef("creating settings table") // handle the driver provided to create the table switch driver { diff --git a/database/step/count.go b/database/step/count.go index 12ef7afc4..d5c64ba34 100644 --- a/database/step/count.go +++ b/database/step/count.go @@ -10,7 +10,7 @@ import ( // CountSteps gets the count of all steps from the database. func (e *engine) CountSteps(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all steps from the database") + e.logger.Tracef("getting count of all steps") // variable to store query results var s int64 diff --git a/database/step/count_build.go b/database/step/count_build.go index 35109fa06..6b4d63fc4 100644 --- a/database/step/count_build.go +++ b/database/step/count_build.go @@ -15,7 +15,7 @@ import ( func (e *engine) CountStepsForBuild(ctx context.Context, b *api.Build, filters map[string]interface{}) (int64, error) { e.logger.WithFields(logrus.Fields{ "build": b.GetNumber(), - }).Tracef("getting count of steps for build %d from the database", b.GetNumber()) + }).Tracef("getting count of steps for build %d", b.GetNumber()) // variable to store query results var s int64 diff --git a/database/step/delete.go b/database/step/delete.go index 98e895def..dfd2d5bfa 100644 --- a/database/step/delete.go +++ b/database/step/delete.go @@ -16,7 +16,7 @@ import ( func (e *engine) DeleteStep(ctx context.Context, s *library.Step) error { e.logger.WithFields(logrus.Fields{ "step": s.GetNumber(), - }).Tracef("deleting step %s from the database", s.GetName()) + }).Tracef("deleting step %s", s.GetName()) // cast the library type to database type // diff --git a/database/step/get.go b/database/step/get.go index d75217f58..7a4e18041 100644 --- a/database/step/get.go +++ b/database/step/get.go @@ -12,7 +12,7 @@ import ( // GetStep gets a step by ID from the database. func (e *engine) GetStep(ctx context.Context, id int64) (*library.Step, error) { - e.logger.Tracef("getting step %d from the database", id) + e.logger.Tracef("getting step %d", id) // variable to store query results s := new(database.Step) diff --git a/database/step/get_build.go b/database/step/get_build.go index 9fd8a937e..5d32ba582 100644 --- a/database/step/get_build.go +++ b/database/step/get_build.go @@ -18,7 +18,7 @@ func (e *engine) GetStepForBuild(ctx context.Context, b *api.Build, number int) e.logger.WithFields(logrus.Fields{ "build": b.GetNumber(), "step": number, - }).Tracef("getting step %d from the database", number) + }).Tracef("getting step %d", number) // variable to store query results s := new(database.Step) diff --git a/database/step/list.go b/database/step/list.go index afbb9f818..0d53297bd 100644 --- a/database/step/list.go +++ b/database/step/list.go @@ -12,7 +12,7 @@ import ( // ListSteps gets a list of all steps from the database. func (e *engine) ListSteps(ctx context.Context) ([]*library.Step, error) { - e.logger.Trace("listing all steps from the database") + e.logger.Trace("listing all steps") // variables to store query results and return value count := int64(0) diff --git a/database/step/list_build.go b/database/step/list_build.go index fce2619e8..ac0a608d1 100644 --- a/database/step/list_build.go +++ b/database/step/list_build.go @@ -17,7 +17,7 @@ import ( func (e *engine) ListStepsForBuild(ctx context.Context, b *api.Build, filters map[string]interface{}, page int, perPage int) ([]*library.Step, int64, error) { e.logger.WithFields(logrus.Fields{ "build": b.GetNumber(), - }).Tracef("listing steps for build %d from the database", b.GetNumber()) + }).Tracef("listing steps for build %d", b.GetNumber()) // variables to store query results and return value count := int64(0) diff --git a/database/step/list_image.go b/database/step/list_image.go index 7669d637a..5b0f46aec 100644 --- a/database/step/list_image.go +++ b/database/step/list_image.go @@ -11,7 +11,7 @@ import ( // ListStepImageCount gets a list of all step images and the count of their occurrence from the database. func (e *engine) ListStepImageCount(ctx context.Context) (map[string]float64, error) { - e.logger.Tracef("getting count of all images for steps from the database") + e.logger.Tracef("getting count of all images for steps") // variables to store query results and return value s := []struct { diff --git a/database/step/list_status.go b/database/step/list_status.go index d93db3c30..c74b8db3c 100644 --- a/database/step/list_status.go +++ b/database/step/list_status.go @@ -11,7 +11,7 @@ import ( // ListStepStatusCount gets a list of all step statuses and the count of their occurrence from the database. func (e *engine) ListStepStatusCount(ctx context.Context) (map[string]float64, error) { - e.logger.Tracef("getting count of all statuses for steps from the database") + e.logger.Tracef("getting count of all statuses for steps") // variables to store query results and return value s := []struct { diff --git a/database/user/count.go b/database/user/count.go index dc5554b37..c48fa00ed 100644 --- a/database/user/count.go +++ b/database/user/count.go @@ -10,7 +10,7 @@ import ( // CountUsers gets the count of all users from the database. func (e *engine) CountUsers(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all users from the database") + e.logger.Tracef("getting count of all users") // variable to store query results var u int64 diff --git a/database/user/create.go b/database/user/create.go index b8cb1e59d..e9cf0f2c6 100644 --- a/database/user/create.go +++ b/database/user/create.go @@ -18,7 +18,7 @@ import ( func (e *engine) CreateUser(ctx context.Context, u *api.User) (*api.User, error) { e.logger.WithFields(logrus.Fields{ "user": u.GetName(), - }).Tracef("creating user %s in the database", u.GetName()) + }).Tracef("creating user %s", u.GetName()) // cast the API type to database type user := types.UserFromAPI(u) diff --git a/database/user/delete.go b/database/user/delete.go index b7a7be169..3154461e2 100644 --- a/database/user/delete.go +++ b/database/user/delete.go @@ -16,7 +16,7 @@ import ( func (e *engine) DeleteUser(ctx context.Context, u *api.User) error { e.logger.WithFields(logrus.Fields{ "user": u.GetName(), - }).Tracef("deleting user %s from the database", u.GetName()) + }).Tracef("deleting user %s", u.GetName()) // cast the API type to database type user := types.UserFromAPI(u) diff --git a/database/user/get.go b/database/user/get.go index e0f8d45d9..a2f7d8468 100644 --- a/database/user/get.go +++ b/database/user/get.go @@ -12,7 +12,7 @@ import ( // GetUser gets a user by ID from the database. func (e *engine) GetUser(ctx context.Context, id int64) (*api.User, error) { - e.logger.Tracef("getting user %d from the database", id) + e.logger.Tracef("getting user %d", id) // variable to store query results u := new(types.User) diff --git a/database/user/get_name.go b/database/user/get_name.go index f74c07166..8d3633eb8 100644 --- a/database/user/get_name.go +++ b/database/user/get_name.go @@ -16,7 +16,7 @@ import ( func (e *engine) GetUserForName(ctx context.Context, name string) (*api.User, error) { e.logger.WithFields(logrus.Fields{ "user": name, - }).Tracef("getting user %s from the database", name) + }).Tracef("getting user %s", name) // variable to store query results u := new(types.User) diff --git a/database/user/index.go b/database/user/index.go index 25c3edef3..d35e1accd 100644 --- a/database/user/index.go +++ b/database/user/index.go @@ -17,7 +17,7 @@ ON users (refresh_token); // CreateUserIndexes creates the indexes for the users table in the database. func (e *engine) CreateUserIndexes(ctx context.Context) error { - e.logger.Tracef("creating indexes for users table in the database") + e.logger.Tracef("creating indexes for users table") // create the refresh_token column index for the users table return e.client.Exec(CreateUserRefreshIndex).Error diff --git a/database/user/list.go b/database/user/list.go index 29b53f7f0..80c7f5681 100644 --- a/database/user/list.go +++ b/database/user/list.go @@ -12,7 +12,7 @@ import ( // ListUsers gets a list of all users from the database. func (e *engine) ListUsers(ctx context.Context) ([]*api.User, error) { - e.logger.Trace("listing all users from the database") + e.logger.Trace("listing all users") // variables to store query results and return value count := int64(0) diff --git a/database/user/list_lite.go b/database/user/list_lite.go index d3ca1c49a..1a5c570f9 100644 --- a/database/user/list_lite.go +++ b/database/user/list_lite.go @@ -14,7 +14,7 @@ import ( // //nolint:lll // ignore long line length due to variable names func (e *engine) ListLiteUsers(ctx context.Context, page, perPage int) ([]*api.User, int64, error) { - e.logger.Trace("listing lite users from the database") + e.logger.Trace("listing lite users") // variables to store query results and return values count := int64(0) diff --git a/database/user/table.go b/database/user/table.go index c5e4d1712..183390b43 100644 --- a/database/user/table.go +++ b/database/user/table.go @@ -46,7 +46,7 @@ users ( // CreateUserTable creates the users table in the database. func (e *engine) CreateUserTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating users table in the database") + e.logger.Tracef("creating users table") // handle the driver provided to create the table switch driver { diff --git a/database/user/update.go b/database/user/update.go index b785352eb..19f653f8d 100644 --- a/database/user/update.go +++ b/database/user/update.go @@ -18,7 +18,7 @@ import ( func (e *engine) UpdateUser(ctx context.Context, u *api.User) (*api.User, error) { e.logger.WithFields(logrus.Fields{ "user": u.GetName(), - }).Tracef("updating user %s in the database", u.GetName()) + }).Tracef("updating user %s", u.GetName()) // cast the library type to database type user := types.UserFromAPI(u) diff --git a/database/user/user.go b/database/user/user.go index 23948bfb3..ad2e2bae5 100644 --- a/database/user/user.go +++ b/database/user/user.go @@ -62,7 +62,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating user database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of users table and indexes in the database") + e.logger.Warning("skipping creation of users table and indexes") return e, nil } diff --git a/database/worker/count.go b/database/worker/count.go index eef910c36..0d992cdca 100644 --- a/database/worker/count.go +++ b/database/worker/count.go @@ -10,7 +10,7 @@ import ( // CountWorkers gets the count of all workers from the database. func (e *engine) CountWorkers(ctx context.Context) (int64, error) { - e.logger.Tracef("getting count of all workers from the database") + e.logger.Tracef("getting count of all workers") // variable to store query results var w int64 diff --git a/database/worker/create.go b/database/worker/create.go index c566ecd0e..445669deb 100644 --- a/database/worker/create.go +++ b/database/worker/create.go @@ -16,7 +16,7 @@ import ( func (e *engine) CreateWorker(ctx context.Context, w *api.Worker) (*api.Worker, error) { e.logger.WithFields(logrus.Fields{ "worker": w.GetHostname(), - }).Tracef("creating worker %s in the database", w.GetHostname()) + }).Tracef("creating worker %s", w.GetHostname()) // cast the library type to database type // diff --git a/database/worker/delete.go b/database/worker/delete.go index 4972efc98..8df8ff5e0 100644 --- a/database/worker/delete.go +++ b/database/worker/delete.go @@ -16,7 +16,7 @@ import ( func (e *engine) DeleteWorker(ctx context.Context, w *api.Worker) error { e.logger.WithFields(logrus.Fields{ "worker": w.GetHostname(), - }).Tracef("deleting worker %s from the database", w.GetHostname()) + }).Tracef("deleting worker %s", w.GetHostname()) // cast the library type to database type // diff --git a/database/worker/get.go b/database/worker/get.go index 7aca60b76..cde920413 100644 --- a/database/worker/get.go +++ b/database/worker/get.go @@ -12,7 +12,7 @@ import ( // GetWorker gets a worker by ID from the database. func (e *engine) GetWorker(ctx context.Context, id int64) (*api.Worker, error) { - e.logger.Tracef("getting worker %d from the database", id) + e.logger.Tracef("getting worker %d", id) // variable to store query results w := new(types.Worker) diff --git a/database/worker/get_hostname.go b/database/worker/get_hostname.go index ec37b9200..1646e262e 100644 --- a/database/worker/get_hostname.go +++ b/database/worker/get_hostname.go @@ -16,7 +16,7 @@ import ( func (e *engine) GetWorkerForHostname(ctx context.Context, hostname string) (*api.Worker, error) { e.logger.WithFields(logrus.Fields{ "worker": hostname, - }).Tracef("getting worker %s from the database", hostname) + }).Tracef("getting worker %s", hostname) // variable to store query results w := new(types.Worker) diff --git a/database/worker/index.go b/database/worker/index.go index 3220d4a63..65fe7326f 100644 --- a/database/worker/index.go +++ b/database/worker/index.go @@ -17,7 +17,7 @@ ON workers (hostname, address); // CreateWorkerIndexes creates the indexes for the workers table in the database. func (e *engine) CreateWorkerIndexes(ctx context.Context) error { - e.logger.Tracef("creating indexes for workers table in the database") + e.logger.Tracef("creating indexes for workers table") // create the hostname and address columns index for the workers table return e.client.Exec(CreateHostnameAddressIndex).Error diff --git a/database/worker/list.go b/database/worker/list.go index a8adc1a79..e2af759dd 100644 --- a/database/worker/list.go +++ b/database/worker/list.go @@ -14,7 +14,7 @@ import ( // ListWorkers gets a list of all workers from the database. func (e *engine) ListWorkers(ctx context.Context, active string, before, after int64) ([]*api.Worker, error) { - e.logger.Trace("listing all workers from the database") + e.logger.Trace("listing all workers") // variables to store query results and return value results := new([]types.Worker) diff --git a/database/worker/table.go b/database/worker/table.go index 9fcfdd113..403dd5ad3 100644 --- a/database/worker/table.go +++ b/database/worker/table.go @@ -53,7 +53,7 @@ workers ( // CreateWorkerTable creates the workers table in the database. func (e *engine) CreateWorkerTable(ctx context.Context, driver string) error { - e.logger.Tracef("creating workers table in the database") + e.logger.Tracef("creating workers table") // handle the driver provided to create the table switch driver { diff --git a/database/worker/update.go b/database/worker/update.go index 996d0c0af..872d9c82d 100644 --- a/database/worker/update.go +++ b/database/worker/update.go @@ -16,7 +16,7 @@ import ( func (e *engine) UpdateWorker(ctx context.Context, w *api.Worker) (*api.Worker, error) { e.logger.WithFields(logrus.Fields{ "worker": w.GetHostname(), - }).Tracef("updating worker %s in the database", w.GetHostname()) + }).Tracef("updating worker %s", w.GetHostname()) // cast the library type to database type // diff --git a/database/worker/worker.go b/database/worker/worker.go index 18e4d90d9..655a7dd2f 100644 --- a/database/worker/worker.go +++ b/database/worker/worker.go @@ -62,7 +62,7 @@ func New(opts ...EngineOpt) (*engine, error) { // check if we should skip creating worker database objects if e.config.SkipCreation { - e.logger.Warning("skipping creation of workers table and indexes in the database") + e.logger.Warning("skipping creation of workers table and indexes") return e, nil } diff --git a/queue/queue.go b/queue/queue.go index 181ed5899..fdff9b364 100644 --- a/queue/queue.go +++ b/queue/queue.go @@ -13,7 +13,7 @@ import ( // FromCLIContext helper function to setup the queue from the CLI arguments. func FromCLIContext(c *cli.Context) (Service, error) { - logrus.Debug("Creating queue client from CLI configuration") + logrus.Debug("creating queue client from CLI configuration") // queue configuration _setup := &Setup{ diff --git a/router/admin.go b/router/admin.go index d349fd7b0..d2eb667ac 100644 --- a/router/admin.go +++ b/router/admin.go @@ -13,7 +13,6 @@ import ( // with the API handlers for admin functionality. // // GET /api/v1/admin/builds/queue -// GET /api/v1/admin/build/:id // PUT /api/v1/admin/build // PUT /api/v1/admin/clean // PUT /api/v1/admin/deployment diff --git a/router/dashboard.go b/router/dashboard.go index 57ffafddb..32e8490db 100644 --- a/router/dashboard.go +++ b/router/dashboard.go @@ -10,11 +10,14 @@ import ( ) // DashboardHandlers is a function that extends the provided base router group -// with the API handlers for resource search functionality. +// with the API handlers for dashboard functionality. // -// GET /api/v1/search/builds/:id . +// POST /api/v1/dashboards +// GET /api/v1/dashboards/:id +// PUT /api/v1/dashboards/:id +// DELETE /api/v1/dashboards/:id . func DashboardHandlers(base *gin.RouterGroup) { - // Search endpoints + // Dashboard endpoints dashboards := base.Group("/dashboards") { dashboards.POST("", dashboard.CreateDashboard) @@ -25,5 +28,5 @@ func DashboardHandlers(base *gin.RouterGroup) { d.PUT("", dashboard.UpdateDashboard) d.DELETE("", dashboard.DeleteDashboard) } - } // end of search endpoints + } // end of dashboard endpoints } diff --git a/router/hook.go b/router/hook.go index 69816fa4c..896279e66 100644 --- a/router/hook.go +++ b/router/hook.go @@ -6,6 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/api/hook" + hmiddleware "github.com/go-vela/server/router/middleware/hook" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/perm" "github.com/go-vela/server/router/middleware/repo" @@ -26,9 +27,13 @@ func HookHandlers(base *gin.RouterGroup) { { _hooks.POST("", perm.MustPlatformAdmin(), hook.CreateHook) _hooks.GET("", perm.MustRead(), hook.ListHooks) - _hooks.GET("/:hook", perm.MustRead(), hook.GetHook) - _hooks.PUT("/:hook", perm.MustPlatformAdmin(), hook.UpdateHook) - _hooks.DELETE("/:hook", perm.MustPlatformAdmin(), hook.DeleteHook) - _hooks.POST("/:hook/redeliver", perm.MustWrite(), hook.RedeliverHook) + + _hook := _hooks.Group("/:hook", hmiddleware.Establish()) + { + _hook.GET("", perm.MustRead(), hook.GetHook) + _hook.PUT("", perm.MustPlatformAdmin(), hook.UpdateHook) + _hook.DELETE("", perm.MustPlatformAdmin(), hook.DeleteHook) + _hook.POST("/redeliver", perm.MustWrite(), hook.RedeliverHook) + } } // end of hooks endpoints } diff --git a/router/middleware/build/build.go b/router/middleware/build/build.go index 06273e532..c59d85aa8 100644 --- a/router/middleware/build/build.go +++ b/router/middleware/build/build.go @@ -12,9 +12,7 @@ import ( api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -26,9 +24,8 @@ func Retrieve(c *gin.Context) *api.Build { // Establish sets the build in the given context. func Establish() gin.HandlerFunc { return func(c *gin.Context) { - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() if r == nil { @@ -54,15 +51,7 @@ func Establish() gin.HandlerFunc { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": number, - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Debugf("reading build %s/%d", r.GetFullName(), number) + l.Debugf("reading build %d", number) b, err := database.FromContext(c).GetBuildForRepo(ctx, r, number) if err != nil { @@ -72,6 +61,14 @@ func Establish() gin.HandlerFunc { return } + l = l.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + }) + + // update the logger with the new fields + c.Set("logger", l) + ToContext(c, b) c.Next() } diff --git a/router/middleware/build/build_test.go b/router/middleware/build/build_test.go index e95036c54..aacf14949 100644 --- a/router/middleware/build/build_test.go +++ b/router/middleware/build/build_test.go @@ -10,6 +10,7 @@ import ( "github.com/gin-gonic/gin" "github.com/google/go-cmp/cmp" + "github.com/sirupsen/logrus" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -128,6 +129,7 @@ func TestBuild_Establish(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -166,6 +168,7 @@ func TestBuild_Establish_NoRepo(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) @@ -212,6 +215,7 @@ func TestBuild_Establish_NoBuildParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -263,6 +267,7 @@ func TestBuild_Establish_InvalidBuildParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/foo", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -311,6 +316,7 @@ func TestBuild_Establish_NoBuild(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) diff --git a/router/middleware/claims/claims.go b/router/middleware/claims/claims.go index 75a30caa0..e306f9e7e 100644 --- a/router/middleware/claims/claims.go +++ b/router/middleware/claims/claims.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/go-vela/server/internal/token" "github.com/go-vela/server/router/middleware/auth" @@ -22,7 +23,9 @@ func Retrieve(c *gin.Context) *token.Claims { // Establish sets the claims in the given context. func Establish() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) tm := c.MustGet("token-manager").(*token.Manager) + // get the access token from the request at, err := auth.RetrieveAccessToken(c.Request) if err != nil { @@ -51,6 +54,13 @@ func Establish() gin.HandlerFunc { return } + l = l.WithFields(logrus.Fields{ + "claim_subject": claims.Subject, + }) + + // update the logger with the new fields + c.Set("logger", l) + ToContext(c, claims) c.Next() } diff --git a/router/middleware/claims/claims_test.go b/router/middleware/claims/claims_test.go index fa61c773f..9d065df0f 100644 --- a/router/middleware/claims/claims_test.go +++ b/router/middleware/claims/claims_test.go @@ -14,6 +14,7 @@ import ( "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v5" + "github.com/sirupsen/logrus" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -198,6 +199,7 @@ func TestClaims_Establish(t *testing.T) { gin.SetMode(gin.TestMode) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(Establish()) engine.PUT(tt.Endpoint, func(c *gin.Context) { @@ -238,6 +240,7 @@ func TestClaims_Establish_NoToken(t *testing.T) { context, engine := gin.CreateTestContext(resp) context.Request, _ = http.NewRequest(http.MethodGet, "/workers/host", nil) + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(Establish()) @@ -290,6 +293,7 @@ func TestClaims_Establish_BadToken(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tkn)) + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { c.Set("secret", "very-secret") }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) diff --git a/router/middleware/dashboard/dashboard.go b/router/middleware/dashboard/dashboard.go index b62db4b90..e1b86335b 100644 --- a/router/middleware/dashboard/dashboard.go +++ b/router/middleware/dashboard/dashboard.go @@ -23,6 +23,7 @@ func Retrieve(c *gin.Context) *api.Dashboard { // Establish sets the build in the given context. func Establish() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) u := user.Retrieve(c) ctx := c.Request.Context() @@ -39,13 +40,7 @@ func Establish() gin.HandlerFunc { id = userBoards[0] } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "dashboard": id, - "user": u.GetName(), - }).Debugf("reading dashboard %s", id) + l.Debugf("reading dashboard %s", id) d, err := database.FromContext(c).GetDashboard(ctx, id) if err != nil { @@ -55,6 +50,13 @@ func Establish() gin.HandlerFunc { return } + l = l.WithFields(logrus.Fields{ + "dashboard": d.GetID(), + }) + + // update the logger with the new fields + c.Set("logger", l) + ToContext(c, d) c.Next() } diff --git a/router/middleware/dashboard/dashboard_test.go b/router/middleware/dashboard/dashboard_test.go index 35509c19e..39c6b6f5f 100644 --- a/router/middleware/dashboard/dashboard_test.go +++ b/router/middleware/dashboard/dashboard_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -81,6 +82,7 @@ func TestDashboard_Establish(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/c8da1302-07d6-11ea-882f-4893bca275b8", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/:dashboard", func(c *gin.Context) { @@ -117,6 +119,7 @@ func TestDashboard_Establish_NoDashboardParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "//test", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/:dashboard/test", func(c *gin.Context) { @@ -147,6 +150,7 @@ func TestDashboard_Establish_NoDashboard(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/c8da1302-07d6-11ea-882f-4893bca275b8", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/:dashboard", func(c *gin.Context) { diff --git a/router/middleware/dashboard/doc.go b/router/middleware/dashboard/doc.go new file mode 100644 index 000000000..e12e9d7d5 --- /dev/null +++ b/router/middleware/dashboard/doc.go @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: Apache-2.0 + +// Package dashboard provides the ability for inserting +// Vela dashboard resources into or extracting Vela dashboard +// resources from the middleware chain for the API. +// +// Usage: +// +// import "github.com/go-vela/server/router/middleware/dashboard" +package dashboard diff --git a/router/middleware/header.go b/router/middleware/header.go index c65a003af..a54d737f8 100644 --- a/router/middleware/header.go +++ b/router/middleware/header.go @@ -31,10 +31,12 @@ func Options(c *gin.Context) { c.Next() } else { c.Header("Access-Control-Allow-Origin", "*") + if len(m.Vela.WebAddress) > 0 { c.Header("Access-Control-Allow-Origin", m.Vela.WebAddress) c.Header("Access-Control-Allow-Credentials", "true") } + c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,OPTIONS") c.Header("Access-Control-Allow-Headers", "authorization, origin, content-type, accept") c.Header("Access-Control-Max-Age", "86400") diff --git a/router/middleware/hook/context.go b/router/middleware/hook/context.go new file mode 100644 index 000000000..b1fa95a4c --- /dev/null +++ b/router/middleware/hook/context.go @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: Apache-2.0 + +package hook + +import ( + "context" + + "github.com/go-vela/types/library" +) + +const key = "hook" + +// Setter defines a context that enables setting values. +type Setter interface { + Set(string, interface{}) +} + +// FromContext returns the Repo associated with this context. +func FromContext(c context.Context) *library.Hook { + value := c.Value(key) + if value == nil { + return nil + } + + r, ok := value.(*library.Hook) + if !ok { + return nil + } + + return r +} + +// ToContext adds the Repo to this context if it supports +// the Setter interface. +func ToContext(c Setter, r *library.Hook) { + c.Set(key, r) +} diff --git a/router/middleware/hook/context_test.go b/router/middleware/hook/context_test.go new file mode 100644 index 000000000..316632435 --- /dev/null +++ b/router/middleware/hook/context_test.go @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: Apache-2.0 + +package hook + +import ( + "testing" + + "github.com/gin-gonic/gin" + + "github.com/go-vela/types/library" +) + +func TestHook_FromContext(t *testing.T) { + // setup types + num := int64(1) + want := &library.Hook{ID: &num} + + // setup context + gin.SetMode(gin.TestMode) + context, _ := gin.CreateTestContext(nil) + context.Set(key, want) + + // run test + got := FromContext(context) + + if got != want { + t.Errorf("FromContext is %v, want %v", got, want) + } +} + +func TestHook_FromContext_Bad(t *testing.T) { + // setup context + gin.SetMode(gin.TestMode) + context, _ := gin.CreateTestContext(nil) + context.Set(key, nil) + + // run test + got := FromContext(context) + + if got != nil { + t.Errorf("FromContext is %v, want nil", got) + } +} + +func TestHook_FromContext_WrongType(t *testing.T) { + // setup context + gin.SetMode(gin.TestMode) + context, _ := gin.CreateTestContext(nil) + context.Set(key, 1) + + // run test + got := FromContext(context) + + if got != nil { + t.Errorf("FromContext is %v, want nil", got) + } +} + +func TestHook_FromContext_Empty(t *testing.T) { + // setup context + gin.SetMode(gin.TestMode) + context, _ := gin.CreateTestContext(nil) + + // run test + got := FromContext(context) + + if got != nil { + t.Errorf("FromContext is %v, want nil", got) + } +} + +func TestHook_ToContext(t *testing.T) { + // setup types + num := int64(1) + want := &library.Hook{ID: &num} + + // setup context + gin.SetMode(gin.TestMode) + context, _ := gin.CreateTestContext(nil) + ToContext(context, want) + + // run test + got := context.Value(key) + + if got != want { + t.Errorf("ToContext is %v, want %v", got, want) + } +} diff --git a/router/middleware/hook/doc.go b/router/middleware/hook/doc.go new file mode 100644 index 000000000..0b110262c --- /dev/null +++ b/router/middleware/hook/doc.go @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: Apache-2.0 + +// Package hook provides the ability for inserting +// Vela hook resources into or extracting Vela hook +// resources from the middleware chain for the API. +// +// Usage: +// +// import "github.com/go-vela/server/router/middleware/hook" +package hook diff --git a/router/middleware/hook/hook.go b/router/middleware/hook/hook.go new file mode 100644 index 000000000..18d0c7c21 --- /dev/null +++ b/router/middleware/hook/hook.go @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: Apache-2.0 + +package hook + +import ( + "fmt" + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + + "github.com/go-vela/server/database" + "github.com/go-vela/server/router/middleware/org" + "github.com/go-vela/server/router/middleware/repo" + "github.com/go-vela/server/util" + "github.com/go-vela/types/library" +) + +// Retrieve gets the hook in the given context. +func Retrieve(c *gin.Context) *library.Hook { + return FromContext(c) +} + +// Establish sets the hook in the given context. +func Establish() gin.HandlerFunc { + return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) + o := org.Retrieve(c) + r := repo.Retrieve(c) + ctx := c.Request.Context() + + if r == nil { + retErr := fmt.Errorf("repo %s/%s not found", o, util.PathParameter(c, "repo")) + util.HandleError(c, http.StatusNotFound, retErr) + + return + } + + hParam := util.PathParameter(c, "hook") + if len(hParam) == 0 { + retErr := fmt.Errorf("no hook parameter provided") + util.HandleError(c, http.StatusBadRequest, retErr) + + return + } + + number, err := strconv.Atoi(hParam) + if err != nil { + retErr := fmt.Errorf("malformed hook parameter provided: %s", hParam) + util.HandleError(c, http.StatusBadRequest, retErr) + + return + } + + l.Debugf("reading hook %s/%d", r.GetFullName(), number) + + h, err := database.FromContext(c).GetHookForRepo(ctx, r, number) + if err != nil { + retErr := fmt.Errorf("unable to read hook %s/%d: %w", r.GetFullName(), number, err) + util.HandleError(c, http.StatusNotFound, retErr) + + return + } + + l = l.WithFields(logrus.Fields{ + "hook": h.GetID(), + }) + + // update the logger with the new fields + c.Set("logger", l) + + ToContext(c, h) + c.Next() + } +} diff --git a/router/middleware/hook/hook_test.go b/router/middleware/hook/hook_test.go new file mode 100644 index 000000000..fa3deef79 --- /dev/null +++ b/router/middleware/hook/hook_test.go @@ -0,0 +1,302 @@ +// SPDX-License-Identifier: Apache-2.0 + +package hook + +import ( + "context" + "net/http" + "net/http/httptest" + "reflect" + "testing" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + + api "github.com/go-vela/server/api/types" + "github.com/go-vela/server/database" + "github.com/go-vela/server/router/middleware/org" + "github.com/go-vela/server/router/middleware/repo" + "github.com/go-vela/types/library" +) + +func TestHook_Retrieve(t *testing.T) { + // setup types + want := new(library.Hook) + want.SetID(1) + + // setup context + gin.SetMode(gin.TestMode) + context, _ := gin.CreateTestContext(nil) + ToContext(context, want) + + // run test + got := Retrieve(context) + + if got != want { + t.Errorf("Retrieve is %v, want %v", got, want) + } +} + +func TestHook_Establish(t *testing.T) { + // setup types + owner := new(api.User) + owner.SetID(1) + + r := new(api.Repo) + r.SetID(1) + r.SetOwner(owner) + r.SetHash("baz") + r.SetOrg("foo") + r.SetName("bar") + r.SetFullName("foo/bar") + r.SetVisibility("public") + + want := new(library.Hook) + want.SetID(1) + want.SetRepoID(1) + want.SetBuildID(0) + want.SetNumber(1) + want.SetSourceID("ok") + want.SetStatus("") + want.SetError("") + want.SetCreated(0) + want.SetHost("") + want.SetEvent("") + want.SetEventAction("") + want.SetBranch("") + want.SetError("") + want.SetStatus("") + want.SetLink("") + want.SetWebhookID(1) + + got := new(library.Hook) + + // setup database + db, err := database.NewTest() + if err != nil { + t.Errorf("unable to create test database engine: %v", err) + } + + defer func() { + _ = db.DeleteRepo(context.TODO(), r) + _ = db.DeleteHook(context.TODO(), want) + db.Close() + }() + + _, _ = db.CreateRepo(context.TODO(), r) + _, _ = db.CreateHook(context.TODO(), want) + + // setup context + gin.SetMode(gin.TestMode) + + resp := httptest.NewRecorder() + context, engine := gin.CreateTestContext(resp) + context.Request, _ = http.NewRequest(http.MethodGet, "/hooks/foo/bar/1", nil) + + // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) + engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) + engine.Use(org.Establish()) + engine.Use(repo.Establish()) + engine.Use(Establish()) + engine.GET("/hooks/:org/:repo/:hook", func(c *gin.Context) { + got = Retrieve(c) + + c.Status(http.StatusOK) + }) + + // run test + engine.ServeHTTP(context.Writer, context.Request) + + if resp.Code != http.StatusOK { + t.Errorf("Establish returned %v, want %v", resp.Code, http.StatusOK) + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("Establish is %v, want %v", got, want) + } +} + +func TestHook_Establish_NoRepo(t *testing.T) { + // setup database + db, err := database.NewTest() + if err != nil { + t.Errorf("unable to create test database engine: %v", err) + } + defer db.Close() + + // setup context + gin.SetMode(gin.TestMode) + + resp := httptest.NewRecorder() + context, engine := gin.CreateTestContext(resp) + context.Request, _ = http.NewRequest(http.MethodGet, "/hooks/foo/bar/1", nil) + + // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) + engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) + engine.Use(Establish()) + engine.GET("/hooks/:org/:repo/:hook", func(c *gin.Context) { + c.Status(http.StatusOK) + }) + + // run test + engine.ServeHTTP(context.Writer, context.Request) + + if resp.Code != http.StatusNotFound { + t.Errorf("Establish returned %v, want %v", resp.Code, http.StatusNotFound) + } +} + +func TestHook_Establish_NoHookParameter(t *testing.T) { + // setup types + owner := new(api.User) + owner.SetID(1) + + r := new(api.Repo) + r.SetID(1) + r.SetOwner(owner) + r.SetHash("baz") + r.SetOrg("foo") + r.SetName("bar") + r.SetFullName("foo/bar") + r.SetVisibility("public") + + // setup database + db, err := database.NewTest() + if err != nil { + t.Errorf("unable to create test database engine: %v", err) + } + + defer func() { + _ = db.DeleteRepo(context.TODO(), r) + db.Close() + }() + + _, _ = db.CreateRepo(context.TODO(), r) + + // setup context + gin.SetMode(gin.TestMode) + + resp := httptest.NewRecorder() + context, engine := gin.CreateTestContext(resp) + context.Request, _ = http.NewRequest(http.MethodGet, "/hooks/foo/bar", nil) + + // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) + engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) + engine.Use(org.Establish()) + engine.Use(repo.Establish()) + engine.Use(Establish()) + engine.GET("/hooks/:org/:repo/:hook", func(c *gin.Context) { + c.Status(http.StatusOK) + }) + + // run test + engine.ServeHTTP(context.Writer, context.Request) + + if resp.Code != http.StatusBadRequest { + t.Errorf("Establish returned %v, want %v", resp.Code, http.StatusBadRequest) + } +} + +func TestHook_Establish_InvalidHookParameter(t *testing.T) { + // setup types + owner := new(api.User) + owner.SetID(1) + + r := new(api.Repo) + r.SetID(1) + r.SetOwner(owner) + r.SetHash("baz") + r.SetOrg("foo") + r.SetName("bar") + r.SetFullName("foo/bar") + r.SetVisibility("public") + + // setup database + db, err := database.NewTest() + if err != nil { + t.Errorf("unable to create test database engine: %v", err) + } + + defer func() { + _ = db.DeleteRepo(context.TODO(), r) + db.Close() + }() + + _, _ = db.CreateRepo(context.TODO(), r) + + // setup context + gin.SetMode(gin.TestMode) + + resp := httptest.NewRecorder() + context, engine := gin.CreateTestContext(resp) + context.Request, _ = http.NewRequest(http.MethodGet, "/hooks/foo/bar/foo", nil) + + // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) + engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) + engine.Use(org.Establish()) + engine.Use(repo.Establish()) + engine.Use(Establish()) + engine.GET("/hooks/:org/:repo/:hook", func(c *gin.Context) { + c.Status(http.StatusOK) + }) + + // run test + engine.ServeHTTP(context.Writer, context.Request) + + if resp.Code != http.StatusBadRequest { + t.Errorf("Establish returned %v, want %v", resp.Code, http.StatusBadRequest) + } +} + +func TestHook_Establish_NoHook(t *testing.T) { + // setup types + r := new(api.Repo) + r.SetID(1) + r.GetOwner().SetID(1) + r.SetHash("baz") + r.SetOrg("foo") + r.SetName("bar") + r.SetFullName("foo/bar") + r.SetVisibility("public") + + // setup database + db, err := database.NewTest() + if err != nil { + t.Errorf("unable to create test database engine: %v", err) + } + + defer func() { + _ = db.DeleteRepo(context.TODO(), r) + db.Close() + }() + + _, _ = db.CreateRepo(context.TODO(), r) + + // setup context + gin.SetMode(gin.TestMode) + + resp := httptest.NewRecorder() + context, engine := gin.CreateTestContext(resp) + context.Request, _ = http.NewRequest(http.MethodGet, "/hooks/foo/bar/1", nil) + + // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) + engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) + engine.Use(org.Establish()) + engine.Use(repo.Establish()) + engine.Use(Establish()) + engine.GET("/hooks/:org/:repo/:hook", func(c *gin.Context) { + c.Status(http.StatusOK) + }) + + // run test + engine.ServeHTTP(context.Writer, context.Request) + + if resp.Code != http.StatusNotFound { + t.Errorf("Establish returned %v, want %v", resp.Code, http.StatusNotFound) + } +} diff --git a/router/middleware/logger.go b/router/middleware/logger.go index 0f1b14c4c..5b8be89c9 100644 --- a/router/middleware/logger.go +++ b/router/middleware/logger.go @@ -9,8 +9,13 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/router/middleware/build" + "github.com/go-vela/server/router/middleware/claims" + "github.com/go-vela/server/router/middleware/dashboard" + "github.com/go-vela/server/router/middleware/hook" "github.com/go-vela/server/router/middleware/org" + "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" + "github.com/go-vela/server/router/middleware/schedule" "github.com/go-vela/server/router/middleware/service" "github.com/go-vela/server/router/middleware/step" "github.com/go-vela/server/router/middleware/user" @@ -46,11 +51,20 @@ func Logger(logger *logrus.Logger, timeFormat string) gin.HandlerFunc { // some evil middlewares modify this values path := util.EscapeValue(c.Request.URL.Path) - c.Next() + fields := logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": path, + } + + entry := logger.WithFields(fields) + + // set the logger in the context so + // downstream handlers can use it + c.Set("logger", entry) - end := time.Now() + c.Next() - latency := end.Sub(start) + latency := time.Since(start) // prevent us from logging the health endpoint if c.Request.URL.Path != "/health" { @@ -73,6 +87,7 @@ func Logger(logger *logrus.Logger, timeFormat string) gin.HandlerFunc { build := build.Retrieve(c) if build != nil { fields["build"] = build.Number + fields["build_id"] = build.ID } org := org.Retrieve(c) @@ -80,29 +95,73 @@ func Logger(logger *logrus.Logger, timeFormat string) gin.HandlerFunc { fields["org"] = org } + pipeline := pipeline.Retrieve(c) + if pipeline != nil { + fields["pipeline_id"] = pipeline.ID + } + repo := repo.Retrieve(c) if repo != nil { fields["repo"] = repo.Name + fields["repo_id"] = repo.ID } service := service.Retrieve(c) if service != nil { fields["service"] = service.Number + fields["service_id"] = service.ID + } + + hook := hook.Retrieve(c) + if hook != nil { + fields["hook"] = hook.Number + fields["hook_id"] = hook.ID } step := step.Retrieve(c) if step != nil { fields["step"] = step.Number + fields["step_id"] = step.ID + } + + schedule := schedule.Retrieve(c) + if schedule != nil { + fields["schedule"] = schedule.Name + fields["schedule_id"] = schedule.ID + } + + dashboard := dashboard.Retrieve(c) + if dashboard != nil { + fields["dashboard"] = dashboard.Name + fields["dashboard_id"] = dashboard.ID } user := user.Retrieve(c) - if user != nil { + // we check to make sure user name is populated + // because when it's not a user token, we still + // inject an empty user object into the context + // which results in log entries with 'user: null' + if user != nil && user.GetName() != "" { fields["user"] = user.Name + fields["user_id"] = user.ID } worker := worker.Retrieve(c) if worker != nil { fields["worker"] = worker.Hostname + fields["worker_id"] = worker.ID + } + + // if there's no user or worker in the context + // of this request, we log claims subject + _, hasUser := fields["user"] + _, hasWorker := fields["worker"] + + if !hasUser && !hasWorker { + claims := claims.Retrieve(c) + if claims != nil { + fields["claims_subject"] = claims.Subject + } } entry := logger.WithFields(fields) @@ -188,7 +247,7 @@ func (f *ECSFormatter) Format(e *logrus.Entry) ([]byte, error) { } jf := logrus.JSONFormatter{ - TimestampFormat: "2006-01-02T15:04:05.000Z0700", + TimestampFormat: time.RFC3339, // same as default in logrus FieldMap: ecsFieldMap, } diff --git a/router/middleware/org/org.go b/router/middleware/org/org.go index 70efcdfc9..78ccbe708 100644 --- a/router/middleware/org/org.go +++ b/router/middleware/org/org.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/go-vela/server/util" ) @@ -19,7 +20,10 @@ func Retrieve(c *gin.Context) string { // Establish used to check if org param is used only. func Establish() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) + oParam := util.PathParameter(c, "org") + if len(oParam) == 0 { retErr := fmt.Errorf("no org parameter provided") util.HandleError(c, http.StatusBadRequest, retErr) @@ -27,8 +31,14 @@ func Establish() gin.HandlerFunc { return } - ToContext(c, oParam) + l = l.WithFields(logrus.Fields{ + "org": oParam, + }) + // update the logger with the new fields + c.Set("logger", l) + + ToContext(c, oParam) c.Next() } } diff --git a/router/middleware/org/org_test.go b/router/middleware/org/org_test.go index f540a8623..2e5607ef0 100644 --- a/router/middleware/org/org_test.go +++ b/router/middleware/org/org_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -74,6 +75,7 @@ func TestOrg_Establish(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/:org", func(c *gin.Context) { @@ -110,6 +112,7 @@ func TestOrg_Establish_NoOrgParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "//test", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/:org/test", func(c *gin.Context) { diff --git a/router/middleware/perm/perm.go b/router/middleware/perm/perm.go index e0f11979d..5206ea9a2 100644 --- a/router/middleware/perm/perm.go +++ b/router/middleware/perm/perm.go @@ -13,7 +13,6 @@ import ( "github.com/go-vela/server/constants" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/claims" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -23,14 +22,10 @@ import ( // MustPlatformAdmin ensures the user has admin access to the platform. func MustPlatformAdmin() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": cl.Subject, - }).Debugf("verifying user %s is a platform admin", cl.Subject) + l.Debugf("verifying user %s is a platform admin", cl.Subject) switch { case cl.IsAdmin: @@ -38,11 +33,10 @@ func MustPlatformAdmin() gin.HandlerFunc { default: if strings.EqualFold(cl.TokenType, constants.WorkerBuildTokenType) { - logrus.WithFields(logrus.Fields{ - "user": cl.Subject, - "repo": cl.Repo, - "build": cl.BuildID, - }).Warnf("attempted access of admin endpoint with build token from %s", cl.Subject) + l.WithFields(logrus.Fields{ + "claims_repo": cl.Repo, + "claims_build": cl.BuildID, + }).Warnf("attempted access of admin endpoint with build token by %s", cl.Subject) } retErr := fmt.Errorf("user %s is not a platform admin", cl.Subject) @@ -56,14 +50,10 @@ func MustPlatformAdmin() gin.HandlerFunc { // MustWorkerRegisterToken ensures the token is a registration token retrieved by a platform admin. func MustWorkerRegisterToken() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "user": cl.Subject, - }).Debugf("verifying user %s has a registration token for worker", cl.Subject) + l.Debugf("verifying user %s has a registration token for worker", cl.Subject) switch cl.TokenType { case constants.WorkerRegisterTokenType: @@ -89,20 +79,14 @@ func MustWorkerRegisterToken() gin.HandlerFunc { // MustWorkerAuthToken ensures the token is a worker auth token. func MustWorkerAuthToken() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "worker": cl.Subject, - }).Debugf("verifying worker %s has a valid auth token", cl.Subject) + l.Debugf("verifying worker %s has a valid auth token", cl.Subject) // global permissions bypass if cl.IsAdmin { - logrus.WithFields(logrus.Fields{ - "user": cl.Subject, - }).Debugf("user %s has platform admin permissions", cl.Subject) + l.Debugf("user %s has platform admin permissions", cl.Subject) return } @@ -131,24 +115,18 @@ func MustWorkerAuthToken() gin.HandlerFunc { // MustBuildAccess ensures the token is a build token for the appropriate build. func MustBuildAccess() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) b := build.Retrieve(c) // global permissions bypass if cl.IsAdmin { - logrus.WithFields(logrus.Fields{ - "user": cl.Subject, - }).Debugf("user %s has platform admin permissions", cl.Subject) + l.Debugf("user %s has platform admin permissions", cl.Subject) return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "worker": cl.Subject, - }).Debugf("verifying worker %s has a valid build token", cl.Subject) + l.Debugf("verifying worker %s has a valid build token", cl.Subject) // validate token type and match build id in request with build id in token claims switch cl.TokenType { @@ -157,10 +135,9 @@ func MustBuildAccess() gin.HandlerFunc { return } - logrus.WithFields(logrus.Fields{ - "user": cl.Subject, - "repo": cl.Repo, - "build": cl.BuildID, + l.WithFields(logrus.Fields{ + "claims_repo": cl.Repo, + "claims_build": cl.BuildID, }).Warnf("build token for build %d attempted to be used for build %d by %s", cl.BuildID, b.GetID(), cl.Subject) fallthrough @@ -176,15 +153,11 @@ func MustBuildAccess() gin.HandlerFunc { // MustIDRequestToken ensures the token is a valid ID request token for the appropriate build. func MustIDRequestToken() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) b := build.Retrieve(c) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "repo": cl.Subject, - }).Debugf("verifying worker %s has a valid build token", cl.Subject) + logrus.Debugf("verifying worker %s has a valid build token", cl.Subject) // verify expected type if !strings.EqualFold(cl.TokenType, constants.IDRequestTokenType) { @@ -203,10 +176,9 @@ func MustIDRequestToken() gin.HandlerFunc { // verify expected build id if b.GetID() != cl.BuildID { - logrus.WithFields(logrus.Fields{ - "user": cl.Subject, - "repo": cl.Repo, - "build": cl.BuildID, + l.WithFields(logrus.Fields{ + "claims_repo": cl.Repo, + "claims_build": cl.BuildID, }).Warnf("request ID token for build %d attempted to be used for %s build %d by %s", cl.BuildID, b.GetStatus(), b.GetID(), cl.Subject) retErr := fmt.Errorf("invalid token") @@ -216,10 +188,9 @@ func MustIDRequestToken() gin.HandlerFunc { } // MustSecretAdmin ensures the user has admin access to the org, repo or team. -// -//nolint:funlen // ignore function length func MustSecretAdmin() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) u := user.Retrieve(c) e := util.PathParameter(c, "engine") @@ -232,29 +203,20 @@ func MustSecretAdmin() gin.HandlerFunc { // create log fields from API metadata fields := logrus.Fields{ - "engine": e, - "org": o, - "repo": n, - "type": t, - "user": u.GetName(), + "secret_engine": e, + "secret_org": o, + "secret_repo": n, + "secret_type": t, } // check if secret is a shared secret if strings.EqualFold(t, constants.SecretShared) { // update log fields from API metadata - fields = logrus.Fields{ - "engine": e, - "org": o, - "team": n, - "type": t, - "user": u.GetName(), - } + delete(fields, "repo") + fields["secret_team"] = n } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(fields) + logger := l.WithFields(fields) if u.GetAdmin() { return @@ -335,8 +297,7 @@ func MustSecretAdmin() gin.HandlerFunc { // check if user is accessing shared secrets in personal org if strings.EqualFold(o, u.GetName()) { logger.WithFields(logrus.Fields{ - "org": o, - "user": u.GetName(), + "secret_org": o, }).Debugf("skipping gathering teams for user %s with org %s", u.GetName(), o) return @@ -386,21 +347,12 @@ func MustSecretAdmin() gin.HandlerFunc { // MustAdmin ensures the user has admin access to the repo. func MustAdmin() gin.HandlerFunc { return func(c *gin.Context) { - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) - - logger.Debugf("verifying user %s has 'admin' permissions for repo %s", u.GetName(), r.GetFullName()) + l.Debugf("verifying user %s has 'admin' permissions for repo %s", u.GetName(), r.GetFullName()) if u.GetAdmin() { return @@ -415,7 +367,7 @@ func MustAdmin() gin.HandlerFunc { // https://docs.github.com/en/rest/reference/repos#get-repository-permissions-for-a-user perm, err = scm.FromContext(c).RepoAccess(ctx, u.GetName(), r.GetOwner().GetToken(), r.GetOrg(), r.GetName()) if err != nil { - logger.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) + l.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) } } @@ -436,21 +388,12 @@ func MustAdmin() gin.HandlerFunc { // MustWrite ensures the user has admin or write access to the repo. func MustWrite() gin.HandlerFunc { return func(c *gin.Context) { - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) - - logger.Debugf("verifying user %s has 'write' permissions for repo %s", u.GetName(), r.GetFullName()) + l.Debugf("verifying user %s has 'write' permissions for repo %s", u.GetName(), r.GetFullName()) if u.GetAdmin() { return @@ -465,7 +408,7 @@ func MustWrite() gin.HandlerFunc { // https://docs.github.com/en/rest/reference/repos#get-repository-permissions-for-a-user perm, err = scm.FromContext(c).RepoAccess(ctx, u.GetName(), r.GetOwner().GetToken(), r.GetOrg(), r.GetName()) if err != nil { - logger.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) + l.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) } } @@ -487,24 +430,15 @@ func MustWrite() gin.HandlerFunc { // MustRead ensures the user has admin, write or read access to the repo. func MustRead() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) - o := org.Retrieve(c) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logger := logrus.WithFields(logrus.Fields{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) - // check if the repo visibility field is set to public if strings.EqualFold(r.GetVisibility(), constants.VisibilityPublic) { - logger.Debugf("skipping 'read' check for repo %s with %s visibility for user %s", r.GetFullName(), r.GetVisibility(), u.GetName()) + l.Debugf("skipping 'read' check for repo %s with %s visibility for user %s", r.GetFullName(), r.GetVisibility(), u.GetName()) return } @@ -523,7 +457,7 @@ func MustRead() gin.HandlerFunc { return } - logger.Debugf("verifying user %s has 'read' permissions for repo %s", u.GetName(), r.GetFullName()) + l.Debugf("verifying user %s has 'read' permissions for repo %s", u.GetName(), r.GetFullName()) // return if user is platform admin if u.GetAdmin() { @@ -539,7 +473,7 @@ func MustRead() gin.HandlerFunc { // https://docs.github.com/en/rest/reference/repos#get-repository-permissions-for-a-user perm, err = scm.FromContext(c).RepoAccess(ctx, u.GetName(), r.GetOwner().GetToken(), r.GetOrg(), r.GetName()) if err != nil { - logger.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) + l.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) } } diff --git a/router/middleware/perm/perm_test.go b/router/middleware/perm/perm_test.go index 772aecf14..a39c3a1b2 100644 --- a/router/middleware/perm/perm_test.go +++ b/router/middleware/perm/perm_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/constants" @@ -83,6 +84,7 @@ func TestPerm_MustPlatformAdmin(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -163,6 +165,7 @@ func TestPerm_MustPlatformAdmin_NotAdmin(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -213,6 +216,7 @@ func TestPerm_MustWorkerRegisterToken(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(claims.Establish()) engine.Use(user.Establish()) @@ -276,6 +280,7 @@ func TestPerm_MustWorkerRegisterToken_PlatAdmin(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(claims.Establish()) @@ -324,6 +329,7 @@ func TestPerm_MustWorkerAuthToken(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(claims.Establish()) engine.Use(user.Establish()) @@ -364,6 +370,7 @@ func TestPerm_MustWorkerAuth_ServerWorkerToken(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", secret)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(claims.Establish()) @@ -448,6 +455,7 @@ func TestPerm_MustBuildAccess(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -542,6 +550,7 @@ func TestPerm_MustBuildAccess_PlatAdmin(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -633,6 +642,7 @@ func TestPerm_MustBuildToken_WrongBuild(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -726,6 +736,7 @@ func TestPerm_MustIDRequestToken(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -824,6 +835,7 @@ func TestPerm_MustIDRequestToken_NotRunning(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -915,6 +927,7 @@ func TestPerm_MustIDRequestToken_WrongBuild(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1003,6 +1016,7 @@ func TestPerm_MustSecretAdmin_BuildToken_Repo(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1088,6 +1102,7 @@ func TestPerm_MustSecretAdmin_BuildToken_Org(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1173,6 +1188,7 @@ func TestPerm_MustSecretAdmin_BuildToken_Shared(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1269,6 +1285,7 @@ func TestPerm_MustAdmin(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1368,6 +1385,7 @@ func TestPerm_MustAdmin_PlatAdmin(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1467,6 +1485,7 @@ func TestPerm_MustAdmin_NotAdmin(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1566,6 +1585,7 @@ func TestPerm_MustWrite(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1665,6 +1685,7 @@ func TestPerm_MustWrite_PlatAdmin(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1764,6 +1785,7 @@ func TestPerm_MustWrite_RepoAdmin(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1863,6 +1885,7 @@ func TestPerm_MustWrite_NotWrite(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -1962,6 +1985,7 @@ func TestPerm_MustRead(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -2061,6 +2085,7 @@ func TestPerm_MustRead_PlatAdmin(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -2149,6 +2174,7 @@ func TestPerm_MustRead_WorkerBuildToken(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -2248,6 +2274,7 @@ func TestPerm_MustRead_RepoAdmin(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -2347,6 +2374,7 @@ func TestPerm_MustRead_RepoWrite(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -2446,6 +2474,7 @@ func TestPerm_MustRead_RepoPublic(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -2545,6 +2574,7 @@ func TestPerm_MustRead_NotRead(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) diff --git a/router/middleware/pipeline/pipeline.go b/router/middleware/pipeline/pipeline.go index e2bf396ea..162c80ea5 100644 --- a/router/middleware/pipeline/pipeline.go +++ b/router/middleware/pipeline/pipeline.go @@ -13,7 +13,6 @@ import ( "github.com/go-vela/server/compiler" "github.com/go-vela/server/database" "github.com/go-vela/server/internal" - "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -29,7 +28,7 @@ func Retrieve(c *gin.Context) *library.Pipeline { // Establish sets the pipeline in the given context. func Establish() gin.HandlerFunc { return func(c *gin.Context) { - o := org.Retrieve(c) + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() @@ -53,15 +52,7 @@ func Establish() gin.HandlerFunc { entry := fmt.Sprintf("%s/%s", r.GetFullName(), p) - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "pipeline": p, - "repo": r.GetName(), - "user": u.GetName(), - }).Debugf("reading pipeline %s", entry) + l.Debugf("reading pipeline %s", entry) pipeline, err := database.FromContext(c).GetPipelineForRepo(ctx, p, r) if err != nil { // assume the pipeline doesn't exist in the database yet (before pipeline support was added) @@ -94,8 +85,15 @@ func Establish() gin.HandlerFunc { } } - ToContext(c, pipeline) + l = l.WithFields(logrus.Fields{ + "pipeline": pipeline.GetCommit(), + "pipeline_id": pipeline.GetID(), + }) + + // update the logger with the new fields + c.Set("logger", l) + ToContext(c, pipeline) c.Next() } } diff --git a/router/middleware/pipeline/pipeline_test.go b/router/middleware/pipeline/pipeline_test.go index 5a8c6d608..1408d0dea 100644 --- a/router/middleware/pipeline/pipeline_test.go +++ b/router/middleware/pipeline/pipeline_test.go @@ -13,6 +13,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" api "github.com/go-vela/server/api/types" @@ -121,6 +122,7 @@ func TestPipeline_Establish(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/pipelines/foo/bar/48afb5bdc41ad69bf22588491333f7cf71135163", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -159,6 +161,7 @@ func TestPipeline_Establish_NoRepo(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/pipelines/foo/bar/48afb5bdc41ad69bf22588491333f7cf71135163", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) @@ -205,6 +208,7 @@ func TestPipeline_Establish_NoPipelineParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/pipelines/foo/bar", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -325,6 +329,7 @@ func TestPipeline_Establish_NoPipeline(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("metadata", m) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) diff --git a/router/middleware/repo/repo.go b/router/middleware/repo/repo.go index 448bc13ed..bbc678660 100644 --- a/router/middleware/repo/repo.go +++ b/router/middleware/repo/repo.go @@ -12,7 +12,6 @@ import ( api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/org" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -24,8 +23,8 @@ func Retrieve(c *gin.Context) *api.Repo { // Establish sets the repo in the given context. func Establish() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) o := org.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() rParam := util.PathParameter(c, "repo") @@ -36,14 +35,7 @@ func Establish() gin.HandlerFunc { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": o, - "repo": rParam, - "user": u.GetName(), - }).Debugf("reading repo %s/%s", o, rParam) + l.Debugf("reading repo %s", rParam) r, err := database.FromContext(c).GetRepoForOrg(ctx, o, rParam) if err != nil { @@ -53,6 +45,14 @@ func Establish() gin.HandlerFunc { return } + l = l.WithFields(logrus.Fields{ + "repo": r.GetName(), + "repo_id": r.GetID(), + }) + + // update the logger with the new fields + c.Set("logger", l) + ToContext(c, r) c.Next() } diff --git a/router/middleware/repo/repo_test.go b/router/middleware/repo/repo_test.go index d2bb0d512..c20067c62 100644 --- a/router/middleware/repo/repo_test.go +++ b/router/middleware/repo/repo_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -91,6 +92,7 @@ func TestRepo_Establish(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(Establish()) @@ -128,6 +130,7 @@ func TestRepo_Establish_NoOrgParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "//bar/test", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/:org/:repo/test", func(c *gin.Context) { @@ -158,6 +161,7 @@ func TestRepo_Establish_NoRepoParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo//test", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/:org/:repo/test", func(c *gin.Context) { @@ -188,6 +192,7 @@ func TestRepo_Establish_NoRepo(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/:org/:repo", func(c *gin.Context) { diff --git a/router/middleware/schedule/schedule.go b/router/middleware/schedule/schedule.go index 41864cef0..ff617bc3a 100644 --- a/router/middleware/schedule/schedule.go +++ b/router/middleware/schedule/schedule.go @@ -12,7 +12,6 @@ import ( api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -24,8 +23,8 @@ func Retrieve(c *gin.Context) *api.Schedule { // Establish sets the schedule in the given context. func Establish() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() sParam := util.PathParameter(c, "schedule") @@ -36,14 +35,7 @@ func Establish() gin.HandlerFunc { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "org": r.GetOrg(), - "repo": r.GetName(), - "user": u.GetName(), - }).Debugf("reading schedule %s for repo %s", sParam, r.GetFullName()) + l.Debugf("reading schedule %s", sParam) s, err := database.FromContext(c).GetScheduleForRepo(ctx, r, sParam) if err != nil { @@ -53,6 +45,14 @@ func Establish() gin.HandlerFunc { return } + l = l.WithFields(logrus.Fields{ + "schedule": s.GetName(), + "schedule_id": s.GetID(), + }) + + // update the logger with the new fields + c.Set("logger", l) + ToContext(c, s) c.Next() } diff --git a/router/middleware/service/service.go b/router/middleware/service/service.go index 32142e16b..7089e22f7 100644 --- a/router/middleware/service/service.go +++ b/router/middleware/service/service.go @@ -14,7 +14,6 @@ import ( "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -28,10 +27,10 @@ func Retrieve(c *gin.Context) *library.Service { func Establish() gin.HandlerFunc { return func(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) o := org.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() if r == nil { @@ -64,16 +63,7 @@ func Establish() gin.HandlerFunc { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "service": number, - "repo": r.GetName(), - "user": u.GetName(), - }).Debugf("reading service %s/%d/%d", r.GetFullName(), b.GetNumber(), number) + l.Debugf("reading service %d", number) s, err := database.FromContext(c).GetServiceForBuild(ctx, b, number) if err != nil { @@ -83,6 +73,14 @@ func Establish() gin.HandlerFunc { return } + l = l.WithFields(logrus.Fields{ + "service": s.GetName(), + "service_id": s.GetID(), + }) + + // update the logger with the new fields + c.Set("logger", l) + ToContext(c, s) c.Next() } diff --git a/router/middleware/service/service_test.go b/router/middleware/service/service_test.go index 65869cea0..681975c36 100644 --- a/router/middleware/service/service_test.go +++ b/router/middleware/service/service_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -100,6 +101,7 @@ func TestService_Establish(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/services/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -139,6 +141,7 @@ func TestService_Establish_NoRepo(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/services/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/:org/:repo/builds/:build/services/:service", func(c *gin.Context) { @@ -185,6 +188,7 @@ func TestService_Establish_NoBuild(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/services/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -243,6 +247,7 @@ func TestService_Establish_NoServiceParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/services", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -302,6 +307,7 @@ func TestService_Establish_InvalidServiceParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/services/foo", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -358,6 +364,7 @@ func TestService_Establish_NoService(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/services/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) diff --git a/router/middleware/step/step.go b/router/middleware/step/step.go index 77a710585..c1e31cf43 100644 --- a/router/middleware/step/step.go +++ b/router/middleware/step/step.go @@ -14,7 +14,6 @@ import ( "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -28,10 +27,10 @@ func Retrieve(c *gin.Context) *library.Step { func Establish() gin.HandlerFunc { return func(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) o := org.Retrieve(c) r := repo.Retrieve(c) - u := user.Retrieve(c) ctx := c.Request.Context() if r == nil { @@ -64,16 +63,7 @@ func Establish() gin.HandlerFunc { return } - // update engine logger with API metadata - // - // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields - logrus.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "org": o, - "step": number, - "repo": r.GetName(), - "user": u.GetName(), - }).Debugf("reading step %s/%d/%d", r.GetFullName(), b.GetNumber(), number) + l.Debugf("reading step %d", number) s, err := database.FromContext(c).GetStepForBuild(ctx, b, number) if err != nil { @@ -83,6 +73,14 @@ func Establish() gin.HandlerFunc { return } + l = l.WithFields(logrus.Fields{ + "step": s.GetNumber(), + "step_id": s.GetID(), + }) + + // update the logger with the new fields + c.Set("logger", l) + ToContext(c, s) c.Next() } diff --git a/router/middleware/step/step_test.go b/router/middleware/step/step_test.go index b96141e5b..120170d04 100644 --- a/router/middleware/step/step_test.go +++ b/router/middleware/step/step_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -103,6 +104,7 @@ func TestStep_Establish(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/steps/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -142,6 +144,7 @@ func TestStep_Establish_NoRepo(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/steps/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/:org/:repo/builds/:build/steps/:step", func(c *gin.Context) { @@ -191,6 +194,7 @@ func TestStep_Establish_NoBuild(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/steps/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -249,6 +253,7 @@ func TestStep_Establish_NoStepParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/steps", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -308,6 +313,7 @@ func TestStep_Establish_InvalidStepParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/steps/foo", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) @@ -367,6 +373,7 @@ func TestStep_Establish_NoStep(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/foo/bar/builds/1/steps/1", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(org.Establish()) engine.Use(repo.Establish()) diff --git a/router/middleware/user/user.go b/router/middleware/user/user.go index 89b286b08..cc883fd41 100644 --- a/router/middleware/user/user.go +++ b/router/middleware/user/user.go @@ -24,6 +24,7 @@ func Retrieve(c *gin.Context) *api.User { // Establish sets the user in the given context. func Establish() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) ctx := c.Request.Context() @@ -37,7 +38,7 @@ func Establish() gin.HandlerFunc { return } - logrus.Debugf("parsing user access token") + l.Debugf("parsing user access token") // lookup user in claims subject in the database u, err := database.FromContext(c).GetUserForName(ctx, cl.Subject) @@ -46,6 +47,14 @@ func Establish() gin.HandlerFunc { return } + l = l.WithFields(logrus.Fields{ + "user": u.GetName(), + "user_id": u.GetID(), + }) + + // update the logger with the new fields + c.Set("logger", l) + ToContext(c, u) c.Next() } diff --git a/router/middleware/user/user_test.go b/router/middleware/user/user_test.go index 8e15f80f7..28d7f7a3d 100644 --- a/router/middleware/user/user_test.go +++ b/router/middleware/user/user_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -111,6 +112,7 @@ func TestUser_Establish(t *testing.T) { client, _ := github.NewTest(s.URL) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) @@ -161,6 +163,7 @@ func TestUser_Establish_NoToken(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/users/foo", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) @@ -198,6 +201,7 @@ func TestUser_Establish_DiffTokenType(t *testing.T) { context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", secret)) // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { c.Set("token-manager", tm) }) engine.Use(claims.Establish()) @@ -251,6 +255,7 @@ func TestUser_Establish_NoAuthorizeUser(t *testing.T) { client, _ := github.NewTest("") // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) @@ -313,6 +318,7 @@ func TestUser_Establish_NoUser(t *testing.T) { client, _ := github.NewTest("") // setup vela mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) diff --git a/router/middleware/worker/worker.go b/router/middleware/worker/worker.go index d46ad1a1a..628ffb9f3 100644 --- a/router/middleware/worker/worker.go +++ b/router/middleware/worker/worker.go @@ -22,6 +22,7 @@ func Retrieve(c *gin.Context) *api.Worker { // Establish sets the worker in the given context. func Establish() gin.HandlerFunc { return func(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() wParam := util.PathParameter(c, "worker") @@ -32,7 +33,7 @@ func Establish() gin.HandlerFunc { return } - logrus.Debugf("Reading worker %s", wParam) + l.Debugf("reading worker %s", wParam) w, err := database.FromContext(c).GetWorkerForHostname(ctx, wParam) if err != nil { @@ -42,6 +43,14 @@ func Establish() gin.HandlerFunc { return } + l = l.WithFields(logrus.Fields{ + "worker": w.GetHostname(), + "worker_id": w.GetID(), + }) + + // update the logger with the new fields + c.Set("logger", l) + ToContext(c, w) c.Next() } diff --git a/router/middleware/worker/worker_test.go b/router/middleware/worker/worker_test.go index d39d43842..6ea01df56 100644 --- a/router/middleware/worker/worker_test.go +++ b/router/middleware/worker/worker_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" @@ -75,6 +76,7 @@ func TestWorker_Establish(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/workers/worker_0", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/workers/:worker", func(c *gin.Context) { @@ -111,6 +113,7 @@ func TestWorker_Establish_NoWorkerParameter(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/workers/", nil) // setup mock server + engine.Use(func(c *gin.Context) { c.Set("logger", logrus.NewEntry(logrus.StandardLogger())) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) engine.Use(Establish()) engine.GET("/workers/:worker", func(c *gin.Context) { diff --git a/router/queue.go b/router/queue.go index 90452c2a8..5082a4284 100644 --- a/router/queue.go +++ b/router/queue.go @@ -12,7 +12,7 @@ import ( // QueueHandlers is a function that extends the provided base router group // with the API handlers for queue registration functionality. // -// POST /api/v1/queue/register. +// GET /api/v1/queue/info . func QueueHandlers(base *gin.RouterGroup) { // Queue endpoints _queue := base.Group("/queue") diff --git a/scm/github/deployment.go b/scm/github/deployment.go index 852ca0bb2..91357afa9 100644 --- a/scm/github/deployment.go +++ b/scm/github/deployment.go @@ -158,9 +158,10 @@ func (c *client) GetDeploymentList(ctx context.Context, u *api.User, r *api.Repo // CreateDeployment creates a new deployment for the GitHub repo. func (c *client) CreateDeployment(ctx context.Context, u *api.User, r *api.Repo, d *library.Deployment) error { c.Logger.WithFields(logrus.Fields{ - "org": r.GetOrg(), - "repo": r.GetName(), - "user": u.GetName(), + "org": r.GetOrg(), + "repo": r.GetName(), + "user": u.GetName(), + "user_id": u.GetID(), }).Tracef("creating deployment for repo %s", r.GetFullName()) // create GitHub OAuth client with user's token diff --git a/scm/github/repo.go b/scm/github/repo.go index c3b7ab7c9..8c0a58457 100644 --- a/scm/github/repo.go +++ b/scm/github/repo.go @@ -25,7 +25,7 @@ func (c *client) ConfigBackoff(ctx context.Context, u *api.User, r *api.Repo, re retryLimit := 5 for i := 0; i < retryLimit; i++ { - logrus.Debugf("Fetching config file - Attempt %d", i+1) + logrus.Debugf("fetching config file - Attempt %d", i+1) // attempt to fetch the config data, err = c.Config(ctx, u, r, ref)