2021-11-13 19:18:06 +00:00
|
|
|
// Copyright 2021 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
|
|
)
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func (s storage) LogFind(step *model.Step) (io.ReadCloser, error) {
|
2021-11-13 19:18:06 +00:00
|
|
|
logs := &model.Logs{
|
2022-10-28 15:38:53 +00:00
|
|
|
StepID: step.ID,
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
if err := wrapGet(s.engine.Get(logs)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buf := bytes.NewBuffer(logs.Data)
|
2022-08-25 06:39:19 +00:00
|
|
|
return io.NopCloser(buf), nil
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func (s storage) LogSave(step *model.Step, reader io.Reader) error {
|
2022-08-25 06:39:19 +00:00
|
|
|
data, _ := io.ReadAll(reader)
|
2021-11-13 19:18:06 +00:00
|
|
|
|
|
|
|
sess := s.engine.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logs := new(model.Logs)
|
2022-10-28 15:38:53 +00:00
|
|
|
exist, err := sess.Where("log_step_id = ?", step.ID).Get(logs)
|
2021-11-13 19:18:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exist {
|
|
|
|
if _, err := sess.ID(logs.ID).Cols("log_data").Update(&model.Logs{Data: data}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := sess.Insert(&model.Logs{
|
2022-10-28 15:38:53 +00:00
|
|
|
StepID: step.ID,
|
2021-11-13 19:18:06 +00:00
|
|
|
Data: data,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|