Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2b100e6
Add endpoint for getting build info
code-asher Apr 5, 2022
6fef81f
Add build info XService
code-asher Apr 5, 2022
c79bdd2
Add version to page footer
code-asher Apr 5, 2022
201c264
Sort buildinfo route alphabetically
code-asher Apr 5, 2022
63dfcf1
Unnest buildinfo route test
code-asher Apr 5, 2022
7fc1570
Move buildinfo route into coderd.go
code-asher Apr 5, 2022
0eac736
Ensure 200 on buildinfo route
code-asher Apr 6, 2022
0479d34
Add frontend mock for buildinfo route
code-asher Apr 6, 2022
123d523
Sort BuildInfoResponse alphabetically
code-asher Apr 6, 2022
cfc4bb8
Inline buildinfo route handler
code-asher Apr 6, 2022
fc65c10
Skip version line when there is no build info
code-asher Apr 6, 2022
aa96e28
Add language object to footer
code-asher Apr 6, 2022
409ff77
Lift buildinfo
code-asher Apr 6, 2022
57f6787
Add external url to build info response
code-asher Apr 6, 2022
987b89d
Link footer build info to external url
code-asher Apr 6, 2022
4c31058
Add TODO for adding retry to frontend API calls
code-asher Apr 6, 2022
86d4a01
Add missing return type
code-asher Apr 6, 2022
d540e09
Merge remote-tracking branch 'origin/main' into asher/footer
code-asher Apr 6, 2022
228ab5e
Run make fmt
code-asher Apr 6, 2022
78d3e2e
Merge remote-tracking branch 'origin/main' into asher/footer
code-asher Apr 6, 2022
49adf16
Remove todo in favor of GitHub issue
code-asher Apr 7, 2022
55833fb
Clear build info on error
code-asher Apr 7, 2022
20c7be3
Add success and failure states to buildInfo machine
code-asher Apr 7, 2022
adfea9e
Merge remote-tracking branch 'origin/main' into asher/footer
code-asher Apr 7, 2022
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
13 changes: 13 additions & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import (
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"google.golang.org/api/idtoken"

chitrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/go-chi/chi.v5"

"cdr.dev/slog"
"github.com/coder/coder/cli/buildinfo"
"github.com/coder/coder/coderd/awsidentity"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/httpmw"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/site"
)

Expand Down Expand Up @@ -57,6 +60,9 @@ func New(options *Options) (http.Handler, func()) {
Message: "👋",
})
})
r.Route("/buildinfo", func(r chi.Router) {
r.Get("/", api.buildInfo)
})
r.Route("/files", func(r chi.Router) {
r.Use(
httpmw.ExtractAPIKey(options.Database, nil),
Expand Down Expand Up @@ -208,3 +214,10 @@ type api struct {
websocketWaitMutex sync.Mutex
websocketWaitGroup sync.WaitGroup
}

func (*api) buildInfo(rw http.ResponseWriter, r *http.Request) {
render.Status(r, http.StatusOK)
render.JSON(rw, r, codersdk.BuildInfoResponse{
Version: buildinfo.Version(),
})
}
14 changes: 14 additions & 0 deletions coderd/coderd_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package coderd_test

import (
"context"
"testing"

"go.uber.org/goleak"

"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/buildinfo"
"github.com/coder/coder/coderd/coderdtest"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}

func TestBuildInfo(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
buildInfo, err := client.BuildInfo(context.Background())
require.NoError(t, err)
require.Equal(t, buildinfo.Version(), buildInfo.Version, "version")
}
28 changes: 28 additions & 0 deletions codersdk/buildinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package codersdk

import (
"context"
"encoding/json"
"net/http"
)

// BuildInfoResponse contains build information for this instance of Coder.
type BuildInfoResponse struct {
Version string `json:"version"`
}

// BuildInfo returns build information for this instance of Coder.
func (c *Client) BuildInfo(ctx context.Context) (BuildInfoResponse, error) {
res, err := c.request(ctx, http.MethodGet, "/api/v2/buildinfo", nil)
if err != nil {
return BuildInfoResponse{}, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return BuildInfoResponse{}, readBodyAsError(res)
}

var buildInfo BuildInfoResponse
return buildInfo, json.NewDecoder(res.Body).Decode(&buildInfo)
}
5 changes: 5 additions & 0 deletions site/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ export const getApiKey = async (): Promise<Types.APIKeyResponse> => {
const response = await axios.post<Types.APIKeyResponse>("/api/v2/users/me/keys")
return response.data
}

export const getBuildInfo = async (): Promise<Types.BuildInfoResponse> => {
const response = await axios.get("/api/v2/buildinfo")
return response.data
}
7 changes: 7 additions & 0 deletions site/src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* `BuildInfoResponse` must be kept in sync with the go struct in buildinfo.go.
*/
export interface BuildInfoResponse {
version: string
}

export interface LoginResponse {
session_token: string
}
Expand Down
3 changes: 2 additions & 1 deletion site/src/components/Page/Footer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { screen } from "@testing-library/react"
import React from "react"
import { render } from "../../test_helpers"
import { MockBuildInfo, render } from "../../test_helpers"
import { Footer } from "./Footer"

describe("Footer", () => {
Expand All @@ -10,5 +10,6 @@ describe("Footer", () => {

// Then
await screen.findByText("Copyright", { exact: false })
await screen.findByText(MockBuildInfo.version, { exact: false })
})
})
8 changes: 6 additions & 2 deletions site/src/components/Page/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { makeStyles } from "@material-ui/core/styles"
import Typography from "@material-ui/core/Typography"
import React from "react"
import { useActor } from "@xstate/react"
import React, { useContext } from "react"
import { XServiceContext } from "../../xServices/StateContext"

export const Footer: React.FC = ({ children }) => {
const styles = useFooterStyles()
const xServices = useContext(XServiceContext)
const [buildInfoState] = useActor(xServices.buildInfoXService)

return (
<div className={styles.root}>
Expand All @@ -15,7 +19,7 @@ export const Footer: React.FC = ({ children }) => {
</div>
<div className={styles.version}>
<Typography color="textSecondary" variant="caption">
v2 0.0.0-prototype
Coder {buildInfoState.context.buildInfo?.version}
</Typography>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion site/src/test_helpers/entities.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Organization, Project, Provisioner, UserAgent, UserResponse, Workspace } from "../api/types"
import { BuildInfoResponse, Organization, Project, Provisioner, UserAgent, UserResponse, Workspace } from "../api/types"

export const MockSessionToken = { session_token: "my-session-token" }

export const MockAPIKey = { key: "my-api-key" }

export const MockBuildInfo: BuildInfoResponse = {
version: "v99.999.9999+c9cdf14",
}

export const MockUser: UserResponse = {
id: "test-user",
username: "TestUser",
Expand Down
5 changes: 5 additions & 0 deletions site/src/test_helpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { rest } from "msw"
import * as M from "./entities"

export const handlers = [
// build info
rest.get("/api/v2/buildinfo", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockBuildInfo))
}),

// organizations
rest.get("/api/v2/organizations/:organizationId", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockOrganization))
Expand Down
15 changes: 12 additions & 3 deletions site/src/xServices/StateContext.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useInterpret } from "@xstate/react"
import React, { createContext } from "react"
import { ActorRefFrom } from "xstate"
import { buildInfoMachine } from "./buildInfo/buildInfoXService"
import { userMachine } from "./user/userXService"

interface XServiceContextType {
buildInfoXService: ActorRefFrom<typeof buildInfoMachine>
userXService: ActorRefFrom<typeof userMachine>
}

Expand All @@ -18,7 +20,14 @@ interface XServiceContextType {
export const XServiceContext = createContext({} as XServiceContextType)

export const XServiceProvider: React.FC = ({ children }) => {
const userXService = useInterpret(userMachine)

return <XServiceContext.Provider value={{ userXService }}>{children}</XServiceContext.Provider>
return (
<XServiceContext.Provider
value={{
buildInfoXService: useInterpret(buildInfoMachine),
userXService: useInterpret(userMachine),
}}
>
{children}
</XServiceContext.Provider>
)
}
62 changes: 62 additions & 0 deletions site/src/xServices/buildInfo/buildInfoXService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { assign, createMachine } from "xstate"
import * as API from "../../api"
import * as Types from "../../api/types"

export interface BuildInfoContext {
getBuildInfoError?: Error | unknown
buildInfo?: Types.BuildInfoResponse
}

export const buildInfoMachine = createMachine(
{
tsTypes: {} as import("./buildInfoXService.typegen").Typegen0,
schema: {
context: {} as BuildInfoContext,
services: {} as {
getBuildInfo: {
data: Types.BuildInfoResponse
}
},
},
context: {
buildInfo: undefined,
},
id: "buildInfoState",
initial: "gettingBuildInfo",
states: {
gettingBuildInfo: {
invoke: {
src: "getBuildInfo",
id: "getBuildInfo",
onDone: [
{
actions: ["assignBuildInfo", "clearGetBuildInfoError"],
},
],
onError: [
{
actions: "assignGetBuildInfoError",
},
],
},
},
},
},
{
services: {
getBuildInfo: API.getBuildInfo,
},
actions: {
assignBuildInfo: assign({
buildInfo: (_, event) => event.data,
}),
assignGetBuildInfoError: assign({
getBuildInfoError: (_, event) => event.data,
}),
clearGetBuildInfoError: assign((context: BuildInfoContext) => ({
...context,
getBuildInfoError: undefined,
})),
},
},
)