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
71 changes: 71 additions & 0 deletions api/client/tasks_v1_alpha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package client

import (
"errors"
"fmt"
"net/url"

models "github.com/semaphoreci/cli/api/models"
)

type TasksApiV1AlphaApi struct {
BaseClient BaseClient
ResourceNameSingular string
ResourceNamePlural string
}

func NewTasksV1AlphaApi() TasksApiV1AlphaApi {
baseClient := NewBaseClientFromConfig()
baseClient.SetApiVersion("v1alpha")

return TasksApiV1AlphaApi{
BaseClient: baseClient,
ResourceNamePlural: "tasks",
ResourceNameSingular: "task",
}
}

func (c *TasksApiV1AlphaApi) ListTasks(projectID string) (models.TaskListV1Alpha, error) {
query := url.Values{}
query.Add("project_id", projectID)

body, status, _, err := c.BaseClient.ListWithParams(c.ResourceNamePlural, query)

if err != nil {
return nil, errors.New(fmt.Sprintf("connecting to Semaphore failed '%s'", err))
}

if status != 200 {
return nil, errors.New(fmt.Sprintf("http status %d with message \"%s\" received from upstream", status, body))
}

return models.NewTaskListV1AlphaFromJSON(body)
}

func (c *TasksApiV1AlphaApi) DescribeTask(id string) (*models.TaskDescribeV1Alpha, error) {
body, status, err := c.BaseClient.Get(c.ResourceNamePlural, id)

if err != nil {
return nil, errors.New(fmt.Sprintf("connecting to Semaphore failed '%s'", err))
}

if status != 200 {
return nil, errors.New(fmt.Sprintf("http status %d with message \"%s\" received from upstream", status, body))
}

return models.NewTaskDescribeV1AlphaFromJSON(body)
}

func (c *TasksApiV1AlphaApi) RunTask(id string, requestBody []byte) (*models.RunTaskResponse, error) {
body, status, err := c.BaseClient.PostAction(c.ResourceNamePlural, id, "run_now", requestBody)

if err != nil {
return nil, errors.New(fmt.Sprintf("connecting to Semaphore failed '%s'", err))
}

if status != 200 {
return nil, errors.New(fmt.Sprintf("http status %d with message \"%s\" received from upstream", status, body))
}

return models.NewRunTaskResponseFromJSON(body)
}
92 changes: 92 additions & 0 deletions api/models/tasks_v1_alpha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package models

import (
"encoding/json"
)

const (
RunTaskRefBranch = "BRANCH"
RunTaskRefTag = "TAG"
)

type TaskParameterV1Alpha struct {
Name string `json:"name" yaml:"name"`
Required bool `json:"required" yaml:"required"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
DefaultValue string `json:"default_value,omitempty" yaml:"default_value,omitempty"`
Options []string `json:"options,omitempty" yaml:"options,omitempty"`
}

type TaskV1Alpha struct {
ID string `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
ProjectID string `json:"project_id" yaml:"project_id"`
Branch string `json:"branch,omitempty" yaml:"branch,omitempty"`
At string `json:"at,omitempty" yaml:"at,omitempty"`
PipelineFile string `json:"pipeline_file" yaml:"pipeline_file"`
RequesterID string `json:"requester_id,omitempty" yaml:"requester_id,omitempty"`
UpdatedAt string `json:"updated_at,omitempty" yaml:"updated_at,omitempty"`
Paused bool `json:"paused" yaml:"paused"`
Suspended bool `json:"suspended" yaml:"suspended"`
Recurring bool `json:"recurring" yaml:"recurring"`
Parameters []TaskParameterV1Alpha `json:"parameters,omitempty" yaml:"parameters,omitempty"`
}

type TriggerV1Alpha struct {
TriggeredAt string `json:"triggered_at" yaml:"triggered_at"`
SchedulingStatus string `json:"scheduling_status" yaml:"scheduling_status"`
ScheduledWorkflowID string `json:"scheduled_workflow_id,omitempty" yaml:"scheduled_workflow_id,omitempty"`
Branch string `json:"branch,omitempty" yaml:"branch,omitempty"`
PipelineFile string `json:"pipeline_file,omitempty" yaml:"pipeline_file,omitempty"`
ErrorDescription string `json:"error_description,omitempty" yaml:"error_description,omitempty"`
}

type TaskListV1Alpha []TaskV1Alpha

type TaskDescribeV1Alpha struct {
Schedule TaskV1Alpha `json:"schedule" yaml:"schedule"`
Triggers []TriggerV1Alpha `json:"triggers,omitempty" yaml:"triggers,omitempty"`
}

type RunTaskReference struct {
Type string `json:"type" yaml:"type"`
Name string `json:"name" yaml:"name"`
}

type RunTaskRequest struct {
Reference *RunTaskReference `json:"reference,omitempty" yaml:"reference,omitempty"`
PipelineFile string `json:"pipeline_file,omitempty" yaml:"pipeline_file,omitempty"`
Parameters map[string]string `json:"parameters,omitempty" yaml:"parameters,omitempty"`
}

type RunTaskResponse struct {
WorkflowID string `json:"workflow_id" yaml:"workflow_id"`
}

func NewTaskListV1AlphaFromJSON(data []byte) (TaskListV1Alpha, error) {
var list TaskListV1Alpha
err := json.Unmarshal(data, &list)
if err != nil {
return nil, err
}
return list, nil
}

func NewTaskDescribeV1AlphaFromJSON(data []byte) (*TaskDescribeV1Alpha, error) {
t := TaskDescribeV1Alpha{}
err := json.Unmarshal(data, &t)
if err != nil {
return nil, err
}
return &t, nil
}

func NewRunTaskResponseFromJSON(data []byte) (*RunTaskResponse, error) {
r := RunTaskResponse{}
err := json.Unmarshal(data, &r)
if err != nil {
return nil, err
}
return &r, nil
}
Loading