Skip to content

Commit

Permalink
new types changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire.Nicholas authored and Claire.Nicholas committed Dec 21, 2023
1 parent c8cc0d7 commit df3f450
Show file tree
Hide file tree
Showing 21 changed files with 136 additions and 391 deletions.
4 changes: 3 additions & 1 deletion api/deployment/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package deployment
import (
"fmt"
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
Expand Down Expand Up @@ -83,7 +84,8 @@ func CreateDeployment(c *gin.Context) {

// update fields in deployment object
input.SetRepoID(r.GetID())
input.SetUser(u.GetName())
input.SetCreatedBy(u.GetName())
input.SetCreatedAt(time.Now().Unix())

if len(input.GetDescription()) == 0 {
input.SetDescription("Deployment request from Vela")
Expand Down
52 changes: 46 additions & 6 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package webhook
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)

var baseErr = "unable to process webhook"
Expand Down Expand Up @@ -664,13 +666,51 @@ func PostWebhook(c *gin.Context) {
// set the BuildID field
h.SetBuildID(b.GetID())

// if event is deployment, update the deployment record to include this build
if b.GetEvent() == constants.EventDeploy {
d, _ := database.FromContext(c).GetDeploymentForRepo(c, repo, *webhook.DeploymentID)
builds := new([]library.Build)
*builds = append(*builds, *b)
d.SetBuilds(builds)
logrus.Debugf("PINEAPPLE %d ", webhook.DeploymentID)
database.FromContext(c).UpdateDeployment(c, d)
builds := []*library.Build{}
builds = append(builds, b)

d, err := database.FromContext(c).GetDeploymentForRepo(c, repo, webhook.Deployment.GetNumber())
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
deployment := webhook.Deployment

deployment.SetRepoID(repo.GetID())
deployment.SetBuilds(builds)

_, err := database.FromContext(c).CreateDeployment(c, deployment)
if err != nil {
retErr := fmt.Errorf("%s: failed to create deployment %s/%d: %w", baseErr, repo.GetFullName(), deployment.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)

h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())

return
}
} else {
retErr := fmt.Errorf("%s: failed to get deployment %s/%d: %w", baseErr, repo.GetFullName(), webhook.Deployment.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)

h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())

return
}
} else {
d.SetBuilds(builds)
_, err := database.FromContext(c).UpdateDeployment(c, d)
if err != nil {
retErr := fmt.Errorf("%s: failed to update deployment %s/%d: %w", baseErr, repo.GetFullName(), d.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)

h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())

return
}
}
}

c.JSON(http.StatusOK, b)
Expand Down
8 changes: 5 additions & 3 deletions database/deployment/count_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,37 @@ import (
)

func TestDeployment_Engine_CountDeploymentssForRepo(t *testing.T) {
builds := new([]library.Build)
builds := []*library.Build{}

// setup types
_deploymentOne := testDeployment()
_deploymentOne.SetID(1)
_deploymentOne.SetRepoID(1)
_deploymentOne.SetNumber(1)
_deploymentOne.SetURL("https://github.com/github/octocat/deployments/1")
_deploymentOne.SetUser("octocat")
_deploymentOne.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163")
_deploymentOne.SetRef("refs/heads/master")
_deploymentOne.SetTask("vela-deploy")
_deploymentOne.SetTarget("production")
_deploymentOne.SetDescription("Deployment request from Vela")
_deploymentOne.SetPayload(map[string]string{"foo": "test1"})
_deploymentOne.SetCreatedAt(1)
_deploymentOne.SetCreatedBy("octocat")
_deploymentOne.SetBuilds(builds)

_deploymentTwo := testDeployment()
_deploymentTwo.SetID(2)
_deploymentTwo.SetRepoID(2)
_deploymentTwo.SetNumber(2)
_deploymentTwo.SetURL("https://github.com/github/octocat/deployments/2")
_deploymentTwo.SetUser("octocat")
_deploymentTwo.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135164")
_deploymentTwo.SetRef("refs/heads/master")
_deploymentTwo.SetTask("vela-deploy")
_deploymentTwo.SetTarget("production")
_deploymentTwo.SetDescription("Deployment request from Vela")
_deploymentTwo.SetPayload(map[string]string{"foo": "test1"})
_deploymentTwo.SetCreatedAt(1)
_deploymentTwo.SetCreatedBy("octocat")
_deploymentTwo.SetBuilds(builds)

_repo := testRepo()
Expand Down
10 changes: 6 additions & 4 deletions database/deployment/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,38 @@ func TestDeployment_Engine_CountDeployments(t *testing.T) {
buildOne.SetRuntime("docker")
buildOne.SetDistribution("linux")

builds := new([]library.Build)
*builds = append(*builds, *buildOne)
builds := []*library.Build{}
builds = append(builds, buildOne)

// setup types
_deploymentOne := testDeployment()
_deploymentOne.SetID(1)
_deploymentOne.SetRepoID(1)
_deploymentOne.SetNumber(1)
_deploymentOne.SetURL("https://github.com/github/octocat/deployments/1")
_deploymentOne.SetUser("octocat")
_deploymentOne.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163")
_deploymentOne.SetRef("refs/heads/master")
_deploymentOne.SetTask("vela-deploy")
_deploymentOne.SetTarget("production")
_deploymentOne.SetDescription("Deployment request from Vela")
_deploymentOne.SetPayload(map[string]string{"foo": "test1"})
_deploymentOne.SetCreatedAt(1)
_deploymentOne.SetCreatedBy("octocat")
_deploymentOne.SetBuilds(builds)

_deploymentTwo := testDeployment()
_deploymentTwo.SetID(2)
_deploymentTwo.SetRepoID(2)
_deploymentTwo.SetNumber(2)
_deploymentTwo.SetURL("https://github.com/github/octocat/deployments/2")
_deploymentTwo.SetUser("octocat")
_deploymentTwo.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135164")
_deploymentTwo.SetRef("refs/heads/master")
_deploymentTwo.SetTask("vela-deploy")
_deploymentTwo.SetTarget("production")
_deploymentTwo.SetDescription("Deployment request from Vela")
_deploymentTwo.SetPayload(map[string]string{"foo": "test1"})
_deploymentTwo.SetCreatedAt(1)
_deploymentTwo.SetCreatedBy("octocat")
_deploymentTwo.SetBuilds(builds)

_postgres, _mock := testPostgres(t)
Expand Down
11 changes: 6 additions & 5 deletions database/deployment/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ import (
)

func TestDeployment_Engine_CreateDeployment(t *testing.T) {
builds := new([]library.Build)
builds := []*library.Build{}

// setup types
_deploymentOne := testDeployment()
_deploymentOne.SetID(1)
_deploymentOne.SetRepoID(1)
_deploymentOne.SetNumber(1)
_deploymentOne.SetURL("https://github.com/github/octocat/deployments/1")
_deploymentOne.SetUser("octocat")
_deploymentOne.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163")
_deploymentOne.SetRef("refs/heads/master")
_deploymentOne.SetTask("vela-deploy")
_deploymentOne.SetTarget("production")
_deploymentOne.SetDescription("Deployment request from Vela")
_deploymentOne.SetPayload(map[string]string{"foo": "test1"})
_deploymentOne.SetCreatedAt(1)
_deploymentOne.SetCreatedBy("octocat")
_deploymentOne.SetBuilds(builds)

_postgres, _mock := testPostgres(t)
Expand All @@ -38,9 +39,9 @@ func TestDeployment_Engine_CreateDeployment(t *testing.T) {

// ensure the mock expects the query
_mock.ExpectQuery(`INSERT INTO "deployments"
("number","repo_id","url","user","commit","ref","task","target","description","payload","builds","id")
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) RETURNING "id"`).
WithArgs(1, 1, "https://github.com/github/octocat/deployments/1", "octocat", "48afb5bdc41ad69bf22588491333f7cf71135163", "refs/heads/master", "vela-deploy", "production", "Deployment request from Vela", "{\"foo\":\"test1\"}", "{}", 1).
("number","repo_id","url","commit","ref","task","target","description","payload","created_at","created_by","builds","id")
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13) RETURNING "id"`).
WithArgs(1, 1, "https://github.com/github/octocat/deployments/1", "48afb5bdc41ad69bf22588491333f7cf71135163", "refs/heads/master", "vela-deploy", "production", "Deployment request from Vela", "{\"foo\":\"test1\"}", 1, "octocat", "{}", 1).
WillReturnRows(_rows)

_sqlite := testSqlite(t)
Expand Down
5 changes: 3 additions & 2 deletions database/deployment/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ import (
)

func TestDeployment_Engine_DeleteDeployment(t *testing.T) {
builds := new([]library.Build)
builds := []*library.Build{}

// setup types
_deploymentOne := testDeployment()
_deploymentOne.SetID(1)
_deploymentOne.SetRepoID(1)
_deploymentOne.SetNumber(1)
_deploymentOne.SetURL("https://github.com/github/octocat/deployments/1")
_deploymentOne.SetUser("octocat")
_deploymentOne.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163")
_deploymentOne.SetRef("refs/heads/master")
_deploymentOne.SetTask("vela-deploy")
_deploymentOne.SetTarget("production")
_deploymentOne.SetDescription("Deployment request from Vela")
_deploymentOne.SetPayload(map[string]string{"foo": "test1"})
_deploymentOne.SetCreatedAt(1)
_deploymentOne.SetCreatedBy("octocat")
_deploymentOne.SetBuilds(builds)

_postgres, _mock := testPostgres(t)
Expand Down
6 changes: 4 additions & 2 deletions database/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,21 @@ func testSqlite(t *testing.T) *engine {
// testDeployment is a test helper function to create a library
// Deployment type with all fields set to their zero values.
func testDeployment() *library.Deployment {
builds := []*library.Build{}
return &library.Deployment{
ID: new(int64),
RepoID: new(int64),
Number: new(int64),
URL: new(string),
User: new(string),
Commit: new(string),
Ref: new(string),
Task: new(string),
Target: new(string),
Description: new(string),
Payload: new(raw.StringSliceMap),
Builds: new([]library.Build),
CreatedAt: new(int64),
CreatedBy: new(string),
Builds: builds,
}
}

Expand Down
4 changes: 2 additions & 2 deletions database/deployment/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (e *engine) GetDeployment(ctx context.Context, id int64) (*library.Deployme
return nil, err
}

builds := new([]library.Build)
builds := []*library.Build{}

for _, a := range d.Builds {
bID, err := strconv.ParseInt(a, 10, 64)
Expand All @@ -50,7 +50,7 @@ func (e *engine) GetDeployment(ctx context.Context, id int64) (*library.Deployme
return nil, err
}

*builds = append(*builds, *b.ToLibrary())
builds = append(builds, b.ToLibrary())
}

// return the deployment
Expand Down
4 changes: 2 additions & 2 deletions database/deployment/get_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (e *engine) GetDeploymentForRepo(ctx context.Context, r *library.Repo, numb
return nil, err
}

builds := new([]library.Build)
builds := []*library.Build{}

for _, a := range d.Builds {
bID, err := strconv.ParseInt(a, 10, 64)
Expand All @@ -56,7 +56,7 @@ func (e *engine) GetDeploymentForRepo(ctx context.Context, r *library.Repo, numb
return nil, err
}

*builds = append(*builds, *b.ToLibrary())
builds = append(builds, b.ToLibrary())
}

return d.ToLibrary(builds), nil
Expand Down
9 changes: 5 additions & 4 deletions database/deployment/get_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ import (
)

func TestDeployment_Engine_GetDeploymentForRepo(t *testing.T) {
builds := new([]library.Build)
builds := []*library.Build{}

// setup types
_deploymentOne := testDeployment()
_deploymentOne.SetID(1)
_deploymentOne.SetRepoID(1)
_deploymentOne.SetNumber(1)
_deploymentOne.SetURL("https://github.com/github/octocat/deployments/1")
_deploymentOne.SetUser("octocat")
_deploymentOne.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163")
_deploymentOne.SetRef("refs/heads/master")
_deploymentOne.SetTask("vela-deploy")
_deploymentOne.SetTarget("production")
_deploymentOne.SetDescription("Deployment request from Vela")
_deploymentOne.SetPayload(map[string]string{"foo": "test1"})
_deploymentOne.SetCreatedAt(1)
_deploymentOne.SetCreatedBy("octocat")
_deploymentOne.SetBuilds(builds)

_repo := testRepo()
Expand All @@ -43,8 +44,8 @@ func TestDeployment_Engine_GetDeploymentForRepo(t *testing.T) {

// create expected result in mock
_rows := sqlmock.NewRows(
[]string{"id", "repo_id", "number", "url", "user", "commit", "ref", "task", "target", "description", "payload", "builds"}).
AddRow(1, 1, 1, "https://github.com/github/octocat/deployments/1", "octocat", "48afb5bdc41ad69bf22588491333f7cf71135163", "refs/heads/master", "vela-deploy", "production", "Deployment request from Vela", "{\"foo\":\"test1\"}", "{}")
[]string{"id", "repo_id", "number", "url", "commit", "ref", "task", "target", "description", "payload", "created_at", "created_by", "builds"}).
AddRow(1, 1, 1, "https://github.com/github/octocat/deployments/1", "48afb5bdc41ad69bf22588491333f7cf71135163", "refs/heads/master", "vela-deploy", "production", "Deployment request from Vela", "{\"foo\":\"test1\"}", 1, "octocat", "{}")

// ensure the mock expects the query
_mock.ExpectQuery(`SELECT * FROM "deployments" WHERE repo_id = $1 AND number = $2 LIMIT 1`).WithArgs(1, 1).WillReturnRows(_rows)
Expand Down
9 changes: 5 additions & 4 deletions database/deployment/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,31 @@ import (
)

func TestDeployment_Engine_GetDeployment(t *testing.T) {
builds := new([]library.Build)
builds := []*library.Build{}

// setup types
_deploymentOne := testDeployment()
_deploymentOne.SetID(1)
_deploymentOne.SetRepoID(1)
_deploymentOne.SetNumber(1)
_deploymentOne.SetURL("https://github.com/github/octocat/deployments/1")
_deploymentOne.SetUser("octocat")
_deploymentOne.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163")
_deploymentOne.SetRef("refs/heads/master")
_deploymentOne.SetTask("vela-deploy")
_deploymentOne.SetTarget("production")
_deploymentOne.SetDescription("Deployment request from Vela")
_deploymentOne.SetPayload(map[string]string{"foo": "test1"})
_deploymentOne.SetCreatedAt(1)
_deploymentOne.SetCreatedBy("octocat")
_deploymentOne.SetBuilds(builds)

_postgres, _mock := testPostgres(t)
defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }()

// create expected result in mock
_rows := sqlmock.NewRows(
[]string{"id", "repo_id", "number", "url", "user", "commit", "ref", "task", "target", "description", "payload", "builds"}).
AddRow(1, 1, 1, "https://github.com/github/octocat/deployments/1", "octocat", "48afb5bdc41ad69bf22588491333f7cf71135163", "refs/heads/master", "vela-deploy", "production", "Deployment request from Vela", "{\"foo\":\"test1\"}", "{}")
[]string{"id", "repo_id", "number", "url", "commit", "ref", "task", "target", "description", "payload", "created_at", "created_by", "builds"}).
AddRow(1, 1, 1, "https://github.com/github/octocat/deployments/1", "48afb5bdc41ad69bf22588491333f7cf71135163", "refs/heads/master", "vela-deploy", "production", "Deployment request from Vela", "{\"foo\":\"test1\"}", 1, "octocat", "{}")

// ensure the mock expects the query
_mock.ExpectQuery(`SELECT * FROM "deployments" WHERE id = $1 LIMIT 1`).WithArgs(1).WillReturnRows(_rows)
Expand Down
4 changes: 2 additions & 2 deletions database/deployment/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (e *engine) ListDeployments(ctx context.Context) ([]*library.Deployment, er
// https://golang.org/doc/faq#closures_and_goroutines
tmp := deployment

builds := new([]library.Build)
builds := []*library.Build{}

for _, a := range tmp.Builds {
bID, err := strconv.ParseInt(a, 10, 64)
Expand All @@ -60,7 +60,7 @@ func (e *engine) ListDeployments(ctx context.Context) ([]*library.Deployment, er
if err2 != nil {
return nil, err
}
*builds = append(*builds, *b.ToLibrary())
builds = append(builds, b.ToLibrary())
}

// convert query result to library type
Expand Down
Loading

0 comments on commit df3f450

Please sign in to comment.