Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f70137

Browse files
committed
WIP
1 parent b23edb4 commit 2f70137

File tree

10 files changed

+457
-446
lines changed

10 files changed

+457
-446
lines changed

coderd/apidoc/docs.go

Lines changed: 179 additions & 181 deletions
Large diffs are not rendered by default.

coderd/apidoc/swagger.json

Lines changed: 168 additions & 170 deletions
Large diffs are not rendered by default.

coderd/coderdtest/swagger_test.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/go-chi/chi/v5"
12+
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314
"golang.org/x/xerrors"
1415
)
@@ -21,25 +22,28 @@ type swaggerComment struct {
2122
func TestAllEndpointsDocumented(t *testing.T) {
2223
t.Parallel()
2324

24-
swaggerCommentss, err := parseSwaggerComments("..") // TODO enterprise
25+
// TODO parse enterprise
26+
swaggerComments, err := parseSwaggerComments("..")
2527
require.NoError(t, err, "can't parse swagger comments")
2628

27-
for _, c := range swaggerCommentss {
28-
t.Log(c.method, c.router)
29-
}
30-
3129
_, _, api := NewWithAPI(t, nil)
32-
err = chi.Walk(api.APIHandler, func(method, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
30+
chi.Walk(api.APIHandler, func(method, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
31+
method = strings.ToLower(method)
32+
if route != "/" && strings.HasSuffix(route, "/") {
33+
route = route[:len(route)-1]
34+
}
35+
36+
c := findSwaggerCommentByMethodAndRoute(swaggerComments, method, route)
37+
assert.NotNil(t, c, "Missing @Router annotation for: [%s] %s", method, route)
3338
return nil
3439
})
35-
require.Error(t, err)
3640
}
3741

3842
func parseSwaggerComments(dir string) ([]swaggerComment, error) {
3943
fileSet := token.NewFileSet()
4044
commentNodes, err := parser.ParseDir(fileSet, dir, nil, parser.ParseComments)
4145
if err != nil {
42-
return nil, xerrors.Errorf(`can't parse directory "%s": %w`, dir)
46+
return nil, xerrors.Errorf(`parser.ParseDir failed "%s": %w`, dir, err)
4347
}
4448

4549
var swaggerComments []swaggerComment
@@ -73,8 +77,19 @@ func parseSwaggerComment(commentGroup *ast.CommentGroup) swaggerComment {
7377
for _, line := range commentGroup.List {
7478
text := strings.TrimSpace(line.Text)
7579
if strings.Contains(text, "@Router ") {
76-
c.router = strings.SplitN(text, " ", 3)[2]
80+
args := strings.SplitN(text, " ", 4)
81+
c.router = args[2]
82+
c.method = args[3][1 : len(args[3])-1]
7783
}
7884
}
7985
return c
8086
}
87+
88+
func findSwaggerCommentByMethodAndRoute(comments []swaggerComment, method, route string) *swaggerComment {
89+
for _, c := range comments {
90+
if c.method == method && c.router == route {
91+
return &c
92+
}
93+
}
94+
return nil
95+
}

coderd/templates.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828
// @Security CoderSessionToken
2929
// @Produce json
3030
// @Tags Templates
31-
// @Param id path string true "Template ID" format(uuid)
31+
// @Param template path string true "Template ID" format(uuid)
3232
// @Success 200 {object} codersdk.Template
33-
// @Router /templates/{id} [get]
33+
// @Router /templates/{template} [get]
3434
// Returns a single template.
3535
func (api *API) template(rw http.ResponseWriter, r *http.Request) {
3636
ctx := r.Context()
@@ -75,9 +75,9 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) {
7575
// @Security CoderSessionToken
7676
// @Produce json
7777
// @Tags Templates
78-
// @Param id path string true "Template ID" format(uuid)
78+
// @Param template path string true "Template ID" format(uuid)
7979
// @Success 200 {object} codersdk.Response
80-
// @Router /templates/{id} [delete]
80+
// @Router /templates/{template} [delete]
8181
func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
8282
var (
8383
ctx = r.Context()
@@ -465,9 +465,9 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
465465
// @Security CoderSessionToken
466466
// @Produce json
467467
// @Tags Templates
468-
// @Param id path string true "Template ID" format(uuid)
468+
// @Param template path string true "Template ID" format(uuid)
469469
// @Success 200 {object} codersdk.Template
470-
// @Router /templates/{id} [patch]
470+
// @Router /templates/{template} [patch]
471471
func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
472472
var (
473473
ctx = r.Context()
@@ -593,9 +593,9 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
593593
// @Security CoderSessionToken
594594
// @Produce json
595595
// @Tags Templates
596-
// @Param id path string true "Template ID" format(uuid)
596+
// @Param template path string true "Template ID" format(uuid)
597597
// @Success 200 {object} codersdk.TemplateDAUsResponse
598-
// @Router /templates/{id}/daus [get]
598+
// @Router /templates/{template}/daus [get]
599599
func (api *API) templateDAUs(rw http.ResponseWriter, r *http.Request) {
600600
ctx := r.Context()
601601
template := httpmw.TemplateParam(r)

coderd/templateversions.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ func (api *API) postTemplateVersionDryRun(rw http.ResponseWriter, r *http.Reques
357357
// @Produce json
358358
// @Tags Templates
359359
// @Param templateversion path string true "Template version ID" format(uuid)
360-
// @Param jobid path string true "Job ID" format(uuid)
360+
// @Param jobID path string true "Job ID" format(uuid)
361361
// @Success 200 {object} codersdk.ProvisionerJob
362-
// @Router /templateversions/{templateversion}/dry-run/{jobid} [get]
362+
// @Router /templateversions/{templateversion}/dry-run/{jobID} [get]
363363
func (api *API) templateVersionDryRun(rw http.ResponseWriter, r *http.Request) {
364364
ctx := r.Context()
365365
job, ok := api.fetchTemplateVersionDryRunJob(rw, r)
@@ -376,9 +376,9 @@ func (api *API) templateVersionDryRun(rw http.ResponseWriter, r *http.Request) {
376376
// @Produce json
377377
// @Tags Templates
378378
// @Param templateversion path string true "Template version ID" format(uuid)
379-
// @Param jobid path string true "Job ID" format(uuid)
379+
// @Param jobID path string true "Job ID" format(uuid)
380380
// @Success 200 {array} codersdk.WorkspaceResource
381-
// @Router /templateversions/{templateversion}/dry-run/{jobid}/resources [get]
381+
// @Router /templateversions/{templateversion}/dry-run/{jobID}/resources [get]
382382
func (api *API) templateVersionDryRunResources(rw http.ResponseWriter, r *http.Request) {
383383
job, ok := api.fetchTemplateVersionDryRunJob(rw, r)
384384
if !ok {
@@ -394,12 +394,12 @@ func (api *API) templateVersionDryRunResources(rw http.ResponseWriter, r *http.R
394394
// @Produce json
395395
// @Tags Templates
396396
// @Param templateversion path string true "Template version ID" format(uuid)
397-
// @Param jobid path string true "Job ID" format(uuid)
397+
// @Param job-ID path string true "Job ID" format(uuid)
398398
// @Param before query int false "Before Unix timestamp"
399399
// @Param after query int false "After Unix timestamp"
400400
// @Param follow query bool false "Follow log stream"
401401
// @Success 200 {array} codersdk.ProvisionerJobLog
402-
// @Router /templateversions/{templateversion}/dry-run/{jobid}/logs [get]
402+
// @Router /templateversions/{templateversion}/dry-run/{jobID}/logs [get]
403403
func (api *API) templateVersionDryRunLogs(rw http.ResponseWriter, r *http.Request) {
404404
job, ok := api.fetchTemplateVersionDryRunJob(rw, r)
405405
if !ok {
@@ -416,7 +416,7 @@ func (api *API) templateVersionDryRunLogs(rw http.ResponseWriter, r *http.Reques
416416
// @Tags Templates
417417
// @Param templateversion path string true "Template version ID" format(uuid)
418418
// @Success 200 {object} codersdk.Response
419-
// @Router /templateversions/{templateversion}/dry-run/{jobid}/cancel [patch]
419+
// @Router /templateversions/{templateversion}/dry-run/{jobID}/cancel [patch]
420420
func (api *API) patchTemplateVersionDryRunCancel(rw http.ResponseWriter, r *http.Request) {
421421
ctx := r.Context()
422422
templateVersion := httpmw.TemplateVersionParam(r)
@@ -540,12 +540,12 @@ func (api *API) fetchTemplateVersionDryRunJob(rw http.ResponseWriter, r *http.Re
540540
// @Security CoderSessionToken
541541
// @Produce json
542542
// @Tags Templates
543-
// @Param id path string true "Template ID" format(uuid)
543+
// @Param template path string true "Template ID" format(uuid)
544544
// @Param after_id query string false "After ID" format(uuid)
545545
// @Param limit query int false "Page limit"
546546
// @Param offset query int false "Page offset"
547547
// @Success 200 {array} codersdk.TemplateVersion
548-
// @Router /templates/{id}/versions [get]
548+
// @Router /templates/{template}/versions [get]
549549
func (api *API) templateVersionsByTemplate(rw http.ResponseWriter, r *http.Request) {
550550
ctx := r.Context()
551551
template := httpmw.TemplateParam(r)
@@ -648,10 +648,10 @@ func (api *API) templateVersionsByTemplate(rw http.ResponseWriter, r *http.Reque
648648
// @Security CoderSessionToken
649649
// @Produce json
650650
// @Tags Templates
651-
// @Param id path string true "Template ID" format(uuid)
651+
// @Param template path string true "Template ID" format(uuid)
652652
// @Param templateversionname path string true "Template version name"
653653
// @Success 200 {array} codersdk.TemplateVersion
654-
// @Router /templates/{id}/versions/{templateversionname} [get]
654+
// @Router /templates/{template}/versions/{templateversionname} [get]
655655
func (api *API) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
656656
ctx := r.Context()
657657
template := httpmw.TemplateParam(r)
@@ -834,9 +834,9 @@ func (api *API) previousTemplateVersionByOrganizationAndName(rw http.ResponseWri
834834
// @Produce json
835835
// @Tags Templates
836836
// @Param request body codersdk.UpdateActiveTemplateVersion true "Modified template version"
837-
// @Param id path string true "Template ID" format(uuid)
837+
// @Param template path string true "Template ID" format(uuid)
838838
// @Success 200 {object} codersdk.Response
839-
// @Router /templates/{id}/versions [patch]
839+
// @Router /templates/{template}/versions [patch]
840840
func (api *API) patchActiveTemplateVersion(rw http.ResponseWriter, r *http.Request) {
841841
var (
842842
ctx = r.Context()

coderd/workspacebuilds.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ func (api *API) workspaceBuild(rw http.ResponseWriter, r *http.Request) {
7777
// @Security CoderSessionToken
7878
// @Produce json
7979
// @Tags Builds
80-
// @Param id path string true "Workspace ID" format(uuid)
80+
// @Param workspace path string true "Workspace ID" format(uuid)
8181
// @Param after_id query string false "After ID" format(uuid)
8282
// @Param limit query int false "Page limit"
8383
// @Param offset query int false "Page offset"
8484
// @Param since query string false "Since timestamp" format(date-time)
8585
// @Success 200 {array} codersdk.WorkspaceBuild
86-
// @Router /workspaces/{id}/builds [get]
86+
// @Router /workspaces/{workspace}/builds [get]
8787
func (api *API) workspaceBuilds(rw http.ResponseWriter, r *http.Request) {
8888
ctx := r.Context()
8989
workspace := httpmw.WorkspaceParam(r)
@@ -293,10 +293,10 @@ func (api *API) workspaceBuildByBuildNumber(rw http.ResponseWriter, r *http.Requ
293293
// @Accepts json
294294
// @Produce json
295295
// @Tags Builds
296-
// @Param id path string true "Workspace ID" format(uuid)
296+
// @Param workspace path string true "Workspace ID" format(uuid)
297297
// @Param request body codersdk.CreateWorkspaceBuildRequest true "Create workspace build request"
298298
// @Success 200 {object} codersdk.WorkspaceBuild
299-
// @Router /workspaces/{id}/builds [post]
299+
// @Router /workspaces/{workspace}/builds [post]
300300
func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
301301
ctx := r.Context()
302302
apiKey := httpmw.APIKey(r)

coderd/workspaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var (
5151
// @Param id path string true "Workspace ID" format(uuid)
5252
// @Param include_deleted query bool false "Return data instead of HTTP 404 if the workspace is deleted"
5353
// @Success 200 {object} codersdk.Workspace
54-
// @Router /workspaces/{id} [get]
54+
// @Router /workspaces/{workspace} [get]
5555
func (api *API) workspace(rw http.ResponseWriter, r *http.Request) {
5656
ctx := r.Context()
5757
workspace := httpmw.WorkspaceParam(r)

docs/api/builds.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -740,22 +740,22 @@ To perform this operation, you must be authenticated. [Learn more](authenticatio
740740

741741
```shell
742742
# Example request using curl
743-
curl -X GET http://coder-server:8080/api/v2/workspaces/{id}/builds \
743+
curl -X GET http://coder-server:8080/api/v2/workspaces/{workspace}/builds \
744744
-H 'Accept: application/json' \
745745
-H 'Coder-Session-Token: API_KEY'
746746
```
747747

748-
`GET /workspaces/{id}/builds`
748+
`GET /workspaces/{workspace}/builds`
749749

750750
### Parameters
751751

752-
| Name | In | Type | Required | Description |
753-
| ---------- | ----- | ----------------- | -------- | --------------- |
754-
| `id` | path | string(uuid) | true | Workspace ID |
755-
| `after_id` | query | string(uuid) | false | After ID |
756-
| `limit` | query | integer | false | Page limit |
757-
| `offset` | query | integer | false | Page offset |
758-
| `since` | query | string(date-time) | false | Since timestamp |
752+
| Name | In | Type | Required | Description |
753+
| ----------- | ----- | ----------------- | -------- | --------------- |
754+
| `workspace` | path | string(uuid) | true | Workspace ID |
755+
| `after_id` | query | string(uuid) | false | After ID |
756+
| `limit` | query | integer | false | Page limit |
757+
| `offset` | query | integer | false | Page offset |
758+
| `since` | query | string(date-time) | false | Since timestamp |
759759

760760
### Example responses
761761

@@ -1019,13 +1019,13 @@ To perform this operation, you must be authenticated. [Learn more](authenticatio
10191019

10201020
```shell
10211021
# Example request using curl
1022-
curl -X POST http://coder-server:8080/api/v2/workspaces/{id}/builds \
1022+
curl -X POST http://coder-server:8080/api/v2/workspaces/{workspace}/builds \
10231023
-H 'Content-Type: application/json' \
10241024
-H 'Accept: application/json' \
10251025
-H 'Coder-Session-Token: API_KEY'
10261026
```
10271027

1028-
`POST /workspaces/{id}/builds`
1028+
`POST /workspaces/{workspace}/builds`
10291029

10301030
> Body parameter
10311031
@@ -1050,10 +1050,10 @@ curl -X POST http://coder-server:8080/api/v2/workspaces/{id}/builds \
10501050

10511051
### Parameters
10521052

1053-
| Name | In | Type | Required | Description |
1054-
| ------ | ---- | -------------------------------------------------------------------------------------- | -------- | ------------------------------ |
1055-
| `id` | path | string(uuid) | true | Workspace ID |
1056-
| `body` | body | [codersdk.CreateWorkspaceBuildRequest](schemas.md#codersdkcreateworkspacebuildrequest) | true | Create workspace build request |
1053+
| Name | In | Type | Required | Description |
1054+
| ----------- | ---- | -------------------------------------------------------------------------------------- | -------- | ------------------------------ |
1055+
| `workspace` | path | string(uuid) | true | Workspace ID |
1056+
| `body` | body | [codersdk.CreateWorkspaceBuildRequest](schemas.md#codersdkcreateworkspacebuildrequest) | true | Create workspace build request |
10571057

10581058
### Example responses
10591059

0 commit comments

Comments
 (0)