Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"errors"
"fmt"
"log"
"strings"

Expand All @@ -25,7 +26,8 @@ type Build struct {
Finish func(error)
Reporter progress.Writer

Response *connect.Response[cliv1.CreateBuildResponse]
Response *connect.Response[cliv1.CreateBuildResponse]
projectID string
}

type Credential struct {
Expand All @@ -35,7 +37,7 @@ type Credential struct {

func (b *Build) AdditionalTags() []string {
if b.Response == nil || b.Response.Msg == nil {
return nil
return []string{fmt.Sprintf("registry.depot.dev/%s:%s", b.projectID, b.ID)}
}

tags := make([]string, 0, len(b.Response.Msg.AdditionalTags))
Expand All @@ -52,10 +54,8 @@ func (b *Build) AdditionalTags() []string {

func (b *Build) AdditionalCredentials() []Credential {
if b.Response == nil || b.Response.Msg == nil {
return nil
}
if len(b.Response.Msg.AdditionalCredentials) == 0 {
return nil
token := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("x-token:%s", b.Token)))
return []Credential{{Host: "registry.depot.dev", Token: token}}
}

creds := make([]Credential, 0, len(b.Response.Msg.AdditionalCredentials))
Expand All @@ -82,6 +82,10 @@ func (b *Build) AuthProvider(dockerAuth driver.Auth) driver.Auth {
// This is important as the API may use a different project ID than the one
// initially requested (e.g. onboarding)
func (b *Build) BuildProject() string {
if b.projectID != "" {
return b.projectID
}

if b.Response == nil || b.Response.Msg == nil {
return ""
}
Expand All @@ -107,8 +111,9 @@ func NewBuild(ctx context.Context, req *cliv1.CreateBuildRequest, token string)
}

func FromExistingBuild(ctx context.Context, buildID, token string) (Build, error) {
client := depotapi.NewBuildClient()

finish := func(buildErr error) {
client := depotapi.NewBuildClient()
req := cliv1.FinishBuildRequest{BuildId: buildID}
req.Result = &cliv1.FinishBuildRequest_Success{Success: &cliv1.FinishBuildRequest_BuildSuccess{}}
if buildErr != nil {
Expand All @@ -130,10 +135,18 @@ func FromExistingBuild(ctx context.Context, buildID, token string) (Build, error
}
}

req := cliv1.GetBuildRequest{BuildId: buildID}
res, err := client.GetBuild(ctx, depotapi.WithAuthentication(connect.NewRequest(&req), token))
if err != nil {
return Build{}, err
}

return Build{
ID: buildID,
Token: token,
Finish: finish,
ID: buildID,
Token: token,
Finish: finish,
BuildURL: res.Msg.BuildUrl,
projectID: res.Msg.ProjectId,
}, nil
}

Expand Down
Loading
Loading