From 6a41f5bb0be440a73a276c683f4d13e9114b43c8 Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Fri, 31 May 2024 10:16:50 -0500 Subject: [PATCH 01/26] wip --- api/admin/build.go | 8 ++++---- api/admin/clean.go | 10 +++------- api/admin/hook.go | 3 --- api/admin/repo.go | 3 --- api/admin/secret.go | 3 --- api/admin/service.go | 5 ++--- api/admin/settings.go | 19 +++++++++++-------- api/admin/step.go | 5 ++--- api/admin/user.go | 3 --- api/admin/worker.go | 8 +------- api/auth/logout.go | 2 +- api/auth/post_token.go | 8 +++++++- api/auth/redirect.go | 2 +- api/auth/validate_oauth.go | 2 +- api/badge.go | 2 +- api/build/approve.go | 13 ++++++++----- api/build/cancel.go | 8 +++++--- api/build/create.go | 2 +- api/build/delete.go | 2 +- api/build/enqueue.go | 4 ++-- api/build/executable.go | 2 +- api/build/get.go | 2 +- api/build/get_id.go | 3 +-- api/build/graph.go | 8 +++----- api/build/list_org.go | 3 +-- api/build/list_repo.go | 2 +- api/build/restart.go | 3 ++- api/build/token.go | 2 +- api/build/update.go | 2 +- api/dashboard/create.go | 2 +- api/dashboard/delete.go | 2 +- api/dashboard/get.go | 2 +- api/dashboard/list_user.go | 2 +- api/dashboard/update.go | 2 +- api/deployment/create.go | 2 +- api/deployment/get.go | 2 +- api/deployment/list.go | 2 +- api/hook/create.go | 2 +- api/hook/delete.go | 2 +- api/hook/get.go | 2 +- api/hook/list.go | 2 +- api/hook/redeliver.go | 2 +- api/hook/update.go | 2 +- api/log/create_service.go | 2 +- api/log/create_step.go | 2 +- api/log/delete_service.go | 2 +- api/log/delete_step.go | 2 +- api/log/get_service.go | 2 +- api/log/get_step.go | 2 +- api/log/list_build.go | 2 +- api/log/update_service.go | 2 +- api/log/update_step.go | 2 +- api/pipeline/compile.go | 2 +- api/pipeline/create.go | 2 +- api/pipeline/delete.go | 2 +- api/pipeline/expand.go | 2 +- api/pipeline/get.go | 2 +- api/pipeline/list.go | 2 +- api/pipeline/template.go | 2 +- api/pipeline/update.go | 2 +- api/pipeline/validate.go | 2 +- api/queue/queue.go | 2 +- api/repo/chown.go | 2 +- api/repo/create.go | 2 +- api/repo/delete.go | 2 +- api/repo/get.go | 2 +- api/repo/list.go | 2 +- api/repo/list_org.go | 2 +- api/repo/repair.go | 2 +- api/repo/update.go | 4 ++-- api/schedule/create.go | 2 +- api/schedule/delete.go | 2 +- api/schedule/get.go | 2 +- api/schedule/list.go | 2 +- api/schedule/update.go | 2 +- api/scm/sync.go | 2 +- api/scm/sync_org.go | 2 +- api/secret/create.go | 2 +- api/secret/delete.go | 2 +- api/secret/get.go | 2 +- api/secret/list.go | 2 +- api/secret/update.go | 2 +- api/service/create.go | 2 +- api/service/delete.go | 2 +- api/service/get.go | 2 +- api/service/list.go | 2 +- api/service/update.go | 2 +- api/step/create.go | 2 +- api/step/delete.go | 2 +- api/step/get.go | 2 +- api/step/list.go | 2 +- api/step/update.go | 2 +- api/user/create.go | 2 +- api/user/create_token.go | 2 +- api/user/delete.go | 2 +- api/user/delete_token.go | 2 +- api/user/get.go | 2 +- api/user/get_current.go | 2 +- api/user/get_source.go | 2 +- api/user/list.go | 2 +- api/user/update.go | 2 +- api/user/update_current.go | 2 +- api/webhook/post.go | 4 ++-- api/worker/create.go | 2 +- api/worker/delete.go | 2 +- api/worker/get.go | 2 +- api/worker/list.go | 2 +- api/worker/refresh.go | 2 +- api/worker/update.go | 2 +- router/middleware/logger.go | 16 +++++++++++++--- 110 files changed, 158 insertions(+), 161 deletions(-) diff --git a/api/admin/build.go b/api/admin/build.go index 1a4fe0489..fb93cfda1 100644 --- a/api/admin/build.go +++ b/api/admin/build.go @@ -14,6 +14,7 @@ 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" ) @@ -47,10 +48,10 @@ import ( // AllBuildsQueue represents the API handler to // capture all running and pending builds stored in the database. func AllBuildsQueue(c *gin.Context) { + logrus.Debug("admin: reading running and pending builds") // capture middleware values ctx := c.Request.Context() - - logrus.Info("Admin: reading running and pending builds") + u := user.Retrieve(c) // 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)) @@ -101,10 +102,9 @@ func AllBuildsQueue(c *gin.Context) { // UpdateBuild represents the API handler to // update any build stored in the database. func UpdateBuild(c *gin.Context) { - logrus.Info("Admin: updating build in database") - // capture middleware values ctx := c.Request.Context() + u := user.Retrieve(c) // capture body from API request input := new(types.Build) diff --git a/api/admin/clean.go b/api/admin/clean.go index 9813a7fc7..bdfc624ae 100644 --- a/api/admin/clean.go +++ b/api/admin/clean.go @@ -61,10 +61,8 @@ import ( // update any user stored in the database. func CleanResources(c *gin.Context) { // capture middleware values - u := user.Retrieve(c) ctx := c.Request.Context() - - logrus.Infof("platform admin %s: updating pending resources in database", u.GetName()) + u := user.Retrieve(c) // default error message msg := "build cleaned by platform admin" @@ -106,7 +104,7 @@ func CleanResources(c *gin.Context) { return } - logrus.Infof("platform admin %s: cleaned %d builds in database", u.GetName(), builds) + logrus.Debugf("platform admin %s: cleaned %d builds in database", u.GetName(), builds) // clean executables executables, err := database.FromContext(c).CleanBuildExecutables(ctx) @@ -128,7 +126,7 @@ func CleanResources(c *gin.Context) { return } - logrus.Infof("platform admin %s: cleaned %d services in database", u.GetName(), services) + logrus.Debugf("platform admin %s: cleaned %d services in database", u.GetName(), services) // clean steps steps, err := database.FromContext(c).CleanSteps(ctx, msg, before) @@ -140,7 +138,5 @@ func CleanResources(c *gin.Context) { return } - logrus.Infof("platform admin %s: cleaned %d steps in database", u.GetName(), 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 156110880..00f034b91 100644 --- a/api/admin/hook.go +++ b/api/admin/hook.go @@ -8,7 +8,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" "github.com/go-vela/server/database" "github.com/go-vela/server/util" @@ -48,8 +47,6 @@ import ( // UpdateHook represents the API handler to // update any hook stored in the database. func UpdateHook(c *gin.Context) { - logrus.Info("Admin: updating hook in database") - // capture middleware values ctx := c.Request.Context() diff --git a/api/admin/repo.go b/api/admin/repo.go index 52da52301..718309bba 100644 --- a/api/admin/repo.go +++ b/api/admin/repo.go @@ -8,7 +8,6 @@ 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" @@ -48,8 +47,6 @@ import ( // UpdateRepo represents the API handler to // update any repo stored in the database. func UpdateRepo(c *gin.Context) { - logrus.Info("Admin: updating repo in database") - // capture middleware values ctx := c.Request.Context() diff --git a/api/admin/secret.go b/api/admin/secret.go index f9573d9dd..e47f8cc4c 100644 --- a/api/admin/secret.go +++ b/api/admin/secret.go @@ -8,7 +8,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" "github.com/go-vela/server/database" "github.com/go-vela/server/util" @@ -48,8 +47,6 @@ import ( // UpdateSecret represents the API handler to // update any secret stored in the database. func UpdateSecret(c *gin.Context) { - logrus.Info("Admin: updating secret in database") - // capture middleware values ctx := c.Request.Context() diff --git a/api/admin/service.go b/api/admin/service.go index 4d74fb388..12b9e58e7 100644 --- a/api/admin/service.go +++ b/api/admin/service.go @@ -8,9 +8,9 @@ import ( "net/http" "github.com/gin-gonic/gin" - "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/library" ) @@ -49,10 +49,9 @@ import ( // UpdateService represents the API handler to // update any service stored in the database. func UpdateService(c *gin.Context) { - logrus.Info("Admin: updating service in database") - // capture middleware values ctx := c.Request.Context() + u := user.Retrieve(c) // capture body from API request input := new(library.Service) diff --git a/api/admin/settings.go b/api/admin/settings.go index f04109680..c100d486a 100644 --- a/api/admin/settings.go +++ b/api/admin/settings.go @@ -48,10 +48,11 @@ import ( // GetSettings represents the API handler to // captures settings stored in the database. func GetSettings(c *gin.Context) { + logrus.Debug("platform admin: reading platform settings") + // capture middleware values s := sMiddleware.FromContext(c) - - logrus.Info("Admin: reading settings") + u := uMiddleware.FromContext(c) // check captured value because we aren't retrieving settings from the database // instead we are retrieving the auto-refreshed middleware value @@ -63,6 +64,8 @@ func GetSettings(c *gin.Context) { return } + logrus.Debugf("platform admin %#q retrieved platform settings", u.GetName()) + c.JSON(http.StatusOK, s) } @@ -108,13 +111,13 @@ func GetSettings(c *gin.Context) { // UpdateSettings represents the API handler to // update the settings singleton stored in the database. func UpdateSettings(c *gin.Context) { + logrus.Debug("platform admin: updating platform settings") + // capture middleware values s := sMiddleware.FromContext(c) u := uMiddleware.FromContext(c) ctx := c.Request.Context() - logrus.Info("Admin: updating 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 { @@ -196,6 +199,8 @@ func UpdateSettings(c *gin.Context) { return } + logrus.Debugf("platform admin %#q updated platform settings", u.GetName()) + c.JSON(http.StatusOK, _s) } @@ -231,12 +236,10 @@ func UpdateSettings(c *gin.Context) { // restore settings stored in the database to the environment defaults. func RestoreSettings(c *gin.Context) { // 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 diff --git a/api/admin/step.go b/api/admin/step.go index aeb2e1f4b..abfe1d5af 100644 --- a/api/admin/step.go +++ b/api/admin/step.go @@ -7,7 +7,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" "github.com/go-vela/server/database" "github.com/go-vela/server/util" @@ -47,11 +46,11 @@ import ( // UpdateStep represents the API handler to // update any step stored in the database. func UpdateStep(c *gin.Context) { - logrus.Info("Admin: updating step in database") + // capture middleware values + ctx := c.Request.Context() // capture body from API request input := new(library.Step) - ctx := c.Request.Context() err := c.Bind(input) if err != nil { diff --git a/api/admin/user.go b/api/admin/user.go index 7e5db6072..47241a4e4 100644 --- a/api/admin/user.go +++ b/api/admin/user.go @@ -8,7 +8,6 @@ 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" @@ -48,8 +47,6 @@ import ( // UpdateUser represents the API handler to // update any user stored in the database. func UpdateUser(c *gin.Context) { - logrus.Info("Admin: updating user in database") - // capture middleware values ctx := c.Request.Context() diff --git a/api/admin/worker.go b/api/admin/worker.go index 82d7e9b5c..6e440de9d 100644 --- a/api/admin/worker.go +++ b/api/admin/worker.go @@ -7,10 +7,8 @@ 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/user" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" @@ -44,11 +42,7 @@ 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()) - + // capture middleware values host := util.PathParameter(c, "worker") tm := c.MustGet("token-manager").(*token.Manager) diff --git a/api/auth/logout.go b/api/auth/logout.go index ad21740ed..f3436b293 100644 --- a/api/auth/logout.go +++ b/api/auth/logout.go @@ -52,7 +52,7 @@ func Logout(c *gin.Context) { "user": u.GetName(), }) - logger.Infof("logging out user %s", u.GetName()) + logger.Debugf("logging out user %s", u.GetName()) // parse the address for the backend server // so we can set it for the cookie domain diff --git a/api/auth/post_token.go b/api/auth/post_token.go index 40ffbf99a..7adbd158c 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" @@ -61,6 +62,8 @@ func PostAuthToken(c *gin.Context) { return } + logrus.Debug("user authenticated via SCM using PAT") + // check if the user exists u, err = database.FromContext(c).GetUserForName(ctx, u.GetName()) if err != nil { @@ -71,6 +74,8 @@ func PostAuthToken(c *gin.Context) { return } + logrus.Infof("user %#q successfully authenticated via SCM PAT", u.GetName()) + // 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) @@ -82,13 +87,14 @@ func PostAuthToken(c *gin.Context) { TokenDuration: tm.UserAccessTokenDuration, } 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) } + logrus.Debugf("new access token created for user %#q via SCM PAT", u.GetName()) + // return the user with their jwt access token c.JSON(http.StatusOK, library.Token{Token: &at}) } diff --git a/api/auth/redirect.go b/api/auth/redirect.go index dcaba1844..a16e8d898 100644 --- a/api/auth/redirect.go +++ b/api/auth/redirect.go @@ -71,7 +71,7 @@ func GetAuthRedirect(c *gin.Context) { // load the metadata m := c.MustGet("metadata").(*internal.Metadata) - logrus.Info("redirecting for final auth flow destination") + logrus.Debug("redirecting for final auth flow destination") // capture the path elements t := util.PathParameter(c, "type") diff --git a/api/auth/validate_oauth.go b/api/auth/validate_oauth.go index 399d3e2e2..307e49b5b 100644 --- a/api/auth/validate_oauth.go +++ b/api/auth/validate_oauth.go @@ -30,7 +30,7 @@ import ( // '200': // description: Successfully validated // schema: -// "$ref": "#/definitions/Token" +// type: string // '401': // description: Unable to validate // schema: diff --git a/api/badge.go b/api/badge.go index 4f3dfc380..17d0ad68a 100644 --- a/api/badge.go +++ b/api/badge.go @@ -55,7 +55,7 @@ func GetBadge(c *gin.Context) { logrus.WithFields(logrus.Fields{ "org": o, "repo": r.GetName(), - }).Infof("creating latest build badge for repo %s on branch %s", r.GetFullName(), branch) + }).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 16c1fcc65..478b475c7 100644 --- a/api/build/approve.go +++ b/api/build/approve.go @@ -83,11 +83,14 @@ func ApproveBuild(c *gin.Context) { // // 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(), + "build": b.GetNumber(), + "org": o, + "repo": r.GetName(), + "user": u.GetName(), }) + logger.Debugf("approving build %d", b.GetID()) + // verify build is in correct status if !strings.EqualFold(b.GetStatus(), constants.StatusPendingApproval) { retErr := fmt.Errorf("unable to approve build %s/%d: build not in pending approval state", r.GetFullName(), b.GetNumber()) @@ -104,8 +107,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()) @@ -117,6 +118,8 @@ func ApproveBuild(c *gin.Context) { logrus.Errorf("Failed to update build %d during publish to queue for %s: %v", b.GetNumber(), r.GetFullName(), err) } + logger.Debugf("user %s approved build %s/%d for execution", u.GetName(), r.GetFullName(), b.GetNumber()) + // publish the build to the queue go Enqueue( ctx, diff --git a/api/build/cancel.go b/api/build/cancel.go index afad5d0e0..b220dd893 100644 --- a/api/build/cancel.go +++ b/api/build/cancel.go @@ -53,7 +53,7 @@ import ( // '200': // description: Successfully canceled the build // schema: -// type: string +// "$ref": "#/definitions/Build" // '400': // description: Unable to cancel build // schema: @@ -84,12 +84,14 @@ func CancelBuild(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{ + logger := logrus.WithFields(logrus.Fields{ "build": b.GetNumber(), "org": o, "repo": r.GetName(), "user": user.GetName(), - }).Infof("canceling build %s", entry) + }) + + logger.Debugf("canceling build %s", entry) switch b.GetStatus() { case constants.StatusRunning: diff --git a/api/build/create.go b/api/build/create.go index f5782d331..49f68da3c 100644 --- a/api/build/create.go +++ b/api/build/create.go @@ -96,7 +96,7 @@ func CreateBuild(c *gin.Context) { "user": u.GetName(), }) - logger.Infof("creating new build for repo %s", r.GetFullName()) + logger.Debugf("creating new build for repo %s", r.GetFullName()) // capture body from API request input := new(types.Build) diff --git a/api/build/delete.go b/api/build/delete.go index 3da622da0..16a3dc931 100644 --- a/api/build/delete.go +++ b/api/build/delete.go @@ -76,7 +76,7 @@ func DeleteBuild(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("deleting build %s", entry) + }).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..fab6679b7 100644 --- a/api/build/enqueue.go +++ b/api/build/enqueue.go @@ -16,7 +16,7 @@ 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()) + logrus.Debugf("Converting queue item to json for build %d for %s", item.Build.GetNumber(), item.Build.GetRepo().GetFullName()) byteItem, err := json.Marshal(item) if err != nil { @@ -28,7 +28,7 @@ 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) + logrus.Debugf("Pushing item for build %d for %s to queue route %s", item.Build.GetNumber(), item.Build.GetRepo().GetFullName(), route) // push item on to the queue err = queue.Push(context.Background(), route, byteItem) diff --git a/api/build/executable.go b/api/build/executable.go index 324984b10..1f7f46556 100644 --- a/api/build/executable.go +++ b/api/build/executable.go @@ -84,7 +84,7 @@ func GetBuildExecutable(c *gin.Context) { "org": o, "repo": r.GetName(), "subject": cl.Subject, - }).Infof("reading build executable %s/%d", r.GetFullName(), b.GetNumber()) + }).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()) diff --git a/api/build/get.go b/api/build/get.go index afa0994d6..d611e98a2 100644 --- a/api/build/get.go +++ b/api/build/get.go @@ -63,7 +63,7 @@ func GetBuild(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("reading build %s/%d", r.GetFullName(), b.GetNumber()) + }).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 d0a72191d..f6a2aab6d 100644 --- a/api/build/get_id.go +++ b/api/build/get_id.go @@ -54,7 +54,6 @@ func GetBuildByID(c *gin.Context) { // Parse build ID from path id, err := strconv.ParseInt(c.Param("id"), 10, 64) - if err != nil { retErr := fmt.Errorf("unable to parse build id: %w", err) @@ -69,7 +68,7 @@ func GetBuildByID(c *gin.Context) { logrus.WithFields(logrus.Fields{ "build": id, "user": u.GetName(), - }).Infof("reading build %d", id) + }).Debugf("reading build %d", id) // Get build from database b, err := database.FromContext(c).GetBuild(ctx, id) diff --git a/api/build/graph.go b/api/build/graph.go index 454cdd27a..29600d8bc 100644 --- a/api/build/graph.go +++ b/api/build/graph.go @@ -159,9 +159,7 @@ func GetBuildGraph(c *gin.Context) { baseErr := "unable to retrieve graph" - logger.Infof("constructing graph for build %s", entry) - - logger.Info("retrieving pipeline configuration") + logger.Debugf("constructing graph for build %s and retrieving pipeline configuration", entry) var config []byte @@ -206,7 +204,7 @@ func GetBuildGraph(c *gin.Context) { } } - logger.Info("compiling pipeline configuration") + logger.Debug("compiling pipeline configuration") // parse and compile the pipeline configuration file p, _, err := compiler.FromContext(c). @@ -327,7 +325,7 @@ func GetBuildGraph(c *gin.Context) { return } - logger.Info("generating build graph") + logger.Debug("generating build graph") // create nodes from pipeline stages nodes := make(map[int]*node) diff --git a/api/build/list_org.go b/api/build/list_org.go index a8e491a47..9b15717ce 100644 --- a/api/build/list_org.go +++ b/api/build/list_org.go @@ -117,7 +117,7 @@ func ListBuildsForOrg(c *gin.Context) { logrus.WithFields(logrus.Fields{ "org": o, "user": u.GetName(), - }).Infof("listing builds for org %s", o) + }).Debugf("listing builds for org %s", o) // capture the branch name parameter branch := c.Query("branch") @@ -201,7 +201,6 @@ func ListBuildsForOrg(c *gin.Context) { // send API call to capture the list of builds for the org (and event type if passed in) b, t, err = database.FromContext(c).ListBuildsForOrg(ctx, o, filters, page, perPage) - if err != nil { retErr := fmt.Errorf("unable to list builds for org %s: %w", o, err) diff --git a/api/build/list_repo.go b/api/build/list_repo.go index 9c3fca345..1565a1f79 100644 --- a/api/build/list_repo.go +++ b/api/build/list_repo.go @@ -139,7 +139,7 @@ func ListBuildsForRepo(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("listing builds for repo %s", r.GetFullName()) + }).Debugf("listing builds for repo %s", r.GetFullName()) // capture the branch name parameter branch := c.Query("branch") diff --git a/api/build/restart.go b/api/build/restart.go index 2569a4915..422468732 100644 --- a/api/build/restart.go +++ b/api/build/restart.go @@ -103,6 +103,8 @@ func RestartBuild(c *gin.Context) { "user": u.GetName(), }) + logger.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) { retErr := fmt.Errorf("unable to restart build %s/%d: cannot restart a build pending approval", r.GetFullName(), b.GetNumber()) @@ -136,7 +138,6 @@ func RestartBuild(c *gin.Context) { compiler.FromContext(c), queue.FromContext(c), ) - if err != nil { util.HandleError(c, code, err) diff --git a/api/build/token.go b/api/build/token.go index 4c6de18ec..688c4fdf4 100644 --- a/api/build/token.go +++ b/api/build/token.go @@ -80,7 +80,7 @@ func GetBuildToken(c *gin.Context) { "org": o, "repo": r.GetName(), "user": cl.Subject, - }).Infof("generating build token for build %s/%d", r.GetFullName(), b.GetNumber()) + }).Debugf("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 e1b64b423..75489af4e 100644 --- a/api/build/update.go +++ b/api/build/update.go @@ -86,7 +86,7 @@ func UpdateBuild(c *gin.Context) { "org": o, "repo": r.GetName(), "user": cl.Subject, - }).Infof("updating build %s", entry) + }).Debugf("updating build %s", entry) // capture body from API request input := new(types.Build) diff --git a/api/dashboard/create.go b/api/dashboard/create.go index f14a06fe1..46ce9c041 100644 --- a/api/dashboard/create.go +++ b/api/dashboard/create.go @@ -82,7 +82,7 @@ func CreateDashboard(c *gin.Context) { // 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()) + }).Debugf("creating new dashboard %s", input.GetName()) d := new(types.Dashboard) diff --git a/api/dashboard/delete.go b/api/dashboard/delete.go index a2798c940..c0abd8110 100644 --- a/api/dashboard/delete.go +++ b/api/dashboard/delete.go @@ -62,7 +62,7 @@ func DeleteDashboard(c *gin.Context) { logrus.WithFields(logrus.Fields{ "dashboard": d.GetID(), "user": u.GetName(), - }).Infof("deleting dashboard %s", d.GetID()) + }).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 3535cb8b4..aafe80b64 100644 --- a/api/dashboard/get.go +++ b/api/dashboard/get.go @@ -66,7 +66,7 @@ func GetDashboard(c *gin.Context) { logrus.WithFields(logrus.Fields{ "dashboard": d.GetID(), "user": u.GetName(), - }).Infof("reading dashboard %s", d.GetID()) + }).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 656704358..abfea7096 100644 --- a/api/dashboard/list_user.go +++ b/api/dashboard/list_user.go @@ -56,7 +56,7 @@ func ListUserDashboards(c *gin.Context) { // 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()) + }).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 7958ef72d..9f19484c7 100644 --- a/api/dashboard/update.go +++ b/api/dashboard/update.go @@ -79,7 +79,7 @@ func UpdateDashboard(c *gin.Context) { // 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()) + }).Debugf("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 b0fb02156..7df9afbdd 100644 --- a/api/deployment/create.go +++ b/api/deployment/create.go @@ -69,7 +69,7 @@ func CreateDeployment(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("creating new deployment for repo %s", r.GetFullName()) + }).Debugf("creating new deployment for repo %s", r.GetFullName()) // capture body from API request input := new(library.Deployment) diff --git a/api/deployment/get.go b/api/deployment/get.go index c881bc800..72418d334 100644 --- a/api/deployment/get.go +++ b/api/deployment/get.go @@ -76,7 +76,7 @@ func GetDeployment(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("reading deployment %s", entry) + }).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 55667fdac..2a6c16bcb 100644 --- a/api/deployment/list.go +++ b/api/deployment/list.go @@ -87,7 +87,7 @@ func ListDeployments(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("reading deployments for repo %s", r.GetFullName()) + }).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 a039371c5..c16bab69e 100644 --- a/api/hook/create.go +++ b/api/hook/create.go @@ -74,7 +74,7 @@ func CreateHook(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("creating new hook for repo %s", r.GetFullName()) + }).Debugf("creating new hook for repo %s", r.GetFullName()) // capture body from API request input := new(library.Hook) diff --git a/api/hook/delete.go b/api/hook/delete.go index e31909a9f..9833bbd65 100644 --- a/api/hook/delete.go +++ b/api/hook/delete.go @@ -80,7 +80,7 @@ func DeleteHook(c *gin.Context) { "hook": hook, "repo": r.GetName(), "user": u.GetName(), - }).Infof("deleting hook %s", entry) + }).Debugf("deleting hook %s", entry) number, err := strconv.Atoi(hook) if err != nil { diff --git a/api/hook/get.go b/api/hook/get.go index 849aa60f7..f74d41e3f 100644 --- a/api/hook/get.go +++ b/api/hook/get.go @@ -76,7 +76,7 @@ func GetHook(c *gin.Context) { "hook": hook, "repo": r.GetName(), "user": u.GetName(), - }).Infof("reading hook %s", entry) + }).Debugf("reading hook %s", entry) number, err := strconv.Atoi(hook) if err != nil { diff --git a/api/hook/list.go b/api/hook/list.go index e7235dba2..885d93ed9 100644 --- a/api/hook/list.go +++ b/api/hook/list.go @@ -88,7 +88,7 @@ func ListHooks(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("reading hooks for repo %s", r.GetFullName()) + }).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 a860c1718..cf58d581e 100644 --- a/api/hook/redeliver.go +++ b/api/hook/redeliver.go @@ -81,7 +81,7 @@ func RedeliverHook(c *gin.Context) { "hook": hook, "repo": r.GetName(), "user": u.GetName(), - }).Infof("redelivering hook %s", entry) + }).Debugf("redelivering hook %s", entry) number, err := strconv.Atoi(hook) if err != nil { diff --git a/api/hook/update.go b/api/hook/update.go index 8d662b31c..e17a34f76 100644 --- a/api/hook/update.go +++ b/api/hook/update.go @@ -87,7 +87,7 @@ func UpdateHook(c *gin.Context) { "hook": hook, "repo": r.GetName(), "user": u.GetName(), - }).Infof("updating hook %s", entry) + }).Debugf("updating hook %s", entry) // capture body from API request input := new(library.Hook) diff --git a/api/log/create_service.go b/api/log/create_service.go index 442714c27..33194f0da 100644 --- a/api/log/create_service.go +++ b/api/log/create_service.go @@ -91,7 +91,7 @@ func CreateServiceLog(c *gin.Context) { "repo": r.GetName(), "service": s.GetNumber(), "user": u.GetName(), - }).Infof("creating logs for service %s", entry) + }).Debugf("creating logs for service %s", entry) // capture body from API request input := new(library.Log) diff --git a/api/log/create_step.go b/api/log/create_step.go index c5ed5f49f..f0ff46f89 100644 --- a/api/log/create_step.go +++ b/api/log/create_step.go @@ -91,7 +91,7 @@ func CreateStepLog(c *gin.Context) { "repo": r.GetName(), "step": s.GetNumber(), "user": u.GetName(), - }).Infof("creating logs for step %s", entry) + }).Debugf("creating logs for step %s", entry) // capture body from API request input := new(library.Log) diff --git a/api/log/delete_service.go b/api/log/delete_service.go index 9893d60d3..eba687a69 100644 --- a/api/log/delete_service.go +++ b/api/log/delete_service.go @@ -81,7 +81,7 @@ func DeleteServiceLog(c *gin.Context) { "repo": r.GetName(), "service": s.GetNumber(), "user": u.GetName(), - }).Infof("deleting logs for service %s", entry) + }).Debugf("deleting logs for service %s", entry) // send API call to capture the service logs l, err := database.FromContext(c).GetLogForService(ctx, s) diff --git a/api/log/delete_step.go b/api/log/delete_step.go index ebf505f1f..b6a08be6d 100644 --- a/api/log/delete_step.go +++ b/api/log/delete_step.go @@ -81,7 +81,7 @@ func DeleteStepLog(c *gin.Context) { "repo": r.GetName(), "step": s.GetNumber(), "user": u.GetName(), - }).Infof("deleting logs for step %s", entry) + }).Debugf("deleting logs for step %s", entry) // send API call to capture the step logs l, err := database.FromContext(c).GetLogForStep(ctx, s) diff --git a/api/log/get_service.go b/api/log/get_service.go index 193850e90..2a4990517 100644 --- a/api/log/get_service.go +++ b/api/log/get_service.go @@ -81,7 +81,7 @@ func GetServiceLog(c *gin.Context) { "repo": r.GetName(), "service": s.GetNumber(), "user": u.GetName(), - }).Infof("reading logs for service %s", entry) + }).Debugf("reading logs for service %s", entry) // send API call to capture the service logs l, err := database.FromContext(c).GetLogForService(ctx, s) diff --git a/api/log/get_step.go b/api/log/get_step.go index 2e8b524be..d38bbba2b 100644 --- a/api/log/get_step.go +++ b/api/log/get_step.go @@ -82,7 +82,7 @@ func GetStepLog(c *gin.Context) { "repo": r.GetName(), "step": s.GetNumber(), "user": u.GetName(), - }).Infof("reading logs for step %s", entry) + }).Debugf("reading logs for step %s", entry) // send API call to capture the step logs l, err := database.FromContext(c).GetLogForStep(ctx, s) diff --git a/api/log/list_build.go b/api/log/list_build.go index 5fc996ef0..e6ada1275 100644 --- a/api/log/list_build.go +++ b/api/log/list_build.go @@ -87,7 +87,7 @@ func ListLogsForBuild(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("listing logs for build %s", entry) + }).Debugf("listing logs for build %s", entry) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/log/update_service.go b/api/log/update_service.go index 6d36b9bea..fc88a6669 100644 --- a/api/log/update_service.go +++ b/api/log/update_service.go @@ -93,7 +93,7 @@ func UpdateServiceLog(c *gin.Context) { "repo": r.GetName(), "service": s.GetNumber(), "user": u.GetName(), - }).Infof("updating logs for service %s", entry) + }).Debugf("updating logs for service %s", entry) // send API call to capture the service logs l, err := database.FromContext(c).GetLogForService(ctx, s) diff --git a/api/log/update_step.go b/api/log/update_step.go index c34596a0f..b72354594 100644 --- a/api/log/update_step.go +++ b/api/log/update_step.go @@ -93,7 +93,7 @@ func UpdateStepLog(c *gin.Context) { "repo": r.GetName(), "step": s.GetNumber(), "user": u.GetName(), - }).Infof("updating logs for step %s", entry) + }).Debugf("updating logs for step %s", entry) // send API call to capture the step logs l, err := database.FromContext(c).GetLogForStep(ctx, s) diff --git a/api/pipeline/compile.go b/api/pipeline/compile.go index 4585451ba..9414604bc 100644 --- a/api/pipeline/compile.go +++ b/api/pipeline/compile.go @@ -88,7 +88,7 @@ func CompilePipeline(c *gin.Context) { "pipeline": p.GetCommit(), "repo": r.GetName(), "user": u.GetName(), - }).Infof("compiling pipeline %s", entry) + }).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 1092a4051..7ca82b087 100644 --- a/api/pipeline/create.go +++ b/api/pipeline/create.go @@ -80,7 +80,7 @@ func CreatePipeline(c *gin.Context) { "user": u.GetName(), }) - logger.Infof("creating new pipeline for repo %s", r.GetFullName()) + logger.Debugf("creating new pipeline for repo %s", r.GetFullName()) // capture body from API request input := new(library.Pipeline) diff --git a/api/pipeline/delete.go b/api/pipeline/delete.go index b59f1adc3..71021ec80 100644 --- a/api/pipeline/delete.go +++ b/api/pipeline/delete.go @@ -76,7 +76,7 @@ func DeletePipeline(c *gin.Context) { "pipeline": p.GetCommit(), "repo": r.GetName(), "user": u.GetName(), - }).Infof("deleting pipeline %s", entry) + }).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 17e62181c..fae704165 100644 --- a/api/pipeline/expand.go +++ b/api/pipeline/expand.go @@ -88,7 +88,7 @@ func ExpandPipeline(c *gin.Context) { "pipeline": p.GetCommit(), "repo": r.GetName(), "user": u.GetName(), - }).Infof("expanding templates for pipeline %s", entry) + }).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 69739f523..2d1979251 100644 --- a/api/pipeline/get.go +++ b/api/pipeline/get.go @@ -63,7 +63,7 @@ func GetPipeline(c *gin.Context) { "pipeline": p.GetCommit(), "repo": r.GetName(), "user": u.GetName(), - }).Infof("reading pipeline %s/%s", r.GetFullName(), p.GetCommit()) + }).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 0ffdcff95..a76eb28e3 100644 --- a/api/pipeline/list.go +++ b/api/pipeline/list.go @@ -88,7 +88,7 @@ func ListPipelines(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("listing pipelines for repo %s", r.GetFullName()) + }).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 66c8cd627..4ccb34a59 100644 --- a/api/pipeline/template.go +++ b/api/pipeline/template.go @@ -92,7 +92,7 @@ func GetTemplates(c *gin.Context) { "pipeline": p.GetCommit(), "repo": r.GetName(), "user": u.GetName(), - }).Infof("reading templates from pipeline %s", entry) + }).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 f5230fbe8..1e8f5bdbf 100644 --- a/api/pipeline/update.go +++ b/api/pipeline/update.go @@ -83,7 +83,7 @@ func UpdatePipeline(c *gin.Context) { "pipeline": p.GetCommit(), "repo": r.GetName(), "user": u.GetName(), - }).Infof("updating pipeline %s", entry) + }).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 b7f43664d..d0d0b7f38 100644 --- a/api/pipeline/validate.go +++ b/api/pipeline/validate.go @@ -86,7 +86,7 @@ func ValidatePipeline(c *gin.Context) { "pipeline": p.GetCommit(), "repo": r.GetName(), "user": u.GetName(), - }).Infof("validating pipeline %s", entry) + }).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..8cd480a6c 100644 --- a/api/queue/queue.go +++ b/api/queue/queue.go @@ -38,7 +38,7 @@ func Info(c *gin.Context) { logrus.WithFields(logrus.Fields{ "user": cl.Subject, - }).Info("requesting queue credentials with registration token") + }).Debug("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 f68a4fac6..2a0c34163 100644 --- a/api/repo/chown.go +++ b/api/repo/chown.go @@ -62,7 +62,7 @@ func ChownRepo(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("changing owner of repo %s to %s", r.GetFullName(), u.GetName()) + }).Debugf("changing owner of repo %s to %s", r.GetFullName(), u.GetName()) // update repo owner r.SetOwner(u) diff --git a/api/repo/create.go b/api/repo/create.go index 7249bbfa4..1b96aeed5 100644 --- a/api/repo/create.go +++ b/api/repo/create.go @@ -102,7 +102,7 @@ func CreateRepo(c *gin.Context) { "org": input.GetOrg(), "repo": input.GetName(), "user": u.GetName(), - }).Infof("creating new repo %s", input.GetFullName()) + }).Debugf("creating new repo %s", input.GetFullName()) // get repo information from the source r, _, err := scm.FromContext(c).GetRepo(ctx, u, input) diff --git a/api/repo/delete.go b/api/repo/delete.go index 7a70ceac4..ad80b38ad 100644 --- a/api/repo/delete.go +++ b/api/repo/delete.go @@ -67,7 +67,7 @@ func DeleteRepo(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("deleting repo %s", r.GetFullName()) + }).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 61df846d4..a35d1f075 100644 --- a/api/repo/get.go +++ b/api/repo/get.go @@ -54,7 +54,7 @@ func GetRepo(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("reading repo %s", r.GetFullName()) + }).Debugf("reading repo %s", r.GetFullName()) c.JSON(http.StatusOK, r) } diff --git a/api/repo/list.go b/api/repo/list.go index 010ae0cfe..187978f6d 100644 --- a/api/repo/list.go +++ b/api/repo/list.go @@ -72,7 +72,7 @@ func ListRepos(c *gin.Context) { // 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()) + }).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 565082f97..a99fa1727 100644 --- a/api/repo/list_org.go +++ b/api/repo/list_org.go @@ -95,7 +95,7 @@ func ListReposForOrg(c *gin.Context) { logrus.WithFields(logrus.Fields{ "org": o, "user": u.GetName(), - }).Infof("listing repos for org %s", o) + }).Debugf("listing repos for org %s", o) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/repo/repair.go b/api/repo/repair.go index 1e3eefb33..f7a4a71a6 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -67,7 +67,7 @@ func RepairRepo(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("repairing repo %s", r.GetFullName()) + }).Debugf("repairing repo %s", r.GetFullName()) // check if we should create the webhook if c.Value("webhookvalidation").(bool) { diff --git a/api/repo/update.go b/api/repo/update.go index 82fd02922..ab9010c31 100644 --- a/api/repo/update.go +++ b/api/repo/update.go @@ -87,7 +87,7 @@ func UpdateRepo(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("updating repo %s", r.GetFullName()) + }).Debugf("updating repo %s", r.GetFullName()) // capture body from API request input := new(types.Repo) @@ -260,7 +260,7 @@ func UpdateRepo(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("platform admin %s updating repo webhook events for repo %s", admn, r.GetFullName()) + }).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 a63d60fa4..f38b23865 100644 --- a/api/schedule/create.go +++ b/api/schedule/create.go @@ -118,7 +118,7 @@ func CreateSchedule(c *gin.Context) { "org": r.GetOrg(), "repo": r.GetName(), "user": u.GetName(), - }).Infof("creating new schedule %s", input.GetName()) + }).Debugf("creating new schedule %s", input.GetName()) // ensure repo is allowed to create new schedules if !util.CheckAllowlist(r, s.GetScheduleAllowlist()) { diff --git a/api/schedule/delete.go b/api/schedule/delete.go index 73c60a8c0..ea73fa8f2 100644 --- a/api/schedule/delete.go +++ b/api/schedule/delete.go @@ -73,7 +73,7 @@ func DeleteSchedule(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("deleting schedule %s", s.GetName()) + }).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 d9ff954b2..5f27c76c1 100644 --- a/api/schedule/get.go +++ b/api/schedule/get.go @@ -62,7 +62,7 @@ func GetSchedule(c *gin.Context) { "repo": r.GetName(), "user": u.GetName(), "schedule": s.GetName(), - }).Infof("reading schedule %s", s.GetName()) + }).Debugf("reading schedule %s", s.GetName()) c.JSON(http.StatusOK, s) } diff --git a/api/schedule/list.go b/api/schedule/list.go index c6c424647..74c653c4d 100644 --- a/api/schedule/list.go +++ b/api/schedule/list.go @@ -83,7 +83,7 @@ func ListSchedules(c *gin.Context) { logrus.WithFields(logrus.Fields{ "repo": r.GetName(), "org": r.GetOrg(), - }).Infof("listing schedules for repo %s", r.GetFullName()) + }).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 c29678570..e4cf36fa5 100644 --- a/api/schedule/update.go +++ b/api/schedule/update.go @@ -85,7 +85,7 @@ func UpdateSchedule(c *gin.Context) { "schedule": scheduleName, "repo": r.GetName(), "org": r.GetOrg(), - }).Infof("updating schedule %s", scheduleName) + }).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 9520fb143..4be93c3b1 100644 --- a/api/scm/sync.go +++ b/api/scm/sync.go @@ -79,7 +79,7 @@ func SyncRepo(c *gin.Context) { "user": u.GetName(), }) - logger.Infof("syncing repo %s", r.GetFullName()) + logger.Debugf("syncing repo %s", r.GetFullName()) // retrieve repo from source code manager service _, respCode, err := scm.FromContext(c).GetRepo(ctx, u, r) diff --git a/api/scm/sync_org.go b/api/scm/sync_org.go index b1162503c..761a42396 100644 --- a/api/scm/sync_org.go +++ b/api/scm/sync_org.go @@ -73,7 +73,7 @@ func SyncReposForOrg(c *gin.Context) { "user": u.GetName(), }) - logger.Infof("syncing repos for org %s", o) + logger.Debugf("syncing repos for org %s", o) // see if the user is an org admin perm, err := scm.FromContext(c).OrgAccess(ctx, u, o) diff --git a/api/secret/create.go b/api/secret/create.go index fd44f1779..5e7fc8002 100644 --- a/api/secret/create.go +++ b/api/secret/create.go @@ -172,7 +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) + logrus.WithFields(fields).Debugf("creating new secret %s for %s service", entry, e) // capture body from API request input := new(library.Secret) diff --git a/api/secret/delete.go b/api/secret/delete.go index dd4db4145..0a1a18c25 100644 --- a/api/secret/delete.go +++ b/api/secret/delete.go @@ -105,7 +105,7 @@ func DeleteSecret(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("deleting secret %s from %s service", entry, e) + logrus.WithFields(fields).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) diff --git a/api/secret/get.go b/api/secret/get.go index 44c379f8b..6da3efc21 100644 --- a/api/secret/get.go +++ b/api/secret/get.go @@ -107,7 +107,7 @@ func GetSecret(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("reading secret %s from %s service", entry, e) + logrus.WithFields(fields).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) diff --git a/api/secret/list.go b/api/secret/list.go index c142b89a6..de1783d63 100644 --- a/api/secret/list.go +++ b/api/secret/list.go @@ -141,7 +141,7 @@ func ListSecrets(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("listing secrets %s from %s service", entry, e) + logrus.WithFields(fields).Debugf("listing secrets %s from %s service", entry, e) // capture page query parameter if present page, err := strconv.Atoi(c.DefaultQuery("page", "1")) diff --git a/api/secret/update.go b/api/secret/update.go index cb012c719..fe6995317 100644 --- a/api/secret/update.go +++ b/api/secret/update.go @@ -117,7 +117,7 @@ func UpdateSecret(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("updating secret %s for %s service", entry, e) + logrus.WithFields(fields).Debugf("updating secret %s for %s service", entry, e) // capture body from API request input := new(library.Secret) diff --git a/api/service/create.go b/api/service/create.go index 2ccb9e314..aa1e6cc5e 100644 --- a/api/service/create.go +++ b/api/service/create.go @@ -85,7 +85,7 @@ func CreateService(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("creating new service for build %s", entry) + }).Debugf("creating new service for build %s", entry) // capture body from API request input := new(library.Service) diff --git a/api/service/delete.go b/api/service/delete.go index 915804a27..c35179223 100644 --- a/api/service/delete.go +++ b/api/service/delete.go @@ -81,7 +81,7 @@ func DeleteService(c *gin.Context) { "repo": r.GetName(), "service": s.GetNumber(), "user": u.GetName(), - }).Infof("deleting service %s", entry) + }).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 b81b69735..bbb816a50 100644 --- a/api/service/get.go +++ b/api/service/get.go @@ -79,7 +79,7 @@ func GetService(c *gin.Context) { "repo": r.GetName(), "service": s.GetNumber(), "user": u.GetName(), - }).Infof("reading service %s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) + }).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 6697a7282..087ef98f2 100644 --- a/api/service/list.go +++ b/api/service/list.go @@ -98,7 +98,7 @@ func ListServices(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("reading services for build %s", entry) + }).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/update.go b/api/service/update.go index a539270e3..da1b7e71a 100644 --- a/api/service/update.go +++ b/api/service/update.go @@ -92,7 +92,7 @@ func UpdateService(c *gin.Context) { "repo": r.GetName(), "service": s.GetNumber(), "user": u.GetName(), - }).Infof("updating service %s", entry) + }).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 2218d214d..ac581591d 100644 --- a/api/step/create.go +++ b/api/step/create.go @@ -85,7 +85,7 @@ func CreateStep(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("creating new step for build %s", entry) + }).Debugf("creating new step for build %s", entry) // capture body from API request input := new(library.Step) diff --git a/api/step/delete.go b/api/step/delete.go index 8d735717a..f2b78a5e5 100644 --- a/api/step/delete.go +++ b/api/step/delete.go @@ -80,7 +80,7 @@ func DeleteStep(c *gin.Context) { "repo": r.GetName(), "step": s.GetNumber(), "user": u.GetName(), - }).Infof("deleting step %s", entry) + }).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 9582b5928..1fd4b30eb 100644 --- a/api/step/get.go +++ b/api/step/get.go @@ -70,7 +70,7 @@ func GetStep(c *gin.Context) { "repo": r.GetName(), "step": s.GetNumber(), "user": u.GetName(), - }).Infof("reading step %s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber()) + }).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 603d7083b..1d6010740 100644 --- a/api/step/list.go +++ b/api/step/list.go @@ -98,7 +98,7 @@ func ListSteps(c *gin.Context) { "org": o, "repo": r.GetName(), "user": u.GetName(), - }).Infof("listing steps for build %s", entry) + }).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/update.go b/api/step/update.go index cb767de4e..c16c15f7f 100644 --- a/api/step/update.go +++ b/api/step/update.go @@ -93,7 +93,7 @@ func UpdateStep(c *gin.Context) { "repo": r.GetName(), "step": s.GetNumber(), "user": u.GetName(), - }).Infof("updating step %s", entry) + }).Debugf("updating step %s", entry) // capture body from API request input := new(library.Step) diff --git a/api/user/create.go b/api/user/create.go index cd68556bf..6e1301966 100644 --- a/api/user/create.go +++ b/api/user/create.go @@ -69,7 +69,7 @@ func CreateUser(c *gin.Context) { // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields logrus.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) diff --git a/api/user/create_token.go b/api/user/create_token.go index e1b8fa9c0..02a948620 100644 --- a/api/user/create_token.go +++ b/api/user/create_token.go @@ -48,7 +48,7 @@ func CreateToken(c *gin.Context) { // 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()) + }).Debugf("composing token for user %s", u.GetName()) tm := c.MustGet("token-manager").(*token.Manager) diff --git a/api/user/delete.go b/api/user/delete.go index fd32d75d0..ddea7b2dd 100644 --- a/api/user/delete.go +++ b/api/user/delete.go @@ -56,7 +56,7 @@ func DeleteUser(c *gin.Context) { // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields logrus.WithFields(logrus.Fields{ "user": u.GetName(), - }).Infof("deleting user %s", user) + }).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 b0caa2ac1..91e18efa3 100644 --- a/api/user/delete_token.go +++ b/api/user/delete_token.go @@ -48,7 +48,7 @@ func DeleteToken(c *gin.Context) { // 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()) + }).Debugf("revoking token for user %s", u.GetName()) tm := c.MustGet("token-manager").(*token.Manager) diff --git a/api/user/get.go b/api/user/get.go index 488cd9705..ee2112aae 100644 --- a/api/user/get.go +++ b/api/user/get.go @@ -52,7 +52,7 @@ func GetUser(c *gin.Context) { // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields logrus.WithFields(logrus.Fields{ "user": u.GetName(), - }).Infof("reading user %s", user) + }).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 455a4e8e7..dace8c8ec 100644 --- a/api/user/get_current.go +++ b/api/user/get_current.go @@ -37,7 +37,7 @@ func GetCurrentUser(c *gin.Context) { // 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()) + }).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 d0b666090..db72d1e80 100644 --- a/api/user/get_source.go +++ b/api/user/get_source.go @@ -47,7 +47,7 @@ func GetSourceRepos(c *gin.Context) { // 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()) + }).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 55f67f64e..9e3590429 100644 --- a/api/user/list.go +++ b/api/user/list.go @@ -72,7 +72,7 @@ func ListUsers(c *gin.Context) { // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields logrus.WithFields(logrus.Fields{ "user": u.GetName(), - }).Info("reading lite users") + }).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 3983abd0f..b875f0bf4 100644 --- a/api/user/update.go +++ b/api/user/update.go @@ -67,7 +67,7 @@ func UpdateUser(c *gin.Context) { // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields logrus.WithFields(logrus.Fields{ "user": u.GetName(), - }).Infof("updating user %s", user) + }).Debugf("updating user %s", user) // capture body from API request input := new(types.User) diff --git a/api/user/update_current.go b/api/user/update_current.go index dec97ba71..7400a358c 100644 --- a/api/user/update_current.go +++ b/api/user/update_current.go @@ -61,7 +61,7 @@ func UpdateCurrentUser(c *gin.Context) { // 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()) + }).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 20600d525..3cad2d182 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -82,7 +82,7 @@ var baseErr = "unable to process webhook" // //nolint:funlen,gocyclo // ignore function length and cyclomatic complexity func PostWebhook(c *gin.Context) { - logrus.Info("webhook received") + logrus.Debug("webhook received") // capture middleware values m := c.MustGet("metadata").(*internal.Metadata) @@ -578,7 +578,7 @@ 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()) + logrus.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()) diff --git a/api/worker/create.go b/api/worker/create.go index 557cb1858..0a443bc13 100644 --- a/api/worker/create.go +++ b/api/worker/create.go @@ -88,7 +88,7 @@ func CreateWorker(c *gin.Context) { logrus.WithFields(logrus.Fields{ "user": u.GetName(), "worker": input.GetHostname(), - }).Infof("creating new worker %s", input.GetHostname()) + }).Debugf("creating new worker %s", input.GetHostname()) _, err = database.FromContext(c).CreateWorker(ctx, input) if err != nil { diff --git a/api/worker/delete.go b/api/worker/delete.go index ebb718aeb..6566d9d1a 100644 --- a/api/worker/delete.go +++ b/api/worker/delete.go @@ -54,7 +54,7 @@ func DeleteWorker(c *gin.Context) { logrus.WithFields(logrus.Fields{ "user": u.GetName(), "worker": w.GetHostname(), - }).Infof("deleting worker %s", w.GetHostname()) + }).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 64ce34173..d98eaa6a0 100644 --- a/api/worker/get.go +++ b/api/worker/get.go @@ -55,7 +55,7 @@ func GetWorker(c *gin.Context) { logrus.WithFields(logrus.Fields{ "user": u.GetName(), "worker": w.GetHostname(), - }).Infof("reading worker %s", w.GetHostname()) + }).Debugf("reading worker %s", w.GetHostname()) rBs := []*types.Build{} diff --git a/api/worker/list.go b/api/worker/list.go index 5141335ba..09c114093 100644 --- a/api/worker/list.go +++ b/api/worker/list.go @@ -64,7 +64,7 @@ func ListWorkers(c *gin.Context) { // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields logrus.WithFields(logrus.Fields{ "user": u.GetName(), - }).Info("reading workers") + }).Debug("reading workers") active := c.Query("active") diff --git a/api/worker/refresh.go b/api/worker/refresh.go index 4aff87a17..151e28a65 100644 --- a/api/worker/refresh.go +++ b/api/worker/refresh.go @@ -93,7 +93,7 @@ func Refresh(c *gin.Context) { // 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()) + }).Debugf("refreshing worker %s authentication", w.GetHostname()) switch cl.TokenType { // if symmetric token configured, send back symmetric token diff --git a/api/worker/update.go b/api/worker/update.go index db187b7c0..1fa0220e1 100644 --- a/api/worker/update.go +++ b/api/worker/update.go @@ -69,7 +69,7 @@ func UpdateWorker(c *gin.Context) { logrus.WithFields(logrus.Fields{ "user": u.GetName(), "worker": w.GetHostname(), - }).Infof("updating worker %s", w.GetHostname()) + }).Debugf("updating worker %s", w.GetHostname()) // capture body from API request input := new(types.Worker) diff --git a/router/middleware/logger.go b/router/middleware/logger.go index 0f1b14c4c..e42f961af 100644 --- a/router/middleware/logger.go +++ b/router/middleware/logger.go @@ -10,6 +10,7 @@ import ( "github.com/go-vela/server/router/middleware/build" "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/service" "github.com/go-vela/server/router/middleware/step" @@ -48,9 +49,7 @@ func Logger(logger *logrus.Logger, timeFormat string) gin.HandlerFunc { c.Next() - end := time.Now() - - latency := end.Sub(start) + latency := time.Since(start) // prevent us from logging the health endpoint if c.Request.URL.Path != "/health" { @@ -73,6 +72,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 +80,39 @@ func Logger(logger *logrus.Logger, timeFormat string) gin.HandlerFunc { fields["org"] = org } + pipeline := pipeline.Retrieve(c) + if pipeline != nil { + fields["pipeline"] = 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 } step := step.Retrieve(c) if step != nil { fields["step"] = step.Number + fields["step_id"] = step.ID } user := user.Retrieve(c) if user != nil { 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 } entry := logger.WithFields(fields) From 5af90334fef0216b658d00d43098c419ccf4178b Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Fri, 31 May 2024 16:10:53 -0500 Subject: [PATCH 02/26] use logrus for gorm --- api/admin/build.go | 3 -- api/admin/hook.go | 2 +- api/admin/service.go | 2 -- database/database.go | 10 ++++-- database/logger.go | 72 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 database/logger.go diff --git a/api/admin/build.go b/api/admin/build.go index fb93cfda1..5c83a47ce 100644 --- a/api/admin/build.go +++ b/api/admin/build.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/user" "github.com/go-vela/server/util" ) @@ -51,7 +50,6 @@ func AllBuildsQueue(c *gin.Context) { logrus.Debug("admin: reading running and pending builds") // capture middleware values ctx := c.Request.Context() - u := user.Retrieve(c) // 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)) @@ -104,7 +102,6 @@ func AllBuildsQueue(c *gin.Context) { func UpdateBuild(c *gin.Context) { // capture middleware values ctx := c.Request.Context() - u := user.Retrieve(c) // capture body from API request input := new(types.Build) diff --git a/api/admin/hook.go b/api/admin/hook.go index 00f034b91..4dc5779d1 100644 --- a/api/admin/hook.go +++ b/api/admin/hook.go @@ -39,7 +39,7 @@ import ( // description: Unable to update the hook in the database // schema: // "$ref": "#/definitions/Error" -// '501': +// '500': // description: Unable to update the hook in the database // schema: // "$ref": "#/definitions/Error" diff --git a/api/admin/service.go b/api/admin/service.go index 12b9e58e7..cbb88e3b4 100644 --- a/api/admin/service.go +++ b/api/admin/service.go @@ -10,7 +10,6 @@ import ( "github.com/gin-gonic/gin" "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/library" ) @@ -51,7 +50,6 @@ import ( func UpdateService(c *gin.Context) { // capture middleware values ctx := c.Request.Context() - u := user.Retrieve(c) // capture body from API request input := new(library.Service) diff --git a/database/database.go b/database/database.go index 5a9e982b2..7c79f4a3a 100644 --- a/database/database.go +++ b/database/database.go @@ -116,16 +116,22 @@ func New(opts ...EngineOpt) (Interface, error) { e.logger.Trace("creating database engine from configuration") // process the database driver being provided + + // configure gorm to use logrus as internal logger + gormConfig := &gorm.Config{ + Logger: NewGormLogger(e.Driver()), + } + switch e.config.Driver { case constants.DriverPostgres: // create the new Postgres database client - e.client, err = gorm.Open(postgres.Open(e.config.Address), &gorm.Config{}) + e.client, err = gorm.Open(postgres.Open(e.config.Address), gormConfig) if err != nil { return nil, err } case constants.DriverSqlite: // create the new Sqlite database client - e.client, err = gorm.Open(sqlite.Open(e.config.Address), &gorm.Config{}) + e.client, err = gorm.Open(sqlite.Open(e.config.Address), gormConfig) if err != nil { return nil, err } diff --git a/database/logger.go b/database/logger.go new file mode 100644 index 000000000..bdb045b2f --- /dev/null +++ b/database/logger.go @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: Apache-2.0 + +package database + +import ( + "context" + "errors" + "time" + + "github.com/sirupsen/logrus" + "gorm.io/gorm" + "gorm.io/gorm/logger" + "gorm.io/gorm/utils" +) + +type gormLogger struct { + SlowThreshold time.Duration + SourceField string + SkipErrRecordNotFound bool + DriverName string +} + +func NewGormLogger(driver string) *gormLogger { + return &gormLogger{ + SkipErrRecordNotFound: true, + DriverName: driver, + } +} + +func (l *gormLogger) LogMode(logger.LogLevel) logger.Interface { + return l +} + +func (l *gormLogger) Info(ctx context.Context, s string, args ...interface{}) { + logrus.WithContext(ctx).WithField("database", l.DriverName).Info(s, args) +} + +func (l *gormLogger) Warn(ctx context.Context, s string, args ...interface{}) { + logrus.WithContext(ctx).WithField("database", l.DriverName).Warn(s, args) +} + +func (l *gormLogger) Error(ctx context.Context, s string, args ...interface{}) { + logrus.WithContext(ctx).WithField("database", l.DriverName).Error(s, args) +} + +func (l *gormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { + elapsed := time.Since(begin) + sql, rows := fc() + fields := logrus.Fields{ + "database": l.DriverName, + "rows": rows, + "elapsed": elapsed, + "sql": sql, + } + + if l.SourceField != "" { + fields[l.SourceField] = utils.FileWithLineNum() + } + + if err != nil && (!errors.Is(err, gorm.ErrRecordNotFound) || !l.SkipErrRecordNotFound) { + fields[logrus.ErrorKey] = err + logrus.WithContext(ctx).WithFields(fields).Error("gorm error") + return + } + + if l.SlowThreshold != 0 && elapsed > l.SlowThreshold { + logrus.WithContext(ctx).WithFields(fields).Warn("slow query") + return + } + + logrus.WithContext(ctx).WithFields(fields).Infof("gorm trace") +} From 8e48d764e085298c7643dc98a8590b58454d08cc Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Fri, 31 May 2024 16:12:09 -0500 Subject: [PATCH 03/26] use witherror --- database/logger.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/database/logger.go b/database/logger.go index bdb045b2f..96c4373e3 100644 --- a/database/logger.go +++ b/database/logger.go @@ -58,8 +58,7 @@ func (l *gormLogger) Trace(ctx context.Context, begin time.Time, fc func() (stri } if err != nil && (!errors.Is(err, gorm.ErrRecordNotFound) || !l.SkipErrRecordNotFound) { - fields[logrus.ErrorKey] = err - logrus.WithContext(ctx).WithFields(fields).Error("gorm error") + logrus.WithContext(ctx).WithError(err).WithFields(fields).Error("gorm error") return } From 590d21e04f488cf207cd3fc48ac561d6f294a08c Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Sun, 2 Jun 2024 01:28:30 -0500 Subject: [PATCH 04/26] add knobs for db logging --- database/context.go | 4 + database/database.go | 49 ++++++++++- database/flags.go | 28 +++++++ database/logger.go | 64 ++++++++------- database/logger_test.go | 51 ++++++++++++ database/opts.go | 40 +++++++++ database/opts_test.go | 176 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 381 insertions(+), 31 deletions(-) create mode 100644 database/logger_test.go diff --git a/database/context.go b/database/context.go index fd4cae217..a11863e7d 100644 --- a/database/context.go +++ b/database/context.go @@ -49,6 +49,10 @@ func FromCLIContext(c *cli.Context) (Interface, error) { WithConnectionOpen(c.Int("database.connection.open")), WithDriver(c.String("database.driver")), WithEncryptionKey(c.String("database.encryption.key")), + WithLogLevel(c.String("database.log.level")), + WithLogSkipNotFound(c.Bool("database.log.skip_notfound")), + WithLogSlowThreshold(c.Duration("database.log.slow_threshold")), + WithLogShowSQL(c.Bool("database.log.show_sql")), WithSkipCreation(c.Bool("database.skip_creation")), ) } diff --git a/database/database.go b/database/database.go index 7c79f4a3a..9454e528b 100644 --- a/database/database.go +++ b/database/database.go @@ -47,6 +47,14 @@ type ( Driver string // specifies the encryption key to use for the database engine EncryptionKey string + // specifies the database engine specific log level + LogLevel string + // specifies to skip logging when a record is not found + LogSkipNotFound bool + // specifies the threshold for slow queries in the database engine + LogSlowThreshold time.Duration + // specifies whether to log SQL queries in the database engine + LogShowSQL bool // specifies to skip creating tables and indexes for the database engine SkipCreation bool } @@ -111,15 +119,50 @@ func New(opts ...EngineOpt) (Interface, error) { return nil, err } - // update the logger with additional metadata + // by default use the global logger with additional metadata e.logger = logrus.NewEntry(logrus.StandardLogger()).WithField("database", e.Driver()) + // translate the log level to logrus level for the database engine + var dbLogLevel logrus.Level + + switch e.config.LogLevel { + case "t", "trace", "Trace", "TRACE": + dbLogLevel = logrus.TraceLevel + case "d", "debug", "Debug", "DEBUG": + dbLogLevel = logrus.DebugLevel + case "i", "info", "Info", "INFO": + dbLogLevel = logrus.InfoLevel + case "w", "warn", "Warn", "WARN": + dbLogLevel = logrus.WarnLevel + case "e", "error", "Error", "ERROR": + dbLogLevel = logrus.ErrorLevel + case "f", "fatal", "Fatal", "FATAL": + dbLogLevel = logrus.FatalLevel + case "p", "panic", "Panic", "PANIC": + dbLogLevel = logrus.PanicLevel + } + + // if the log level for the database engine is different than + // the global log level, create a new logrus instance + if dbLogLevel != logrus.GetLevel() { + log := logrus.New() + + // set the custom log level + log.Level = dbLogLevel + + // copy the formatter from the global logger to + // retain the same format for the database engine + log.Formatter = logrus.StandardLogger().Formatter + + // update the logger with additional metadata + e.logger = logrus.NewEntry(log).WithField("database", e.Driver()) + } + e.logger.Trace("creating database engine from configuration") - // process the database driver being provided // configure gorm to use logrus as internal logger gormConfig := &gorm.Config{ - Logger: NewGormLogger(e.Driver()), + Logger: NewGormLogger(e.logger, e.config.LogSlowThreshold, e.config.LogSkipNotFound, e.config.LogShowSQL), } switch e.config.Driver { diff --git a/database/flags.go b/database/flags.go index 8655c1808..96b71a5bd 100644 --- a/database/flags.go +++ b/database/flags.go @@ -60,6 +60,34 @@ var Flags = []cli.Flag{ Name: "database.encryption.key", Usage: "AES-256 key for encrypting and decrypting values in the database", }, + &cli.StringFlag{ + EnvVars: []string{"VELA_DATABASE_LOG_LEVEL", "DATABASE_LOG_LEVEL"}, + FilePath: "/vela/database/log_level", + Name: "database.log.level", + Usage: "set log level - options: (trace|info|warn|error)", + Value: "warn", + }, + &cli.BoolFlag{ + EnvVars: []string{"VELA_DATABASE_LOG_SHOW_SQL", "DATABASE_LOG_SHOW_SQL"}, + FilePath: "/vela/database/log_show_sql", + Name: "database.log.show_sql", + Usage: "show the SQL query in the logs", + Value: false, + }, + &cli.BoolFlag{ + EnvVars: []string{"VELA_DATABASE_LOG_SKIP_NOTFOUND", "DATABASE_LOG_SKIP_NOTFOUND"}, + FilePath: "/vela/database/log_skip_notfound", + Name: "database.log.skip_notfound", + Usage: "skip logging when a resource is not found in the database", + Value: true, + }, + &cli.DurationFlag{ + EnvVars: []string{"VELA_DATABASE_LOG_SLOW_THRESHOLD", "DATABASE_LOG_SLOW_THRESHOLD"}, + FilePath: "/vela/database/log_slow_threshold", + Name: "database.log.slow_threshold", + Usage: "queries that take longer than this threshold are considered slow and will be logged", + Value: 200 * time.Millisecond, + }, &cli.BoolFlag{ EnvVars: []string{"VELA_DATABASE_SKIP_CREATION", "DATABASE_SKIP_CREATION"}, FilePath: "/vela/database/skip_creation", diff --git a/database/logger.go b/database/logger.go index 96c4373e3..819574704 100644 --- a/database/logger.go +++ b/database/logger.go @@ -13,59 +13,67 @@ import ( "gorm.io/gorm/utils" ) -type gormLogger struct { - SlowThreshold time.Duration - SourceField string - SkipErrRecordNotFound bool - DriverName string +// GormLogger is a custom logger for Gorm. +type GormLogger struct { + slowThreshold time.Duration + skipErrRecordNotFound bool + showSQL bool + entry *logrus.Entry } -func NewGormLogger(driver string) *gormLogger { - return &gormLogger{ - SkipErrRecordNotFound: true, - DriverName: driver, +// NewGormLogger creates a new Gorm logger. +func NewGormLogger(logger *logrus.Entry, slowThreshold time.Duration, skipNotFound, showSQL bool) *GormLogger { + return &GormLogger{ + skipErrRecordNotFound: skipNotFound, + slowThreshold: slowThreshold, + showSQL: showSQL, + entry: logger, } } -func (l *gormLogger) LogMode(logger.LogLevel) logger.Interface { +// LogMode sets the log mode for the logger. +func (l *GormLogger) LogMode(logger.LogLevel) logger.Interface { return l } -func (l *gormLogger) Info(ctx context.Context, s string, args ...interface{}) { - logrus.WithContext(ctx).WithField("database", l.DriverName).Info(s, args) +// Info implements the logger.Interface. +func (l *GormLogger) Info(ctx context.Context, msg string, args ...interface{}) { + l.entry.WithContext(ctx).Info(msg, args) } -func (l *gormLogger) Warn(ctx context.Context, s string, args ...interface{}) { - logrus.WithContext(ctx).WithField("database", l.DriverName).Warn(s, args) +// Warn implements the logger.Interface. +func (l *GormLogger) Warn(ctx context.Context, msg string, args ...interface{}) { + l.entry.WithContext(ctx).Warn(msg, args) } -func (l *gormLogger) Error(ctx context.Context, s string, args ...interface{}) { - logrus.WithContext(ctx).WithField("database", l.DriverName).Error(s, args) +// Error implements the logger.Interface. +func (l *GormLogger) Error(ctx context.Context, msg string, args ...interface{}) { + l.entry.WithContext(ctx).Error(msg, args) } -func (l *gormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { +// Trace implements the logger.Interface. +func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { elapsed := time.Since(begin) sql, rows := fc() fields := logrus.Fields{ - "database": l.DriverName, - "rows": rows, - "elapsed": elapsed, - "sql": sql, + "rows": rows, + "elapsed": elapsed, + "source": utils.FileWithLineNum(), } - if l.SourceField != "" { - fields[l.SourceField] = utils.FileWithLineNum() + if l.showSQL { + fields["sql"] = sql } - if err != nil && (!errors.Is(err, gorm.ErrRecordNotFound) || !l.SkipErrRecordNotFound) { - logrus.WithContext(ctx).WithError(err).WithFields(fields).Error("gorm error") + if err != nil && (!errors.Is(err, gorm.ErrRecordNotFound) || !l.skipErrRecordNotFound) { + l.entry.WithContext(ctx).WithError(err).WithFields(fields).Error("gorm error") return } - if l.SlowThreshold != 0 && elapsed > l.SlowThreshold { - logrus.WithContext(ctx).WithFields(fields).Warn("slow query") + if l.slowThreshold != 0 && elapsed > l.slowThreshold { + l.entry.WithContext(ctx).WithFields(fields).Warnf("gorm warn SLOW QUERY >= %s, took %s", l.slowThreshold, elapsed) return } - logrus.WithContext(ctx).WithFields(fields).Infof("gorm trace") + l.entry.WithContext(ctx).WithFields(fields).Infof("gorm info") } diff --git a/database/logger_test.go b/database/logger_test.go new file mode 100644 index 000000000..ce64513bf --- /dev/null +++ b/database/logger_test.go @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: Apache-2.0 + +package database + +import ( + "testing" + "time" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/sirupsen/logrus" +) + +func TestNewGormLogger(t *testing.T) { + logger := logrus.NewEntry(logrus.New()) + + type args struct { + logger *logrus.Entry + slowThreshold time.Duration + skipNotFound bool + showSQL bool + } + tests := []struct { + name string + args args + want *GormLogger + }{ + { + name: "logger set", + args: args{ + logger: logger, + slowThreshold: time.Second, + skipNotFound: false, + showSQL: true, + }, + want: &GormLogger{ + slowThreshold: time.Second, + skipErrRecordNotFound: false, + showSQL: true, + entry: logger, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if diff := cmp.Diff(NewGormLogger(tt.args.logger, tt.args.slowThreshold, tt.args.skipNotFound, tt.args.showSQL), tt.want, cmpopts.EquateComparable(GormLogger{})); diff != "" { + t.Errorf("NewGormLogger() mismatch (-got +want):\n%s", diff) + } + }) + } +} diff --git a/database/opts.go b/database/opts.go index 2e758d802..3a35d751e 100644 --- a/database/opts.go +++ b/database/opts.go @@ -80,6 +80,46 @@ func WithEncryptionKey(encryptionKey string) EngineOpt { } } +// WithLogLevel sets the log level in the database engine. +func WithLogLevel(logLevel string) EngineOpt { + return func(e *engine) error { + // set the log level for the database engine + e.config.LogLevel = logLevel + + return nil + } +} + +// WithLogSkipNotFound sets the log skip not found option in the database engine. +func WithLogSkipNotFound(logSkipNotFound bool) EngineOpt { + return func(e *engine) error { + // set the log skip not found option for the database engine + e.config.LogSkipNotFound = logSkipNotFound + + return nil + } +} + +// WithLogSlowThreshold sets the log slow query threshold in the database engine. +func WithLogSlowThreshold(logSlowThreshold time.Duration) EngineOpt { + return func(e *engine) error { + // set the slow query threshold for the database engine + e.config.LogSlowThreshold = logSlowThreshold + + return nil + } +} + +// WithLogShowSQL sets the log show SQL option in the database engine. +func WithLogShowSQL(logShowSQL bool) EngineOpt { + return func(e *engine) error { + // set the log show SQL option for the database engine + e.config.LogShowSQL = logShowSQL + + return nil + } +} + // WithSkipCreation sets the skip creation logic in the database engine. func WithSkipCreation(skipCreation bool) EngineOpt { return func(e *engine) error { diff --git a/database/opts_test.go b/database/opts_test.go index 7089d9271..abd65ad94 100644 --- a/database/opts_test.go +++ b/database/opts_test.go @@ -405,3 +405,179 @@ func TestDatabase_EngineOpt_WithSkipCreation(t *testing.T) { }) } } + +func TestDatabase_EngineOpt_WithLogLevel(t *testing.T) { + e := &engine{config: new(config)} + + tests := []struct { + failure bool + name string + logLevel string + want string + }{ + { + failure: false, + name: "log level set to debug", + logLevel: "debug", + want: "debug", + }, + { + failure: false, + name: "log level set to info", + logLevel: "info", + want: "info", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + err := WithLogLevel(test.logLevel)(e) + + if test.failure { + if err == nil { + t.Errorf("WithLogLevel for %s should have returned err", test.name) + } + + return + } + + if err != nil { + t.Errorf("WithLogLevel returned err: %v", err) + } + + if !reflect.DeepEqual(e.config.LogLevel, test.want) { + t.Errorf("WithLogLevel is %v, want %v", e.config.SkipCreation, test.want) + } + }) + } +} + +func TestDatabase_EngineOpt_WithLogSkipNotFound(t *testing.T) { + e := &engine{config: new(config)} + + tests := []struct { + failure bool + name string + skip bool + want bool + }{ + { + failure: false, + name: "log skip not found set to true", + skip: true, + want: true, + }, + { + failure: false, + name: "log skip not found set to false", + skip: false, + want: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + err := WithLogSkipNotFound(test.skip)(e) + + if test.failure { + if err == nil { + t.Errorf("WithLogSkipNotFound for %s should have returned err", test.name) + } + + if err != nil { + t.Errorf("WithLogSkipNotFound for %s returned err: %v", test.name, err) + } + + if !reflect.DeepEqual(e.config.LogSkipNotFound, test.want) { + t.Errorf("WithLogSkipNotFound for %s is %v, want %v", test.name, e.config.LogSkipNotFound, test.want) + } + } + }) + } +} + +func TestDatabase_EngineOpt_WithLogSlowThreshold(t *testing.T) { + e := &engine{config: new(config)} + + tests := []struct { + failure bool + name string + threshold time.Duration + want time.Duration + }{ + { + failure: false, + name: "log slow threshold set to 1ms", + threshold: 1 * time.Millisecond, + want: 1 * time.Millisecond, + }, + { + failure: false, + name: "log slow threshold set to 2ms", + threshold: 2 * time.Millisecond, + want: 2 * time.Millisecond, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + err := WithLogSlowThreshold(test.threshold)(e) + + if test.failure { + if err == nil { + t.Errorf("WithLogSlowThreshold for %s should have returned err", test.name) + } + + if err != nil { + t.Errorf("WithLogSlowThreshold for %s returned err: %v", test.name, err) + } + + if !reflect.DeepEqual(e.config.LogSlowThreshold, test.want) { + t.Errorf("WithLogSlowThreshold for %s is %v, want %v", test.name, e.config.LogSlowThreshold, test.want) + } + } + }) + } +} + +func TestDatabase_EngineOpt_WithLogShowSQL(t *testing.T) { + e := &engine{config: new(config)} + + tests := []struct { + failure bool + name string + show bool + want bool + }{ + { + failure: false, + name: "log show SQL set to true", + show: true, + want: true, + }, + { + failure: false, + name: "log show SQL set to false", + show: false, + want: false, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + err := WithLogShowSQL(test.show)(e) + + if test.failure { + if err == nil { + t.Errorf("WithLogShowSQL for %s should have returned err", test.name) + } + + if err != nil { + t.Errorf("WithLogShowSQL for %s returned err: %v", test.name, err) + } + + if !reflect.DeepEqual(e.config.LogShowSQL, test.want) { + t.Errorf("WithLogShowSQL for %s is %v, want %v", test.name, e.config.LogShowSQL, test.want) + } + } + }) + } +} From 444e5d10c6b9fd576f18e3684db3c77460cdccde Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Sun, 2 Jun 2024 01:29:20 -0500 Subject: [PATCH 05/26] format is deprecated this also fixes a bug when working with golangci linter in (neo)vim --- .golangci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index f857a2c9f..df7592d27 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,7 +8,10 @@ # outputs it results from the linters it executes. output: # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" - format: colored-line-number + formats: + - format: json + path: stderr + - format: colored-line-number # print lines of code with issue, default is true print-issued-lines: true From ee5ed83408677cb859720f35dd331f9086504565 Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Sun, 2 Jun 2024 23:22:57 -0500 Subject: [PATCH 06/26] tweak logging for admin endpoint also includes fixes for swagger annotations --- api/admin/build.go | 45 ++++++++++++++++++++++++++++++++++++----- api/admin/clean.go | 25 +++++++++++++++++------ api/admin/deployment.go | 4 ++++ api/admin/hook.go | 31 ++++++++++++++++++++++++---- api/admin/repo.go | 34 +++++++++++++++++++++++++++---- api/admin/secret.go | 41 +++++++++++++++++++++++++++++++++---- api/admin/service.go | 30 +++++++++++++++++++++++++-- api/admin/settings.go | 25 ++++++++++------------- api/admin/step.go | 33 +++++++++++++++++++++++++++--- api/admin/user.go | 36 ++++++++++++++++++++++++++++----- api/admin/worker.go | 3 +++ 11 files changed, 260 insertions(+), 47 deletions(-) diff --git a/api/admin/build.go b/api/admin/build.go index 5c83a47ce..3478f192c 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 ( @@ -14,6 +13,7 @@ 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" ) @@ -39,6 +39,10 @@ import ( // type: array // items: // "$ref": "#/definitions/BuildQueue" +// '401': +// description: Unauthorized to retrieve running and pending builds +// schema: +// "$ref": "#/definitions/Error // '500': // description: Unable to retrieve all running and pending builds from the database // schema: @@ -47,7 +51,8 @@ import ( // AllBuildsQueue represents the API handler to // capture all running and pending builds stored in the database. func AllBuildsQueue(c *gin.Context) { - logrus.Debug("admin: reading running and pending builds") + logrus.Debug("platform admin: reading running and pending builds") + // capture middleware values ctx := c.Request.Context() @@ -88,8 +93,12 @@ func AllBuildsQueue(c *gin.Context) { // description: Successfully updated the build in the database // schema: // "$ref": "#/definitions/Build" -// '404': -// description: Unable to update the build in the database +// '401': +// description: Unauthorized to update the build in the database +// schema: +// "$ref": "#/definitions/Error +// '400': +// description: Unable to update the build in the database - bad request // schema: // "$ref": "#/definitions/Error" // '500': @@ -102,6 +111,16 @@ func AllBuildsQueue(c *gin.Context) { func UpdateBuild(c *gin.Context) { // capture middleware values ctx := c.Request.Context() + u := user.Retrieve(c) + + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) + + logger.Debug("updating build") // capture body from API request input := new(types.Build) @@ -110,11 +129,19 @@ func UpdateBuild(c *gin.Context) { if err != nil { retErr := fmt.Errorf("unable to decode JSON for build %d: %w", input.GetID(), err) - util.HandleError(c, http.StatusNotFound, retErr) + util.HandleError(c, http.StatusBadRequest, retErr) return } + logger.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()), + }).Info("attempting to update build") + // send API call to update the build b, err := database.FromContext(c).UpdateBuild(ctx, input) if err != nil { @@ -125,5 +152,13 @@ func UpdateBuild(c *gin.Context) { return } + logger.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), + "org": b.GetRepo().GetOrg(), + }).Info("updated build") + c.JSON(http.StatusOK, b) } diff --git a/api/admin/clean.go b/api/admin/clean.go index bdfc624ae..f7bf8f18f 100644 --- a/api/admin/clean.go +++ b/api/admin/clean.go @@ -43,15 +43,15 @@ import ( // '200': // description: Successfully updated pending resources with error message // schema: -// type: string +// type: string // '400': // description: Unable to update resources — bad request // schema: // "$ref": "#/definitions/Error" // '401': -// description: Unable to update resources — unauthorized +// description: Unauthorized to clean resources // schema: -// "$ref": "#/definitions/Error" +// "$ref": "#/definitions/Error // '500': // description: Unable to update resources // schema: @@ -60,10 +60,19 @@ import ( // CleanResources represents the API handler to // update any user stored in the database. func CleanResources(c *gin.Context) { + logrus.Debug("platform admin: cleaning resources") + // capture middleware values ctx := c.Request.Context() u := user.Retrieve(c) + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) + // default error message msg := "build cleaned by platform admin" @@ -81,7 +90,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 @@ -104,7 +113,7 @@ func CleanResources(c *gin.Context) { return } - logrus.Debugf("platform admin %s: cleaned %d builds in database", u.GetName(), builds) + logger.Debugf("cleaned %d builds in database", builds) // clean executables executables, err := database.FromContext(c).CleanBuildExecutables(ctx) @@ -116,6 +125,8 @@ func CleanResources(c *gin.Context) { return } + logger.Debugf("cleaned %d executables in database", executables) + // clean services services, err := database.FromContext(c).CleanServices(ctx, msg, before) if err != nil { @@ -126,7 +137,7 @@ func CleanResources(c *gin.Context) { return } - logrus.Debugf("platform admin %s: cleaned %d services in database", u.GetName(), services) + logger.Debugf("cleaned %d services in database", services) // clean steps steps, err := database.FromContext(c).CleanSteps(ctx, msg, before) @@ -138,5 +149,7 @@ func CleanResources(c *gin.Context) { return } + logger.Debugf("cleaned %d steps in database", 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/deployment.go b/api/admin/deployment.go index deeac0045..61af90302 100644 --- a/api/admin/deployment.go +++ b/api/admin/deployment.go @@ -17,6 +17,10 @@ import ( // - application/json // parameters: // responses: +// '401': +// description: Unauthorized to update deployment +// schema: +// "$ref": "#/definitions/Error // '501': // description: This endpoint is not implemented // schema: diff --git a/api/admin/hook.go b/api/admin/hook.go index 4dc5779d1..dee867105 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 ( @@ -8,8 +7,10 @@ import ( "net/http" "github.com/gin-gonic/gin" + "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/library" ) @@ -35,8 +36,12 @@ import ( // description: Successfully updated the hook in the database // schema: // "$ref": "#/definitions/Webhook" -// '404': -// description: Unable to update the hook in the database +// '401': +// description: Unauthorized to update the hook in the database +// schema: +// "$ref": "#/definitions/Error +// '400': +// description: Unable to update the hook in the database - bad request // schema: // "$ref": "#/definitions/Error" // '500': @@ -47,8 +52,18 @@ import ( // UpdateHook represents the API handler to // update any hook stored in the database. func UpdateHook(c *gin.Context) { + logrus.Debug("platform admin: updating hook") + // capture middleware values ctx := c.Request.Context() + u := user.Retrieve(c) + + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) // capture body from API request input := new(library.Hook) @@ -57,11 +72,15 @@ func UpdateHook(c *gin.Context) { if err != nil { retErr := fmt.Errorf("unable to decode JSON for hook %d: %w", input.GetID(), err) - util.HandleError(c, http.StatusNotFound, retErr) + util.HandleError(c, http.StatusBadRequest, retErr) return } + logger.WithFields(logrus.Fields{ + "hook_id": input.GetID(), + }).Info("attempting to update hook") + // send API call to update the hook h, err := database.FromContext(c).UpdateHook(ctx, input) if err != nil { @@ -72,5 +91,9 @@ func UpdateHook(c *gin.Context) { return } + logger.WithFields(logrus.Fields{ + "hook_id": h.GetID(), + }).Info("hook updated") + c.JSON(http.StatusOK, h) } diff --git a/api/admin/repo.go b/api/admin/repo.go index 718309bba..003b9e8c3 100644 --- a/api/admin/repo.go +++ b/api/admin/repo.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 -//nolint:dupl // ignore similar code +//nolint:dupl // ignore similar code with service.go package admin import ( @@ -8,9 +8,11 @@ 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" + "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -35,8 +37,12 @@ import ( // description: Successfully updated the repo in the database // schema: // "$ref": "#/definitions/Repo" -// '404': -// description: Unable to update the repo in the database +// '401': +// description: Unauthorized to update the repo in the database +// schema: +// "$ref": "#/definitions/Error +// '400': +// description: Unable to update the repo in the database - bad request // schema: // "$ref": "#/definitions/Error" // '501': @@ -47,8 +53,18 @@ import ( // UpdateRepo represents the API handler to // update any repo stored in the database. func UpdateRepo(c *gin.Context) { + logrus.Debug("platform admin: updating repo") + // capture middleware values ctx := c.Request.Context() + u := user.Retrieve(c) + + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) // capture body from API request input := new(types.Repo) @@ -57,11 +73,16 @@ func UpdateRepo(c *gin.Context) { if err != nil { retErr := fmt.Errorf("unable to decode JSON for repo %d: %w", input.GetID(), err) - util.HandleError(c, http.StatusNotFound, retErr) + util.HandleError(c, http.StatusBadRequest, retErr) return } + logger.WithFields(logrus.Fields{ + "repo_id": input.GetID(), + "repo": util.EscapeValue(input.GetFullName()), + }).Info("attempting to update repo") + // send API call to update the repo r, err := database.FromContext(c).UpdateRepo(ctx, input) if err != nil { @@ -72,5 +93,10 @@ func UpdateRepo(c *gin.Context) { return } + logger.WithFields(logrus.Fields{ + "repo_id": r.GetID(), + "repo": r.GetFullName(), + }).Info("repo updated") + c.JSON(http.StatusOK, r) } diff --git a/api/admin/secret.go b/api/admin/secret.go index e47f8cc4c..3b06ea315 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 ( @@ -8,8 +7,10 @@ import ( "net/http" "github.com/gin-gonic/gin" + "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/library" ) @@ -35,8 +36,12 @@ import ( // description: Successfully updated the secret in the database // schema: // "$ref": "#/definitions/Secret" -// '404': -// description: Unable to update the secret in the database +// '401': +// description: Unauthorized to update the secret in the database +// schema: +// "$ref": "#/definitions/Error +// '400': +// description: Unable to update the secret in the database - bad request // schema: // "$ref": "#/definitions/Error" // '501': @@ -47,8 +52,18 @@ import ( // UpdateSecret represents the API handler to // update any secret stored in the database. func UpdateSecret(c *gin.Context) { + logrus.Debug("platform admin: updating secret") + // capture middleware values ctx := c.Request.Context() + u := user.Retrieve(c) + + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) // capture body from API request input := new(library.Secret) @@ -57,11 +72,20 @@ func UpdateSecret(c *gin.Context) { if err != nil { retErr := fmt.Errorf("unable to decode JSON for secret %d: %w", input.GetID(), err) - util.HandleError(c, http.StatusNotFound, retErr) + util.HandleError(c, http.StatusBadRequest, retErr) return } + logger.WithFields(logrus.Fields{ + "secret_id": input.GetID(), + "org": util.EscapeValue(input.GetOrg()), + "repo": util.EscapeValue(input.GetRepo()), + "type": util.EscapeValue(input.GetType()), + "name": util.EscapeValue(input.GetName()), + "team": util.EscapeValue(input.GetTeam()), + }).Info("attempting to update secret") + // send API call to update the secret s, err := database.FromContext(c).UpdateSecret(ctx, input) if err != nil { @@ -72,5 +96,14 @@ func UpdateSecret(c *gin.Context) { return } + logger.WithFields(logrus.Fields{ + "secret_id": s.GetID(), + "org": s.GetOrg(), + "repo": s.GetRepo(), + "type": s.GetType(), + "name": s.GetName(), + "team": s.GetTeam(), + }).Info("secret updated") + c.JSON(http.StatusOK, s) } diff --git a/api/admin/service.go b/api/admin/service.go index cbb88e3b4..977243522 100644 --- a/api/admin/service.go +++ b/api/admin/service.go @@ -8,8 +8,10 @@ import ( "net/http" "github.com/gin-gonic/gin" + "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/library" ) @@ -36,7 +38,11 @@ import ( // type: json // schema: // "$ref": "#/definitions/Service" -// '404': +// '401': +// description: Unauthorized to update the service in the database +// schema: +// "$ref": "#/definitions/Error +// '400': // description: Unable to update the service in the database // schema: // "$ref": "#/definitions/Error" @@ -48,8 +54,18 @@ import ( // UpdateService represents the API handler to // update any service stored in the database. func UpdateService(c *gin.Context) { + logrus.Debug("platform admin: updating service") + // capture middleware values ctx := c.Request.Context() + u := user.Retrieve(c) + + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) // capture body from API request input := new(library.Service) @@ -58,11 +74,16 @@ func UpdateService(c *gin.Context) { if err != nil { retErr := fmt.Errorf("unable to decode JSON for service %d: %w", input.GetID(), err) - util.HandleError(c, http.StatusNotFound, retErr) + util.HandleError(c, http.StatusBadRequest, retErr) return } + logger.WithFields(logrus.Fields{ + "service_id": input.GetID(), + "service": util.EscapeValue(input.GetName()), + }).Info("attempting to update service") + // send API call to update the service s, err := database.FromContext(c).UpdateService(ctx, input) if err != nil { @@ -73,5 +94,10 @@ func UpdateService(c *gin.Context) { return } + logger.WithFields(logrus.Fields{ + "service_id": s.GetID(), + "service": s.GetName(), + }).Info("updated service") + c.JSON(http.StatusOK, s) } diff --git a/api/admin/settings.go b/api/admin/settings.go index c100d486a..1fc116ab9 100644 --- a/api/admin/settings.go +++ b/api/admin/settings.go @@ -52,20 +52,17 @@ func GetSettings(c *gin.Context) { // capture middleware values s := sMiddleware.FromContext(c) - u := uMiddleware.FromContext(c) // 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) return } - logrus.Debugf("platform admin %#q retrieved platform settings", u.GetName()) - c.JSON(http.StatusOK, s) } @@ -121,7 +118,7 @@ func UpdateSettings(c *gin.Context) { // 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) @@ -140,7 +137,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) @@ -154,7 +151,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) @@ -192,15 +189,13 @@ 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) return } - logrus.Debugf("platform admin %#q updated platform settings", u.GetName()) - c.JSON(http.StatusOK, _s) } @@ -235,6 +230,8 @@ func UpdateSettings(c *gin.Context) { // RestoreSettings represents the API handler to // restore settings stored in the database to the environment defaults. func RestoreSettings(c *gin.Context) { + logrus.Debug("platform admin: restoring platform settings") + // capture middleware values ctx := c.Request.Context() cliCtx := cliMiddleware.FromContext(c) @@ -244,7 +241,7 @@ func RestoreSettings(c *gin.Context) { // 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) @@ -253,7 +250,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) @@ -262,7 +259,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) @@ -285,7 +282,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 abfe1d5af..f0269f1b4 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 ( @@ -7,8 +8,10 @@ import ( "net/http" "github.com/gin-gonic/gin" + "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/library" ) @@ -34,8 +37,12 @@ import ( // description: Successfully updated the step in the database // schema: // "$ref": "#/definitions/Step" -// '404': -// description: Unable to update the step in the database +// '401': +// description: Unauthorized to update the step in the database +// schema: +// "$ref": "#/definitions/Error +// '400': +// description: Unable to update the step in the database - bad request // schema: // "$ref": "#/definitions/Error" // '500': @@ -46,8 +53,18 @@ import ( // UpdateStep represents the API handler to // update any step stored in the database. func UpdateStep(c *gin.Context) { + logrus.Debug("platform admin: updating step") + // capture middleware values ctx := c.Request.Context() + u := user.Retrieve(c) + + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) // capture body from API request input := new(library.Step) @@ -56,11 +73,16 @@ func UpdateStep(c *gin.Context) { if err != nil { retErr := fmt.Errorf("unable to decode JSON for step %d: %w", input.GetID(), err) - util.HandleError(c, http.StatusNotFound, retErr) + util.HandleError(c, http.StatusBadRequest, retErr) return } + logger.WithFields(logrus.Fields{ + "step_id": input.GetID(), + "step": util.EscapeValue(input.GetName()), + }).Info("attempting to update step") + // send API call to update the step s, err := database.FromContext(c).UpdateStep(ctx, input) if err != nil { @@ -71,5 +93,10 @@ func UpdateStep(c *gin.Context) { return } + logger.WithFields(logrus.Fields{ + "step_id": s.GetID(), + "step": s.GetName(), + }).Info("updated step") + c.JSON(http.StatusOK, s) } diff --git a/api/admin/user.go b/api/admin/user.go index 47241a4e4..1a2ec5ffb 100644 --- a/api/admin/user.go +++ b/api/admin/user.go @@ -8,9 +8,11 @@ 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" + "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" ) @@ -35,8 +37,12 @@ import ( // description: Successfully updated the user in the database // schema: // "$ref": "#/definitions/User" -// '404': -// description: Unable to update the user in the database +// '401': +// description: Unauthorized to update the user in the database +// schema: +// "$ref": "#/definitions/Error +// '400': +// description: Unable to update the user in the database - bad request // schema: // "$ref": "#/definitions/Error" // '500': @@ -47,8 +53,18 @@ import ( // UpdateUser represents the API handler to // update any user stored in the database. func UpdateUser(c *gin.Context) { + logrus.Debug("platform admin: updating user") + // capture middleware values ctx := c.Request.Context() + u := user.Retrieve(c) + + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) // capture body from API request input := new(types.User) @@ -57,13 +73,18 @@ func UpdateUser(c *gin.Context) { if err != nil { retErr := fmt.Errorf("unable to decode JSON for user %d: %w", input.GetID(), err) - util.HandleError(c, http.StatusNotFound, retErr) + util.HandleError(c, http.StatusBadRequest, retErr) return } + logger.WithFields(logrus.Fields{ + "target_user_id": input.GetID(), + "target_user": util.EscapeValue(input.GetName()), + }).Info("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) @@ -72,5 +93,10 @@ func UpdateUser(c *gin.Context) { return } - c.JSON(http.StatusOK, u) + logger.WithFields(logrus.Fields{ + "target_user_id": tu.GetID(), + "target_user": tu.GetName(), + }).Info("updated user") + + c.JSON(http.StatusOK, tu) } diff --git a/api/admin/worker.go b/api/admin/worker.go index 6e440de9d..4864140af 100644 --- a/api/admin/worker.go +++ b/api/admin/worker.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/util" @@ -42,6 +43,8 @@ import ( // RegisterToken represents the API handler to // generate a registration token for onboarding a worker. func RegisterToken(c *gin.Context) { + logrus.Debug("platform admin: generating worker registration token") + // capture middleware values host := util.PathParameter(c, "worker") From 44cedd146ff747e86e7cb2792fc4a0b6573132f4 Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Mon, 3 Jun 2024 09:22:25 -0500 Subject: [PATCH 07/26] add more logging --- api/auth/get_token.go | 10 ++++++++++ api/auth/logout.go | 2 ++ api/auth/post_token.go | 1 + api/queue/queue.go | 5 ++++- api/scm/sync.go | 7 ++++++- api/scm/sync_org.go | 6 ++++++ api/worker/refresh.go | 22 ++++++++++++---------- router/middleware/logger.go | 19 ++++++++++++++++++- 8 files changed, 59 insertions(+), 13 deletions(-) diff --git a/api/auth/get_token.go b/api/auth/get_token.go index 82fe08892..3a5c5d640 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,6 +62,11 @@ import ( func GetAuthToken(c *gin.Context) { var err error + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + }) + tm := c.MustGet("token-manager").(*token.Manager) // capture middleware values ctx := c.Request.Context() @@ -132,6 +138,8 @@ func GetAuthToken(c *gin.Context) { return } + logger.Infof("new user %#q created", u.GetName()) + // return the jwt access token c.JSON(http.StatusOK, library.Token{Token: &at}) @@ -165,6 +173,8 @@ func GetAuthToken(c *gin.Context) { return } + logger.Infof("user %#q updated", u.GetName()) + // return the user with their jwt access token c.JSON(http.StatusOK, library.Token{Token: &at}) } diff --git a/api/auth/logout.go b/api/auth/logout.go index f3436b293..2de7fee45 100644 --- a/api/auth/logout.go +++ b/api/auth/logout.go @@ -82,6 +82,8 @@ func Logout(c *gin.Context) { return } + logger.Infof("updated user %#q", u.GetName()) + // 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 7adbd158c..ed4e45d5d 100644 --- a/api/auth/post_token.go +++ b/api/auth/post_token.go @@ -86,6 +86,7 @@ func PostAuthToken(c *gin.Context) { TokenType: constants.UserAccessTokenType, TokenDuration: tm.UserAccessTokenDuration, } + at, err := tm.MintToken(amto) if err != nil { retErr := fmt.Errorf("unable to compose token for user %s: %w", u.GetName(), err) diff --git a/api/queue/queue.go b/api/queue/queue.go index 8cd480a6c..b8d7b73e2 100644 --- a/api/queue/queue.go +++ b/api/queue/queue.go @@ -9,6 +9,7 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/router/middleware/claims" + "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -37,8 +38,10 @@ func Info(c *gin.Context) { cl := claims.Retrieve(c) logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), "user": cl.Subject, - }).Debug("requesting queue credentials with registration token") + }).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/scm/sync.go b/api/scm/sync.go index 4be93c3b1..7f7d0c72a 100644 --- a/api/scm/sync.go +++ b/api/scm/sync.go @@ -74,6 +74,8 @@ func SyncRepo(c *gin.Context) { // // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), "org": o, "repo": r.GetName(), "user": u.GetName(), @@ -83,7 +85,6 @@ func SyncRepo(c *gin.Context) { // retrieve repo from source code manager service _, respCode, err := scm.FromContext(c).GetRepo(ctx, u, r) - // if there is an error retrieving repo, we know it is deleted: set to inactive if err != nil { if respCode == http.StatusNotFound { @@ -100,6 +101,8 @@ func SyncRepo(c *gin.Context) { return } + logger.Infof("repo %#q has been updated (set to inactive)", r.GetFullName()) + // exit with success as hook sync will be unnecessary c.JSON(http.StatusOK, r) @@ -158,6 +161,8 @@ func SyncRepo(c *gin.Context) { return } + logger.Infof("repo %#q 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 761a42396..05b733bb6 100644 --- a/api/scm/sync_org.go +++ b/api/scm/sync_org.go @@ -69,6 +69,8 @@ func SyncReposForOrg(c *gin.Context) { // // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), "org": o, "user": u.GetName(), }) @@ -135,6 +137,8 @@ func SyncReposForOrg(c *gin.Context) { return } + logger.Infof("repo %#q 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) @@ -175,6 +179,8 @@ func SyncReposForOrg(c *gin.Context) { return } + logger.Infof("repo %#q has been updated (set to inactive)", repo.GetFullName()) + results = append(results, repo) continue diff --git a/api/worker/refresh.go b/api/worker/refresh.go index 151e28a65..53cdad6da 100644 --- a/api/worker/refresh.go +++ b/api/worker/refresh.go @@ -61,14 +61,19 @@ func Refresh(c *gin.Context) { cl := claims.Retrieve(c) ctx := c.Request.Context() + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "claims_subject": cl.Subject, + "worker": w.GetHostname(), + "worker_id": w.GetID(), + }) + // if we are not using a symmetric token, and the subject does not match the input, request should be denied 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) + logger.Warnf("attempted refresh of worker %s using token from worker %s", w.GetHostname(), cl.Subject) util.HandleError(c, http.StatusBadRequest, retErr) @@ -88,12 +93,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(), - }).Debugf("refreshing worker %s authentication", w.GetHostname()) + logger.Info("worker updated (check-in time)") + + logger.Debugf("refreshing worker %s authentication", w.GetHostname()) switch cl.TokenType { // if symmetric token configured, send back symmetric token diff --git a/router/middleware/logger.go b/router/middleware/logger.go index e42f961af..8cfa9a0bb 100644 --- a/router/middleware/logger.go +++ b/router/middleware/logger.go @@ -9,6 +9,7 @@ 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/org" "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" @@ -104,7 +105,11 @@ func Logger(logger *logrus.Logger, timeFormat string) gin.HandlerFunc { } 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 } @@ -115,6 +120,18 @@ func Logger(logger *logrus.Logger, timeFormat string) gin.HandlerFunc { 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) if len(c.Errors) > 0 { From 1de0d68f4802d23424dbf023af1e0e13f1950216 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Mon, 3 Jun 2024 14:43:57 -0500 Subject: [PATCH 08/26] more logging on update/create events in post webhook --- api/build/update.go | 17 +++++++ api/webhook/post.go | 116 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 113 insertions(+), 20 deletions(-) diff --git a/api/build/update.go b/api/build/update.go index 75489af4e..bfa6d3e03 100644 --- a/api/build/update.go +++ b/api/build/update.go @@ -182,6 +182,13 @@ func UpdateBuild(c *gin.Context) { func UpdateComponentStatuses(c *gin.Context, b *types.Build, status string) error { ctx := c.Request.Context() + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "build": b.GetNumber(), + "build_id": b.GetID(), + "repo": b.GetRepo().GetFullName(), + }) + // retrieve the steps for the build from the step table steps := []*library.Step{} page := 1 @@ -214,6 +221,11 @@ func UpdateComponentStatuses(c *gin.Context, b *types.Build, status string) erro if err != nil { return err } + + logger.WithFields(logrus.Fields{ + "step": step.GetNumber(), + "step_id": step.GetID(), + }).Infof("step updated") } // retrieve the services for the build from the service table @@ -247,6 +259,11 @@ func UpdateComponentStatuses(c *gin.Context, b *types.Build, status string) erro if err != nil { return err } + + logger.WithFields(logrus.Fields{ + "service": service.GetNumber(), + "service_id": service.GetID(), + }).Info("service updated") } return nil diff --git a/api/webhook/post.go b/api/webhook/post.go index 3cad2d182..90d21d4b4 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -82,12 +82,16 @@ var baseErr = "unable to process webhook" // //nolint:funlen,gocyclo // ignore function length and cyclomatic complexity func PostWebhook(c *gin.Context) { - logrus.Debug("webhook received") - // capture middleware values m := c.MustGet("metadata").(*internal.Metadata) ctx := c.Request.Context() + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + }) + + logger.Debug("webhook received") + // duplicate request so we can perform operations on the request body // // https://golang.org/pkg/net/http/#Request.Clone @@ -142,8 +146,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) + logger.Debugf("hook generated from SCM: %v", h) + logger.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 +177,7 @@ func PostWebhook(c *gin.Context) { return } - logrus.Debugf(`build author: %s, + logger.Debugf(`build author: %s, build branch: %s, build commit: %s, build ref: %s`, @@ -191,8 +195,14 @@ 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) + logger.Errorf("unable to update webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err) } + + logger.WithFields(logrus.Fields{ + "hook": h.GetNumber(), + "hook_id": h.GetID(), + "repo": r.GetFullName(), + }).Info("hook updated in database") }() // send API call to capture parsed repo from webhook @@ -242,6 +252,12 @@ func PostWebhook(c *gin.Context) { return } + logrus.WithFields(logrus.Fields{ + "hook": h.GetNumber(), + "hook_id": h.GetID(), + "repo": repo.GetFullName(), + }).Info("hook created in database") + // verify the webhook from the source control provider if c.Value("webhookvalidation").(bool) { err = scm.FromContext(c).VerifyWebhook(ctx, dupRequest, repo) @@ -352,7 +368,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) @@ -362,6 +378,12 @@ func PostWebhook(c *gin.Context) { return } + + logger.WithFields(logrus.Fields{ + "deployment": dr.GetNumber(), + "deployment_id": dr.GetID(), + "repo": repo.GetFullName(), + }).Info("deployment created in database") } 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) @@ -386,6 +408,12 @@ func PostWebhook(c *gin.Context) { return } + + logger.WithFields(logrus.Fields{ + "deployment": d.GetNumber(), + "deployment_id": d.GetID(), + "repo": repo.GetFullName(), + }).Info("deployment updated in database") } } @@ -397,7 +425,7 @@ 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) + logger.Errorf("unable to fetch pending and running builds for %s: %v", repo.GetFullName(), err) } for _, rB := range rBs { @@ -406,9 +434,9 @@ func PostWebhook(c *gin.Context) { 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) + logger.Errorf("unable to update canceled build error message: %v", err) } else { - logrus.Errorf("unable to cancel running build: %v", err) + logger.Errorf("unable to cancel running build: %v", err) } } } @@ -417,7 +445,7 @@ func PostWebhook(c *gin.Context) { // 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()) + logger.Tracef("inside %s workflow for fork PR build %s/%d", repo.GetApproveBuild(), r.GetFullName(), b.GetNumber()) switch repo.GetApproveBuild() { case constants.ApproveForkAlways: @@ -439,7 +467,7 @@ func PostWebhook(c *gin.Context) { return } - logrus.Debugf("fork PR build %s/%d automatically running without approval", repo.GetFullName(), b.GetNumber()) + logger.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 // @@ -463,14 +491,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()) + logger.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) + logger.Errorf("unable to set commit status for %s/%d: %v", repo.GetFullName(), b.GetNumber(), err) } // publish the build to the queue @@ -486,14 +514,25 @@ 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) { + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "event_type": h.GetEvent(), + }) + logrus.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) + logger.Errorf("unable to create webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err) } + + logger.WithFields(logrus.Fields{ + "hook": hr.GetNumber(), + "hook_id": hr.GetID(), + "repo": r.GetFullName(), + }).Info("hook created in database") }() switch h.GetEventAction() { @@ -510,7 +549,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()) + logger.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 { @@ -566,6 +605,11 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *internal.Meta return nil, err } + logger.WithFields(logrus.Fields{ + "repo_id": dbRepo.GetID(), + "repo": dbRepo.GetFullName(), + }).Info("repo updated in database") + return dbRepo, nil // all other repo event actions are skippable default: @@ -578,7 +622,12 @@ 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.Debugf("renaming repository from %s to %s", r.GetPreviousName(), r.GetName()) + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "event_type": h.GetEvent(), + }) + + logger.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()) @@ -643,6 +692,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) } + + logger.WithFields(logrus.Fields{ + "secret_id": secret.GetID(), + "repo": secret.GetRepo(), + "org": secret.GetOrg(), + }).Info("secret updated in database") } // get total number of builds associated with repository @@ -675,6 +730,12 @@ 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) } + + logger.WithFields(logrus.Fields{ + "build_id": build.GetID(), + "build": build.GetNumber(), + "repo": dbR.GetFullName(), + }).Info("build updated in database") } // update the repo name information @@ -697,13 +758,26 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi return nil, retErr } + logger.WithFields(logrus.Fields{ + "repo_id": dbR.GetID(), + "repo": dbR.GetFullName(), + }).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()) + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "repo": r.GetFullName(), + "build": b.GetNumber(), + "build_id": b.GetID(), + }) + + logger.Debug("fork PR build waiting for approval") + b.SetStatus(constants.StatusPendingApproval) _, err := database.FromContext(c).UpdateBuild(c, b) @@ -711,6 +785,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) } + logger.Info("build updated in database") + // update the build components to pending approval status err = build.UpdateComponentStatuses(c, b, constants.StatusPendingApproval) if err != nil { @@ -720,7 +796,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) + logger.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), b.GetNumber(), err) } return nil From 4625c8ebfcf8299ced9615b090d6d98510d58ac9 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:11:49 -0500 Subject: [PATCH 09/26] updated enqueue logging --- api/build/enqueue.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/api/build/enqueue.go b/api/build/enqueue.go index fab6679b7..9b92a04d3 100644 --- a/api/build/enqueue.go +++ b/api/build/enqueue.go @@ -16,11 +16,16 @@ 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.Debugf("Converting queue item to json for build %d for %s", item.Build.GetNumber(), item.Build.GetRepo().GetFullName()) + logger := logrus.WithFields(logrus.Fields{ + "build": item.Build.GetNumber(), + "repo": item.Build.GetRepo().GetFullName(), + }) + + logger.Debug("converting queue item to json") 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) + logger.Errorf("failed to convert item to json: %v", err) // error out the build CleanBuild(ctx, db, item.Build, nil, nil, err) @@ -28,16 +33,16 @@ func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, it return } - logrus.Debugf("Pushing item for build %d for %s to queue route %s", item.Build.GetNumber(), item.Build.GetRepo().GetFullName(), route) + logger.Debugf("pushing item for build to queue route %#q", 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) + logger.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) + logger.Errorf("failed to publish build: %v", err) // error out the build CleanBuild(ctx, db, item.Build, nil, nil, err) @@ -52,6 +57,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) + logger.Errorf("failed to update build during publish to queue: %v", err) } + + logger.Info("updated build as enqueued") } From 30c1ef209d04b396624b1a2389d53557ad3f977f Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Tue, 4 Jun 2024 21:17:39 -0500 Subject: [PATCH 10/26] update logging messages for various components --- api/auth/get_token.go | 14 ++++++++++---- api/auth/logout.go | 9 +++++++-- api/build/approve.go | 18 ++++++++++++------ api/build/auto_cancel.go | 21 +++++++++++++++++++++ api/build/cancel.go | 21 +++++++++++++++++---- api/build/clean.go | 20 ++++++++++++++++++++ api/build/compile_publish.go | 18 +++++++++++++++++- api/build/enqueue.go | 5 +++-- api/build/executable.go | 8 ++++++-- api/build/plan.go | 6 ++++++ api/dashboard/create.go | 19 +++++++++++++------ api/repo/chown.go | 18 +++++++++++++----- api/repo/create.go | 2 ++ api/repo/repair.go | 20 +++++++++++++++----- api/scm/sync.go | 4 ++-- api/scm/sync_org.go | 4 ++-- api/service/plan.go | 14 ++++++++++++++ api/step/plan.go | 13 +++++++++++++ api/user/create_token.go | 13 ++++++++++--- api/user/delete_token.go | 13 ++++++++++--- api/worker/refresh.go | 2 +- 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 +++--- queue/queue.go | 2 +- router/middleware/worker/worker.go | 2 +- scm/github/deployment.go | 7 ++++--- scm/github/repo.go | 2 +- 33 files changed, 252 insertions(+), 68 deletions(-) diff --git a/api/auth/get_token.go b/api/auth/get_token.go index 3a5c5d640..075d57932 100644 --- a/api/auth/get_token.go +++ b/api/auth/get_token.go @@ -129,7 +129,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) @@ -138,7 +138,10 @@ func GetAuthToken(c *gin.Context) { return } - logger.Infof("new user %#q created", u.GetName()) + logger.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}) @@ -164,7 +167,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) @@ -173,7 +176,10 @@ func GetAuthToken(c *gin.Context) { return } - logger.Infof("user %#q updated", u.GetName()) + logger.WithFields(logrus.Fields{ + "user": ur.GetName(), + "user_id": ur.GetID(), + }).Info("user updated") // return the user with their jwt access token c.JSON(http.StatusOK, library.Token{Token: &at}) diff --git a/api/auth/logout.go b/api/auth/logout.go index 2de7fee45..606c866ce 100644 --- a/api/auth/logout.go +++ b/api/auth/logout.go @@ -50,6 +50,8 @@ func Logout(c *gin.Context) { // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields logger := logrus.WithFields(logrus.Fields{ "user": u.GetName(), + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), }) logger.Debugf("logging out user %s", u.GetName()) @@ -73,7 +75,7 @@ func Logout(c *gin.Context) { u.SetRefreshToken("") // 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) @@ -82,7 +84,10 @@ func Logout(c *gin.Context) { return } - logger.Infof("updated user %#q", u.GetName()) + logger.WithFields(logrus.Fields{ + "user": ur.GetName(), + "user_id": ur.GetID(), + }).Info("updated user") // return 200 for successful logout c.JSON(http.StatusOK, "ok") diff --git a/api/build/approve.go b/api/build/approve.go index 478b475c7..69c30902f 100644 --- a/api/build/approve.go +++ b/api/build/approve.go @@ -83,10 +83,12 @@ func ApproveBuild(c *gin.Context) { // // 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(), + "ip": util.EscapeValue(c.ClientIP()), + "build": b.GetNumber(), + "build_id": b.GetID(), + "org": o, + "repo": r.GetName(), + "user": u.GetName(), }) logger.Debugf("approving build %d", b.GetID()) @@ -115,10 +117,14 @@ 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) + logger.Errorf("failed to update build during publish to queue: %v", err) } - logger.Debugf("user %s approved build %s/%d for execution", u.GetName(), r.GetFullName(), b.GetNumber()) + logger.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + "repo": r.GetFullName(), + }).Info("build updated - user approved build execution") // publish the build to the queue go Enqueue( diff --git a/api/build/auto_cancel.go b/api/build/auto_cancel.go index 62b2922a1..c10cbda3a 100644 --- a/api/build/auto_cancel.go +++ b/api/build/auto_cancel.go @@ -12,10 +12,12 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/internal/token" + "github.com/go-vela/server/util" "github.com/go-vela/types/constants" "github.com/go-vela/types/pipeline" ) @@ -23,6 +25,15 @@ 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) { + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "build": b.GetNumber(), + "build_id": b.GetID(), + }) + + logger.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 +55,11 @@ func AutoCancel(c *gin.Context, b *types.Build, rB *types.Build, cancelOpts *pip return false, err } + logger.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 +83,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 } + + logger.WithFields(logrus.Fields{ + "build": rB.GetNumber(), + "build_id": rB.GetID(), + }).Info("build updated - build canceled") } return true, nil diff --git a/api/build/cancel.go b/api/build/cancel.go index b220dd893..4b5b475b8 100644 --- a/api/build/cancel.go +++ b/api/build/cancel.go @@ -85,10 +85,13 @@ func CancelBuild(c *gin.Context) { // // 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": user.GetName(), + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "build": b.GetNumber(), + "build_id": b.GetID(), + "org": o, + "repo": r.GetName(), + "user": user.GetName(), }) logger.Debugf("canceling build %s", entry) @@ -180,6 +183,11 @@ func CancelBuild(c *gin.Context) { return } + logger.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + }).Info("build updated - build canceled") + c.JSON(resp.StatusCode, b) return @@ -209,6 +217,11 @@ func CancelBuild(c *gin.Context) { return } + logger.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 235efc989..01e899c1d 100644 --- a/api/build/clean.go +++ b/api/build/clean.go @@ -20,6 +20,14 @@ import ( // like steps and services, for the build in the // configured backend. func CleanBuild(ctx context.Context, database database.Interface, b *types.Build, services []*library.Service, steps []*library.Step, e error) { + logger := logrus.WithFields(logrus.Fields{ + "build": b.GetNumber(), + "build_id": b.GetID(), + "repo": b.GetRepo().GetFullName(), + }) + + logger.Debug("cleaning build") + // update fields in build object b.SetError(fmt.Sprintf("unable to publish to queue: %s", e.Error())) b.SetStatus(constants.StatusError) @@ -31,6 +39,8 @@ func CleanBuild(ctx context.Context, database database.Interface, b *types.Build logrus.Errorf("unable to kill build %d: %v", b.GetNumber(), err) } + logger.Info("build updated - build cleaned") + for _, s := range services { // update fields in service object s.SetStatus(constants.StatusKilled) @@ -41,6 +51,11 @@ func CleanBuild(ctx context.Context, database database.Interface, b *types.Build if err != nil { logrus.Errorf("unable to kill service %s for build %d: %v", s.GetName(), b.GetNumber(), err) } + + logger.WithFields(logrus.Fields{ + "service": s.GetName(), + "service_id": s.GetID(), + }).Info("service updated - service cleaned") } for _, s := range steps { @@ -53,5 +68,10 @@ func CleanBuild(ctx context.Context, database database.Interface, b *types.Build if err != nil { logrus.Errorf("unable to kill step %s for build %d: %v", s.GetName(), b.GetNumber(), err) } + + logger.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..e246ecbcf 100644 --- a/api/build/compile_publish.go +++ b/api/build/compile_publish.go @@ -49,7 +49,13 @@ 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{ + "repo": cfg.Build.GetRepo().GetFullName(), + "build": cfg.Build.GetNumber(), + "build_id": cfg.Build.GetID(), + }) + + logger.Debug("generating queue items") // assign variables from form for readibility r := cfg.Build.GetRepo() @@ -322,6 +328,11 @@ func CompileAndPublish( return nil, nil, http.StatusInternalServerError, retErr } + + logrus.WithFields(logrus.Fields{ + "pipeline": pipeline.GetID(), + "repo": repo.GetFullName(), + }).Info("pipeline created") } b.SetPipelineID(pipeline.GetID()) @@ -365,6 +376,11 @@ func CompileAndPublish( return nil, nil, http.StatusInternalServerError, retErr } + logger.WithFields(logrus.Fields{ + "repo": repo.GetFullName(), + "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/enqueue.go b/api/build/enqueue.go index 9b92a04d3..2b0a229e9 100644 --- a/api/build/enqueue.go +++ b/api/build/enqueue.go @@ -17,8 +17,9 @@ 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) { logger := logrus.WithFields(logrus.Fields{ - "build": item.Build.GetNumber(), - "repo": item.Build.GetRepo().GetFullName(), + "build": item.Build.GetNumber(), + "build_id": item.Build.GetID(), + "repo": item.Build.GetRepo().GetFullName(), }) logger.Debug("converting queue item to json") diff --git a/api/build/executable.go b/api/build/executable.go index 1f7f46556..336e585a8 100644 --- a/api/build/executable.go +++ b/api/build/executable.go @@ -104,7 +104,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) @@ -120,7 +120,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) @@ -128,5 +128,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 in the database") + return nil } diff --git a/api/build/plan.go b/api/build/plan.go index 49e700d52..0393ad28f 100644 --- a/api/build/plan.go +++ b/api/build/plan.go @@ -13,6 +13,7 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/scm" "github.com/go-vela/types/pipeline" + "github.com/sirupsen/logrus" ) // PlanBuild is a helper function to plan the build for @@ -40,6 +41,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/dashboard/create.go b/api/dashboard/create.go index 46ce9c041..3b3a9972c 100644 --- a/api/dashboard/create.go +++ b/api/dashboard/create.go @@ -58,6 +58,16 @@ func CreateDashboard(c *gin.Context) { // capture middleware values u := user.Retrieve(c) + // update engine logger with API metadata + // + // https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) + // capture body from API request input := new(types.Dashboard) @@ -77,12 +87,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(), - }).Debugf("creating new dashboard %s", input.GetName()) + logger.Debugf("creating new dashboard %s", input.GetName()) d := new(types.Dashboard) @@ -136,6 +141,8 @@ func CreateDashboard(c *gin.Context) { return } + logger.Infof("user updated with new dashboard %s", d.GetName()) + c.JSON(http.StatusCreated, d) } diff --git a/api/repo/chown.go b/api/repo/chown.go index 2a0c34163..2e9a76ba3 100644 --- a/api/repo/chown.go +++ b/api/repo/chown.go @@ -58,11 +58,17 @@ func ChownRepo(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{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Debugf("changing owner of repo %s to %s", r.GetFullName(), u.GetName()) + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "org": o, + "repo": r.GetName(), + "repo_id": r.GetID(), + "user": u.GetName(), + "user_id": u.GetID(), + }) + + logger.Debugf("changing owner of repo %s to %s", r.GetFullName(), u.GetName()) // update repo owner r.SetOwner(u) @@ -77,5 +83,7 @@ func ChownRepo(c *gin.Context) { return } + logger.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 1b96aeed5..726d51ec1 100644 --- a/api/repo/create.go +++ b/api/repo/create.go @@ -287,6 +287,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) diff --git a/api/repo/repair.go b/api/repo/repair.go index f7a4a71a6..4a3e5be8b 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -63,11 +63,17 @@ func RepairRepo(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{ - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }).Debugf("repairing repo %s", r.GetFullName()) + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "org": o, + "repo": r.GetName(), + "repo_id": r.GetID(), + "user": u.GetName(), + "user_id": u.GetID(), + }) + + logger.Debugf("repairing repo %s", r.GetFullName()) // check if we should create the webhook if c.Value("webhookvalidation").(bool) { @@ -119,6 +125,10 @@ func RepairRepo(c *gin.Context) { return } + + logger.WithFields(logrus.Fields{ + "hook": hook.GetID(), + }).Info("new webhook created") } // get repo information from the source diff --git a/api/scm/sync.go b/api/scm/sync.go index 7f7d0c72a..f1cea567c 100644 --- a/api/scm/sync.go +++ b/api/scm/sync.go @@ -101,7 +101,7 @@ func SyncRepo(c *gin.Context) { return } - logger.Infof("repo %#q has been updated (set to inactive)", r.GetFullName()) + logger.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) @@ -161,7 +161,7 @@ func SyncRepo(c *gin.Context) { return } - logger.Infof("repo %#q has been updated (set to inactive)", r.GetFullName()) + logger.Infof("repo %s has been updated - set to inactive", r.GetFullName()) c.JSON(http.StatusOK, r) diff --git a/api/scm/sync_org.go b/api/scm/sync_org.go index 05b733bb6..0975e3c25 100644 --- a/api/scm/sync_org.go +++ b/api/scm/sync_org.go @@ -137,7 +137,7 @@ func SyncReposForOrg(c *gin.Context) { return } - logger.Infof("repo %#q has been updated (set to inactive)", repo.GetFullName()) + logger.Infof("repo %s has been updated - set to inactive", repo.GetFullName()) results = append(results, repo) } else { @@ -179,7 +179,7 @@ func SyncReposForOrg(c *gin.Context) { return } - logger.Infof("repo %#q has been updated (set to inactive)", repo.GetFullName()) + logger.Infof("repo %s has been updated - set to inactive", repo.GetFullName()) results = append(results, repo) diff --git a/api/service/plan.go b/api/service/plan.go index 05673f05e..3c92eeeca 100644 --- a/api/service/plan.go +++ b/api/service/plan.go @@ -12,6 +12,7 @@ import ( "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/go-vela/types/pipeline" + "github.com/sirupsen/logrus" ) // PlanServices is a helper function to plan all services @@ -39,6 +40,12 @@ 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(), + "repo": b.GetRepo().GetFullName(), + }).Info("service created") + // populate environment variables from service library // // https://pkg.go.dev/github.com/go-vela/types/library#Service.Environment @@ -59,6 +66,13 @@ 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(), + "repo": b.GetRepo().GetFullName(), + }).Info("log for service created") } return services, nil diff --git a/api/step/plan.go b/api/step/plan.go index f685170a3..7747b06b5 100644 --- a/api/step/plan.go +++ b/api/step/plan.go @@ -70,6 +70,12 @@ 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(), + "repo": b.GetRepo().GetFullName(), + }).Info("step created") + // populate environment variables from step library // // https://pkg.go.dev/github.com/go-vela/types/library#step.Environment @@ -91,6 +97,13 @@ 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(), + "repo": b.GetRepo().GetFullName(), + }).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/user/create_token.go b/api/user/create_token.go index 02a948620..83e19dd72 100644 --- a/api/user/create_token.go +++ b/api/user/create_token.go @@ -46,9 +46,14 @@ func CreateToken(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{ - "user": u.GetName(), - }).Debugf("composing token for user %s", u.GetName()) + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) + + logger.Debugf("composing token for user %s", u.GetName()) tm := c.MustGet("token-manager").(*token.Manager) @@ -74,5 +79,7 @@ func CreateToken(c *gin.Context) { return } + logger.Info("user updated - token created") + c.JSON(http.StatusOK, library.Token{Token: &at}) } diff --git a/api/user/delete_token.go b/api/user/delete_token.go index 91e18efa3..7b1e20455 100644 --- a/api/user/delete_token.go +++ b/api/user/delete_token.go @@ -46,9 +46,14 @@ func DeleteToken(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{ - "user": u.GetName(), - }).Debugf("revoking token for user %s", u.GetName()) + logger := logrus.WithFields(logrus.Fields{ + "ip": util.EscapeValue(c.ClientIP()), + "path": util.EscapeValue(c.Request.URL.Path), + "user": u.GetName(), + "user_id": u.GetID(), + }) + + logger.Debugf("revoking token for user %s", u.GetName()) tm := c.MustGet("token-manager").(*token.Manager) @@ -74,5 +79,7 @@ func DeleteToken(c *gin.Context) { return } + logger.Info("user updated - token rotated") + c.JSON(http.StatusOK, library.Token{Token: &at}) } diff --git a/api/worker/refresh.go b/api/worker/refresh.go index 53cdad6da..023391f7d 100644 --- a/api/worker/refresh.go +++ b/api/worker/refresh.go @@ -93,7 +93,7 @@ func Refresh(c *gin.Context) { return } - logger.Info("worker updated (check-in time)") + logger.Info("worker updated - check-in time updated") logger.Debugf("refreshing worker %s authentication", w.GetHostname()) 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 f2832935e..c0d45c036 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") } } @@ -232,4 +242,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 dd5b6d6c6..b5d278cdb 100644 --- a/cmd/vela-server/server.go +++ b/cmd/vela-server/server.go @@ -140,6 +140,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 04a756fca..a4ad6f585 100644 --- a/cmd/vela-server/token.go +++ b/cmd/vela-server/token.go @@ -12,7 +12,7 @@ import ( // helper function to setup the tokenmanager from the CLI arguments. func setupTokenManager(c *cli.Context) *token.Manager { - logrus.Debug("Creating token manager from CLI configuration") + logrus.Debug("creating token manager from CLI configuration") tm := &token.Manager{ PrivateKey: 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/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/middleware/worker/worker.go b/router/middleware/worker/worker.go index d46ad1a1a..e643949c3 100644 --- a/router/middleware/worker/worker.go +++ b/router/middleware/worker/worker.go @@ -32,7 +32,7 @@ func Establish() gin.HandlerFunc { return } - logrus.Debugf("Reading worker %s", wParam) + logrus.Debugf("reading worker %s", wParam) w, err := database.FromContext(c).GetWorkerForHostname(ctx, wParam) if err != nil { diff --git a/scm/github/deployment.go b/scm/github/deployment.go index 24d0f083e..718e7c4a3 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 c9b143763..89284b67a 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) From 15c3a8309abd8752678598d72df9904a5b9efc26 Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Wed, 5 Jun 2024 11:09:35 -0500 Subject: [PATCH 11/26] moving things around a little --- api/admin/build.go | 4 ++-- api/admin/clean.go | 4 ++-- api/admin/hook.go | 6 +++--- api/admin/repo.go | 6 +++--- api/admin/secret.go | 6 +++--- api/admin/service.go | 6 +++--- api/admin/step.go | 6 +++--- api/admin/user.go | 6 +++--- api/admin/worker.go | 4 ++-- api/auth/post_token.go | 4 ++-- router/middleware/logger.go | 2 +- 11 files changed, 27 insertions(+), 27 deletions(-) diff --git a/api/admin/build.go b/api/admin/build.go index 3478f192c..2abac59fe 100644 --- a/api/admin/build.go +++ b/api/admin/build.go @@ -120,7 +120,7 @@ func UpdateBuild(c *gin.Context) { "user_id": u.GetID(), }) - logger.Debug("updating build") + logger.Debug("platform admin: updating build") // capture body from API request input := new(types.Build) @@ -140,7 +140,7 @@ func UpdateBuild(c *gin.Context) { "repo": util.EscapeValue(input.GetRepo().GetName()), "repo_id": input.GetRepo().GetID(), "org": util.EscapeValue(input.GetRepo().GetOrg()), - }).Info("attempting to update build") + }).Debug("attempting to update build") // send API call to update the build b, err := database.FromContext(c).UpdateBuild(ctx, input) diff --git a/api/admin/clean.go b/api/admin/clean.go index f7bf8f18f..d555c21cd 100644 --- a/api/admin/clean.go +++ b/api/admin/clean.go @@ -60,8 +60,6 @@ import ( // CleanResources represents the API handler to // update any user stored in the database. func CleanResources(c *gin.Context) { - logrus.Debug("platform admin: cleaning resources") - // capture middleware values ctx := c.Request.Context() u := user.Retrieve(c) @@ -73,6 +71,8 @@ func CleanResources(c *gin.Context) { "user_id": u.GetID(), }) + logger.Debug("platform admin: cleaning resources") + // default error message msg := "build cleaned by platform admin" diff --git a/api/admin/hook.go b/api/admin/hook.go index dee867105..f46afb9ea 100644 --- a/api/admin/hook.go +++ b/api/admin/hook.go @@ -52,8 +52,6 @@ import ( // UpdateHook represents the API handler to // update any hook stored in the database. func UpdateHook(c *gin.Context) { - logrus.Debug("platform admin: updating hook") - // capture middleware values ctx := c.Request.Context() u := user.Retrieve(c) @@ -65,6 +63,8 @@ func UpdateHook(c *gin.Context) { "user_id": u.GetID(), }) + logrus.Debug("platform admin: updating hook") + // capture body from API request input := new(library.Hook) @@ -79,7 +79,7 @@ func UpdateHook(c *gin.Context) { logger.WithFields(logrus.Fields{ "hook_id": input.GetID(), - }).Info("attempting to update hook") + }).Debug("attempting to update hook") // send API call to update the hook h, err := database.FromContext(c).UpdateHook(ctx, input) diff --git a/api/admin/repo.go b/api/admin/repo.go index 003b9e8c3..41340d492 100644 --- a/api/admin/repo.go +++ b/api/admin/repo.go @@ -53,8 +53,6 @@ import ( // UpdateRepo represents the API handler to // update any repo stored in the database. func UpdateRepo(c *gin.Context) { - logrus.Debug("platform admin: updating repo") - // capture middleware values ctx := c.Request.Context() u := user.Retrieve(c) @@ -66,6 +64,8 @@ func UpdateRepo(c *gin.Context) { "user_id": u.GetID(), }) + logrus.Debug("platform admin: updating repo") + // capture body from API request input := new(types.Repo) @@ -81,7 +81,7 @@ func UpdateRepo(c *gin.Context) { logger.WithFields(logrus.Fields{ "repo_id": input.GetID(), "repo": util.EscapeValue(input.GetFullName()), - }).Info("attempting to update repo") + }).Debug("attempting to update repo") // send API call to update the repo r, err := database.FromContext(c).UpdateRepo(ctx, input) diff --git a/api/admin/secret.go b/api/admin/secret.go index 3b06ea315..79da8ef7a 100644 --- a/api/admin/secret.go +++ b/api/admin/secret.go @@ -52,8 +52,6 @@ import ( // UpdateSecret represents the API handler to // update any secret stored in the database. func UpdateSecret(c *gin.Context) { - logrus.Debug("platform admin: updating secret") - // capture middleware values ctx := c.Request.Context() u := user.Retrieve(c) @@ -65,6 +63,8 @@ func UpdateSecret(c *gin.Context) { "user_id": u.GetID(), }) + logrus.Debug("platform admin: updating secret") + // capture body from API request input := new(library.Secret) @@ -84,7 +84,7 @@ func UpdateSecret(c *gin.Context) { "type": util.EscapeValue(input.GetType()), "name": util.EscapeValue(input.GetName()), "team": util.EscapeValue(input.GetTeam()), - }).Info("attempting to update secret") + }).Debug("attempting to update secret") // send API call to update the secret s, err := database.FromContext(c).UpdateSecret(ctx, input) diff --git a/api/admin/service.go b/api/admin/service.go index 977243522..8b6cafe54 100644 --- a/api/admin/service.go +++ b/api/admin/service.go @@ -54,8 +54,6 @@ import ( // UpdateService represents the API handler to // update any service stored in the database. func UpdateService(c *gin.Context) { - logrus.Debug("platform admin: updating service") - // capture middleware values ctx := c.Request.Context() u := user.Retrieve(c) @@ -67,6 +65,8 @@ func UpdateService(c *gin.Context) { "user_id": u.GetID(), }) + logrus.Debug("platform admin: updating service") + // capture body from API request input := new(library.Service) @@ -82,7 +82,7 @@ func UpdateService(c *gin.Context) { logger.WithFields(logrus.Fields{ "service_id": input.GetID(), "service": util.EscapeValue(input.GetName()), - }).Info("attempting to update service") + }).Debug("attempting to update service") // send API call to update the service s, err := database.FromContext(c).UpdateService(ctx, input) diff --git a/api/admin/step.go b/api/admin/step.go index f0269f1b4..670e4a978 100644 --- a/api/admin/step.go +++ b/api/admin/step.go @@ -53,8 +53,6 @@ import ( // UpdateStep represents the API handler to // update any step stored in the database. func UpdateStep(c *gin.Context) { - logrus.Debug("platform admin: updating step") - // capture middleware values ctx := c.Request.Context() u := user.Retrieve(c) @@ -66,6 +64,8 @@ func UpdateStep(c *gin.Context) { "user_id": u.GetID(), }) + logrus.Debug("platform admin: updating step") + // capture body from API request input := new(library.Step) @@ -81,7 +81,7 @@ func UpdateStep(c *gin.Context) { logger.WithFields(logrus.Fields{ "step_id": input.GetID(), "step": util.EscapeValue(input.GetName()), - }).Info("attempting to update step") + }).Debug("attempting to update step") // send API call to update the step s, err := database.FromContext(c).UpdateStep(ctx, input) diff --git a/api/admin/user.go b/api/admin/user.go index 1a2ec5ffb..87f8bd39e 100644 --- a/api/admin/user.go +++ b/api/admin/user.go @@ -53,8 +53,6 @@ import ( // UpdateUser represents the API handler to // update any user stored in the database. func UpdateUser(c *gin.Context) { - logrus.Debug("platform admin: updating user") - // capture middleware values ctx := c.Request.Context() u := user.Retrieve(c) @@ -66,6 +64,8 @@ func UpdateUser(c *gin.Context) { "user_id": u.GetID(), }) + logrus.Debug("platform admin: updating user") + // capture body from API request input := new(types.User) @@ -81,7 +81,7 @@ func UpdateUser(c *gin.Context) { logger.WithFields(logrus.Fields{ "target_user_id": input.GetID(), "target_user": util.EscapeValue(input.GetName()), - }).Info("attempting to update user") + }).Debug("attempting to update user") // send API call to update the user tu, err := database.FromContext(c).UpdateUser(ctx, input) diff --git a/api/admin/worker.go b/api/admin/worker.go index 4864140af..b65989e7d 100644 --- a/api/admin/worker.go +++ b/api/admin/worker.go @@ -43,8 +43,6 @@ import ( // RegisterToken represents the API handler to // generate a registration token for onboarding a worker. func RegisterToken(c *gin.Context) { - logrus.Debug("platform admin: generating worker registration token") - // capture middleware values host := util.PathParameter(c, "worker") @@ -55,6 +53,8 @@ func RegisterToken(c *gin.Context) { TokenDuration: tm.WorkerRegisterTokenDuration, } + logrus.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) diff --git a/api/auth/post_token.go b/api/auth/post_token.go index ed4e45d5d..65b3b9e87 100644 --- a/api/auth/post_token.go +++ b/api/auth/post_token.go @@ -74,7 +74,7 @@ func PostAuthToken(c *gin.Context) { return } - logrus.Infof("user %#q successfully authenticated via SCM PAT", u.GetName()) + logrus.Infof("user %s successfully authenticated via SCM PAT", u.GetName()) // We don't need refresh token for this scenario // We only need access token and are configured based on the config defined @@ -94,7 +94,7 @@ func PostAuthToken(c *gin.Context) { util.HandleError(c, http.StatusServiceUnavailable, retErr) } - logrus.Debugf("new access token created for user %#q via SCM PAT", u.GetName()) + logrus.Debugf("new access token created for user %s via SCM PAT", u.GetName()) // return the user with their jwt access token c.JSON(http.StatusOK, library.Token{Token: &at}) diff --git a/router/middleware/logger.go b/router/middleware/logger.go index 8cfa9a0bb..91f5e2fc5 100644 --- a/router/middleware/logger.go +++ b/router/middleware/logger.go @@ -83,7 +83,7 @@ func Logger(logger *logrus.Logger, timeFormat string) gin.HandlerFunc { pipeline := pipeline.Retrieve(c) if pipeline != nil { - fields["pipeline"] = pipeline.ID + fields["pipeline_id"] = pipeline.ID } repo := repo.Retrieve(c) From 97485cebba951300c58962ee2636d9b7f9662da1 Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:04:19 -0500 Subject: [PATCH 12/26] oops --- api/admin/clean.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/admin/clean.go b/api/admin/clean.go index 11e779af8..f156b2b19 100644 --- a/api/admin/clean.go +++ b/api/admin/clean.go @@ -51,7 +51,7 @@ import ( // '401': // description: Unauthorized // schema: -// "$ref": "#/definitions/Error +// "$ref": "#/definitions/Error" // '500': // description: Unexpected server error // schema: From 9f5ebe6399d886885706934125db8d87d0372249 Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Sun, 16 Jun 2024 17:42:50 -0500 Subject: [PATCH 13/26] use logger from context --- api/admin/build.go | 24 +- api/admin/clean.go | 20 +- api/admin/hook.go | 20 +- api/admin/repo.go | 26 +- api/admin/rotate_keys.go | 4 +- api/admin/secret.go | 44 ++-- api/admin/service.go | 20 +- api/admin/settings.go | 25 +- api/admin/step.go | 20 +- api/admin/user.go | 20 +- api/admin/worker.go | 6 +- api/auth/get_token.go | 14 +- api/auth/login.go | 7 +- api/auth/logout.go | 21 +- api/auth/post_token.go | 14 +- 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 | 27 +-- api/build/auto_cancel.go | 19 +- api/build/cancel.go | 22 +- api/build/clean.go | 10 +- api/build/compile_publish.go | 27 ++- api/build/create.go | 21 +- api/build/delete.go | 15 +- api/build/enqueue.go | 4 +- api/build/executable.go | 15 +- api/build/get.go | 15 +- api/build/get_id.go | 11 +- api/build/graph.go | 24 +- 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 | 3 +- api/build/restart.go | 22 +- api/build/token.go | 13 +- api/build/update.go | 35 ++- api/dashboard/create.go | 20 +- 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 | 10 +- api/repo/chown.go | 20 +- api/repo/create.go | 26 +- 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 | 25 +- 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 | 20 +- api/scm/sync_org.go | 19 +- api/secret/create.go | 39 +-- 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 | 13 +- 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 | 10 +- api/step/update.go | 18 +- api/types/dashboard.go | 2 + api/user/create.go | 8 +- api/user/create_token.go | 15 +- api/user/delete.go | 10 +- api/user/delete_token.go | 15 +- 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 | 127 ++++++---- 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/token.go | 2 +- router/middleware/build/build.go | 23 +- router/middleware/claims/claims.go | 10 + router/middleware/dashboard/dashboard.go | 16 +- router/middleware/dashboard/doc.go | 10 + 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 | 296 +++++++++++++++++++++++ router/middleware/logger.go | 34 ++- router/middleware/org/org.go | 12 +- router/middleware/perm/perm.go | 148 ++++-------- router/middleware/pipeline/pipeline.go | 22 +- router/middleware/repo/repo.go | 20 +- router/middleware/schedule/schedule.go | 20 +- router/middleware/service/service.go | 22 +- router/middleware/step/step.go | 22 +- router/middleware/user/user.go | 11 +- router/middleware/worker/worker.go | 11 +- 145 files changed, 1403 insertions(+), 1682 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 c130447f5..13c8c2101 100644 --- a/api/admin/build.go +++ b/api/admin/build.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" ) @@ -50,7 +49,9 @@ import ( // AllBuildsQueue represents the API handler to get running and pending builds. func AllBuildsQueue(c *gin.Context) { - logrus.Debug("platform admin: reading running and pending builds") + l := c.MustGet("logger").(*logrus.Entry) + + l.Debug("platform admin: reading running and pending builds") // capture middleware values ctx := c.Request.Context() @@ -105,16 +106,9 @@ func AllBuildsQueue(c *gin.Context) { func UpdateBuild(c *gin.Context) { // capture middleware values ctx := c.Request.Context() - u := user.Retrieve(c) - - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) + l := c.MustGet("logger").(*logrus.Entry) - logger.Debug("platform admin: updating build") + l.Debug("platform admin: updating build") // capture body from API request input := new(types.Build) @@ -128,13 +122,13 @@ func UpdateBuild(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + 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("attempting to update build") + }).Debug("platform admin: attempting to update build") // send API call to update the build b, err := database.FromContext(c).UpdateBuild(ctx, input) @@ -146,13 +140,13 @@ func UpdateBuild(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + 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("updated build") + }).Info("platform admin: updated build") c.JSON(http.StatusOK, b) } diff --git a/api/admin/clean.go b/api/admin/clean.go index f156b2b19..60e352689 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,17 +59,10 @@ import ( // CleanResources represents the API handler to update stale resources. func CleanResources(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - u := user.Retrieve(c) - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logger.Debug("platform admin: cleaning resources") + l.Debug("platform admin: cleaning resources") // default error message msg := "build cleaned by platform admin" @@ -112,7 +104,7 @@ func CleanResources(c *gin.Context) { return } - logger.Debugf("cleaned %d builds in database", builds) + l.Debugf("platform admin: cleaned %d builds in database", builds) // clean executables executables, err := database.FromContext(c).CleanBuildExecutables(ctx) @@ -124,7 +116,7 @@ func CleanResources(c *gin.Context) { return } - logger.Debugf("cleaned %d executables in database", executables) + l.Debugf("platform admin: cleaned %d executables in database", executables) // clean services services, err := database.FromContext(c).CleanServices(ctx, msg, before) @@ -136,7 +128,7 @@ func CleanResources(c *gin.Context) { return } - logger.Debugf("cleaned %d services in database", services) + l.Debugf("platform admin: cleaned %d services in database", services) // clean steps steps, err := database.FromContext(c).CleanSteps(ctx, msg, before) @@ -148,7 +140,7 @@ func CleanResources(c *gin.Context) { return } - logger.Debugf("cleaned %d steps in database", steps) + l.Debugf("platform admin: cleaned %d steps in database", 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 4c351cf06..a7d0ef303 100644 --- a/api/admin/hook.go +++ b/api/admin/hook.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" "github.com/go-vela/types/library" ) @@ -52,17 +51,10 @@ import ( // UpdateHook represents the API handler to update a hook. func UpdateHook(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - u := user.Retrieve(c) - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logrus.Debug("platform admin: updating hook") + l.Debug("platform admin: updating hook") // capture body from API request input := new(library.Hook) @@ -76,9 +68,9 @@ func UpdateHook(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "hook_id": input.GetID(), - }).Debug("attempting to update hook") + }).Debug("platform admin: attempting to update hook") // send API call to update the hook h, err := database.FromContext(c).UpdateHook(ctx, input) @@ -90,9 +82,9 @@ func UpdateHook(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "hook_id": h.GetID(), - }).Info("hook updated") + }).Info("platform admin: hook updated") c.JSON(http.StatusOK, h) } diff --git a/api/admin/repo.go b/api/admin/repo.go index c97795e49..a5a5729a9 100644 --- a/api/admin/repo.go +++ b/api/admin/repo.go @@ -12,7 +12,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" ) @@ -53,17 +52,10 @@ import ( // UpdateRepo represents the API handler to update a repo. func UpdateRepo(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - u := user.Retrieve(c) - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logrus.Debug("platform admin: updating repo") + l.Debug("platform admin: updating repo") // capture body from API request input := new(types.Repo) @@ -77,10 +69,11 @@ func UpdateRepo(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ + "org": util.EscapeValue(input.GetOrg()), + "repo": util.EscapeValue(input.GetName()), "repo_id": input.GetID(), - "repo": util.EscapeValue(input.GetFullName()), - }).Debug("attempting to update repo") + }).Debug("platform admin: attempting to update repo") // send API call to update the repo r, err := database.FromContext(c).UpdateRepo(ctx, input) @@ -92,10 +85,11 @@ func UpdateRepo(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ + "org": r.GetOrg(), + "repo": r.GetName(), "repo_id": r.GetID(), - "repo": r.GetFullName(), - }).Info("repo updated") + }).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 106040625..f287e4227 100644 --- a/api/admin/secret.go +++ b/api/admin/secret.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" "github.com/go-vela/types/library" ) @@ -52,17 +51,10 @@ import ( // UpdateSecret represents the API handler to update a secret. func UpdateSecret(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - u := user.Retrieve(c) - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logrus.Debug("platform admin: updating secret") + l.Debug("platform admin: updating secret") // capture body from API request input := new(library.Secret) @@ -76,14 +68,14 @@ func UpdateSecret(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ - "secret_id": input.GetID(), - "org": util.EscapeValue(input.GetOrg()), - "repo": util.EscapeValue(input.GetRepo()), - "type": util.EscapeValue(input.GetType()), - "name": util.EscapeValue(input.GetName()), - "team": util.EscapeValue(input.GetTeam()), - }).Debug("attempting to update secret") + 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) @@ -95,14 +87,14 @@ func UpdateSecret(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ - "secret_id": s.GetID(), - "org": s.GetOrg(), - "repo": s.GetRepo(), - "type": s.GetType(), - "name": s.GetName(), - "team": s.GetTeam(), - }).Info("secret updated") + 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 1ad721f47..13d33d199 100644 --- a/api/admin/service.go +++ b/api/admin/service.go @@ -11,7 +11,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/library" ) @@ -54,17 +53,10 @@ import ( // UpdateService represents the API handler to update a service. func UpdateService(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - u := user.Retrieve(c) - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logrus.Debug("platform admin: updating service") + l.Debug("platform admin: updating service") // capture body from API request input := new(library.Service) @@ -78,10 +70,10 @@ func UpdateService(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "service_id": input.GetID(), "service": util.EscapeValue(input.GetName()), - }).Debug("attempting to update service") + }).Debug("platform admin: attempting to update service") // send API call to update the service s, err := database.FromContext(c).UpdateService(ctx, input) @@ -93,10 +85,10 @@ func UpdateService(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "service_id": s.GetID(), "service": s.GetName(), - }).Info("updated service") + }).Info("platform admin: updated service") c.JSON(http.StatusOK, s) } diff --git a/api/admin/settings.go b/api/admin/settings.go index 305b494be..dfb8cb338 100644 --- a/api/admin/settings.go +++ b/api/admin/settings.go @@ -47,7 +47,9 @@ import ( // GetSettings represents the API handler to get platform settings. func GetSettings(c *gin.Context) { - logrus.Debug("platform admin: reading platform settings") + l := c.MustGet("logger").(*logrus.Entry) + + l.Debug("platform admin: reading platform settings") // capture middleware values s := sMiddleware.FromContext(c) @@ -107,13 +109,14 @@ func GetSettings(c *gin.Context) { // UpdateSettings represents the API handler to update the // platform settings singleton. func UpdateSettings(c *gin.Context) { - logrus.Debug("platform admin: updating platform settings") - // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) s := sMiddleware.FromContext(c) u := uMiddleware.FromContext(c) ctx := c.Request.Context() + 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 { @@ -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()) @@ -229,7 +244,9 @@ func UpdateSettings(c *gin.Context) { // RestoreSettings represents the API handler to // restore platform settings to the environment defaults. func RestoreSettings(c *gin.Context) { - logrus.Debug("platform admin: restoring platform settings") + l := c.MustGet("logger").(*logrus.Entry) + + l.Debug("platform admin: restoring platform settings") // capture middleware values ctx := c.Request.Context() diff --git a/api/admin/step.go b/api/admin/step.go index 52b9ee6ab..b29fc57e0 100644 --- a/api/admin/step.go +++ b/api/admin/step.go @@ -11,7 +11,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/library" ) @@ -53,17 +52,10 @@ import ( // UpdateStep represents the API handler to update a step. func UpdateStep(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - u := user.Retrieve(c) - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logrus.Debug("platform admin: updating step") + l.Debug("platform admin: updating step") // capture body from API request input := new(library.Step) @@ -77,10 +69,10 @@ func UpdateStep(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "step_id": input.GetID(), "step": util.EscapeValue(input.GetName()), - }).Debug("attempting to update step") + }).Debug("platform admin: attempting to update step") // send API call to update the step s, err := database.FromContext(c).UpdateStep(ctx, input) @@ -92,10 +84,10 @@ func UpdateStep(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "step_id": s.GetID(), "step": s.GetName(), - }).Info("updated step") + }).Info("platform admin: updated step") c.JSON(http.StatusOK, s) } diff --git a/api/admin/user.go b/api/admin/user.go index 96d75c018..3f6d1b1ca 100644 --- a/api/admin/user.go +++ b/api/admin/user.go @@ -12,7 +12,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" ) @@ -53,17 +52,10 @@ import ( // UpdateUser represents the API handler to update a user. func UpdateUser(c *gin.Context) { // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - u := user.Retrieve(c) - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logrus.Debug("platform admin: updating user") + l.Debug("platform admin: updating user") // capture body from API request input := new(types.User) @@ -77,10 +69,10 @@ func UpdateUser(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "target_user_id": input.GetID(), "target_user": util.EscapeValue(input.GetName()), - }).Debug("attempting to update user") + }).Debug("platform admin: attempting to update user") // send API call to update the user tu, err := database.FromContext(c).UpdateUser(ctx, input) @@ -92,10 +84,10 @@ func UpdateUser(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "target_user_id": tu.GetID(), "target_user": tu.GetName(), - }).Info("updated user") + }).Info("platform admin: updated user") c.JSON(http.StatusOK, tu) } diff --git a/api/admin/worker.go b/api/admin/worker.go index df1ac8f85..59ed71a5e 100644 --- a/api/admin/worker.go +++ b/api/admin/worker.go @@ -47,6 +47,8 @@ import ( // RegisterToken represents the API handler to // generate a registration token for onboarding a worker. func RegisterToken(c *gin.Context) { + l := c.MustGet("logger").(*logrus.Entry) + // capture middleware values host := util.PathParameter(c, "worker") @@ -57,7 +59,7 @@ func RegisterToken(c *gin.Context) { TokenDuration: tm.WorkerRegisterTokenDuration, } - logrus.Debug("platform admin: generating worker registration token") + l.Debug("platform admin: generating worker registration token") rt, err := tm.MintToken(rmto) if err != nil { @@ -68,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 5c375e77e..07df70b0c 100644 --- a/api/auth/get_token.go +++ b/api/auth/get_token.go @@ -62,13 +62,9 @@ import ( func GetAuthToken(c *gin.Context) { var err error - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - }) - - 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 @@ -138,7 +134,7 @@ func GetAuthToken(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "user": ur.GetName(), "user_id": ur.GetID(), }).Info("new user created") @@ -176,10 +172,10 @@ func GetAuthToken(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "user": ur.GetName(), "user_id": ur.GetID(), - }).Info("user updated") + }).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..c4de66530 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.Debug("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.Debugf("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.Debugf("cli login request, setting redirect to: %s", r) } } diff --git a/api/auth/logout.go b/api/auth/logout.go index 0759f0c8d..9acb575cd 100644 --- a/api/auth/logout.go +++ b/api/auth/logout.go @@ -45,27 +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(), - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - }) - - logger.Debugf("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 @@ -79,7 +71,7 @@ func Logout(c *gin.Context) { u.SetRefreshToken("") // send API call to update the user in the database - ur, err := database.FromContext(c).UpdateUser(ctx, u) + _, err = database.FromContext(c).UpdateUser(ctx, u) if err != nil { retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err) @@ -88,10 +80,7 @@ func Logout(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ - "user": ur.GetName(), - "user_id": ur.GetID(), - }).Info("updated user") + 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 81176118a..1a5ea195a 100644 --- a/api/auth/post_token.go +++ b/api/auth/post_token.go @@ -49,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 @@ -61,19 +62,22 @@ func PostAuthToken(c *gin.Context) { return } - logrus.Debug("user authenticated via SCM using PAT") + l.Debugf("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 } - logrus.Infof("user %s successfully authenticated via SCM PAT", u.GetName()) + 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 @@ -93,8 +97,6 @@ func PostAuthToken(c *gin.Context) { util.HandleError(c, http.StatusServiceUnavailable, retErr) } - logrus.Debugf("new access token created for user %s via SCM PAT", u.GetName()) - - // 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 943bc310d..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.Debug("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..110828798 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.Debug("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..231253a13 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.Debug("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..a341e896a 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.Debug("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 ff1cb2463..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(), - }).Debugf("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 f80f73b13..b75994216 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,30 +71,18 @@ 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{ - "ip": util.EscapeValue(c.ClientIP()), - "build": b.GetNumber(), - "build_id": b.GetID(), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) - - logger.Debugf("approving build %d", b.GetID()) + l.Debugf("approving build %d", b.GetID()) // verify build is in correct status if !strings.EqualFold(b.GetStatus(), constants.StatusPendingApproval) { retErr := fmt.Errorf("unable to approve build %s/%d: build not in pending approval state", r.GetFullName(), b.GetNumber()) - util.HandleError(c, http.StatusBadRequest, retErr) + util.HandleError(c, http.StatusNotModified, retErr) return } @@ -116,14 +103,10 @@ func ApproveBuild(c *gin.Context) { // update the build in the db _, err := database.FromContext(c).UpdateBuild(ctx, b) if err != nil { - logger.Errorf("failed to update build during publish to queue: %v", err) + l.Errorf("failed to update build during publish to queue: %v", err) } - logger.WithFields(logrus.Fields{ - "build": b.GetNumber(), - "build_id": b.GetID(), - "repo": r.GetFullName(), - }).Info("build updated - user approved build execution") + l.Info("build updated - user approved build execution") // publish the build to the queue go Enqueue( diff --git a/api/build/auto_cancel.go b/api/build/auto_cancel.go index c10cbda3a..d1ba091fe 100644 --- a/api/build/auto_cancel.go +++ b/api/build/auto_cancel.go @@ -17,7 +17,6 @@ import ( "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/internal/token" - "github.com/go-vela/server/util" "github.com/go-vela/types/constants" "github.com/go-vela/types/pipeline" ) @@ -25,14 +24,16 @@ 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) { - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), + 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(), }) - logger.Debug("checking if builds should be auto canceled") + l.Debug("checking if builds should be auto canceled") // if build is the current build, continue if rB.GetID() == b.GetID() { @@ -55,7 +56,7 @@ func AutoCancel(c *gin.Context, b *types.Build, rB *types.Build, cancelOpts *pip return false, err } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "build": rB.GetNumber(), "build_id": rB.GetID(), }).Info("build updated - build canceled") @@ -84,7 +85,7 @@ func AutoCancel(c *gin.Context, b *types.Build, rB *types.Build, cancelOpts *pip return true, err } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "build": rB.GetNumber(), "build_id": rB.GetID(), }).Info("build updated - build canceled") @@ -96,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()) @@ -191,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 8fd877c14..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,29 +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 - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "build": b.GetNumber(), - "build_id": b.GetID(), - "org": o, - "repo": r.GetName(), - "user": user.GetName(), - }) - - logger.Debugf("canceling build %s", entry) + l.Debugf("canceling build %s", entry) switch b.GetStatus() { case constants.StatusRunning: @@ -187,7 +173,7 @@ func CancelBuild(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "build": b.GetNumber(), "build_id": b.GetID(), }).Info("build updated - build canceled") @@ -221,7 +207,7 @@ func CancelBuild(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "build": b.GetNumber(), "build_id": b.GetID(), }).Info("build updated - build canceled") diff --git a/api/build/clean.go b/api/build/clean.go index 848c329d2..be29e5ca1 100644 --- a/api/build/clean.go +++ b/api/build/clean.go @@ -22,7 +22,9 @@ func CleanBuild(ctx context.Context, database database.Interface, b *types.Build logger := logrus.WithFields(logrus.Fields{ "build": b.GetNumber(), "build_id": b.GetID(), - "repo": b.GetRepo().GetFullName(), + "org": b.GetRepo().GetOrg(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), }) logger.Debug("cleaning build") @@ -35,7 +37,7 @@ 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) + logger.Errorf("unable to kill build %d: %v", b.GetNumber(), err) } logger.Info("build updated - build cleaned") @@ -48,7 +50,7 @@ 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) + logger.Errorf("unable to kill service %s for build %d: %v", s.GetName(), b.GetNumber(), err) } logger.WithFields(logrus.Fields{ @@ -65,7 +67,7 @@ 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) + logger.Errorf("unable to kill step %s for build %d: %v", s.GetName(), b.GetNumber(), err) } logger.WithFields(logrus.Fields{ diff --git a/api/build/compile_publish.go b/api/build/compile_publish.go index e246ecbcf..a4e458c7a 100644 --- a/api/build/compile_publish.go +++ b/api/build/compile_publish.go @@ -50,7 +50,9 @@ func CompileAndPublish( queue queue.Service, ) (*pipeline.Build, *models.Item, int, error) { logger := logrus.WithFields(logrus.Fields{ - "repo": cfg.Build.GetRepo().GetFullName(), + "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(), }) @@ -124,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() { @@ -192,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 @@ -220,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 @@ -273,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) } @@ -295,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, @@ -320,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 @@ -329,9 +331,11 @@ func CompileAndPublish( return nil, nil, http.StatusInternalServerError, retErr } - logrus.WithFields(logrus.Fields{ + logger.WithFields(logrus.Fields{ "pipeline": pipeline.GetID(), - "repo": repo.GetFullName(), + "org": repo.GetOrg(), + "repo": repo.GetName(), + "repo_id": repo.GetID(), }).Info("pipeline created") } @@ -350,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("") @@ -377,7 +381,8 @@ func CompileAndPublish( } logger.WithFields(logrus.Fields{ - "repo": repo.GetFullName(), + "org": repo.GetOrg(), + "repo": repo.GetName(), "repo_id": repo.GetID(), }).Info("repo updated - counter incremented") diff --git a/api/build/create.go b/api/build/create.go index b43652f8e..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.Debugf("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 32d8510e8..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(), - }).Debugf("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 2b0a229e9..5fc5deb63 100644 --- a/api/build/enqueue.go +++ b/api/build/enqueue.go @@ -19,7 +19,9 @@ func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, it logger := logrus.WithFields(logrus.Fields{ "build": item.Build.GetNumber(), "build_id": item.Build.GetID(), - "repo": item.Build.GetRepo().GetFullName(), + "org": item.Build.GetRepo().GetOrg(), + "repo": item.Build.GetRepo().GetName(), + "repo_id": item.Build.GetRepo().GetID(), }) logger.Debug("converting queue item to json") diff --git a/api/build/executable.go b/api/build/executable.go index ad9502743..0ea31bef9 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, - }).Debugf("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()) diff --git a/api/build/get.go b/api/build/get.go index b10612ce4..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(), - }).Debugf("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 e05685692..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(), - }).Debugf("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 91bdaf26a..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,27 +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.Debugf("constructing graph for build %s and retrieving pipeline configuration", entry) + l.Debugf("constructing graph for build %s and retrieving pipeline configuration", entry) var config []byte @@ -208,7 +198,7 @@ func GetBuildGraph(c *gin.Context) { } } - logger.Debug("compiling pipeline configuration") + l.Debug("compiling pipeline configuration") // parse and compile the pipeline configuration file p, _, err := compiler.FromContext(c). @@ -224,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) @@ -236,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) @@ -329,7 +319,7 @@ func GetBuildGraph(c *gin.Context) { return } - logger.Debug("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 1f86af318..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(), - }).Debugf("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 ed8da2c5c..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(), - }).Debugf("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 fc368c4f7..fcdcd2dff 100644 --- a/api/build/plan.go +++ b/api/build/plan.go @@ -7,13 +7,14 @@ 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" "github.com/go-vela/server/database" "github.com/go-vela/server/scm" "github.com/go-vela/types/pipeline" - "github.com/sirupsen/logrus" ) // PlanBuild is a helper function to plan the build for diff --git a/api/build/restart.go b/api/build/restart.go index 63424bf1c..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,17 +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(), - }) - - logger.Debugf("restarting build %d", b.GetNumber()) + 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) { @@ -133,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{ @@ -159,6 +148,11 @@ func RestartBuild(c *gin.Context) { 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 e43f0bda6..3ff2493af 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, - }).Debugf("generating build token for build %s/%d", r.GetFullName(), b.GetNumber()) + l.Debugf("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 a50e59675..a79ecd5b6 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.Logger) 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, - }).Debugf("updating build %s", entry) + l.Debugf("updating build %s", entry) // capture body from API request input := new(types.Build) @@ -181,22 +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() - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), + l = l.WithFields(logrus.Fields{ "build": b.GetNumber(), "build_id": b.GetID(), - "repo": b.GetRepo().GetFullName(), + "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 @@ -230,10 +223,10 @@ func UpdateComponentStatuses(c *gin.Context, b *types.Build, status string) erro return err } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "step": step.GetNumber(), "step_id": step.GetID(), - }).Infof("step updated") + }).Infof("step status updated") } // retrieve the services for the build from the service table @@ -268,10 +261,10 @@ func UpdateComponentStatuses(c *gin.Context, b *types.Build, status string) erro return err } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "service": service.GetNumber(), "service_id": service.GetID(), - }).Info("service updated") + }).Info("service status updated") } return nil diff --git a/api/dashboard/create.go b/api/dashboard/create.go index 70e4f25c4..104a59074 100644 --- a/api/dashboard/create.go +++ b/api/dashboard/create.go @@ -56,18 +56,9 @@ import ( // create a dashboard. func CreateDashboard(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 - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) - // capture body from API request input := new(types.Dashboard) @@ -87,7 +78,7 @@ func CreateDashboard(c *gin.Context) { return } - logger.Debugf("creating new dashboard %s", input.GetName()) + l.Debugf("creating new dashboard %s", input.GetName()) d := new(types.Dashboard) @@ -128,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())) @@ -141,7 +137,7 @@ func CreateDashboard(c *gin.Context) { return } - logger.Infof("user updated with new dashboard %s", d.GetName()) + 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 c213f3816..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(), - }).Debugf("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 46757eb1b..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(), - }).Debugf("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 bdcbb7cea..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(), - }).Debugf("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 8a05b0cb4..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(), - }).Debugf("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 53cf49c09..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(), - }).Debugf("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 930169ad1..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(), - }).Debugf("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 927ab981f..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(), - }).Debugf("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 bacd14af5..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(), - }).Debugf("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 9b53e15a9..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(), - }).Debugf("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 7e2daf698..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(), - }).Debugf("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 c1796fc0a..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(), - }).Debugf("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 606bc1efd..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(), - }).Debugf("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 6fae1b2bd..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(), - }).Debugf("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 bcd2e547c..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(), - }).Debugf("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 ac1ee93e8..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(), - }).Debugf("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 411a6fba1..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(), - }).Debugf("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 c754a098c..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(), - }).Debugf("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 62d400eb9..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(), - }).Debugf("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 305fc74cd..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(), - }).Debugf("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 f0f0f7d06..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(), - }).Debugf("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 658716970..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(), - }).Debugf("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 436edabc9..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(), - }).Debugf("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 a054dc484..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(), - }).Debugf("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 3835b7ff7..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.Debugf("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 b0764123d..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(), - }).Debugf("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 aa0446eaf..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(), - }).Debugf("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 133ddec65..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(), - }).Debugf("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 8befcd8f1..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(), - }).Debugf("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 20221aea9..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(), - }).Debugf("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 b473d66c7..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(), - }).Debugf("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 f3150b818..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(), - }).Debugf("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 b8d7b73e2..2c5698bb6 100644 --- a/api/queue/queue.go +++ b/api/queue/queue.go @@ -8,8 +8,6 @@ import ( "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" - "github.com/go-vela/server/router/middleware/claims" - "github.com/go-vela/server/util" "github.com/go-vela/types/library" ) @@ -35,13 +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{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "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 5b3a2ee91..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,25 +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 - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "org": o, - "repo": r.GetName(), - "repo_id": r.GetID(), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logger.Debugf("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) @@ -95,7 +81,7 @@ func ChownRepo(c *gin.Context) { return } - logger.Infof("updated repo - changed owner to %s", u.GetName()) + 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 477e5d7a4..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(), - }).Debugf("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) @@ -300,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) @@ -310,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 @@ -325,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 1f8a7c6ab..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(), - }).Debugf("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 312748389..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(), - }).Debugf("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 8df8a2e0f..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(), - }).Debugf("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 b9995ae57..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(), - }).Debugf("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 4cd26cb47..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,27 +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 - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "org": o, - "repo": r.GetName(), - "repo_id": r.GetID(), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logger.Debugf("repairing repo %s", r.GetFullName()) + l.Debugf("repairing repo %s", r.GetFullName()) // check if we should create the webhook if c.Value("webhookvalidation").(bool) { @@ -138,7 +123,7 @@ func RepairRepo(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "hook": hook.GetID(), }).Info("new webhook created") } @@ -191,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 ba4b5f1c0..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(), - }).Debugf("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(), - }).Debugf("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 5a11dc9fc..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(), - }).Debugf("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 53c5b8f0c..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(), - }).Debugf("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 d4b49a2eb..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(), - }).Debugf("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 ba6074729..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(), - }).Debugf("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 62b4131be..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(), - }).Debugf("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 e04dc98fc..66522510a 100644 --- a/api/scm/sync.go +++ b/api/scm/sync.go @@ -73,23 +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{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "org": o, - "repo": r.GetName(), - "user": u.GetName(), - }) - - logger.Debugf("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) @@ -109,7 +99,7 @@ func SyncRepo(c *gin.Context) { return } - logger.Infof("repo %s has been updated - set to inactive", r.GetFullName()) + 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) @@ -128,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") { @@ -169,7 +159,7 @@ func SyncRepo(c *gin.Context) { return } - logger.Infof("repo %s has been updated - set to inactive", r.GetFullName()) + l.Infof("repo %s has been updated - set to inactive", r.GetFullName()) c.JSON(http.StatusOK, r) diff --git a/api/scm/sync_org.go b/api/scm/sync_org.go index 631d9c0c4..e8efee734 100644 --- a/api/scm/sync_org.go +++ b/api/scm/sync_org.go @@ -69,26 +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{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "org": o, - "user": u.GetName(), - }) - - logger.Debugf("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 @@ -145,7 +136,7 @@ func SyncReposForOrg(c *gin.Context) { return } - logger.Infof("repo %s has been updated - set to inactive", repo.GetFullName()) + l.Infof("repo %s has been updated - set to inactive", repo.GetFullName()) results = append(results, repo) } else { @@ -187,7 +178,7 @@ func SyncReposForOrg(c *gin.Context) { return } - logger.Infof("repo %s has been updated - set to inactive", repo.GetFullName()) + l.Infof("repo %s has been updated - set to inactive", repo.GetFullName()) results = append(results, repo) diff --git a/api/secret/create.go b/api/secret/create.go index 0ac6342db..ecae46ff5 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(), - } + 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).Debugf("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,14 @@ func CreateSecret(c *gin.Context) { return } + l.WithFields(logrus.Fields{ + "secret_id": s.GetID(), + "secret_name": s.GetName(), + "secret_org": s.GetOrg(), + "secret_repo": s.GetRepo(), + "secret_type": s.GetType(), + "secret_team": s.GetTeam(), + }).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 ff470bf3d..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).Debugf("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 1912749c4..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).Debugf("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 718731de0..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).Debugf("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 b1a52a176..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).Debugf("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 038ef1568..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(), - }).Debugf("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 8c02fa9e5..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(), - }).Debugf("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 4fcd1f2e7..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(), - }).Debugf("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 13e68e65b..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(), - }).Debugf("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 abc26687f..bc8408ddb 100644 --- a/api/service/plan.go +++ b/api/service/plan.go @@ -7,12 +7,13 @@ 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" "github.com/go-vela/types/library" "github.com/go-vela/types/pipeline" - "github.com/sirupsen/logrus" ) // PlanServices is a helper function to plan all services @@ -43,7 +44,9 @@ func PlanServices(ctx context.Context, database database.Interface, p *pipeline. logrus.WithFields(logrus.Fields{ "service": s.GetName(), "service_id": s.GetID(), - "repo": b.GetRepo().GetFullName(), + "org": b.GetRepo().GetOrg(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), }).Info("service created") // populate environment variables from service library @@ -70,8 +73,10 @@ func PlanServices(ctx context.Context, database database.Interface, p *pipeline. logrus.WithFields(logrus.Fields{ "service": s.GetName(), "service_id": s.GetID(), - "log_id": l.GetID(), - "repo": b.GetRepo().GetFullName(), + "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") } diff --git a/api/service/update.go b/api/service/update.go index c1c5b66aa..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(), - }).Debugf("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 d56c7aaf1..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(), - }).Debugf("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 1172bc1a0..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(), - }).Debugf("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 e54392813..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(), - }).Debugf("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 124f21665..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(), - }).Debugf("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 7727fab20..cfb55652e 100644 --- a/api/step/plan.go +++ b/api/step/plan.go @@ -73,7 +73,9 @@ func planStep(ctx context.Context, database database.Interface, scm scm.Service, logrus.WithFields(logrus.Fields{ "step": s.GetName(), "step_id": s.GetID(), - "repo": b.GetRepo().GetFullName(), + "org": b.GetRepo().GetOrg(), + "repo": b.GetRepo().GetName(), + "repo_id": b.GetRepo().GetID(), }).Info("step created") // populate environment variables from step library @@ -100,8 +102,10 @@ func planStep(ctx context.Context, database database.Interface, scm scm.Service, logrus.WithFields(logrus.Fields{ "step": s.GetName(), "step_id": s.GetID(), - "log_id": l.GetID(), - "repo": b.GetRepo().GetFullName(), + "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 { diff --git a/api/step/update.go b/api/step/update.go index 58984c60d..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(), - }).Debugf("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 3dc5d7f16..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,7 +71,7 @@ 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(), }).Debugf("creating new user %s", input.GetName()) @@ -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 00835f55d..2eb0c9028 100644 --- a/api/user/create_token.go +++ b/api/user/create_token.go @@ -44,20 +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 - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logger.Debugf("composing token for user %s", u.GetName()) + l.Debugf("composing token for user %s", u.GetName()) tm := c.MustGet("token-manager").(*token.Manager) @@ -83,7 +74,7 @@ func CreateToken(c *gin.Context) { return } - logger.Info("user updated - token created") + 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 a476200a2..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(), - }).Debugf("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 1a602c43f..8ee9f27da 100644 --- a/api/user/delete_token.go +++ b/api/user/delete_token.go @@ -44,20 +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 - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "user": u.GetName(), - "user_id": u.GetID(), - }) - - logger.Debugf("revoking token for user %s", u.GetName()) + l.Debugf("revoking token for user %s", u.GetName()) tm := c.MustGet("token-manager").(*token.Manager) @@ -83,7 +74,7 @@ func DeleteToken(c *gin.Context) { return } - logger.Info("user updated - token rotated") + 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 befe2a4aa..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(), - }).Debugf("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 c709e9738..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(), - }).Debugf("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 0596700d8..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(), - }).Debugf("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 80f16e3ff..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(), - }).Debug("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 8f0477312..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(), - }).Debugf("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 a3b1f1731..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(), - }).Debugf("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 86a15ca0f..9351f50a7 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -84,13 +84,10 @@ var baseErr = "unable to process webhook" func PostWebhook(c *gin.Context) { // capture middleware values m := c.MustGet("metadata").(*internal.Metadata) + l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - }) - - logger.Debug("webhook received") + l.Info("webhook received") // duplicate request so we can perform operations on the request body // @@ -146,8 +143,8 @@ func PostWebhook(c *gin.Context) { h, r, b := webhook.Hook, webhook.Repo, webhook.Build - logger.Debugf("hook generated from SCM: %v", h) - logger.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) { @@ -177,7 +174,7 @@ func PostWebhook(c *gin.Context) { return } - logger.Debugf(`build author: %s, + l.Debugf(`build author: %s, build branch: %s, build commit: %s, build ref: %s`, @@ -195,13 +192,15 @@ func PostWebhook(c *gin.Context) { // send API call to update the webhook _, err = database.FromContext(c).UpdateHook(ctx, h) if err != nil { - logger.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) } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "hook": h.GetNumber(), "hook_id": h.GetID(), - "repo": r.GetFullName(), + "org": r.GetOrg(), + "repo": r.GetName(), + "repo_id": r.GetID(), }).Info("hook updated in database") }() @@ -271,11 +270,12 @@ func PostWebhook(c *gin.Context) { return } - logrus.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "hook": h.GetNumber(), "hook_id": h.GetID(), - "repo": repo.GetFullName(), - }).Info("hook created in database") + "org": repo.GetOrg(), + "repo": repo.GetName(), + }).Info("hook created") // verify the webhook from the source control provider if c.Value("webhookvalidation").(bool) { @@ -398,10 +398,12 @@ func PostWebhook(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "deployment": dr.GetNumber(), "deployment_id": dr.GetID(), - "repo": repo.GetFullName(), + "org": repo.GetOrg(), + "repo": repo.GetName(), + "repo_id": repo.GetID(), }).Info("deployment created in database") } else { retErr := fmt.Errorf("%s: failed to get deployment %s/%d: %w", baseErr, repo.GetFullName(), webhook.Deployment.GetNumber(), err) @@ -428,10 +430,12 @@ func PostWebhook(c *gin.Context) { return } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "deployment": d.GetNumber(), "deployment_id": d.GetID(), - "repo": repo.GetFullName(), + "org": repo.GetOrg(), + "repo": repo.GetName(), + "repo_id": repo.GetID(), }).Info("deployment updated in database") } } @@ -444,27 +448,43 @@ func PostWebhook(c *gin.Context) { // fetch pending and running builds rBs, err := database.FromContext(c).ListPendingAndRunningBuildsForRepo(c, repo) if err != nil { - logger.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 { - logger.Errorf("unable to update canceled build error message: %v", err) + l.Errorf("unable to update canceled build error message: %v", err) } else { - logger.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 { - logger.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: @@ -486,7 +506,7 @@ func PostWebhook(c *gin.Context) { return } - logger.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 // @@ -510,14 +530,14 @@ func PostWebhook(c *gin.Context) { case constants.ApproveNever: fallthrough default: - logger.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 { - logger.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 @@ -533,24 +553,27 @@ 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) { - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), + l := c.MustGet("logger").(*logrus.Entry) + + l = l.WithFields(logrus.Fields{ "event_type": h.GetEvent(), }) - logrus.Debugf("webhook is repository event, making necessary updates to repo %s", r.GetFullName()) + l.Debugf("webhook is repository event, making necessary updates to repo %s", r.GetFullName()) defer func() { // send API call to update the webhook hr, err := database.FromContext(c).CreateHook(ctx, h) if err != nil { - logger.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) } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "hook": hr.GetNumber(), "hook_id": hr.GetID(), - "repo": r.GetFullName(), + "org": r.GetOrg(), + "repo": r.GetName(), + "repo_id": r.GetID(), }).Info("hook created in database") }() @@ -568,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: - logger.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 { @@ -624,9 +647,10 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *internal.Meta return nil, err } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ + "org": dbRepo.GetOrg(), + "repo": dbRepo.GetName(), "repo_id": dbRepo.GetID(), - "repo": dbRepo.GetFullName(), }).Info("repo updated in database") return dbRepo, nil @@ -641,12 +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) { - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), + l := c.MustGet("logger").(*logrus.Entry) + + l = l.WithFields(logrus.Fields{ "event_type": h.GetEvent(), }) - logger.Debugf("renaming repository from %s to %s", r.GetPreviousName(), r.GetName()) + 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()) @@ -712,7 +737,7 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi return nil, fmt.Errorf("unable to update secret for repo %s/%s: %w", dbR.GetOrg(), dbR.GetName(), err) } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "secret_id": secret.GetID(), "repo": secret.GetRepo(), "org": secret.GetOrg(), @@ -750,10 +775,12 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi return nil, fmt.Errorf("unable to update build for repo %s: %w", dbR.GetFullName(), err) } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "build_id": build.GetID(), "build": build.GetNumber(), - "repo": dbR.GetFullName(), + "org": dbR.GetOrg(), + "repo": dbR.GetName(), + "repo_id": dbR.GetID(), }).Info("build updated in database") } @@ -777,9 +804,10 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi return nil, retErr } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ + "org": dbR.GetOrg(), + "repo": dbR.GetName(), "repo_id": dbR.GetID(), - "repo": dbR.GetFullName(), }).Infof("repo updated in database (previous name: %s)", r.GetPreviousName()) return dbR, nil @@ -788,14 +816,17 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi // 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 { - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "repo": r.GetFullName(), + 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(), }) - logger.Debug("fork PR build waiting for approval") + l.Debug("fork PR build waiting for approval") b.SetStatus(constants.StatusPendingApproval) @@ -804,7 +835,7 @@ 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) } - logger.Info("build updated in database") + l.Info("build updated in database") // update the build components to pending approval status err = build.UpdateComponentStatuses(c, b, constants.StatusPendingApproval) @@ -815,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 { - logger.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 d3372bd11..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(), - }).Debugf("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 e3412f222..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(), - }).Debugf("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 e448d8522..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(), - }).Debugf("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 313ebd5f4..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(), - }).Debug("reading workers") + l.Debug("reading workers") active := c.Query("active") diff --git a/api/worker/refresh.go b/api/worker/refresh.go index d0048653b..51098efe2 100644 --- a/api/worker/refresh.go +++ b/api/worker/refresh.go @@ -61,23 +61,16 @@ 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() - logger := logrus.WithFields(logrus.Fields{ - "ip": util.EscapeValue(c.ClientIP()), - "path": util.EscapeValue(c.Request.URL.Path), - "claims_subject": cl.Subject, - "worker": w.GetHostname(), - "worker_id": w.GetID(), - }) - // if we are not using a symmetric token, and the subject does not match the input, request should be denied 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()) - logger.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) @@ -97,9 +90,9 @@ func Refresh(c *gin.Context) { return } - logger.Info("worker updated - check-in time updated") + l.Info("worker updated - check-in time updated") - logger.Debugf("refreshing worker %s authentication", w.GetHostname()) + l.Debugf("refreshing worker %s authentication", w.GetHostname()) switch cl.TokenType { // if symmetric token configured, send back symmetric token @@ -107,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 3f8555eae..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(), - }).Debugf("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/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/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/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/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/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/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..6c1236a43 --- /dev/null +++ b/router/middleware/hook/hook_test.go @@ -0,0 +1,296 @@ +// SPDX-License-Identifier: Apache-2.0 + +package hook + +import ( + "context" + "net/http" + "net/http/httptest" + "reflect" + "testing" + + "github.com/gin-gonic/gin" + + 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) { 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) { 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) { 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) { 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) { 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 91f5e2fc5..5b8be89c9 100644 --- a/router/middleware/logger.go +++ b/router/middleware/logger.go @@ -10,9 +10,12 @@ import ( "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" @@ -48,6 +51,17 @@ func Logger(logger *logrus.Logger, timeFormat string) gin.HandlerFunc { // some evil middlewares modify this values path := util.EscapeValue(c.Request.URL.Path) + 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) + c.Next() latency := time.Since(start) @@ -98,12 +112,30 @@ func Logger(logger *logrus.Logger, timeFormat string) gin.HandlerFunc { 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) // we check to make sure user name is populated // because when it's not a user token, we still @@ -215,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/perm/perm.go b/router/middleware/perm/perm.go index e0f11979d..c049d0f4e 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) r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() + l := c.MustGet("logger").(*logrus.Entry) - // 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/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/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/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/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/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/worker/worker.go b/router/middleware/worker/worker.go index e643949c3..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() } From 0a77bbdded37f27496339a4227913feadf273571 Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Sun, 16 Jun 2024 17:43:05 -0500 Subject: [PATCH 14/26] fix swagger annotations --- router/admin.go | 1 - router/dashboard.go | 11 +++++++---- router/hook.go | 13 +++++++++---- router/middleware/header.go | 2 ++ router/queue.go | 2 +- 5 files changed, 19 insertions(+), 10 deletions(-) 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/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/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") From 9e8c99661716d4bfa4edfea309534bfb1a2ba1be Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:24:12 -0500 Subject: [PATCH 15/26] fix tests --- router/middleware/build/build_test.go | 6 ++++ router/middleware/claims/claims_test.go | 4 +++ router/middleware/dashboard/dashboard_test.go | 4 +++ router/middleware/hook/hook_test.go | 6 ++++ router/middleware/org/org_test.go | 3 ++ router/middleware/perm/perm_test.go | 30 +++++++++++++++++++ router/middleware/pipeline/pipeline_test.go | 5 ++++ router/middleware/repo/repo_test.go | 5 ++++ router/middleware/service/service_test.go | 7 +++++ router/middleware/step/step_test.go | 7 +++++ router/middleware/user/user_test.go | 6 ++++ router/middleware/worker/worker_test.go | 3 ++ 12 files changed, 86 insertions(+) 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_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_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/hook/hook_test.go b/router/middleware/hook/hook_test.go index 6c1236a43..fa3deef79 100644 --- a/router/middleware/hook/hook_test.go +++ b/router/middleware/hook/hook_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" @@ -93,6 +94,7 @@ func TestHook_Establish(t *testing.T) { 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()) @@ -131,6 +133,7 @@ func TestHook_Establish_NoRepo(t *testing.T) { 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) { @@ -180,6 +183,7 @@ func TestHook_Establish_NoHookParameter(t *testing.T) { 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()) @@ -231,6 +235,7 @@ func TestHook_Establish_InvalidHookParameter(t *testing.T) { 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()) @@ -279,6 +284,7 @@ func TestHook_Establish_NoHook(t *testing.T) { 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()) 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_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_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_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/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_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_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_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) { From d5473b0e2a3698ad365da0f1c487080f943fdcd3 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Mon, 17 Jun 2024 10:51:12 -0500 Subject: [PATCH 16/26] revert return status code change accidental commit --- api/build/approve.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/build/approve.go b/api/build/approve.go index b75994216..86c491c04 100644 --- a/api/build/approve.go +++ b/api/build/approve.go @@ -82,7 +82,7 @@ func ApproveBuild(c *gin.Context) { // verify build is in correct status if !strings.EqualFold(b.GetStatus(), constants.StatusPendingApproval) { retErr := fmt.Errorf("unable to approve build %s/%d: build not in pending approval state", r.GetFullName(), b.GetNumber()) - util.HandleError(c, http.StatusNotModified, retErr) + util.HandleError(c, http.StatusBadRequest, retErr) return } From 708f551702abf9b3c9fb630c0b03b4930c223393 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Mon, 17 Jun 2024 11:14:18 -0500 Subject: [PATCH 17/26] linter says it's not similar code --- api/admin/repo.go | 1 - 1 file changed, 1 deletion(-) diff --git a/api/admin/repo.go b/api/admin/repo.go index a5a5729a9..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 with service.go package admin import ( From 53edfd650dbff8ab902ed3490e4fc55bd36ee151 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Tue, 18 Jun 2024 16:32:56 -0500 Subject: [PATCH 18/26] remove redundant wording (feedback) removes "in the/from (the) database" wording from log messages within the database package, they should all have "database": "" as a log field anwyay. also removes it from api package. --- api/admin/clean.go | 8 ++++---- api/build/executable.go | 2 +- api/webhook/post.go | 18 +++++++++--------- 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 +- 169 files changed, 188 insertions(+), 189 deletions(-) diff --git a/api/admin/clean.go b/api/admin/clean.go index 60e352689..da3949568 100644 --- a/api/admin/clean.go +++ b/api/admin/clean.go @@ -104,7 +104,7 @@ func CleanResources(c *gin.Context) { return } - l.Debugf("platform admin: cleaned %d builds in database", builds) + l.Debugf("platform admin: cleaned %d builds", builds) // clean executables executables, err := database.FromContext(c).CleanBuildExecutables(ctx) @@ -116,7 +116,7 @@ func CleanResources(c *gin.Context) { return } - l.Debugf("platform admin: cleaned %d executables in database", executables) + l.Debugf("platform admin: cleaned %d executables", executables) // clean services services, err := database.FromContext(c).CleanServices(ctx, msg, before) @@ -128,7 +128,7 @@ func CleanResources(c *gin.Context) { return } - l.Debugf("platform admin: cleaned %d services in database", services) + l.Debugf("platform admin: cleaned %d services", services) // clean steps steps, err := database.FromContext(c).CleanSteps(ctx, msg, before) @@ -140,7 +140,7 @@ func CleanResources(c *gin.Context) { return } - l.Debugf("platform admin: cleaned %d steps in database", 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/build/executable.go b/api/build/executable.go index 0ea31bef9..19f66c3a6 100644 --- a/api/build/executable.go +++ b/api/build/executable.go @@ -123,7 +123,7 @@ func PublishBuildExecutable(ctx context.Context, db database.Interface, p *pipel logrus.WithFields(logrus.Fields{ "build_executable_id": bExecutable.GetBuildID(), - }).Info("created build executable in the database") + }).Info("created build executable") return nil } diff --git a/api/webhook/post.go b/api/webhook/post.go index 9351f50a7..f4e9f26c7 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -201,7 +201,7 @@ func PostWebhook(c *gin.Context) { "org": r.GetOrg(), "repo": r.GetName(), "repo_id": r.GetID(), - }).Info("hook updated in database") + }).Info("hook updated") }() // send API call to capture parsed repo from webhook @@ -404,7 +404,7 @@ func PostWebhook(c *gin.Context) { "org": repo.GetOrg(), "repo": repo.GetName(), "repo_id": repo.GetID(), - }).Info("deployment created in database") + }).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) @@ -436,7 +436,7 @@ func PostWebhook(c *gin.Context) { "org": repo.GetOrg(), "repo": repo.GetName(), "repo_id": repo.GetID(), - }).Info("deployment updated in database") + }).Info("deployment updated") } } @@ -574,7 +574,7 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *internal.Meta "org": r.GetOrg(), "repo": r.GetName(), "repo_id": r.GetID(), - }).Info("hook created in database") + }).Info("hook created") }() switch h.GetEventAction() { @@ -651,7 +651,7 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *internal.Meta "org": dbRepo.GetOrg(), "repo": dbRepo.GetName(), "repo_id": dbRepo.GetID(), - }).Info("repo updated in database") + }).Info("repo updated") return dbRepo, nil // all other repo event actions are skippable @@ -741,7 +741,7 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi "secret_id": secret.GetID(), "repo": secret.GetRepo(), "org": secret.GetOrg(), - }).Info("secret updated in database") + }).Info("secret updated") } // get total number of builds associated with repository @@ -781,7 +781,7 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gi "org": dbR.GetOrg(), "repo": dbR.GetName(), "repo_id": dbR.GetID(), - }).Info("build updated in database") + }).Info("build updated") } // update the repo name information @@ -795,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) @@ -835,7 +835,7 @@ 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 in database") + l.Info("build updated") // update the build components to pending approval status err = build.UpdateComponentStatuses(c, b, constants.StatusPendingApproval) 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 } From 6038bcde2802c967acd66c2f796412ee28d9f3e9 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Tue, 18 Jun 2024 16:35:18 -0500 Subject: [PATCH 19/26] use %s over %#q (feedback) --- api/build/enqueue.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/build/enqueue.go b/api/build/enqueue.go index 5fc5deb63..ca5a42b54 100644 --- a/api/build/enqueue.go +++ b/api/build/enqueue.go @@ -36,7 +36,7 @@ func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, it return } - logger.Debugf("pushing item for build to queue route %#q", route) + logger.Debugf("pushing item for build to queue route %s", route) // push item on to the queue err = queue.Push(context.Background(), route, byteItem) From 3c55bab42604d78a61a2ff8bf43f0c909b011f66 Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Thu, 20 Jun 2024 00:50:03 -0500 Subject: [PATCH 20/26] fix panic - use Entry, not Logger --- api/build/update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/build/update.go b/api/build/update.go index a79ecd5b6..eabe94981 100644 --- a/api/build/update.go +++ b/api/build/update.go @@ -76,7 +76,7 @@ import ( // a build for a repo. func UpdateBuild(c *gin.Context) { // capture middleware values - l := c.MustGet("logger").(*logrus.Logger) + l := c.MustGet("logger").(*logrus.Entry) b := build.Retrieve(c) r := repo.Retrieve(c) ctx := c.Request.Context() From 2c8afeee31314c8095f1782a3b31f31394464f2d Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Thu, 20 Jun 2024 00:57:42 -0500 Subject: [PATCH 21/26] info -> debug in webhook/post (feedback) --- api/webhook/post.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/webhook/post.go b/api/webhook/post.go index f4e9f26c7..e4ac32c3d 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -87,7 +87,7 @@ func PostWebhook(c *gin.Context) { l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - l.Info("webhook received") + l.Debug("webhook received") // duplicate request so we can perform operations on the request body // From 2508d5f5fd067e9b3510914c27d4322e20ca3e2e Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Thu, 20 Jun 2024 00:58:01 -0500 Subject: [PATCH 22/26] consistent order (feedback) --- api/admin/build.go | 2 +- router/middleware/perm/perm.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/admin/build.go b/api/admin/build.go index 13c8c2101..525179256 100644 --- a/api/admin/build.go +++ b/api/admin/build.go @@ -105,8 +105,8 @@ func AllBuildsQueue(c *gin.Context) { // UpdateBuild represents the API handler to update a build. func UpdateBuild(c *gin.Context) { // capture middleware values - ctx := c.Request.Context() l := c.MustGet("logger").(*logrus.Entry) + ctx := c.Request.Context() l.Debug("platform admin: updating build") diff --git a/router/middleware/perm/perm.go b/router/middleware/perm/perm.go index c049d0f4e..5206ea9a2 100644 --- a/router/middleware/perm/perm.go +++ b/router/middleware/perm/perm.go @@ -347,10 +347,10 @@ func MustSecretAdmin() gin.HandlerFunc { // MustAdmin ensures the user has admin access to the repo. func MustAdmin() 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() - l := c.MustGet("logger").(*logrus.Entry) l.Debugf("verifying user %s has 'admin' permissions for repo %s", u.GetName(), r.GetFullName()) From 76997452b4a41417c00847b7b485fc1d0df3bda9 Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Thu, 20 Jun 2024 01:24:53 -0500 Subject: [PATCH 23/26] secret creation log adjust (feedback) --- api/secret/create.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/api/secret/create.go b/api/secret/create.go index ecae46ff5..6c4c502b1 100644 --- a/api/secret/create.go +++ b/api/secret/create.go @@ -104,7 +104,7 @@ func CreateSecret(c *gin.Context) { // check if secret is a shared secret if strings.EqualFold(t, constants.SecretShared) { - // update log fields from API metadata + // update log fields for shared secret delete(fields, "secret_repo") fields["secret_team"] = n } @@ -255,14 +255,23 @@ func CreateSecret(c *gin.Context) { return } - l.WithFields(logrus.Fields{ + // 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(), - "secret_team": s.GetTeam(), - }).Infof("created secret %s for %s service", entry, e) + } + + // 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()) } From 924390dfb13b70146a4ddcb0fda4abc9bbe88f16 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:05:01 -0500 Subject: [PATCH 24/26] consistent nameing (feedback) --- api/build/clean.go | 16 ++++++++-------- api/build/enqueue.go | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/api/build/clean.go b/api/build/clean.go index be29e5ca1..5c70b5164 100644 --- a/api/build/clean.go +++ b/api/build/clean.go @@ -19,7 +19,7 @@ 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) { - logger := logrus.WithFields(logrus.Fields{ + l := logrus.WithFields(logrus.Fields{ "build": b.GetNumber(), "build_id": b.GetID(), "org": b.GetRepo().GetOrg(), @@ -27,7 +27,7 @@ func CleanBuild(ctx context.Context, database database.Interface, b *types.Build "repo_id": b.GetRepo().GetID(), }) - logger.Debug("cleaning build") + l.Debug("cleaning build") // update fields in build object b.SetError(fmt.Sprintf("unable to publish to queue: %s", e.Error())) @@ -37,10 +37,10 @@ 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 { - logger.Errorf("unable to kill build %d: %v", b.GetNumber(), err) + l.Errorf("unable to kill build %d: %v", b.GetNumber(), err) } - logger.Info("build updated - build cleaned") + l.Info("build updated - build cleaned") for _, s := range services { // update fields in service object @@ -50,10 +50,10 @@ 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 { - logger.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) } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "service": s.GetName(), "service_id": s.GetID(), }).Info("service updated - service cleaned") @@ -67,10 +67,10 @@ 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 { - logger.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) } - logger.WithFields(logrus.Fields{ + l.WithFields(logrus.Fields{ "step": s.GetName(), "step_id": s.GetID(), }).Info("step updated - step cleaned") diff --git a/api/build/enqueue.go b/api/build/enqueue.go index ca5a42b54..c07a5403a 100644 --- a/api/build/enqueue.go +++ b/api/build/enqueue.go @@ -16,7 +16,7 @@ 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) { - logger := logrus.WithFields(logrus.Fields{ + l := logrus.WithFields(logrus.Fields{ "build": item.Build.GetNumber(), "build_id": item.Build.GetID(), "org": item.Build.GetRepo().GetOrg(), @@ -24,11 +24,11 @@ func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, it "repo_id": item.Build.GetRepo().GetID(), }) - logger.Debug("converting queue item to json") + l.Debug("converting queue item to json") byteItem, err := json.Marshal(item) if err != nil { - logger.Errorf("failed to convert item to json: %v", err) + l.Errorf("failed to convert item to json: %v", err) // error out the build CleanBuild(ctx, db, item.Build, nil, nil, err) @@ -36,16 +36,16 @@ func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, it return } - logger.Debugf("pushing item for build to queue route %s", 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 { - logger.Errorf("retrying; failed to publish build: %v", err) + l.Errorf("retrying; failed to publish build: %v", err) err = queue.Push(context.Background(), route, byteItem) if err != nil { - logger.Errorf("failed to publish build: %v", err) + l.Errorf("failed to publish build: %v", err) // error out the build CleanBuild(ctx, db, item.Build, nil, nil, err) @@ -60,8 +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 { - logger.Errorf("failed to update build during publish to queue: %v", err) + l.Errorf("failed to update build during publish to queue: %v", err) } - logger.Info("updated build as enqueued") + l.Info("updated build as enqueued") } From e7d9fd3c04ac7a281aecb058947dfb2703a16daa Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:10:36 -0500 Subject: [PATCH 25/26] reword --- api/build/enqueue.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/build/enqueue.go b/api/build/enqueue.go index c07a5403a..aa87f4473 100644 --- a/api/build/enqueue.go +++ b/api/build/enqueue.go @@ -24,7 +24,7 @@ func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, it "repo_id": item.Build.GetRepo().GetID(), }) - l.Debug("converting queue item to json") + l.Debug("adding item to queue") byteItem, err := json.Marshal(item) if err != nil { From e1c4e37bb01afccec49b65490641faa9917f86ae Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:26:41 -0500 Subject: [PATCH 26/26] debug -> info on auth/token activity --- api/auth/login.go | 6 +++--- api/auth/post_token.go | 2 +- api/auth/refresh.go | 2 +- api/auth/validate.go | 2 +- api/auth/validate_oauth.go | 2 +- api/build/token.go | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/api/auth/login.go b/api/auth/login.go index c4de66530..f2bf5adc5 100644 --- a/api/auth/login.go +++ b/api/auth/login.go @@ -52,20 +52,20 @@ func Login(c *gin.Context) { // default path (headless mode) path := "/authenticate" - l.Debug("logging in user") + l.Info("logging in user") // handle web and cli logins switch t { case "web": r = fmt.Sprintf("%s/authenticate/%s", m.Vela.Address, t) - l.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) - l.Debugf("cli login request, setting redirect to: %s", r) + l.Infof("cli login request, setting redirect to: %s", r) } } diff --git a/api/auth/post_token.go b/api/auth/post_token.go index 1a5ea195a..d5fc345c9 100644 --- a/api/auth/post_token.go +++ b/api/auth/post_token.go @@ -62,7 +62,7 @@ func PostAuthToken(c *gin.Context) { return } - l.Debugf("SCM user %s authenticated using PAT", u.GetName()) + l.Infof("SCM user %s authenticated using PAT", u.GetName()) // check if the user exists u, err = database.FromContext(c).GetUserForName(ctx, u.GetName()) diff --git a/api/auth/refresh.go b/api/auth/refresh.go index 110828798..69d61bb1e 100644 --- a/api/auth/refresh.go +++ b/api/auth/refresh.go @@ -39,7 +39,7 @@ import ( func RefreshAccessToken(c *gin.Context) { l := c.MustGet("logger").(*logrus.Entry) - l.Debug("refreshing access token") + l.Info("refreshing access token") // capture the refresh token // TODO: move this into token package and do it internally diff --git a/api/auth/validate.go b/api/auth/validate.go index 231253a13..40bab2c38 100644 --- a/api/auth/validate.go +++ b/api/auth/validate.go @@ -39,7 +39,7 @@ func ValidateServerToken(c *gin.Context) { l := c.MustGet("logger").(*logrus.Entry) cl := claims.Retrieve(c) - l.Debug("validating server token") + 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 a341e896a..25325a353 100644 --- a/api/auth/validate_oauth.go +++ b/api/auth/validate_oauth.go @@ -44,7 +44,7 @@ func ValidateOAuthToken(c *gin.Context) { l := c.MustGet("logger").(*logrus.Entry) ctx := c.Request.Context() - l.Debug("validating oauth token") + l.Info("validating oauth token") token := c.Request.Header.Get("Token") if len(token) == 0 { diff --git a/api/build/token.go b/api/build/token.go index 3ff2493af..b2126bb84 100644 --- a/api/build/token.go +++ b/api/build/token.go @@ -75,7 +75,7 @@ func GetBuildToken(c *gin.Context) { r := repo.Retrieve(c) cl := claims.Retrieve(c) - l.Debugf("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) {