Skip to content

Commit

Permalink
fix: improve logging (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusheine authored Sep 14, 2023
1 parent 7ae6613 commit 059a904
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
10 changes: 7 additions & 3 deletions plugins/auto_merge/auto_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion plugins/auto_merge/checks/has_enough_approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion plugins/auto_merge/checks/passes_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions plugins/auto_merge/merge_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"regexp"
"strings"

log "github.com/sirupsen/logrus"
"github.com/GEPROG/lassie-bot-dog/utils"
"github.com/xanzy/go-gitlab"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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")
Expand Down
18 changes: 18 additions & 0 deletions utils/logger.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 059a904

Please sign in to comment.