-
Notifications
You must be signed in to change notification settings - Fork 28
/
last_repo.go
48 lines (39 loc) · 1.14 KB
/
last_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
// SPDX-License-Identifier: Apache-2.0
package hook
import (
"context"
"errors"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
// LastHookForRepo gets the last hook by repo ID from the database.
func (e *engine) LastHookForRepo(ctx context.Context, r *library.Repo) (*library.Hook, error) {
e.logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
}).Tracef("getting last hook for repo %s from the database", r.GetFullName())
// variable to store query results
h := new(database.Hook)
// send query to the database and store result in variable
err := e.client.
Table(constants.TableHook).
Where("repo_id = ?", r.GetID()).
Order("number DESC").
Take(h).
Error
if err != nil {
// check if the query returned a record not found error
if errors.Is(err, gorm.ErrRecordNotFound) {
// the record will not exist if it is a new repo
return nil, nil
}
return nil, err
}
// return the hook
//
// https://pkg.go.dev/github.com/go-vela/types/database#Hook.ToLibrary
return h.ToLibrary(), nil
}