-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathupdate.go
60 lines (49 loc) · 1.54 KB
/
update.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
// SPDX-License-Identifier: Apache-2.0
//nolint:dupl // ignore similar code with create.go
package repo
import (
"context"
"fmt"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)
// UpdateRepo updates an existing repo in the database.
func (e *engine) UpdateRepo(ctx context.Context, r *library.Repo) (*library.Repo, error) {
e.logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
}).Tracef("creating repo %s in the database", r.GetFullName())
// cast the library type to database type
//
// https://pkg.go.dev/github.com/go-vela/types/database#RepoFromLibrary
repo := database.RepoFromLibrary(r)
// validate the necessary fields are populated
//
// https://pkg.go.dev/github.com/go-vela/types/database#Repo.Validate
err := repo.Validate()
if err != nil {
return nil, err
}
// encrypt the fields for the repo
//
// https://pkg.go.dev/github.com/go-vela/types/database#Repo.Encrypt
err = repo.Encrypt(e.config.EncryptionKey)
if err != nil {
return nil, fmt.Errorf("unable to encrypt repo %s: %w", r.GetFullName(), err)
}
// send query to the database
err = e.client.Table(constants.TableRepo).Save(repo).Error
if err != nil {
return nil, err
}
// decrypt the fields for the repo
err = repo.Decrypt(e.config.EncryptionKey)
if err != nil {
// only log to preserve backwards compatibility
e.logger.Errorf("unable to decrypt repo %d: %v", r.GetID(), err)
return repo.ToLibrary(), nil
}
return repo.ToLibrary(), nil
}