Skip to content
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3
github.com/coreos/go-semver v0.3.0
github.com/gin-gonic/gin v1.8.1
github.com/go-vela/server v0.17.0
github.com/go-vela/types v0.17.0
github.com/go-vela/server v0.17.1-0.20230224163114-c4283eb079e8
github.com/go-vela/types v0.17.1-0.20230223155025-1c8a34f71425
github.com/golang-jwt/jwt/v4 v4.4.3
github.com/google/go-cmp v0.5.9
github.com/google/go-querystring v1.1.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-vela/server v0.17.0 h1:sEJ4a9mus43Qw4E431OYwN8qvYugoA0ZmaU0sFGgESk=
github.com/go-vela/server v0.17.0/go.mod h1:Z8YT/IFJTQ80a63GINRHCaFBTkkkHAL7mDohw4xLbEE=
github.com/go-vela/types v0.17.0 h1:nvKBbNO8BSiLtYPMScT0XWosGqEWX85UKSkkclb2DVA=
github.com/go-vela/types v0.17.0/go.mod h1:6KoRkvXMw9DkAcLdtI7PxPqMlT2Bl0DiigQamLGGjwo=
github.com/go-vela/server v0.17.1-0.20230224163114-c4283eb079e8 h1:CUXtUYOu8Jz9IYuTMXivu4GXr8BdQXLAYjFYnDnkndE=
github.com/go-vela/server v0.17.1-0.20230224163114-c4283eb079e8/go.mod h1:WJZQJ2nwSSDuB56LskZ3gEoSWI0IwKRtxaFE8GdTLIc=
github.com/go-vela/types v0.17.1-0.20230223155025-1c8a34f71425 h1:tdjas7NJLWlU2vmETaU36wjbd+zvWPLtznE4uwtnFlw=
github.com/go-vela/types v0.17.1-0.20230223155025-1c8a34f71425/go.mod h1:6KoRkvXMw9DkAcLdtI7PxPqMlT2Bl0DiigQamLGGjwo=
github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v4 v4.4.3 h1:Hxl6lhQFj4AnOX6MLrsCb/+7tCj7DxP7VA+2rDIq5AU=
Expand Down
6 changes: 3 additions & 3 deletions vela/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (svc *AuthenticationService) RefreshAccessToken(refreshToken string) (*Resp
// set the API endpoint path we send the request to
u := "/token-refresh"

v := new(library.Login)
v := new(library.Token)

// building a custom request -
// we can't use svc.client.NewRequest because
Expand Down Expand Up @@ -124,7 +124,7 @@ func (svc *AuthenticationService) AuthenticateWithToken(token string) (string, *
}

// will hold access token
v := new(library.Login)
v := new(library.Token)

// building a custom request -
// we can't use svc.client.NewRequest because
Expand Down Expand Up @@ -157,7 +157,7 @@ func (svc *AuthenticationService) ExchangeTokens(opt *OAuthExchangeOptions) (str
u := "/authenticate"

// will hold access token
v := new(library.Login)
v := new(library.Token)

// check required arguments
if len(opt.Code) == 0 || len(opt.State) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions vela/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestVela_Authentication_RefreshAccessToken(t *testing.T) {

data := []byte(server.TokenRefreshResp)

var want library.Login
var want library.Token
_ = json.Unmarshal(data, &want)

// run test
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestVela_Authentication_AuthenticateWithToken(t *testing.T) {

data := []byte(server.TokenRefreshResp)

var want library.Login
var want library.Token
_ = json.Unmarshal(data, &want)

// run test
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestVela_Authentication_ExchangeTokens(t *testing.T) {

data := []byte(server.TokenRefreshResp)

var want library.Login
var want library.Token
_ = json.Unmarshal(data, &want)

// create options
Expand Down
14 changes: 14 additions & 0 deletions vela/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,17 @@ func (svc *BuildService) Cancel(org, repo string, build int) (*library.Build, *R

return v, resp, err
}

// GetBuildToken returns an auth token for updating build resources.
func (svc *BuildService) GetBuildToken(org, repo string, build int) (*library.Token, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/repos/%s/%s/builds/%d/token", org, repo, build)

// library Token type we want to return
t := new(library.Token)

// send request using client
resp, err := svc.client.Call("GET", u, nil, t)

return t, resp, err
}
94 changes: 94 additions & 0 deletions vela/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,84 @@ func TestBuild_Cancel_404(t *testing.T) {
}
}

func TestBuild_GetBuildToken_200(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

data := []byte(server.BuildTokenResp)

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

// run test
got, resp, err := c.Build.GetBuildToken("github", "octocat", 1)

if err != nil {
t.Errorf("GetBuildToken returned err: %v", err)
}

if resp.StatusCode != http.StatusOK {
t.Errorf("Build returned %v, want %v", resp.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("GetBuildToken is %v, want %v", got, want)
}
}

func TestBuild_GetBuildToken_404(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

var want library.Token

// run test
got, resp, err := c.Build.GetBuildToken("github", "octocat", 0)

if err != nil {
t.Errorf("GetBuildToken returned err: %v", err)
}

if resp.StatusCode != http.StatusNotFound {
t.Errorf("Build returned %v, want %v", resp.StatusCode, http.StatusNotFound)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("GetBuildToken is %v, want %v", got, want)
}
}

func TestBuild_GetBuildToken_400(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

var want library.Token

// run test
got, resp, err := c.Build.GetBuildToken("github", "octocat", 2)

if err != nil {
t.Errorf("GetBuildToken returned err: %v", err)
}

if resp.StatusCode != http.StatusBadRequest {
t.Errorf("Build returned %v, want %v", resp.StatusCode, http.StatusBadRequest)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("GetBuildToken is %v, want %v", got, want)
}
}

func ExampleBuildService_Get() {
// Create a new vela client for interacting with server
c, _ := NewClient("http://localhost:8080", "", nil)
Expand Down Expand Up @@ -565,3 +643,19 @@ func ExampleBuildService_Cancel() {

fmt.Printf("Received response code %d, for github/octocat/1", resp.StatusCode)
}

func ExampleBuildService_GetBuildToken() {
// Create a new vela client for interacting with server
c, _ := NewClient("http://localhost:8080", "", nil)

// Set new token in existing client
c.Authentication.SetPersonalAccessTokenAuth("token")

// Get token for a build from the server
token, resp, err := c.Build.GetBuildToken("github", "octocat", 1)
if err != nil {
fmt.Println(err)
}

fmt.Printf("Received response code %d, for build token %+v", resp.StatusCode, token)
}