From 059a9040941dcd11a2c70f11279b563aeb59ac1a Mon Sep 17 00:00:00 2001 From: mariusheine Date: Thu, 14 Sep 2023 08:07:38 +0200 Subject: [PATCH] fix: improve logging (#69) --- plugins/auto_merge/auto_merge.go | 10 +++++++--- .../auto_merge/checks/has_enough_approvals.go | 2 +- plugins/auto_merge/checks/passes_ci.go | 3 ++- plugins/auto_merge/merge_status.go | 8 +++++--- utils/logger.go | 18 ++++++++++++++++++ 5 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 utils/logger.go diff --git a/plugins/auto_merge/auto_merge.go b/plugins/auto_merge/auto_merge.go index ab640ac..f83bec0 100644 --- a/plugins/auto_merge/auto_merge.go +++ b/plugins/auto_merge/auto_merge.go @@ -9,7 +9,7 @@ import ( "github.com/GEPROG/lassie-bot-dog/config" autoMergeConfig "github.com/GEPROG/lassie-bot-dog/plugins/auto_merge/config" - log "github.com/sirupsen/logrus" + "github.com/GEPROG/lassie-bot-dog/utils" "github.com/xanzy/go-gitlab" ) @@ -32,6 +32,7 @@ func (plugin *AutoMergePlugin) Name() string { } func (plugin *AutoMergePlugin) Execute(project *gitlab.Project, config config.ProjectConfig) { + log := utils.Logger(project, nil) err := json.Unmarshal(config.Plugins[plugin.Name()], &plugin.loadedConfig) if err != nil { log.Debug("Can't load config", err) @@ -60,6 +61,7 @@ func (plugin *AutoMergePlugin) Execute(project *gitlab.Project, config config.Pr } func (plugin *AutoMergePlugin) autoMerge(project *gitlab.Project, mergeRequest *gitlab.MergeRequest) { + log := utils.Logger(project, mergeRequest) log.Debug("trying to auto merge >>>", mergeRequest.Title) status := plugin.checkMergeRequest(project, mergeRequest) @@ -90,6 +92,7 @@ func (plugin *AutoMergePlugin) autoMerge(project *gitlab.Project, mergeRequest * } func (plugin *AutoMergePlugin) getUpdatedPipelineMergeRequests(project *gitlab.Project) []*gitlab.MergeRequest { + log := utils.Logger(project, nil) var mergeRequests []*gitlab.MergeRequest lastCheck := plugin.lastestChecks[project.ID] @@ -119,12 +122,12 @@ func (plugin *AutoMergePlugin) getUpdatedPipelineMergeRequests(project *gitlab.P if IsRefMergeRequest(pipeline.Ref) { mergeRequestIID, err := GetMergeRequestIDFromRef(pipeline.Ref) if err != nil { - log.Debug("Can't get merge-request id from ref", err) + log.Debug("Can't get merge-request id of pipeline ", pipeline.ID, " from ref ", pipeline.Ref, ": ", err) continue } mergeRequest, _, err := plugin.Client.MergeRequests.GetMergeRequest(project.ID, mergeRequestIID, &gitlab.GetMergeRequestsOptions{}) if err != nil { - log.Debug("Can't load merge-request", err) + log.Debug("Can't load merge-request ", mergeRequestIID, ": ", err) continue } mergeRequests = append(mergeRequests, mergeRequest) @@ -144,6 +147,7 @@ func (plugin *AutoMergePlugin) getUpdatedPipelineMergeRequests(project *gitlab.P } func (plugin *AutoMergePlugin) getMergeRequests(project *gitlab.Project, lastCheck *time.Time) []*gitlab.MergeRequest { + log := utils.Logger(project, nil) var mergeRequests []*gitlab.MergeRequest opt := &gitlab.ListProjectMergeRequestsOptions{ diff --git a/plugins/auto_merge/checks/has_enough_approvals.go b/plugins/auto_merge/checks/has_enough_approvals.go index ea216c3..665976c 100644 --- a/plugins/auto_merge/checks/has_enough_approvals.go +++ b/plugins/auto_merge/checks/has_enough_approvals.go @@ -6,7 +6,6 @@ import ( "github.com/GEPROG/lassie-bot-dog/plugins/auto_merge/config" "github.com/GEPROG/lassie-bot-dog/utils" - log "github.com/sirupsen/logrus" "github.com/xanzy/go-gitlab" ) @@ -17,6 +16,7 @@ type HasEnoughApprovalsCheck struct { var missingApprovalForLabels map[int][]string func (check HasEnoughApprovalsCheck) Check(config *config.AutoMergeConfig, project *gitlab.Project, mergeRequest *gitlab.MergeRequest) bool { + log := utils.Logger(project, mergeRequest) approvals, _, err := check.Client.MergeRequests.GetMergeRequestApprovals(project.ID, mergeRequest.IID) if err != nil { log.Error("Can't load merge-request approvals", err) diff --git a/plugins/auto_merge/checks/passes_ci.go b/plugins/auto_merge/checks/passes_ci.go index a52911a..082b788 100644 --- a/plugins/auto_merge/checks/passes_ci.go +++ b/plugins/auto_merge/checks/passes_ci.go @@ -2,7 +2,7 @@ package checks import ( "github.com/GEPROG/lassie-bot-dog/plugins/auto_merge/config" - log "github.com/sirupsen/logrus" + "github.com/GEPROG/lassie-bot-dog/utils" "github.com/xanzy/go-gitlab" ) @@ -12,6 +12,7 @@ type PassesCICheck struct { } func (check PassesCICheck) Check(_ *config.AutoMergeConfig, project *gitlab.Project, mergeRequest *gitlab.MergeRequest) bool { + log := utils.Logger(project, mergeRequest) pipelines, _, err := check.Client.MergeRequests.ListMergeRequestPipelines(project.ID, mergeRequest.IID) if err != nil { log.Error("Can't load merge-request pipelines", err) diff --git a/plugins/auto_merge/merge_status.go b/plugins/auto_merge/merge_status.go index 5405730..26b0cb7 100644 --- a/plugins/auto_merge/merge_status.go +++ b/plugins/auto_merge/merge_status.go @@ -4,7 +4,7 @@ import ( "regexp" "strings" - log "github.com/sirupsen/logrus" + "github.com/GEPROG/lassie-bot-dog/utils" "github.com/xanzy/go-gitlab" ) @@ -54,6 +54,7 @@ func (plugin AutoMergePlugin) encodeMergeStatus(status *mergeStatus) string { } func (plugin AutoMergePlugin) updateStatusComment(project *gitlab.Project, mergeRequest *gitlab.MergeRequest, status *mergeStatus) { + log := utils.Logger(project, mergeRequest) note, err := plugin.getStatusComment(project, mergeRequest) if err != nil { log.Error("Failed to get status comment: ", err) @@ -95,6 +96,7 @@ func (plugin AutoMergePlugin) getStatusComment(project *gitlab.Project, mergeReq } func (plugin AutoMergePlugin) saveStatusComment(project *gitlab.Project, mergeRequest *gitlab.MergeRequest, comment string, note *gitlab.Note) { + log := utils.Logger(project, mergeRequest) log.Trace("comment", comment) // update existing note @@ -109,7 +111,7 @@ func (plugin AutoMergePlugin) saveStatusComment(project *gitlab.Project, mergeRe } _, _, err := plugin.Client.Notes.UpdateMergeRequestNote(project.ID, mergeRequest.IID, note.ID, updateMergeRequestNoteOptions) if err != nil { - log.Error("Failed to update merge request note") + log.Error("Failed to update merge request note: ", err) } log.Debug("update comment") @@ -122,7 +124,7 @@ func (plugin AutoMergePlugin) saveStatusComment(project *gitlab.Project, mergeRe } _, _, err := plugin.Client.Notes.CreateMergeRequestNote(project.ID, mergeRequest.IID, createMergeRequestNoteOptions) if err != nil { - log.Error("Failed to create merge request note") + log.Error("Failed to create merge request note: ", err) } log.Debug("create comment") diff --git a/utils/logger.go b/utils/logger.go new file mode 100644 index 0000000..88b3e61 --- /dev/null +++ b/utils/logger.go @@ -0,0 +1,18 @@ +package utils + +import ( + log "github.com/sirupsen/logrus" + "github.com/xanzy/go-gitlab" +) + +func Logger(project *gitlab.Project, mergeRequest *gitlab.MergeRequest) *log.Entry { + fields := log.Fields{ + "webURL": project.WebURL, + } + if mergeRequest != nil { + fields = log.Fields{ + "webURL": mergeRequest.WebURL, + } + } + return log.WithFields(fields) +}