diff --git a/woodpecker-go/woodpecker/client.go b/woodpecker-go/woodpecker/client.go index c8976101c..be18d153b 100644 --- a/woodpecker-go/woodpecker/client.go +++ b/woodpecker-go/woodpecker/client.go @@ -54,6 +54,9 @@ const ( pathPipelineQueue = "%s/api/pipelines" pathQueue = "%s/api/queue" pathLogLevel = "%s/api/log-level" + pathAgents = "%s/api/agents" + pathAgent = "%s/api/agents/%d" + pathAgentTasks = "%s/api/agents/%d/tasks" // TODO: implement endpoints // pathFeed = "%s/api/user/feed" // pathVersion = "%s/version" @@ -515,6 +518,41 @@ func (c *client) CronGet(owner, repo string, cronID int64) (*Cron, error) { return out, c.get(uri, out) } +func (c *client) AgentList() ([]*Agent, error) { + out := make([]*Agent, 0, 5) + uri := fmt.Sprintf(pathAgents, c.addr) + return out, c.get(uri, &out) +} + +func (c *client) Agent(agentID int64) (*Agent, error) { + out := new(Agent) + uri := fmt.Sprintf(pathAgent, c.addr, agentID) + return out, c.get(uri, out) +} + +func (c *client) AgentCreate(in *Agent) (*Agent, error) { + out := new(Agent) + uri := fmt.Sprintf(pathAgents, c.addr) + return out, c.post(uri, in, out) +} + +func (c *client) AgentUpdate(in *Agent) (*Agent, error) { + out := new(Agent) + uri := fmt.Sprintf(pathAgent, c.addr, in.ID) + return out, c.patch(uri, in, out) +} + +func (c *client) AgentDelete(agentID int64) error { + uri := fmt.Sprintf(pathAgent, c.addr, agentID) + return c.delete(uri) +} + +func (c *client) AgentTasksList(agentID int64) ([]*Task, error) { + out := make([]*Task, 0, 5) + uri := fmt.Sprintf(pathAgentTasks, c.addr, agentID) + return out, c.get(uri, &out) +} + // // http request helper functions // diff --git a/woodpecker-go/woodpecker/interface.go b/woodpecker-go/woodpecker/interface.go index a75206c7a..04ec9a83a 100644 --- a/woodpecker-go/woodpecker/interface.go +++ b/woodpecker-go/woodpecker/interface.go @@ -198,4 +198,22 @@ type Client interface { // CronUpdate update an existing cron job of a repo CronUpdate(owner, repo string, cron *Cron) (*Cron, error) + + // AgentList returns a list of all registered agents + AgentList() ([]*Agent, error) + + // Agent returns an agent by id + Agent(int64) (*Agent, error) + + // AgentCreate creates a new agent + AgentCreate(*Agent) (*Agent, error) + + // AgentUpdate updates an existing agent + AgentUpdate(*Agent) (*Agent, error) + + // AgentDelete deletes an agent + AgentDelete(int64) error + + // AgentTasksList returns a list of all tasks executed by an agent + AgentTasksList(int64) ([]*Task, error) } diff --git a/woodpecker-go/woodpecker/types.go b/woodpecker-go/woodpecker/types.go index 2995f2b08..2b901ce99 100644 --- a/woodpecker-go/woodpecker/types.go +++ b/woodpecker-go/woodpecker/types.go @@ -196,4 +196,31 @@ type ( Branch string `json:"branch"` Variables map[string]string `json:"variables"` } + + // Agent is the JSON data for an agent + Agent struct { + ID int64 `json:"id"` + Created int64 `json:"created"` + Updated int64 `json:"updated"` + Name string `json:"name"` + OwnerID int64 `json:"owner_id"` + Token string `json:"token"` + LastContact int64 `json:"last_contact"` + Platform string `json:"platform"` + Backend string `json:"backend"` + Capacity int32 `json:"capacity"` + Version string `json:"version"` + NoSchedule bool `json:"no_schedule"` + } + + // Task is the JSON data for a task + Task struct { + ID string `json:"id"` + Data []byte `json:"data"` + Labels map[string]string `json:"labels"` + Dependencies []string `json:"dependencies"` + RunOn []string `json:"run_on"` + DepStatus map[string]string `json:"dep_status"` + AgentID int64 `json:"agent_id"` + } )