format do 'simplify' and check via CI (#509)

* `make format` simplify code now

* code format

* check simplified via linter
This commit is contained in:
6543 2021-11-14 22:33:45 +01:00 committed by GitHub
parent 2524c6900c
commit 86bb8f195c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 61 deletions

View file

@ -1,6 +1,6 @@
linters-settings: linters-settings:
gofmt: gofmt:
simplify: false simplify: true
linters: linters:
disable-all: true disable-all: true

View file

@ -26,7 +26,7 @@ vendor:
go mod vendor go mod vendor
format: format:
@gofmt -w ${GOFILES_NOVENDOR} @gofmt -s -w ${GOFILES_NOVENDOR}
.PHONY: clean .PHONY: clean
clean: clean:
@ -36,7 +36,7 @@ clean:
.PHONY: lint .PHONY: lint
lint: lint:
@echo "Running golangci-lint" @echo "Running golangci-lint"
go run vendor/github.com/golangci/golangci-lint/cmd/golangci-lint/main.go run go run vendor/github.com/golangci/golangci-lint/cmd/golangci-lint/main.go run --timeout 5m
@echo "Running zerolog linter" @echo "Running zerolog linter"
go run vendor/github.com/rs/zerolog/cmd/lint/lint.go github.com/woodpecker-ci/woodpecker/cmd/agent go run vendor/github.com/rs/zerolog/cmd/lint/lint.go github.com/woodpecker-ci/woodpecker/cmd/agent
go run vendor/github.com/rs/zerolog/cmd/lint/lint.go github.com/woodpecker-ci/woodpecker/cmd/cli go run vendor/github.com/rs/zerolog/cmd/lint/lint.go github.com/woodpecker-ci/woodpecker/cmd/cli

View file

@ -93,27 +93,22 @@ func (r *Runner) Run(ctx context.Context) error {
Str("id", work.ID). Str("id", work.ID).
Logger() Logger()
logger.Debug(). logger.Debug().Msg("received execution")
Msg("received execution")
ctx, cancel := context.WithTimeout(ctxmeta, timeout) ctx, cancel := context.WithTimeout(ctxmeta, timeout)
defer cancel() defer cancel()
cancelled := abool.New() cancelled := abool.New()
go func() { go func() {
logger.Debug(). logger.Debug().Msg("listen for cancel signal")
Msg("listen for cancel signal")
if werr := r.client.Wait(ctx, work.ID); werr != nil { if werr := r.client.Wait(ctx, work.ID); werr != nil {
cancelled.SetTo(true) cancelled.SetTo(true)
logger.Warn(). logger.Warn().Err(werr).Msg("cancel signal received")
Err(werr).
Msg("cancel signal received")
cancel() cancel()
} else { } else {
logger.Debug(). logger.Debug().Msg("stop listening for cancel signal")
Msg("stop listening for cancel signal")
} }
}() }()
@ -121,13 +116,11 @@ func (r *Runner) Run(ctx context.Context) error {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
logger.Debug(). logger.Debug().Msg("pipeline done")
Msg("pipeline done")
return return
case <-time.After(time.Minute): case <-time.After(time.Minute):
logger.Debug(). logger.Debug().Msg("pipeline lease renewed")
Msg("pipeline lease renewed")
r.client.Extend(ctx, work.ID) r.client.Extend(ctx, work.ID)
} }
@ -139,9 +132,7 @@ func (r *Runner) Run(ctx context.Context) error {
err = r.client.Init(ctxmeta, work.ID, state) err = r.client.Init(ctxmeta, work.ID, state)
if err != nil { if err != nil {
logger.Error(). logger.Error().Err(err).Msg("pipeline initialization failed")
Err(err).
Msg("pipeline initialization failed")
} }
var uploads sync.WaitGroup var uploads sync.WaitGroup
@ -173,30 +164,29 @@ func (r *Runner) Run(ctx context.Context) error {
loglogger.Debug().Msg("log stream copied") loglogger.Debug().Msg("log stream copied")
file := &rpc.File{} data, err := json.Marshal(logstream.Lines())
file.Mime = "application/json+logs" if err != nil {
file.Proc = proc.Alias loglogger.Err(err).Msg("could not marshal logstream")
file.Name = "logs.json"
file.Data, _ = json.Marshal(logstream.Lines())
file.Size = len(file.Data)
file.Time = time.Now().Unix()
loglogger.Debug().
Msg("log stream uploading")
if serr := r.client.Upload(ctxmeta, work.ID, file); serr != nil {
loglogger.Error().
Err(serr).
Msg("log stream upload error")
} }
loglogger.Debug(). file := &rpc.File{
Msg("log stream upload complete") Mime: "application/json+logs",
Proc: proc.Alias,
Name: "logs.json",
Data: data,
Size: len(data),
Time: time.Now().Unix(),
}
loglogger.Debug().Msg("log stream uploading")
if serr := r.client.Upload(ctxmeta, work.ID, file); serr != nil {
loglogger.Error().Err(serr).Msg("log stream upload error")
} else {
loglogger.Debug().Msg("log stream upload complete")
}
defer func() { defer func() {
loglogger.Debug(). loglogger.Debug().Msg("log stream closed")
Msg("log stream closed")
uploads.Done() uploads.Done()
}() }()
@ -206,15 +196,20 @@ func (r *Runner) Run(ctx context.Context) error {
} }
// TODO should be configurable // TODO should be configurable
limitedPart = io.LimitReader(part, maxFileUpload) limitedPart = io.LimitReader(part, maxFileUpload)
file = &rpc.File{} data, err = ioutil.ReadAll(limitedPart)
file.Mime = part.Header().Get("Content-Type") if err != nil {
file.Proc = proc.Alias loglogger.Err(err).Msg("could not read limited part")
file.Name = part.FileName() }
file.Data, _ = ioutil.ReadAll(limitedPart)
file.Size = len(file.Data)
file.Time = time.Now().Unix()
file.Meta = map[string]string{}
file = &rpc.File{
Mime: part.Header().Get("Content-Type"),
Proc: proc.Alias,
Name: part.FileName(),
Data: data,
Size: len(data),
Time: time.Now().Unix(),
Meta: make(map[string]string),
}
for key, value := range part.Header() { for key, value := range part.Header() {
file.Meta[key] = value[0] file.Meta[key] = value[0]
} }
@ -255,8 +250,7 @@ func (r *Runner) Run(ctx context.Context) error {
Finished: time.Now().Unix(), Finished: time.Now().Unix(),
} }
defer func() { defer func() {
proclogger.Debug(). proclogger.Debug().Msg("update step status")
Msg("update step status")
if uerr := r.client.Update(ctxmeta, work.ID, procState); uerr != nil { if uerr := r.client.Update(ctxmeta, work.ID, procState); uerr != nil {
proclogger.Debug(). proclogger.Debug().
@ -264,8 +258,7 @@ func (r *Runner) Run(ctx context.Context) error {
Msg("update step status error") Msg("update step status error")
} }
proclogger.Debug(). proclogger.Debug().Msg("update step status complete")
Msg("update step status complete")
}() }()
if state.Process.Exited { if state.Process.Exited {
return nil return nil
@ -325,13 +318,11 @@ func (r *Runner) Run(ctx context.Context) error {
Int("exit_code", state.ExitCode). Int("exit_code", state.ExitCode).
Msg("pipeline complete") Msg("pipeline complete")
logger.Debug(). logger.Debug().Msg("uploading logs")
Msg("uploading logs")
uploads.Wait() uploads.Wait()
logger.Debug(). logger.Debug().Msg("uploading logs complete")
Msg("uploading logs complete")
logger.Debug(). logger.Debug().
Str("error", state.Error). Str("error", state.Error).
@ -340,11 +331,9 @@ func (r *Runner) Run(ctx context.Context) error {
err = r.client.Done(ctxmeta, work.ID, state) err = r.client.Done(ctxmeta, work.ID, state)
if err != nil { if err != nil {
logger.Error().Err(err). logger.Error().Err(err).Msg("updating pipeline status failed")
Msg("updating pipeline status failed")
} else { } else {
logger.Debug(). logger.Debug().Msg("updating pipeline status complete")
Msg("updating pipeline status complete")
} }
return nil return nil

View file

@ -186,12 +186,12 @@ func TestPipelineName(t *testing.T) {
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yamls: []*remote.FileMeta{ Yamls: []*remote.FileMeta{
&remote.FileMeta{Name: ".woodpecker/lint.yml", Data: []byte(` {Name: ".woodpecker/lint.yml", Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
`)}, `)},
&remote.FileMeta{Name: ".woodpecker/.test.yml", Data: []byte(` {Name: ".woodpecker/.test.yml", Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch