mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: Add streaming endpoint for workspace history (#157)
* feat: Add parameter querying to the API * feat: Add streaming endpoint for workspace history Enables a buildlog-like flow for reading job output. * Fix empty parameter source and destination * Add comment for usage of workspace history logs endpoint
This commit is contained in:
@@ -99,6 +99,20 @@ func (c *Client) CreateProjectHistory(ctx context.Context, organization, project
|
||||
return projectVersion, json.NewDecoder(res.Body).Decode(&projectVersion)
|
||||
}
|
||||
|
||||
// ProjectHistoryParameters returns project parameters for history by name.
|
||||
func (c *Client) ProjectHistoryParameters(ctx context.Context, organization, project, history string) ([]coderd.ProjectParameter, error) {
|
||||
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projects/%s/%s/history/%s/parameters", organization, project, history), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, readBodyAsError(res)
|
||||
}
|
||||
var params []coderd.ProjectParameter
|
||||
return params, json.NewDecoder(res.Body).Decode(¶ms)
|
||||
}
|
||||
|
||||
// ProjectParameters returns parameters scoped to a project.
|
||||
func (c *Client) ProjectParameters(ctx context.Context, organization, project string) ([]coderd.ParameterValue, error) {
|
||||
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projects/%s/%s/parameters", organization, project), nil)
|
||||
|
||||
@@ -163,4 +163,12 @@ func TestProjects(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "hi", param.Name)
|
||||
})
|
||||
|
||||
t.Run("HistoryParametersError", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
server := coderdtest.New(t)
|
||||
user := server.RandomInitialUser(t)
|
||||
_, err := server.Client.ProjectHistoryParameters(context.Background(), user.Organization, "nothing", "nope")
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/coder/coder/coderd"
|
||||
)
|
||||
@@ -131,3 +134,74 @@ func (c *Client) CreateWorkspaceHistory(ctx context.Context, owner, workspace st
|
||||
var workspaceHistory coderd.WorkspaceHistory
|
||||
return workspaceHistory, json.NewDecoder(res.Body).Decode(&workspaceHistory)
|
||||
}
|
||||
|
||||
// WorkspaceHistoryLogs returns all logs for workspace history.
|
||||
// To stream logs, use the FollowWorkspaceHistoryLogs function.
|
||||
func (c *Client) WorkspaceHistoryLogs(ctx context.Context, owner, workspace, history string) ([]coderd.WorkspaceHistoryLog, error) {
|
||||
return c.WorkspaceHistoryLogsBetween(ctx, owner, workspace, history, time.Time{}, time.Time{})
|
||||
}
|
||||
|
||||
// WorkspaceHistoryLogsBetween returns logs between a specific time.
|
||||
func (c *Client) WorkspaceHistoryLogsBetween(ctx context.Context, owner, workspace, history string, after, before time.Time) ([]coderd.WorkspaceHistoryLog, error) {
|
||||
if owner == "" {
|
||||
owner = "me"
|
||||
}
|
||||
values := url.Values{}
|
||||
if !after.IsZero() {
|
||||
values["after"] = []string{strconv.FormatInt(after.UTC().UnixMilli(), 10)}
|
||||
}
|
||||
if !before.IsZero() {
|
||||
values["before"] = []string{strconv.FormatInt(before.UTC().UnixMilli(), 10)}
|
||||
}
|
||||
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s/%s/history/%s/logs?%s", owner, workspace, history, values.Encode()), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
defer res.Body.Close()
|
||||
return nil, readBodyAsError(res)
|
||||
}
|
||||
|
||||
var logs []coderd.WorkspaceHistoryLog
|
||||
return logs, json.NewDecoder(res.Body).Decode(&logs)
|
||||
}
|
||||
|
||||
// FollowWorkspaceHistoryLogsAfter returns a stream of workspace history logs.
|
||||
// The channel will close when the workspace history job is no longer active.
|
||||
func (c *Client) FollowWorkspaceHistoryLogsAfter(ctx context.Context, owner, workspace, history string, after time.Time) (<-chan coderd.WorkspaceHistoryLog, error) {
|
||||
afterQuery := ""
|
||||
if !after.IsZero() {
|
||||
afterQuery = fmt.Sprintf("&after=%d", after.UTC().UnixMilli())
|
||||
}
|
||||
if owner == "" {
|
||||
owner = "me"
|
||||
}
|
||||
|
||||
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s/%s/history/%s/logs?follow%s", owner, workspace, history, afterQuery), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
defer res.Body.Close()
|
||||
return nil, readBodyAsError(res)
|
||||
}
|
||||
|
||||
logs := make(chan coderd.WorkspaceHistoryLog)
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
go func() {
|
||||
defer close(logs)
|
||||
var log coderd.WorkspaceHistoryLog
|
||||
for {
|
||||
err = decoder.Decode(&log)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case logs <- log:
|
||||
}
|
||||
}
|
||||
}()
|
||||
return logs, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user