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

feat: add support for schedules #446

Merged
merged 7 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
64 changes: 64 additions & 0 deletions action/schedule/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

//nolint:dupl // ignore similar code among actions
package schedule

import (
"github.com/go-vela/cli/internal/output"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// Add creates a schedule based off the provided configuration.
func (c *Config) Add(client *vela.Client) error {
logrus.Debug("executing add for schedule configuration")

// create the schedule object
s := &library.Schedule{
Active: vela.Bool(c.Active),
Name: vela.String(c.Name),
Entry: vela.String(c.Entry),
}

logrus.Tracef("adding schedule %s/%s/%s", c.Org, c.Repo, c.Name)

// send API call to add a schedule
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#ScheduleService.Add
schedule, _, err := client.Schedule.Add(c.Org, c.Repo, s)
if err != nil {
return err
}

// handle the output based off the provided configuration
switch c.Output {
case output.DriverDump:
// output the schedule in dump format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump
return output.Dump(schedule)
case output.DriverJSON:
// output the schedule in JSON format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON
return output.JSON(schedule)
case output.DriverSpew:
// output the schedule in spew format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew
return output.Spew(schedule)
case output.DriverYAML:
// output the schedule in YAML format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML
return output.YAML(schedule)
default:
// output the schedule in stdout format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
return output.Stdout(schedule)
}
}
114 changes: 114 additions & 0 deletions action/schedule/add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package schedule

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

"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/server/mock/server"
)

func TestSchedule_Config_Add(t *testing.T) {
// setup test server
s := httptest.NewServer(server.FakeHandler())

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

// setup tests
tests := []struct {
name string
failure bool
config *Config
}{
{
name: "success with empty output",
failure: false,
config: &Config{
Action: "add",
Org: "github",
Repo: "octocat",
Active: true,
Name: "foo",
Entry: "@weekly",
Output: "",
},
},
{
name: "success with dump output",
failure: false,
config: &Config{
Action: "add",
Org: "github",
Repo: "octocat",
Active: true,
Name: "foo",
Entry: "@weekly",
Output: "dump",
},
},
{
name: "success with json output",
failure: false,
config: &Config{
Action: "add",
Org: "github",
Repo: "octocat",
Active: true,
Name: "foo",
Entry: "@weekly",
Output: "json",
},
},
{
name: "success with spew output",
failure: false,
config: &Config{
Action: "add",
Org: "github",
Repo: "octocat",
Active: true,
Name: "foo",
Entry: "@weekly",
Output: "spew",
},
},
{
name: "success with yaml output",
failure: false,
config: &Config{
Action: "add",
Org: "github",
Repo: "octocat",
Active: true,
Name: "foo",
Entry: "@weekly",
Output: "yaml",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err = test.config.Add(client)

if test.failure {
if err == nil {
t.Errorf("Add for %s should have returned err", test.name)
}

return
}

if err != nil {
t.Errorf("Add for %s returned err: %v", err, test.name)
}
})
}
}
10 changes: 10 additions & 0 deletions action/schedule/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

// Package schedule provides the defined CLI schedule actions for Vela.
//
// Usage:
//
// import "github.com/go-vela/cli/action/schedule"
package schedule
64 changes: 64 additions & 0 deletions action/schedule/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package schedule

import (
"github.com/go-vela/cli/internal/output"
"github.com/go-vela/sdk-go/vela"
"github.com/sirupsen/logrus"
)

// Get captures a list of schedules based off the provided configuration.
func (c *Config) Get(client *vela.Client) error {
logrus.Debug("executing get for schedule configuration")

// set the pagination options for list of schedules
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#ListOptions
opts := &vela.ListOptions{
Page: c.Page,
PerPage: c.PerPage,
}

logrus.Tracef("capturing schedules for repo %s/%s", c.Org, c.Repo)

// send API call to capture a list of schedules
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#ScheduleService.GetAll
schedules, _, err := client.Schedule.GetAll(c.Org, c.Repo, opts)
if err != nil {
return err
}

// handle the output based off the provided configuration
switch c.Output {
case output.DriverDump:
// output the schedules in dump format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump
return output.Dump(schedules)
case output.DriverJSON:
// output the schedules in JSON format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON
return output.JSON(schedules)
case output.DriverSpew:
// output the schedules in spew format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew
return output.Spew(schedules)
case "wide":
// output the schedules in wide table format
return wideTable(schedules)
case output.DriverYAML:
// output the schedules in YAML format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML
return output.YAML(schedules)
default:
// output the schedules in table format
return table(schedules)
}
}
123 changes: 123 additions & 0 deletions action/schedule/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package schedule

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

"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/server/mock/server"
)

func TestSchedule_Config_Get(t *testing.T) {
// setup test server
s := httptest.NewServer(server.FakeHandler())

// create a vela client
client, err := vela.NewClient(s.URL, "vela", nil)
if err != nil {
t.Errorf("unable to create tes client: %v", err)
jbrockopp marked this conversation as resolved.
Show resolved Hide resolved
}

// setup tests
tests := []struct {
name string
failure bool
config *Config
}{
{
name: "success with empty output",
failure: false,
config: &Config{
Action: "get",
Org: "github",
Repo: "octocat",
Page: 1,
PerPage: 10,
Output: "",
},
},
{
name: "success with dump output",
failure: false,
config: &Config{
Action: "get",
Org: "github",
Repo: "octocat",
Page: 1,
PerPage: 10,
Output: "dump",
},
},
{
name: "success with json output",
failure: false,
config: &Config{
Action: "get",
Org: "github",
Repo: "octocat",
Page: 1,
PerPage: 10,
Output: "json",
},
},
{
name: "success with spew output",
failure: false,
config: &Config{
Action: "get",
Org: "github",
Repo: "octocat",
Page: 1,
PerPage: 10,
Output: "spew",
},
},
{
name: "success with wide output",
failure: false,
config: &Config{
Action: "get",
Org: "github",
Repo: "octocat",
Page: 1,
PerPage: 10,
Output: "wide",
},
},
{
name: "success with yaml output",
failure: false,
config: &Config{
Action: "get",
Org: "github",
Repo: "octocat",
Page: 1,
PerPage: 10,
Output: "yaml",
},
},
}

// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err = test.config.Get(client)

if test.failure {
if err == nil {
t.Errorf("Get for %s should have returned err", test.name)
}

return
}

if err != nil {
t.Errorf("Get for %s returned err: %v", err, test.name)
jbrockopp marked this conversation as resolved.
Show resolved Hide resolved
}
})
}
}
Loading