Skip to content

Commit

Permalink
Merge pull request #124 from berty/dev/moul/new-build-filters
Browse files Browse the repository at this point in the history
  • Loading branch information
moul authored Apr 29, 2020
2 parents eb4a195 + 745c6d2 commit 41c939c
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 160 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ vendor/
coverage.txt
packrd/
*-packr.go
*.tmp

# Junk files
*~
Expand Down
12 changes: 12 additions & 0 deletions api/yolopb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ message BuildList {

// filter builds without any artifacts
bool with_artifacts = 3;

// only a specific build by its ID or yolo_id
repeated string build_id = 4 [(gogoproto.customname) = "BuildID"];

// builds of a specific project by its ID or yolo_id
repeated string project_id = 5 [(gogoproto.customname) = "ProjectID"];

// filter on builds that contain at least on of these artifacts
repeated string artifact_id = 6 [(gogoproto.customname) = "ArtifactID"];

// filter by build driver (GitHub, CircleCI, ...)
Driver build_driver = 7;
}
message Response {
repeated Build builds = 1;
Expand Down
2 changes: 1 addition & 1 deletion deployments/yolo.berty.io/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'3.7'
version: '3.7'

services:
yolo:
Expand Down
2 changes: 1 addition & 1 deletion gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

504 changes: 356 additions & 148 deletions pkg/yolopb/yolopb.pb.go

Large diffs are not rendered by default.

39 changes: 31 additions & 8 deletions pkg/yolosvc/api_buildlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"fmt"

"berty.tech/yolo/v2/pkg/yolopb"
"moul.io/godev"
)

func (svc service) BuildList(ctx context.Context, req *yolopb.BuildList_Request) (*yolopb.BuildList_Response, error) {
fmt.Println(godev.PrettyJSON(req))
if req == nil {
req = &yolopb.BuildList_Request{}
}
if req.Limit == 0 {
req.Limit = 30
req.Limit = 50
}
if !req.WithArtifacts {
req.WithArtifacts = len(req.ArtifactKinds) > 0
Expand All @@ -24,17 +26,38 @@ func (svc service) BuildList(ctx context.Context, req *yolopb.BuildList_Request)
Model(&yolopb.Build{})

switch {
case len(req.ArtifactKinds) > 0:
case len(req.BuildID) > 0:
query = query.
Joins("JOIN artifact ON artifact.has_build_id = build.id AND artifact.kind IN (?)", req.ArtifactKinds).
Preload("HasArtifacts", "kind IN (?)", req.ArtifactKinds)
case req.WithArtifacts:
query = query.
Joins("JOIN artifact ON artifact.has_build_id = build.id", req.ArtifactKinds).
Where("id IN (?) OR yolo_id IN (?)", req.BuildID, req.BuildID).
Preload("HasArtifacts")
default:
case len(req.ArtifactID) > 0:
query = query.
Joins("JOIN artifact ON artifact.has_build_id = build.id AND (artifact.id IN (?) OR artifact.yolo_id IN (?))", req.ArtifactID, req.ArtifactID).
Preload("HasArtifacts")
default:
switch {
case len(req.ArtifactKinds) > 0:
query = query.
Joins("JOIN artifact ON artifact.has_build_id = build.id AND artifact.kind IN (?)", req.ArtifactKinds).
Preload("HasArtifacts", "kind IN (?)", req.ArtifactKinds)
case req.WithArtifacts:
query = query.
Joins("JOIN artifact ON artifact.has_build_id = build.id", req.ArtifactKinds).
Preload("HasArtifacts")
default:
query = query.
Preload("HasArtifacts")
}

if req.BuildDriver != yolopb.Driver_UnknownDriver {
query = query.Where(&yolopb.Build{
Driver: req.BuildDriver,
})
}

if len(req.ProjectID) > 0 {
query = query.Joins("JOIN project ON project.id = build.has_project_id AND (project.id IN (?) OR project.yolo_id IN (?))", req.ProjectID, req.ProjectID)
}
}

query = query.
Expand Down
4 changes: 2 additions & 2 deletions pkg/yolosvc/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package yolosvc
import (
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
"strings"
"time"

"berty.tech/yolo/v2/pkg/yolopb"
"github.com/jinzhu/gorm"
"github.com/mr-tron/base58"
"go.uber.org/zap"
"moul.io/zapgorm"
)
Expand Down Expand Up @@ -109,7 +109,7 @@ func beforeCreate(scope *gorm.Scope) {
table := scope.TableName()
id := field.Field.String()
hash := sha256.Sum256([]byte(id))
encoded := base64.StdEncoding.EncodeToString(hash[:])
encoded := base58.Encode(hash[:])
prefix := strings.ToLower(table[:1])
yoloID := fmt.Sprintf("%s:%s", prefix, encoded)
err := scope.SetColumn("yolo_id", yoloID)
Expand Down

0 comments on commit 41c939c

Please sign in to comment.