Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/gin-gonic/gin v1.7.4
github.com/go-vela/mock v0.10.0
github.com/go-vela/pkg-executor v0.10.0
github.com/go-vela/pkg-queue v0.10.0
github.com/go-vela/pkg-runtime v0.10.0
Expand Down
11 changes: 11 additions & 0 deletions internal/build/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

// Package build provides the ability for Vela to
// manipulate and manage a build from a pipeline.
//
// Usage:
//
// import "github.com/go-vela/worker/internal/build"
package build
51 changes: 51 additions & 0 deletions internal/build/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package build

import (
"strings"
"time"

"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// Snapshot creates a moment in time record of the build
// and attempts to upload it to the server.
func Snapshot(b *library.Build, c *vela.Client, e error, l *logrus.Entry, r *library.Repo) {
// check if the build is not in a canceled status
if !strings.EqualFold(b.GetStatus(), constants.StatusCanceled) {
// check if the error provided is empty
if e != nil {
// populate build fields with error based values
b.SetError(e.Error())
b.SetStatus(constants.StatusError)
b.SetFinished(time.Now().UTC().Unix())
}
}

// check if the logger provided is empty
if l == nil {
// create new logger
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#NewEntry
l = logrus.NewEntry(logrus.StandardLogger())
}

// check if the Vela client provided is empty
if c != nil {
l.Debug("uploading build snapshot")

// send API call to update the build
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#BuildService.Update
_, _, err := c.Build.Update(r.GetOrg(), r.GetName(), b)
if err != nil {
l.Errorf("unable to upload build snapshot: %v", err)
}
}
}
105 changes: 105 additions & 0 deletions internal/build/snapshot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package build

import (
"errors"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/go-vela/mock/server"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/types/library"
)

func TestBuild_Snapshot(t *testing.T) {
// setup types
b := &library.Build{
ID: vela.Int64(1),
Number: vela.Int(1),
Parent: vela.Int(1),
Event: vela.String("push"),
Status: vela.String("success"),
Error: vela.String(""),
Enqueued: vela.Int64(1563474077),
Created: vela.Int64(1563474076),
Started: vela.Int64(1563474077),
Finished: vela.Int64(0),
Deploy: vela.String(""),
Clone: vela.String("https://github.com/github/octocat.git"),
Source: vela.String("https://github.com/github/octocat/abcdefghi123456789"),
Title: vela.String("push received from https://github.com/github/octocat"),
Message: vela.String("First commit..."),
Commit: vela.String("48afb5bdc41ad69bf22588491333f7cf71135163"),
Sender: vela.String("OctoKitty"),
Author: vela.String("OctoKitty"),
Branch: vela.String("master"),
Ref: vela.String("refs/heads/master"),
BaseRef: vela.String(""),
Host: vela.String("example.company.com"),
Runtime: vela.String("docker"),
Distribution: vela.String("linux"),
}

r := &library.Repo{
ID: vela.Int64(1),
Org: vela.String("github"),
Name: vela.String("octocat"),
FullName: vela.String("github/octocat"),
Link: vela.String("https://github.com/github/octocat"),
Clone: vela.String("https://github.com/github/octocat.git"),
Branch: vela.String("master"),
Timeout: vela.Int64(60),
Visibility: vela.String("public"),
Private: vela.Bool(false),
Trusted: vela.Bool(false),
Active: vela.Bool(true),
AllowPull: vela.Bool(false),
AllowPush: vela.Bool(true),
AllowDeploy: vela.Bool(false),
AllowTag: vela.Bool(false),
}

gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

_client, err := vela.NewClient(s.URL, "", nil)
if err != nil {
t.Errorf("unable to create Vela API client: %v", err)
}

tests := []struct {
build *library.Build
client *vela.Client
err error
repo *library.Repo
}{
{
build: b,
client: _client,
err: errors.New("unable to create network"),
repo: r,
},
{
build: nil,
client: _client,
err: errors.New("unable to create network"),
repo: r,
},
{
build: nil,
client: nil,
err: nil,
repo: nil,
},
}

// run test
for _, test := range tests {
Snapshot(test.build, test.client, test.err, nil, test.repo)
}
}
79 changes: 79 additions & 0 deletions internal/build/upload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package build

import (
"strings"
"time"

"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// Upload tracks the final state of the build
// and attempts to upload it to the server.
func Upload(b *library.Build, c *vela.Client, e error, l *logrus.Entry, r *library.Repo) {
// handle the build based off the status provided
switch b.GetStatus() {
// build is in a canceled state
case constants.StatusCanceled:
fallthrough
// build is in a error state
case constants.StatusError:
fallthrough
// build is in a failure state
case constants.StatusFailure:
// if the build is in a canceled, error
// or failure state we DO NOT want to
// update the state to be success
break
// build is in a pending state
case constants.StatusPending:
// if the build is in a pending state
// then something must have gone
// drastically wrong because this
// SHOULD NOT happen
b.SetStatus(constants.StatusKilled)
default:
// update the build with a success state
b.SetStatus(constants.StatusSuccess)
}

// check if the build is not in a canceled status
if !strings.EqualFold(b.GetStatus(), constants.StatusCanceled) {
// check if the error provided is empty
if e != nil {
// update the build with error based values
b.SetError(e.Error())
b.SetStatus(constants.StatusError)
}
}

// update the build with the finished timestamp
b.SetFinished(time.Now().UTC().Unix())

// check if the logger provided is empty
if l == nil {
// create new logger
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#NewEntry
l = logrus.NewEntry(logrus.StandardLogger())
}

// check if the Vela client provided is empty
if c != nil {
l.Debug("uploading final build state")

// send API call to update the build
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#BuildService.Update
_, _, err := c.Build.Update(r.GetOrg(), r.GetName(), b)
if err != nil {
l.Errorf("unable to upload final build state: %v", err)
}
}
}
132 changes: 132 additions & 0 deletions internal/build/upload_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package build

import (
"errors"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/go-vela/mock/server"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/types/library"
)

func TestBuild_Upload(t *testing.T) {
// setup types
_build := &library.Build{
ID: vela.Int64(1),
Number: vela.Int(1),
Parent: vela.Int(1),
Event: vela.String("push"),
Status: vela.String("success"),
Error: vela.String(""),
Enqueued: vela.Int64(1563474077),
Created: vela.Int64(1563474076),
Started: vela.Int64(1563474077),
Finished: vela.Int64(0),
Deploy: vela.String(""),
Clone: vela.String("https://github.com/github/octocat.git"),
Source: vela.String("https://github.com/github/octocat/abcdefghi123456789"),
Title: vela.String("push received from https://github.com/github/octocat"),
Message: vela.String("First commit..."),
Commit: vela.String("48afb5bdc41ad69bf22588491333f7cf71135163"),
Sender: vela.String("OctoKitty"),
Author: vela.String("OctoKitty"),
Branch: vela.String("master"),
Ref: vela.String("refs/heads/master"),
BaseRef: vela.String(""),
Host: vela.String("example.company.com"),
Runtime: vela.String("docker"),
Distribution: vela.String("linux"),
}

_canceled := *_build
_canceled.SetStatus("canceled")

_error := *_build
_error.SetStatus("error")

_pending := *_build
_pending.SetStatus("pending")

_repo := &library.Repo{
ID: vela.Int64(1),
Org: vela.String("github"),
Name: vela.String("octocat"),
FullName: vela.String("github/octocat"),
Link: vela.String("https://github.com/github/octocat"),
Clone: vela.String("https://github.com/github/octocat.git"),
Branch: vela.String("master"),
Timeout: vela.Int64(60),
Visibility: vela.String("public"),
Private: vela.Bool(false),
Trusted: vela.Bool(false),
Active: vela.Bool(true),
AllowPull: vela.Bool(false),
AllowPush: vela.Bool(true),
AllowDeploy: vela.Bool(false),
AllowTag: vela.Bool(false),
}

gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

_client, err := vela.NewClient(s.URL, "", nil)
if err != nil {
t.Errorf("unable to create Vela API client: %v", err)
}

tests := []struct {
build *library.Build
client *vela.Client
err error
repo *library.Repo
}{
{
build: _build,
client: _client,
err: errors.New("unable to create network"),
repo: _repo,
},
{
build: &_canceled,
client: _client,
err: errors.New("unable to create network"),
repo: _repo,
},
{
build: &_error,
client: _client,
err: errors.New("unable to create network"),
repo: _repo,
},
{
build: &_pending,
client: _client,
err: errors.New("unable to create network"),
repo: _repo,
},
{
build: nil,
client: _client,
err: errors.New("unable to create network"),
repo: _repo,
},
{
build: nil,
client: nil,
err: nil,
repo: nil,
},
}

// run test
for _, test := range tests {
Upload(test.build, test.client, test.err, nil, test.repo)
}
}
Loading