Skip to content

Commit

Permalink
fix: set db in the compiler to support repo sync
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Oct 30, 2024
1 parent 8668615 commit 2246b3d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions api/build/compile_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func CompileAndPublish(
WithUser(u).
WithLabels(cfg.Labels).
WithSCM(scm).
WithDatabase(database).
Compile(ctx, pipelineFile)
if err != nil {
// format the error message with extra information
Expand Down
4 changes: 4 additions & 0 deletions compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/go-vela/server/compiler/types/pipeline"
"github.com/go-vela/server/compiler/types/raw"
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/scm"
)
Expand Down Expand Up @@ -150,6 +151,9 @@ type Engine interface {
// WithSCM defines a function that sets
// the scm in the Engine.
WithSCM(scm.Service) Engine
// WithDatabase defines a function that sets
// the database in the Engine.
WithDatabase(database.Interface) Engine
// WithPrivateGitHub defines a function that sets
// the private github client in the Engine.
WithPrivateGitHub(context.Context, string, string) Engine
Expand Down
2 changes: 1 addition & 1 deletion compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *client) Compile(ctx context.Context, v interface{}) (*pipeline.Build, *
// netrc can be provided directly using WithNetrc for situations like local exec
if c.netrc == nil && c.scm != nil {
// get the netrc password from the scm
netrc, err := c.scm.GetNetrcPassword(ctx, c.repo, c.user, p.Git)
netrc, err := c.scm.GetNetrcPassword(ctx, c.db, c.repo, c.user, p.Git)
if err != nil {
return nil, nil, err
}
Expand Down
9 changes: 9 additions & 0 deletions compiler/native/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/compiler/registry"
"github.com/go-vela/server/compiler/registry/github"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/internal/image"
"github.com/go-vela/server/scm"
Expand Down Expand Up @@ -46,6 +47,7 @@ type client struct {
repo *api.Repo
user *api.User
labels []string
db database.Interface
scm scm.Service
netrc *string
}
Expand Down Expand Up @@ -249,3 +251,10 @@ func (c *client) WithSCM(_scm scm.Service) compiler.Engine {

return c
}

// WithDatabase sets the database in the Engine.
func (c *client) WithDatabase(db database.Interface) compiler.Engine {
c.db = db

return c
}
14 changes: 8 additions & 6 deletions scm/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func (c *client) GetBranch(ctx context.Context, r *api.Repo, branch string) (str

// GetNetrcPassword returns a clone token using the repo's github app installation if it exists.
// If not, it defaults to the user OAuth token.
func (c *client) GetNetrcPassword(ctx context.Context, r *api.Repo, u *api.User, g yaml.Git) (string, error) {
func (c *client) GetNetrcPassword(ctx context.Context, db database.Interface, r *api.Repo, u *api.User, g yaml.Git) (string, error) {
l := c.Logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
Expand Down Expand Up @@ -749,12 +749,14 @@ func (c *client) GetNetrcPassword(ctx context.Context, r *api.Repo, u *api.User,
if installToken != nil && len(installToken.GetToken()) != 0 {
l.Tracef("using github app installation token for %s/%s", r.GetOrg(), r.GetName())

// sync the install ID with the repo
r.SetInstallID(installID)
// (optional) sync the install ID with the repo
if db != nil {
r.SetInstallID(installID)

_, err = database.FromContext(ctx).UpdateRepo(ctx, r)
if err != nil {
c.Logger.Tracef("unable to update repo with install ID %d: %v", installID, err)
_, err = db.UpdateRepo(ctx, r)
if err != nil {
c.Logger.Tracef("unable to update repo with install ID %d: %v", installID, err)
}
}

return installToken.GetToken(), nil
Expand Down
2 changes: 1 addition & 1 deletion scm/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ func TestGithub_GetNetrcPassword(t *testing.T) {
client.AppsTransport = NewTestAppsTransport(s.URL)
}

got, err := client.GetNetrcPassword(context.TODO(), test.repo, test.user, test.git)
got, err := client.GetNetrcPassword(context.TODO(), nil, test.repo, test.user, test.git)
if (err != nil) != test.wantErr {
t.Errorf("GetNetrcPassword() error = %v, wantErr %v", err, test.wantErr)
return
Expand Down
2 changes: 1 addition & 1 deletion scm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ type Service interface {
GetHTMLURL(context.Context, *api.User, string, string, string, string) (string, error)
// GetNetrc defines a function that returns the netrc
// password injected into build steps.
GetNetrcPassword(context.Context, *api.Repo, *api.User, yaml.Git) (string, error)
GetNetrcPassword(context.Context, database.Interface, *api.Repo, *api.User, yaml.Git) (string, error)
// SyncRepoWithInstallation defines a function that syncs
// a repo with the installation, if it exists.
SyncRepoWithInstallation(context.Context, *api.Repo) (*api.Repo, error)
Expand Down

0 comments on commit 2246b3d

Please sign in to comment.