-
Notifications
You must be signed in to change notification settings - Fork 28
/
list_repo.go
73 lines (60 loc) · 1.79 KB
/
list_repo.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
65
66
67
68
69
70
71
72
73
// SPDX-License-Identifier: Apache-2.0
package pipeline
import (
"context"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)
// ListPipelinesForRepo gets a list of pipelines by repo ID from the database.
//
//nolint:lll // ignore long line length due to variable names
func (e *engine) ListPipelinesForRepo(ctx context.Context, r *library.Repo, page, perPage int) ([]*library.Pipeline, int64, error) {
e.logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
}).Tracef("listing pipelines for repo %s from the database", r.GetFullName())
// variables to store query results and return values
count := int64(0)
p := new([]database.Pipeline)
pipelines := []*library.Pipeline{}
// count the results
count, err := e.CountPipelinesForRepo(ctx, r)
if err != nil {
return pipelines, 0, err
}
// short-circuit if there are no results
if count == 0 {
return pipelines, 0, nil
}
// calculate offset for pagination through results
offset := perPage * (page - 1)
err = e.client.
Table(constants.TablePipeline).
Where("repo_id = ?", r.GetID()).
Limit(perPage).
Offset(offset).
Find(&p).
Error
if err != nil {
return nil, count, err
}
// iterate through all query results
for _, pipeline := range *p {
// https://golang.org/doc/faq#closures_and_goroutines
tmp := pipeline
// decompress data for the pipeline
//
// https://pkg.go.dev/github.com/go-vela/types/database#Pipeline.Decompress
err = tmp.Decompress()
if err != nil {
return nil, count, err
}
// convert query result to library type
//
// https://pkg.go.dev/github.com/go-vela/types/database#Pipeline.ToLibrary
pipelines = append(pipelines, tmp.ToLibrary())
}
return pipelines, count, nil
}