Skip to content

Commit

Permalink
add logger level
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed May 23, 2022
1 parent 6a830dd commit 338929c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

gitlab review-bot

## Preconditions
- Gitlab version 13+

## Quick start

### Config
Expand Down
2 changes: 2 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ scm:
host: https://gitlab.com
token: <your-private-token>
secret: <your-webhook-secret>
logger:
level: info
5 changes: 5 additions & 0 deletions global/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const JWTSecret = "bot"
type Config struct {
Server server.Config `json:"server"`
SCM scm.Config `json:"scm"`
Logger LoggerConfig `json:"logger"`
}

type LoggerConfig struct {
Level string `json:"level"` // debug、info、warn、error、fatal、panic、trace
}

func Environ() *Config {
Expand Down
9 changes: 9 additions & 0 deletions global/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.
package global

import (
"fmt"

"github.com/pkgms/go/ctr"
"github.com/sirupsen/logrus"

Expand All @@ -26,6 +28,13 @@ var config *Config

func InitCfg(cfg *Config) (err error) {
config = cfg

level, err := logrus.ParseLevel(cfg.Logger.Level)
if err != nil {
return fmt.Errorf("parse logger level failed: %v", err)
}
logrus.SetLevel(level)

logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
DisableLevelTruncation: true,
Expand Down
8 changes: 6 additions & 2 deletions handler/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ func completeAssignees(event *gitlab.MergeEvent, opt *scm.UpdatePullRequest) {
}
return ids
}
opt.AssigneeIDs = getAssigneeIDs(event)
opt.AssigneeID = event.Assignee.ID
if event.Assignees != nil {
opt.AssigneeIDs = getAssigneeIDs(event)
}
if event.Assignee != nil {
opt.AssigneeID = event.Assignee.ID
}
}
8 changes: 7 additions & 1 deletion pkg/scm/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package scm
import (
"path"

"github.com/sirupsen/logrus"

"github.com/xanzy/go-gitlab"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -111,7 +113,6 @@ func (s *gitlabClient) GetPullRequest(pid string, prID int) (*PullRequest, error

func (s *gitlabClient) UpdatePullRequest(pid string, prID int, data *UpdatePullRequest) error {
opt := &gitlab.UpdateMergeRequestOptions{
AssigneeIDs: &data.AssigneeIDs,
AddLabels: (*gitlab.Labels)(&data.AddLabels),
RemoveLabels: (*gitlab.Labels)(&data.RemoveLabels),
}
Expand All @@ -127,6 +128,11 @@ func (s *gitlabClient) UpdatePullRequest(pid string, prID int, data *UpdatePullR
if data.AssigneeID > 0 {
opt.AssigneeID = &data.AssigneeID
}
if len(data.AssigneeIDs) > 0 {
opt.AssigneeIDs = &data.AssigneeIDs
}
logrus.Debugf("UpdatePullRequest before options: %+v", opt)
logrus.Debugf("UpdateMergeRequest after options: %+v", opt)
_, _, err := s.client.MergeRequests.UpdateMergeRequest(pid, prID, opt)
return err
}
Expand Down

0 comments on commit 338929c

Please sign in to comment.