Skip to content

Commit

Permalink
#2 First build-mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
BrobotDubstep committed May 22, 2020
1 parent 3acc0cf commit 05f7fc1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 29 deletions.
22 changes: 12 additions & 10 deletions pkg/harbourbuild/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ func (b Builder) buildImage(job models.BuildJob) {
redisClient := redisconfig.OpenClient(b.redisOptions)
redisClient.HSet(job.BuildKey, "build_status", "Running")

buildCtx, err := b.createBuildContext(job.Request.Project)
buildCtx, err := b.createBuildContext(job.Request.Repository)
if err != nil {
log.WithError(err).Error("Failed to create build context")
return
}

tag := []string{b.getImageString(job.RegistryUrl, job.Request.Repository, job.Request.Tag)}
opt := types.ImageBuildOptions{
Tags: job.Request.Tags,
Tags: tag,
Dockerfile: job.Request.Dockerfile,
}

Expand All @@ -87,25 +88,23 @@ func (b Builder) buildImage(job models.BuildJob) {
}

logs := buf.String()
fmt.Println(logs)
if strings.Contains(logs, "errorDetail") {
redisClient.HSet(job.BuildKey, "build_status", "Failed", "logs", logs)
return
}

redisClient.HSet(job.BuildKey, "build_status", "Success", "logs", logs)
log.Tracef("Image %s was built", job.Request.Project)
log.Tracef("Image %s was built", job.Request.Repository)

imageString := b.getImageString(job.RegistryUrl, job.Request.Project)
if len(job.Request.Tags) > 0 {
imageString += ":" + job.Request.Tags[0]
}
imageString := b.getImageString(job.RegistryUrl, job.Request.Repository, job.Request.Tag)

if err = b.pushImage(imageString, job.RegistryToken); err != nil {
log.WithError(err).Error("Error while pushing image to registry")
return
}

log.Tracef("Image %s was pushed to registry %s", job.Request.Project, job.RegistryUrl)
log.Tracef("Image %s was pushed to registry %s", job.Request.Repository, job.RegistryUrl)
}

func (b Builder) createBuildContext(project string) (*os.File, error) {
Expand Down Expand Up @@ -164,8 +163,11 @@ func (b Builder) pushImage(image string, token string) error {
return nil
}

func (b Builder) getImageString(registryUrl string, image string) string {
return fmt.Sprintf("%s/%s", strings.Split(registryUrl, "//")[1], image)
func (b Builder) getImageString(registryUrl string, repository string, tag string) string {
if tag != "" {
return fmt.Sprintf("%s/%s:%s", strings.Split(registryUrl, "//")[1], repository, tag)
}
return fmt.Sprintf("%s/%s", strings.Split(registryUrl, "//")[1], repository)
}

//TODO Communicate with Harbour SCM in order to receive the path to the project-files
Expand Down
7 changes: 3 additions & 4 deletions pkg/harbourbuild/handler/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (b BuilderModel) BuildImage(w http.ResponseWriter, r *http.Request) {
return
}

registryToken, err := fetchRegistryToken(r.Context(), buildRequest.Project, b.config)
registryToken, err := fetchRegistryToken(r.Context(), buildRequest.Repository, b.config)
if err != nil {
return // Error is already logged in get
}
Expand Down Expand Up @@ -68,10 +68,9 @@ func createBuildEntry(ctx context.Context, request models.BuildRequest) (string,

err := client.HSet(buildKey,
"build_id", buildId.String(),
"project", request.Project,
"repository", request.Repository,
"commit", request.Commit,
"logs", nil,
"repository", request.Project,
"build_status", "Pending").Err()

if err != nil {
Expand All @@ -88,7 +87,7 @@ func fetchRegistryToken(ctx context.Context, repository string, registry *config
var registryToken string
var tokenResponse registryModels.DockerTokenResponse

resp, err := apiclient.Get(ctx, tokenUrl, &tokenResponse, oidcTokenStr)
resp, err := apiclient.Get(ctx, tokenUrl, &tokenResponse, oidcTokenStr, nil)
if err != nil {
return registryToken, err
}
Expand Down
10 changes: 0 additions & 10 deletions pkg/harbourbuild/models/build.go

This file was deleted.

8 changes: 4 additions & 4 deletions pkg/harbourbuild/models/buildrequest.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package models

type BuildRequest struct {
Dockerfile string `json:"dockerfile"`
Tags []string `json:"tags"`
Project string `json:"project"`
Commit string `json:"commit"`
Dockerfile string `json:"dockerfile"`
Tag string `json:"tag"`
Repository string `json:"repository"`
Commit string `json:"commit"`
}
75 changes: 75 additions & 0 deletions pkg/harbourgateway/graphql/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package graphql

import (
"github.com/graphql-go/graphql"
"github.com/harbourrocks/harbour/pkg/apiclient"
"github.com/harbourrocks/harbour/pkg/auth"
"github.com/harbourrocks/harbour/pkg/harbourbuild/models"
)

var triggerBuildType = graphql.NewObject(
graphql.ObjectConfig{
Name: "Build",
Fields: graphql.Fields{
"dockerfile": &graphql.Field{
Type: graphql.String,
Description: "Name of dockerfile which should be used for build",
},
"tag": &graphql.Field{
Type: graphql.String,
Description: "Tag which should be used for the image",
},
"repository": &graphql.Field{
Type: graphql.String,
Description: "Code-Repo which should be built",
},
"commit": &graphql.Field{
Type: graphql.String,
Description: "Commit which sould be used for built",
},
},
})

func TriggerBuildField() *graphql.Field {
return &graphql.Field{
Type: triggerBuildType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
oidcTokenStr := auth.GetOidcTokenStrCtx(p.Context)

repository, isOK := p.Args["repository"].(string)
if !isOK {
return nil, nil
}

dockerfile, isOK := p.Args["dockerfile"].(string)
if !isOK {
return nil, nil
}

tag, isOK := p.Args["tag"].(string)
if !isOK {
return nil, nil
}

commit, isOK := p.Args["commit"].(string)
if !isOK {
return nil, nil
}

build := &models.BuildRequest{
Repository: repository,
Dockerfile: dockerfile,
Tag: tag,
Commit: commit,
}

var response interface{}
_, err := apiclient.Post(p.Context, "http://localhost:5200/build", response, build, oidcTokenStr, nil)
if err != nil {
return nil, err
}

return nil, nil
},
}
}
11 changes: 10 additions & 1 deletion pkg/harbourgateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ func RunGatewayServer(o *configuration.Options) error {
},
})

var mutationType = graphql.NewObject(
graphql.ObjectConfig{
Name: "Mutation",
Fields: graphql.Fields{
"triggerBuild": graphql2.TriggerBuildField(),
},
})

var schema, _ = graphql.NewSchema(
graphql.SchemaConfig{
Query: queryType,
Query: queryType,
Mutation: mutationType,
},
)

Expand Down

0 comments on commit 05f7fc1

Please sign in to comment.