-
Notifications
You must be signed in to change notification settings - Fork 28
/
list_build.go
64 lines (53 loc) · 1.58 KB
/
list_build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: Apache-2.0
package step
import (
"context"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)
// ListStepsForBuild gets a list of all steps from the database.
func (e *engine) ListStepsForBuild(ctx context.Context, b *library.Build, filters map[string]interface{}, page int, perPage int) ([]*library.Step, int64, error) {
e.logger.WithFields(logrus.Fields{
"build": b.GetNumber(),
}).Tracef("listing steps for build %d from the database", b.GetNumber())
// variables to store query results and return value
count := int64(0)
s := new([]database.Step)
steps := []*library.Step{}
// count the results
count, err := e.CountStepsForBuild(ctx, b, filters)
if err != nil {
return steps, 0, err
}
// short-circuit if there are no results
if count == 0 {
return steps, 0, nil
}
// calculate offset for pagination through results
offset := perPage * (page - 1)
// send query to the database and store result in variable
err = e.client.
Table(constants.TableStep).
Where("build_id = ?", b.GetID()).
Where(filters).
Order("id DESC").
Limit(perPage).
Offset(offset).
Find(&s).
Error
if err != nil {
return nil, count, err
}
// iterate through all query results
for _, step := range *s {
// https://golang.org/doc/faq#closures_and_goroutines
tmp := step
// convert query result to library type
//
// https://pkg.go.dev/github.com/go-vela/types/database#Step.ToLibrary
steps = append(steps, tmp.ToLibrary())
}
return steps, count, nil
}