8000 docs: add API documentation for Coder plugin by Parkreiner · Pull Request #15 · coder/backstage-plugins · GitHub
[go: up one dir, main page]

Skip to content

docs: add API documentation for Coder plugin #15

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

Merged
merged 36 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7153251
docs: finish drafts for hooks/types README files
Parkreiner Mar 4, 2024
6d67225
wip: add stub for components README
Parkreiner Mar 4, 2024
be54ee6
fix: remove typo in main docs
Parkreiner Mar 4, 2024
cb0d816
wip: add stub for component docs
Parkreiner Mar 4, 2024
d0fa96c
wip: add outline for component documentation
Parkreiner Mar 4, 2024
aead300
docs: add documentation for CoderAuthWrapper and CoderErrorBoundary
Parkreiner Mar 5, 2024
d37ae30
docs: finish up section for CoderProvider
Parkreiner Mar 5, 2024
5fed31e
fix: update typo in library name
Parkreiner Mar 5, 2024
89fde98
docs: finish section for CoderAppConfig
Parkreiner Mar 5, 2024
06963f7
fix: export useWorkspacesCardContext
Parkreiner Mar 5, 2024
a2e2e4b
docs: finish section for useWorkspacesCardContext
Parkreiner Mar 5, 2024
c89d7ad
wip: add stubs for CoderWorkspacesCard
Parkreiner Mar 5, 2024
e4d5e0a
fix: update link in main directory README
Parkreiner Mar 5, 2024
3f8c852
docs: add more info to component docs
Parkreiner Mar 6, 2024
4a31ac6
fix: ensure link configs are combined properly
Parkreiner Mar 6, 2024
93bb583
docs: finish docs for CreateWorkspacesLink
Parkreiner Mar 6, 2024
833ad2d
docs: finish section for ExtraActionsButton
Parkreiner Mar 6, 2024
cbca5fb
docs: finish section for HeaderRow
Parkreiner Mar 6, 2024
21f06eb
docs: finish Root
Parkreiner Mar 6, 2024
f3a8dae
docs: finish section for SearchBox
Parkreiner Mar 6, 2024
88bd95d
docs: finish rough draft of API docs
Parkreiner Mar 7, 2024
cfe2c4d
Merge branch 'main' into mes/coder-api-docs
Parkreiner Mar 7, 2024
605b2b9
fix: fill in missing section for CoderProvider
Parkreiner Mar 7, 2024
d5a46af
chore: move API docs up one level
Parkreiner Mar 7, 2024
abfcf0f
docs: fill in link for TODO comment
Parkreiner Mar 7, 2024
799132c
fix: reword docs message for clarity
Parkreiner Mar 7, 2024
8085717
Update plugins/backstage-plugin-coder/docs/README.md
Parkreiner Mar 7, 2024
54c65c2
fix: update version for Coder plugin
Parkreiner Mar 7, 2024
4511773
Merge branch 'mes/coder-api-docs' of github.com:coder/backstage-plugi…
Parkreiner Mar 7, 2024
dd56f00
docs: add info about useCoderEntityConfig
Parkreiner Mar 7, 2024
a899bad
fix: make sure all exported components have unique name values
Parkreiner Mar 8, 2024
af5e67a
fix: make sure all values are properly exported at top-level plugin root
Parkreiner Mar 8, 2024
6d67468
Merge branch 'main' into mes/coder-api-docs
Parkreiner Mar 8, 2024
3a09f9c
fix: remove forwardRef from exported sub-components
Parkreiner Mar 8, 2024
f99a1a4
fix: remove forwardRef from Root
Parkreiner Mar 8, 2024
63a71e8
Merge branch 'main' into mes/coder-api-docs
Parkreiner Mar 11, 2024
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
Next Next commit
docs: finish drafts for hooks/types README files
  • Loading branch information
Parkreiner committed Mar 4, 2024
commit 7153251058ddc80012cf7eec59057e23b8bc4d97
9 changes: 9 additions & 0 deletions plugins/backstage-plugin-coder/docs/api-reference/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Plugin API Reference – Coder for Backstage

For users who need more information about how to extend and modify the Coder plugin. For general setup, please see our main [README](link goes here).

## Documentation directory

- [Components](./components.md)
- [Custom React hooks](./hooks.md)
- [General types](./types.md)
129 changes: 129 additions & 0 deletions plugins/backstage-plugin-coder/docs/api-reference/hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Plugin API reference – React hooks

This is the main documentation page for the Coder plugin's React hooks.

## Hook list

- [`useCoderEntityConfig`](#useCoderEntityConfig)
- [`useCoderWorkspaces`](#useCoderWorkspaces)

## `useCoderEntityConfig`

This hook gives you access to compiled [`CoderEntityConfig`](./types.md#coderentityconfig) data.

### Type signature

```tsx
declare function useCoderEntityConfig(): CoderEntityConfig;
```

[Type definition for `CoderEntityConfig`](./types.md#coderentityconfig)

### Example usage

```tsx
function YourComponent() {
const config = useCoderEntityConfig();
return <p>Your repo URL is {config.repoUrl}</p>;
}

// All other components provided via @backstage/plugin-catalog
// and should be statically initialized
const overviewContent = (
<Grid container spacing={3} alignItems="stretch">
<Grid item md={6} xs={12}>
<YourComponent />
</Grid>
</Grid>
);

const serviceEntityPage = (
<EntityLayout>
<EntityLayout.Route path="/" title="Overview">
{overviewContent}
</EntityLayout.Route>
</EntityLayout>
);

// etc.
```

### Throws

- Will throw an error if called outside a React component
- Will throw an error if called outside an `EntityLayout` (or any other Backstage component that exposes `Entity` data via React Context)

### Notes

- The type definition for `CoderEntityConfig` [can be found here](./types.md#coderentityconfig). That section also includes info on the heuristic used for compiling the data
- The hook tries to ensure that the returned value maintains a stable memory reference as much as possible, if you ever need to use that value in other React hooks that use dependency arrays (e.g., `useEffect`, `useCallback`)

## `useCoderWorkspaces`

This hook gives you access to all workspaces that match a given query string. If
[`repoConfig`](#usecoderentityconfig) is defined via `options`, the workspaces returned will be filtered down further to only those that match the the repo.

### Type signature

```ts
type UseCoderWorkspacesOptions = Readonly<
Partial<{
repoConfig: CoderEntityConfig;
}>
>;

declare function useCoderEntityConfig(
coderQuery: string,
options?: UseCoderWorkspacesOptions,
): UseQueryResult<readonly Workspace[]>;
```

### Example usage

```tsx
function YourComponent() {
const entityConfig = useCoderEntityConfig();
const [filter, setFilter] = useState('owner:me');

const query = useCoderWorkspaces(filter, {
repoConfig: entityConfig,
});

return (
<>
{query.isLoading && <YourLoadingIndicator />}
{query.isError && <YourErrorDisplay />}

{query.data?.map(workspace => (
<ol>
<li key={workspace.key}>{workspace.name}</li>
</ol>
))}
</>
);
}

const coderAppConfig: CoderAppConfig = {
// ...Properties go here
};

<CoderProvider appConfig={coderAppConfig}>
<CoderAuthWrapper>
<YourComponent />
</CoderAuthWrapper>
</CoderProvider>;
```

### Throws

- Will throw an error if called outside a React component
- Will throw an error if the component calling the hook is not wrapped inside a [`CoderProvider`](./components.md#CoderProvider)

### Notes

- `UseQueryResult` is taken from [React Query v4](https://tanstack.com/query/v4/docs/framework/react/reference/useQuery)
- We recommend [TK Dodo's Practical React Query blog series](https://tkdodo.eu/blog/practical-react-query) for how to make the most of its features. (Particularly the article on [React Query status checks](https://tkdodo.eu/blog/status-checks-in-react-query))
- The underlying query will not be enabled if:
1. The user is not currently authenticated (We recommend wrapping your component inside [`CoderAuthWrapper`](./components.md#coderauthwrapper) to make these checks easier)
2. If `repoConfig` is passed in via `options`: when the value of `coderQuery` is an empty string
- `CoderEntityConfig` is the return type of [`useCoderEntityConfig`](#usecoderentityconfig)
169 changes: 169 additions & 0 deletions plugins/backstage-plugin-coder/docs/api-reference/types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Plugin API reference – Important types

## Types directory

- [`CoderAppConfig`](#coderappconfig)
- [`CoderEntityConfig`](#coderentityconfig)
- [`Workspace`](#workspace)
- [`WorkspaceResponse`](#workspaceresponse)

## `CoderAppConfig`

## `CoderEntityConfig`

Represents the result of compiling Coder plugin configuration data. All data will be compiled from the following sources:

1. The [`CoderAppConfig`](#coderappconfig) passed to [`CoderProvider`](./components.md#coderprovider)
2. The entity-specific fields for a given repo's `catalog-info.yaml` file
3. The entity's location metadata (corresponding to the repo)

### Type definition

```tsx
type CoderEntityConfig = Readonly<{
mode: 'manual' | 'auto';
params: Record<string, string | undefined>;
repoUrl: string | undefined;
repoUrlParamKeys: [string, ...string[]][];
templateName: string;
}>;
```

### Example

Let's say that you have these inputs:

```tsx
const appConfig: CoderAppConfig = {
deployment: {
accessUrl: 'https://dev.coder.com',
},

workspaces: {
templateName: 'devcontainers',
mode: 'manual',
repoUrlParamKeys: ['custom_repo', 'repo_url'],
params: {
repo: 'custom',
region: 'eu-helsinki',
},
},
};
```

```yaml
# https://github.com/Parkreiner/python-project/blob/main/catalog-info.yaml
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: python-project
spec:
type: other
lifecycle: unknown
owner: pms
coder:
templateName: 'devcontainers'
mode: 'auto'
params:
repo: 'custom'
region: 'us-pittsburgh'
```

Your output will look like this:

```tsx
const config: CoderEntityConfig = {
mode: 'auto',
params: {
repo: 'custom',
region: 'us-pittsburgh',
custom_repo: 'https://github.com/Parkreiner/python-project/',
repo_url: 'https://github.com/Parkreiner/python-project/',
},
repoUrl: 'https://github.com/Parkreiner/python-project/',
repoUrlParamKeys: ['custom_repo', 'repo_url'],
templateName: 'devcontainers',
};
```

### Notes

- See the notes for [`CoderAppConfig`](#coderappconfig) for additional information on some of the fields.
- The value of the `repoUrl` property is derived from [Backstage's `getEntitySourceLocation`](https://backstage.io/docs/reference/plugin-catalog-react.getentitysourcelocation/), which does not guarantee that a URL will always be defined.
- This is the current order of operations used to reconcile param data between `CoderAppConfig`, `catalog-info.yaml`, and the entity location data:
1. Start with an empty `Record<string, string | undefined>` value
2. Populate the record with the data from `CoderAppConfig`
3. Go through all properties parsed from `catalog-info.yaml` and inject those. If the properties are already defined, overwrite them
4. Grab the repo URL from the entity's location fields.
5. For each key in `CoderAppConfig`'s `workspaces.repoUrlParamKeys` property, take that key, and inject it as a key-value pair, using the URL as the value. If the key already exists, always override it with the URL

## `Workspace`

Represents a single Coder workspace.

### Type definition

The below type definitions are likely to be split up at a later date. They are currently defined together for convenience.

```tsx
type WorkspaceAgentStatus =
| 'connected'
| 'connecting'
| 'disconnected'
| 'timeout';

type WorkspaceAgent = {
id: string;
status: WorkspaceAgentStatus;
};

type WorkspaceResource = {
id: string;
agents: WorkspaceAgent[];
};

type WorkspaceStatus =
| 'canceled'
| 'canceling'
| 'deleted'
| 'deleting'
| 'failed'
| 'pending'
| 'running'
| 'starting'
| 'stopped'
| 'stopping';

type Workspace = {
name: string;
id: string;
template_icon: string;
owner_name: string;
latest_build: {
id: string;
status: WorkspaceStatus;
resources: WorkspaceResource[];
};
};
```

### Notes

- Right now, the number of fields is limited. One planned feature is to expand the type definition to make all Coder workspace properties available

## `WorkspaceResponse`

Represents the JSON value that will be part of the response to any workspace API call.

### Type definition

```tsx
type WorkspaceResponse = {
count: number;
workspaces: Workspace[];
};
```

### Notes

- `count` is the total number of workspaces in the response
0