Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(build): nested API + migration to server types #308

Merged
merged 2 commits into from
Apr 24, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835
github.com/coreos/go-semver v0.3.1
github.com/gin-gonic/gin v1.9.1
github.com/go-vela/server v0.23.4-0.20240412180845-04f930869c93
github.com/go-vela/server v0.23.4-0.20240424144436-b55aa2bb3684
github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/go-cmp v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-vela/server v0.23.4-0.20240412180845-04f930869c93 h1:U3w0Cj2/I4X6dr3+usBc8E4Mmvd+uRmeTPdlDfyJwdg=
github.com/go-vela/server v0.23.4-0.20240412180845-04f930869c93/go.mod h1:QV9JFv+LdpAgkRJhHE92dh4vVdh0kNv8OJnyOLt++84=
github.com/go-vela/server v0.23.4-0.20240424144436-b55aa2bb3684 h1:O0gfupNx7aYPCCrXwVuisrbbqbVkvrRqRBAkSg1bIww=
github.com/go-vela/server v0.23.4-0.20240424144436-b55aa2bb3684/go.mod h1:QV9JFv+LdpAgkRJhHE92dh4vVdh0kNv8OJnyOLt++84=
github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7 h1:3mN7ej69dMH3Vis3G/tPLzLL0Rfp8nR5qd0gpj5ejRM=
github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7/go.mod h1:mEF9dLkk00rUXf/t39n2WvXZgJbxnPEEWy+DHqIlRUo=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
Expand Down
8 changes: 4 additions & 4 deletions vela/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ type CleanOptions struct {
}

// Update modifies a build with the provided details.
func (svc *AdminBuildService) Update(b *library.Build) (*library.Build, *Response, error) {
func (svc *AdminBuildService) Update(b *api.Build) (*api.Build, *Response, error) {
// set the API endpoint path we send the request to
u := "/api/v1/admin/build"

// library Build type we want to return
v := new(library.Build)
v := new(api.Build)

// send request using client
resp, err := svc.client.Call("PUT", u, b, v)
Expand All @@ -119,7 +119,7 @@ func (svc *AdminCleanService) Clean(e *types.Error, opt *CleanOptions) (*string,
}

// GetQueue returns the list of builds in pending and running status.
func (svc *AdminBuildService) GetQueue(opt *GetQueueOptions) (*[]library.BuildQueue, *Response, error) {
func (svc *AdminBuildService) GetQueue(opt *GetQueueOptions) (*[]api.QueueBuild, *Response, error) {
// set the API endpoint path we send the request to
u := "/api/v1/admin/builds/queue"

Expand All @@ -130,7 +130,7 @@ func (svc *AdminBuildService) GetQueue(opt *GetQueueOptions) (*[]library.BuildQu
}

// BuildQueue type we want to return
v := new([]library.BuildQueue)
v := new([]api.QueueBuild)

resp, err := svc.client.Call("GET", u, nil, v)

Expand Down
6 changes: 3 additions & 3 deletions vela/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func TestAdmin_Build_Update_200(t *testing.T) {

data := []byte(server.BuildResp)

var want library.Build
var want api.Build
_ = json.Unmarshal(data, &want)

req := library.Build{
req := api.Build{
Number: Int(1),
Parent: Int(1),
Event: String("push"),
Expand Down Expand Up @@ -391,7 +391,7 @@ func TestAdmin_Build_Queue_200(t *testing.T) {

data := []byte(server.BuildQueueResp)

var want *[]library.BuildQueue
var want *[]api.QueueBuild

err := json.Unmarshal(data, &want)
if err != nil {
Expand Down
29 changes: 15 additions & 14 deletions vela/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vela
import (
"fmt"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/types/library"
)

Expand All @@ -25,12 +26,12 @@ type BuildListOptions struct {
}

// Get returns the provided build.
func (svc *BuildService) Get(org, repo string, build int) (*library.Build, *Response, error) {
func (svc *BuildService) Get(org, repo string, build int) (*api.Build, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/repos/%s/%s/builds/%d", org, repo, build)

// library Build type we want to return
v := new(library.Build)
v := new(api.Build)

// send request using client
resp, err := svc.client.Call("GET", u, nil, v)
Expand All @@ -53,7 +54,7 @@ func (svc *BuildService) GetBuildExecutable(org, repo string, build int) (*libra
}

// GetAll returns a list of all builds.
func (svc *BuildService) GetAll(org, repo string, opt *BuildListOptions) (*[]library.Build, *Response, error) {
func (svc *BuildService) GetAll(org, repo string, opt *BuildListOptions) (*[]api.Build, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/repos/%s/%s/builds", org, repo)

Expand All @@ -64,7 +65,7 @@ func (svc *BuildService) GetAll(org, repo string, opt *BuildListOptions) (*[]lib
}

// slice library Build type we want to return
v := new([]library.Build)
v := new([]api.Build)

// send request using client
resp, err := svc.client.Call("GET", u, nil, v)
Expand Down Expand Up @@ -93,12 +94,12 @@ func (svc *BuildService) GetLogs(org, repo string, build int, opt *ListOptions)
}

// Add constructs a build with the provided details.
func (svc *BuildService) Add(org, repo string, b *library.Build) (*library.Build, *Response, error) {
func (svc *BuildService) Add(b *api.Build) (*api.Build, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/repos/%s/%s/builds", org, repo)
u := fmt.Sprintf("/api/v1/repos/%s/%s/builds", b.GetRepo().GetOrg(), b.GetRepo().GetName())

// library Build type we want to return
v := new(library.Build)
v := new(api.Build)

// send request using client
resp, err := svc.client.Call("POST", u, b, v)
Expand All @@ -107,12 +108,12 @@ func (svc *BuildService) Add(org, repo string, b *library.Build) (*library.Build
}

// Update modifies a build with the provided details.
func (svc *BuildService) Update(org, repo string, b *library.Build) (*library.Build, *Response, error) {
func (svc *BuildService) Update(b *api.Build) (*api.Build, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/repos/%s/%s/builds/%d", org, repo, b.GetNumber())
u := fmt.Sprintf("/api/v1/repos/%s/%s/builds/%d", b.GetRepo().GetOrg(), b.GetRepo().GetName(), b.GetNumber())

// library Build type we want to return
v := new(library.Build)
v := new(api.Build)

// send request using client
resp, err := svc.client.Call("PUT", u, b, v)
Expand All @@ -135,12 +136,12 @@ func (svc *BuildService) Remove(org, repo string, build int) (*string, *Response
}

// Restart takes the build provided and restarts it.
func (svc *BuildService) Restart(org, repo string, build int) (*library.Build, *Response, error) {
func (svc *BuildService) Restart(org, repo string, build int) (*api.Build, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/repos/%s/%s/builds/%d", org, repo, build)

// library Build type we want to return
v := new(library.Build)
v := new(api.Build)

// send request using client
resp, err := svc.client.Call("POST", u, nil, v)
Expand All @@ -149,12 +150,12 @@ func (svc *BuildService) Restart(org, repo string, build int) (*library.Build, *
}

// Cancel takes the build provided and cancels it.
func (svc *BuildService) Cancel(org, repo string, build int) (*library.Build, *Response, error) {
func (svc *BuildService) Cancel(org, repo string, build int) (*api.Build, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/repos/%s/%s/builds/%d/cancel", org, repo, build)

// library Build type we want to return
v := new(library.Build)
v := new(api.Build)

// send request using client
resp, err := svc.client.Call("DELETE", u, nil, v)
Expand Down
53 changes: 34 additions & 19 deletions vela/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/gin-gonic/gin"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/mock/server"
"github.com/go-vela/types/library"
)
Expand All @@ -26,7 +27,7 @@ func TestBuild_Get_200(t *testing.T) {

data := []byte(server.BuildResp)

var want library.Build
var want api.Build
_ = json.Unmarshal(data, &want)

// run test
Expand All @@ -52,7 +53,7 @@ func TestBuild_Get_404(t *testing.T) {
s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

want := library.Build{}
want := api.Build{}

// run test
got, resp, err := c.Build.Get("github", "octocat", 0)
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestBuild_GetAll_200(t *testing.T) {

data := []byte(server.BuildsResp)

var want []library.Build
var want []api.Build
_ = json.Unmarshal(data, &want)

// run test
Expand Down Expand Up @@ -213,11 +214,15 @@ func TestBuild_Add_201(t *testing.T) {

data := []byte(server.BuildResp)

var want library.Build
var want api.Build
_ = json.Unmarshal(data, &want)

req := library.Build{
Number: Int(1),
req := api.Build{
Number: Int(1),
Repo: &api.Repo{
Org: String("github"),
Name: String("octocat"),
},
Parent: Int(1),
Event: String("push"),
Status: String("created"),
Expand Down Expand Up @@ -245,7 +250,7 @@ func TestBuild_Add_201(t *testing.T) {
}

// run test
got, resp, err := c.Build.Add("github", "octocat", &req)
got, resp, err := c.Build.Add(&req)

if err != nil {
t.Errorf("New returned err: %v", err)
Expand All @@ -269,18 +274,22 @@ func TestBuild_Update_200(t *testing.T) {

data := []byte(server.BuildResp)

var want library.Build
var want api.Build
_ = json.Unmarshal(data, &want)

req := library.Build{
req := api.Build{
Number: Int(1),
Repo: &api.Repo{
Org: String("github"),
Name: String("octocat"),
},
Parent: Int(1),
Event: String("push"),
Status: String("running"),
}

// run test
got, resp, err := c.Build.Update("github", "octocat", &req)
got, resp, err := c.Build.Update(&req)

if err != nil {
t.Errorf("New returned err: %v", err)
Expand All @@ -302,17 +311,21 @@ func TestBuild_Update_404(t *testing.T) {
s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

want := library.Build{}
want := api.Build{}

req := library.Build{
req := api.Build{
Number: Int(0),
Repo: &api.Repo{
Org: String("github"),
Name: String("octocat"),
},
Parent: Int(1),
Event: String("push"),
Status: String("running"),
}

// run test
got, resp, err := c.Build.Update("github", "octocat", &req)
got, resp, err := c.Build.Update(&req)

if err == nil {
t.Errorf("New returned err: %v", err)
Expand Down Expand Up @@ -374,7 +387,7 @@ func TestBuild_Restart_200(t *testing.T) {

data := []byte(server.BuildResp)

var want library.Build
var want api.Build
_ = json.Unmarshal(data, &want)

// run test
Expand All @@ -400,7 +413,7 @@ func TestBuild_Restart_404(t *testing.T) {
s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

want := library.Build{}
want := api.Build{}

// run test
got, resp, err := c.Build.Restart("github", "octocat", 0)
Expand Down Expand Up @@ -627,8 +640,9 @@ func ExampleBuildService_Add() {
// Set new token in existing client
c.Authentication.SetPersonalAccessTokenAuth("token")

req := library.Build{
req := api.Build{
Number: Int(1),
Repo: &api.Repo{Org: String("github"), Name: String("octocat")},
Parent: Int(1),
Event: String("push"),
Status: String("created"),
Expand Down Expand Up @@ -656,7 +670,7 @@ func ExampleBuildService_Add() {
}

// Create the build in the server
build, resp, err := c.Build.Add("github", "octocat", &req)
build, resp, err := c.Build.Add(&req)
if err != nil {
fmt.Println(err)
}
Expand All @@ -671,13 +685,14 @@ func ExampleBuildService_Update() {
// Set new token in existing client
c.Authentication.SetPersonalAccessTokenAuth("token")

req := library.Build{
req := api.Build{
Repo: &api.Repo{Org: String("github"), Name: String("octocat")},
Status: String("error"),
Error: String(""),
}

// Update the step in the server
build, resp, err := c.Build.Update("github", "octocat", &req)
build, resp, err := c.Build.Update(&req)
if err != nil {
fmt.Println(err)
}
Expand Down