diff --git a/internal/api/controller/repo/create.go b/internal/api/controller/repo/create.go index 3e2ed03a6..d5e970cc3 100644 --- a/internal/api/controller/repo/create.go +++ b/internal/api/controller/repo/create.go @@ -38,7 +38,8 @@ type CreateInput struct { } // Create creates a new repository. -//nolint:funlen,goimports // needs refactor +// +//nolint:funlen // needs refactor func (c *Controller) Create(ctx context.Context, session *auth.Session, in *CreateInput) (*types.Repository, error) { log := zerolog.Ctx(ctx) // ensure we reference a space @@ -100,8 +101,7 @@ func (c *Controller) Create(ctx context.Context, session *auth.Session, in *Crea } if in.License != "" && in.License != "none" { - // TODO: The caller shouldn't need to know the actual location. - content, err = resources.Licence.ReadFile(fmt.Sprintf("license/%s.txt", in.License)) + content, err = resources.ReadLicense(in.License) if err != nil { return nil, err } @@ -112,8 +112,7 @@ func (c *Controller) Create(ctx context.Context, session *auth.Session, in *Crea } if in.GitIgnore != "" { - // TODO: The caller shouldn't need to know the actual location. - content, err = resources.Gitignore.ReadFile(fmt.Sprintf("gitignore/%s.gitignore", in.GitIgnore)) + content, err = resources.ReadGitIgnore(in.GitIgnore) if err != nil { return nil, err } @@ -139,7 +138,7 @@ func (c *Controller) Create(ctx context.Context, session *auth.Session, in *Crea log.Error().Err(err). Msg("Repository creation failed.") - // TODO: cleanup git repo! + // TODO: cleanup git repo! return nil, err } diff --git a/internal/api/handler/repo/resource.go b/internal/api/handler/repo/resource.go index 31ca2aff5..bccdd9971 100644 --- a/internal/api/handler/repo/resource.go +++ b/internal/api/handler/repo/resource.go @@ -6,7 +6,6 @@ package repo import ( "net/http" - "strings" "github.com/harness/gitness/internal/api/render" "github.com/harness/gitness/resources" @@ -14,22 +13,18 @@ import ( func HandleGitIgnore() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - entries, err := resources.Gitignore.ReadDir("gitignore") - files := make([]string, len(entries)) + files, err := resources.GitIgnores() if err != nil { render.ErrorMessagef(w, http.StatusInternalServerError, "error loading gitignore files: %v", err) return } - for i, filename := range entries { - files[i] = strings.ReplaceAll(filename.Name(), ".gitignore", "") - } render.JSON(w, http.StatusOK, files) } } func HandleLicence() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - response, err := resources.Licence.ReadFile("licence/index.json") + response, err := resources.Licenses() if err != nil { render.ErrorMessagef(w, http.StatusInternalServerError, "error loading licence file: %v", err) return diff --git a/internal/api/handler/space/create.go b/internal/api/handler/space/create.go index 419bea858..cbe53e069 100644 --- a/internal/api/handler/space/create.go +++ b/internal/api/handler/space/create.go @@ -13,9 +13,7 @@ import ( "github.com/harness/gitness/internal/api/request" ) -/* - * HandleCreate returns an http.HandlerFunc that creates a new space. - */ +// HandleCreate returns an http.HandlerFunc that creates a new space. func HandleCreate(spaceCtrl *space.Controller) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/internal/api/handler/space/createPath.go b/internal/api/handler/space/createPath.go index 7e021a649..74c3580ff 100644 --- a/internal/api/handler/space/createPath.go +++ b/internal/api/handler/space/createPath.go @@ -13,9 +13,7 @@ import ( "github.com/harness/gitness/internal/api/request" ) -/* - * Writes json-encoded path information to the http response body. - */ +// HandleCreatePath writes json-encoded path information to the http response body. func HandleCreatePath(spaceCtrl *space.Controller) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/internal/api/handler/space/listPaths.go b/internal/api/handler/space/listPaths.go index 57f07cef0..9b710eede 100644 --- a/internal/api/handler/space/listPaths.go +++ b/internal/api/handler/space/listPaths.go @@ -13,9 +13,7 @@ import ( "github.com/harness/gitness/types/enum" ) -/* - * Writes json-encoded path information to the http response body. - */ +// HandleListPaths writes json-encoded path information to the http response body. func HandleListPaths(spaceCtrl *space.Controller) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/internal/api/handler/space/listServiceAccounts.go b/internal/api/handler/space/listServiceAccounts.go index fe2d580fa..c0214b161 100644 --- a/internal/api/handler/space/listServiceAccounts.go +++ b/internal/api/handler/space/listServiceAccounts.go @@ -12,9 +12,7 @@ import ( "github.com/harness/gitness/internal/api/request" ) -/* - * Writes json-encoded service account information to the http response body. - */ +// HandleListServiceAccounts Writes json-encoded service account information to the http response body. func HandleListServiceAccounts(spaceCtrl *space.Controller) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/internal/api/handler/space/update.go b/internal/api/handler/space/update.go index 3041e9389..e608d44e6 100644 --- a/internal/api/handler/space/update.go +++ b/internal/api/handler/space/update.go @@ -13,9 +13,7 @@ import ( "github.com/harness/gitness/internal/api/request" ) -/* - * Updates an existing space. - */ +// HandleUpdate updates an existing space. func HandleUpdate(spaceCtrl *space.Controller) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/internal/api/openapi/openapi.go b/internal/api/openapi/openapi.go index 0b09761b3..0ec71d69f 100644 --- a/internal/api/openapi/openapi.go +++ b/internal/api/openapi/openapi.go @@ -41,6 +41,8 @@ func Generate() *openapi3.Spec { buildAccount(&reflector) buildUser(&reflector) buildUsers(&reflector) + spaceOperations(&reflector) + repoOperations(&reflector) // // define security scheme diff --git a/internal/api/openapi/repo.go b/internal/api/openapi/repo.go new file mode 100644 index 000000000..2de4b279d --- /dev/null +++ b/internal/api/openapi/repo.go @@ -0,0 +1,174 @@ +// Copyright 2022 Harness Inc. All rights reserved. +// Use of this source code is governed by the Polyform Free Trial License +// that can be found in the LICENSE.md file for this repository. + +package openapi + +import ( + "net/http" + + "github.com/harness/gitness/internal/api/controller/repo" + "github.com/harness/gitness/internal/api/usererror" + "github.com/harness/gitness/types" + "github.com/swaggest/openapi-go/openapi3" +) + +type createRepositoryRequest struct { + repo.CreateInput +} + +type gitignoreRequest struct { +} + +type licenseRequest struct { +} + +type repoRequest struct { + Ref string `json:"ref" path:"ref"` +} + +type updateRepoRequest struct { + repoRequest + repo.UpdateInput +} + +type moveRepoRequest struct { + repoRequest + repo.MoveInput +} + +type createRepoPathRequest struct { + repoRequest + repo.CreatePathInput +} + +type deleteRepoPathRequest struct { + repoRequest + PathID string `json:"pathID" path:"pathID"` +} + +//nolint:funlen +func repoOperations(reflector *openapi3.Reflector) { + createRepository := openapi3.Operation{} + createRepository.WithTags("repository") + createRepository.WithMapOfAnything(map[string]interface{}{"operationId": "createRepository"}) + _ = reflector.SetRequest(&createRepository, new(createRepositoryRequest), http.MethodPost) + _ = reflector.SetJSONResponse(&createRepository, new(types.Repository), http.StatusCreated) + _ = reflector.SetJSONResponse(&createRepository, new(usererror.Error), http.StatusBadRequest) + _ = reflector.SetJSONResponse(&createRepository, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&createRepository, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&createRepository, new(usererror.Error), http.StatusForbidden) + _ = reflector.Spec.AddOperation(http.MethodPost, "/repos", createRepository) + + opListGitignore := openapi3.Operation{} + opListGitignore.WithTags("repository") + opListGitignore.WithMapOfAnything(map[string]interface{}{"operationId": "listGitignore"}) + _ = reflector.SetRequest(&opListGitignore, new(gitignoreRequest), http.MethodGet) + _ = reflector.SetJSONResponse(&opListGitignore, []string{}, http.StatusOK) + _ = reflector.SetJSONResponse(&opListGitignore, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opListGitignore, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opListGitignore, new(usererror.Error), http.StatusForbidden) + _ = reflector.Spec.AddOperation(http.MethodGet, "/repos/resources/gitignore", opListGitignore) + + opListLicenses := openapi3.Operation{} + opListLicenses.WithTags("repository") + opListLicenses.WithMapOfAnything(map[string]interface{}{"operationId": "listLicenses"}) + _ = reflector.SetRequest(&opListLicenses, new(licenseRequest), http.MethodGet) + _ = reflector.SetJSONResponse(&opListLicenses, []struct { + Label string `json:"label"` + Value string `json:"value"` + }{}, http.StatusOK) + _ = reflector.SetJSONResponse(&opListLicenses, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opListLicenses, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opListLicenses, new(usererror.Error), http.StatusForbidden) + _ = reflector.Spec.AddOperation(http.MethodGet, "/repos/resources/license", opListLicenses) + + opFind := openapi3.Operation{} + opFind.WithTags("repository") + opFind.WithMapOfAnything(map[string]interface{}{"operationId": "findRepository"}) + _ = reflector.SetRequest(&opFind, new(repoRequest), http.MethodGet) + _ = reflector.SetJSONResponse(&opFind, new(types.Repository), http.StatusOK) + _ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodGet, "/repos/{ref}", opFind) + + opUpdate := openapi3.Operation{} + opUpdate.WithTags("repository") + opUpdate.WithMapOfAnything(map[string]interface{}{"operationId": "updateRepository"}) + _ = reflector.SetRequest(&opUpdate, new(updateRepoRequest), http.MethodPatch) + _ = reflector.SetJSONResponse(&opUpdate, new(types.Repository), http.StatusOK) + _ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusBadRequest) + _ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodPatch, "/repos/{ref}", opUpdate) + + opDelete := openapi3.Operation{} + opDelete.WithTags("repository") + opDelete.WithMapOfAnything(map[string]interface{}{"operationId": "deleteRepository"}) + _ = reflector.SetRequest(&opDelete, new(repoRequest), http.MethodDelete) + _ = reflector.SetJSONResponse(&opDelete, nil, http.StatusNoContent) + _ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodDelete, "/repos/{ref}", opDelete) + + opMove := openapi3.Operation{} + opMove.WithTags("repository") + opMove.WithMapOfAnything(map[string]interface{}{"operationId": "moveRepository"}) + _ = reflector.SetRequest(&opMove, new(moveRepoRequest), http.MethodPost) + _ = reflector.SetJSONResponse(&opMove, new(types.Repository), http.StatusOK) + _ = reflector.SetJSONResponse(&opMove, new(usererror.Error), http.StatusBadRequest) + _ = reflector.SetJSONResponse(&opMove, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opMove, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opMove, new(usererror.Error), http.StatusForbidden) + _ = reflector.Spec.AddOperation(http.MethodPost, "/repos/{ref}/move", opMove) + + opServiceAccounts := openapi3.Operation{} + opServiceAccounts.WithTags("repository") + opServiceAccounts.WithMapOfAnything(map[string]interface{}{"operationId": "listRepositoryServiceAccounts"}) + _ = reflector.SetRequest(&opServiceAccounts, new(repoRequest), http.MethodGet) + _ = reflector.SetJSONResponse(&opServiceAccounts, []types.ServiceAccount{}, http.StatusOK) + _ = reflector.SetJSONResponse(&opServiceAccounts, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opServiceAccounts, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opServiceAccounts, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opServiceAccounts, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodGet, "/repos/{ref}/serviceAccounts", opServiceAccounts) + + opListPaths := openapi3.Operation{} + opListPaths.WithTags("repository") + opListPaths.WithMapOfAnything(map[string]interface{}{"operationId": "listRepositoryPaths"}) + _ = reflector.SetRequest(&opListPaths, new(repoRequest), http.MethodGet) + _ = reflector.SetJSONResponse(&opListPaths, []types.Path{}, http.StatusOK) + _ = reflector.SetJSONResponse(&opListPaths, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opListPaths, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opListPaths, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opListPaths, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodGet, "/repos/{ref}/paths", opListPaths) + + opCreatePath := openapi3.Operation{} + opCreatePath.WithTags("repository") + opCreatePath.WithMapOfAnything(map[string]interface{}{"operationId": "createRepositoryPath"}) + _ = reflector.SetRequest(&opCreatePath, new(createRepoPathRequest), http.MethodPost) + _ = reflector.SetJSONResponse(&opCreatePath, new(types.Path), http.StatusCreated) + _ = reflector.SetJSONResponse(&opCreatePath, new(usererror.Error), http.StatusBadRequest) + _ = reflector.SetJSONResponse(&opCreatePath, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opCreatePath, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opCreatePath, new(usererror.Error), http.StatusForbidden) + _ = reflector.Spec.AddOperation(http.MethodPost, "/repos/{ref}/paths", opCreatePath) + + onDeletePath := openapi3.Operation{} + onDeletePath.WithTags("repository") + onDeletePath.WithMapOfAnything(map[string]interface{}{"operationId": "deleteRepositoryPath"}) + _ = reflector.SetRequest(&onDeletePath, new(deleteRepoPathRequest), http.MethodDelete) + _ = reflector.SetJSONResponse(&onDeletePath, nil, http.StatusNoContent) + _ = reflector.SetJSONResponse(&onDeletePath, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&onDeletePath, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&onDeletePath, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&onDeletePath, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodDelete, "/repos/{ref}/paths/{pathID}", onDeletePath) +} diff --git a/internal/api/openapi/space.go b/internal/api/openapi/space.go new file mode 100644 index 000000000..15750c2ef --- /dev/null +++ b/internal/api/openapi/space.go @@ -0,0 +1,167 @@ +// Copyright 2022 Harness Inc. All rights reserved. +// Use of this source code is governed by the Polyform Free Trial License +// that can be found in the LICENSE.md file for this repository. + +package openapi + +import ( + "net/http" + + "github.com/harness/gitness/internal/api/controller/space" + "github.com/harness/gitness/internal/api/usererror" + "github.com/harness/gitness/types" + "github.com/swaggest/openapi-go/openapi3" +) + +type createSpaceRequest struct { + space.CreateInput +} + +type spaceRequest struct { + Ref string `json:"ref" path:"ref"` +} + +type updateSpaceRequest struct { + spaceRequest + space.UpdateInput +} + +type moveSpaceRequest struct { + spaceRequest + space.MoveInput +} + +type createPathRequest struct { + spaceRequest + space.CreatePathInput +} + +type deletePathRequest struct { + spaceRequest + PathID string `json:"pathID" path:"pathID"` +} + +//nolint:funlen // api spec generation no need for checking func complexity +func spaceOperations(reflector *openapi3.Reflector) { + opCreate := openapi3.Operation{} + opCreate.WithTags("space") + opCreate.WithMapOfAnything(map[string]interface{}{"operationId": "createSpace"}) + _ = reflector.SetRequest(&opCreate, new(createSpaceRequest), http.MethodPost) + _ = reflector.SetJSONResponse(&opCreate, new(types.Space), http.StatusCreated) + _ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusBadRequest) + _ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusForbidden) + _ = reflector.Spec.AddOperation(http.MethodPost, "/spaces", opCreate) + + opGet := openapi3.Operation{} + opGet.WithTags("space") + opGet.WithMapOfAnything(map[string]interface{}{"operationId": "getSpace"}) + _ = reflector.SetRequest(&opGet, new(spaceRequest), http.MethodGet) + _ = reflector.SetJSONResponse(&opGet, new(types.Space), http.StatusOK) + _ = reflector.SetJSONResponse(&opGet, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opGet, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opGet, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opGet, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodGet, "/spaces/{ref}", opGet) + + opUpdate := openapi3.Operation{} + opUpdate.WithTags("space") + opUpdate.WithMapOfAnything(map[string]interface{}{"operationId": "updateSpace"}) + _ = reflector.SetRequest(&opUpdate, new(updateSpaceRequest), http.MethodPatch) + _ = reflector.SetJSONResponse(&opUpdate, new(types.Space), http.StatusOK) + _ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusBadRequest) + _ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodPatch, "/spaces/{ref}", opUpdate) + + opDelete := openapi3.Operation{} + opDelete.WithTags("space") + opDelete.WithMapOfAnything(map[string]interface{}{"operationId": "deleteSpace"}) + _ = reflector.SetRequest(&opDelete, new(spaceRequest), http.MethodDelete) + _ = reflector.SetJSONResponse(&opDelete, nil, http.StatusNoContent) + _ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodDelete, "/spaces/{ref}", opDelete) + + opMove := openapi3.Operation{} + opMove.WithTags("space") + opMove.WithMapOfAnything(map[string]interface{}{"operationId": "moveSpace"}) + _ = reflector.SetRequest(&opMove, new(moveSpaceRequest), http.MethodPost) + _ = reflector.SetJSONResponse(&opMove, new(types.Space), http.StatusOK) + _ = reflector.SetJSONResponse(&opMove, new(usererror.Error), http.StatusBadRequest) + _ = reflector.SetJSONResponse(&opMove, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opMove, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opMove, new(usererror.Error), http.StatusForbidden) + _ = reflector.Spec.AddOperation(http.MethodPost, "/spaces/{ref}/move", opMove) + + opSpaces := openapi3.Operation{} + opSpaces.WithTags("space") + opSpaces.WithMapOfAnything(map[string]interface{}{"operationId": "listSpaces"}) + _ = reflector.SetRequest(&opSpaces, new(spaceRequest), http.MethodGet) + _ = reflector.SetJSONResponse(&opSpaces, []types.Space{}, http.StatusOK) + _ = reflector.SetJSONResponse(&opSpaces, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opSpaces, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opSpaces, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opSpaces, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodGet, "/spaces/{ref}/spaces", opSpaces) + + opRepos := openapi3.Operation{} + opRepos.WithTags("space") + opRepos.WithMapOfAnything(map[string]interface{}{"operationId": "listRepos"}) + _ = reflector.SetRequest(&opRepos, new(spaceRequest), http.MethodGet) + _ = reflector.SetJSONResponse(&opRepos, []types.Repository{}, http.StatusOK) + _ = reflector.SetJSONResponse(&opRepos, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opRepos, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opRepos, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opRepos, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodGet, "/spaces/{ref}/repos", opRepos) + + opServiceAccounts := openapi3.Operation{} + opServiceAccounts.WithTags("space") + opServiceAccounts.WithMapOfAnything(map[string]interface{}{"operationId": "listServiceAccounts"}) + _ = reflector.SetRequest(&opServiceAccounts, new(spaceRequest), http.MethodGet) + _ = reflector.SetJSONResponse(&opServiceAccounts, []types.ServiceAccount{}, http.StatusOK) + _ = reflector.SetJSONResponse(&opServiceAccounts, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opServiceAccounts, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opServiceAccounts, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opServiceAccounts, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodGet, "/spaces/{ref}/serviceAccounts", opServiceAccounts) + + opListPaths := openapi3.Operation{} + opListPaths.WithTags("space") + opListPaths.WithMapOfAnything(map[string]interface{}{"operationId": "listPaths"}) + _ = reflector.SetRequest(&opListPaths, new(spaceRequest), http.MethodGet) + _ = reflector.SetJSONResponse(&opListPaths, []types.Path{}, http.StatusOK) + _ = reflector.SetJSONResponse(&opListPaths, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opListPaths, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opListPaths, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&opListPaths, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodGet, "/spaces/{ref}/paths", opListPaths) + + opCreatePath := openapi3.Operation{} + opCreatePath.WithTags("space") + opCreatePath.WithMapOfAnything(map[string]interface{}{"operationId": "createPath"}) + _ = reflector.SetRequest(&opCreatePath, new(createPathRequest), http.MethodPost) + _ = reflector.SetJSONResponse(&opCreatePath, new(types.Path), http.StatusCreated) + _ = reflector.SetJSONResponse(&opCreatePath, new(usererror.Error), http.StatusBadRequest) + _ = reflector.SetJSONResponse(&opCreatePath, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&opCreatePath, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&opCreatePath, new(usererror.Error), http.StatusForbidden) + _ = reflector.Spec.AddOperation(http.MethodPost, "/spaces/{ref}/paths", opCreatePath) + + onDeletePath := openapi3.Operation{} + onDeletePath.WithTags("space") + onDeletePath.WithMapOfAnything(map[string]interface{}{"operationId": "deletePath"}) + _ = reflector.SetRequest(&onDeletePath, new(deletePathRequest), http.MethodDelete) + _ = reflector.SetJSONResponse(&onDeletePath, nil, http.StatusNoContent) + _ = reflector.SetJSONResponse(&onDeletePath, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&onDeletePath, new(usererror.Error), http.StatusUnauthorized) + _ = reflector.SetJSONResponse(&onDeletePath, new(usererror.Error), http.StatusForbidden) + _ = reflector.SetJSONResponse(&onDeletePath, new(usererror.Error), http.StatusNotFound) + _ = reflector.Spec.AddOperation(http.MethodDelete, "/spaces/{ref}/paths/{pathID}", onDeletePath) +} diff --git a/internal/gitrpc/rpc/repo.pb.go b/internal/gitrpc/rpc/repo.pb.go index b4405ad05..4b3d690b3 100644 --- a/internal/gitrpc/rpc/repo.pb.go +++ b/internal/gitrpc/rpc/repo.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.27.1 -// protoc v3.19.4 +// protoc-gen-go v1.28.1 +// protoc v3.21.7 // source: repo.proto package rpc diff --git a/internal/gitrpc/rpc/repo_grpc.pb.go b/internal/gitrpc/rpc/repo_grpc.pb.go index 1c48e2a57..607231308 100644 --- a/internal/gitrpc/rpc/repo_grpc.pb.go +++ b/internal/gitrpc/rpc/repo_grpc.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.2.0 -// - protoc v3.19.4 +// - protoc v3.21.7 // source: repo.proto package rpc diff --git a/internal/gitrpc/rpc/shared.pb.go b/internal/gitrpc/rpc/shared.pb.go index 6153c058b..16b116ae7 100644 --- a/internal/gitrpc/rpc/shared.pb.go +++ b/internal/gitrpc/rpc/shared.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.27.1 -// protoc v3.19.4 +// protoc-gen-go v1.28.1 +// protoc v3.21.7 // source: shared.proto package rpc diff --git a/internal/request/request.go b/internal/request/request.go index c8dc89c90..43230d7ae 100644 --- a/internal/request/request.go +++ b/internal/request/request.go @@ -2,8 +2,8 @@ // Use of this source code is governed by the Polyform Free Trial License // that can be found in the LICENSE.md file for this repository. -// Package router provides http handlers for serving the -// web applicationa and API endpoints. +// Package request provides http handlers for serving the +// web applications and API endpoints. package request import ( diff --git a/internal/router/api.go b/internal/router/api.go index cfaca6fcf..70c1c067e 100644 --- a/internal/router/api.go +++ b/internal/router/api.go @@ -141,9 +141,10 @@ func setupRepos(r chi.Router, config *types.Config, repoCtrl *repo.Controller) { r.Route("/repos", func(r chi.Router) { // Create takes path and parentId via body, not uri r.Post("/", handlerrepo.HandleCreate(config, repoCtrl)) - r.Get("/gitignore", handlerrepo.HandleGitIgnore()) - r.Get("/licence", handlerrepo.HandleLicence()) - + r.Route("/resources", func(r chi.Router) { + r.Get("/gitignore", handlerrepo.HandleGitIgnore()) + r.Get("/license", handlerrepo.HandleLicence()) + }) r.Route(fmt.Sprintf("/{%s}", request.PathParamRepoRef), func(r chi.Router) { // repo level operations r.Get("/", handlerrepo.HandleFind(repoCtrl)) @@ -152,8 +153,6 @@ func setupRepos(r chi.Router, config *types.Config, repoCtrl *repo.Controller) { r.Post("/move", handlerrepo.HandleMove(repoCtrl)) r.Get("/serviceAccounts", handlerrepo.HandleListServiceAccounts(repoCtrl)) - r.Get("/commits", handlerrepo.HandleListCommits(repoCtrl)) - r.Get("/content/*", handlerrepo.HandleGetContent(repoCtrl)) // repo path operations r.Route("/paths", func(r chi.Router) { diff --git a/internal/router/git.go b/internal/router/git.go index 3a7c7a489..51a76cc56 100644 --- a/internal/router/git.go +++ b/internal/router/git.go @@ -25,9 +25,7 @@ type GitHandler interface { http.Handler } -/* - * NewGitHandler returns a new GitHandler. - */ +// NewGitHandler returns a new GitHandler. func NewGitHandler( repoStore store.RepoStore, authenticator authn.Authenticator) GitHandler { diff --git a/internal/router/web.go b/internal/router/web.go index f9b11b418..24ae774b2 100644 --- a/internal/router/web.go +++ b/internal/router/web.go @@ -8,6 +8,9 @@ import ( "context" "net/http" + "github.com/harness/gitness/internal/api/openapi" + "github.com/harness/gitness/internal/api/render" + "github.com/harness/gitness/internal/store" "github.com/harness/gitness/web" "github.com/swaggest/swgui/v3emb" @@ -51,7 +54,18 @@ func NewWebHandler(systemStore store.SystemStore) WebHandler { ) // openapi playground endpoints - swagger := v3emb.NewHandler("API Definition", "/api/v1/swagger.yaml", "/swagger") + r.HandleFunc("/openapi.yaml", func(w http.ResponseWriter, r *http.Request) { + spec := openapi.Generate() + data, err := spec.MarshalYAML() + if err != nil { + render.ErrorMessagef(w, http.StatusInternalServerError, "error serializing openapi.yaml: %v", err) + return + } + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/yaml") + _, _ = w.Write(data) + }) + swagger := v3emb.NewHandler("API Definition", "/openapi.yaml", "/swagger") r.With(sec.Handler).Handle("/swagger", swagger) r.With(sec.Handler).Handle("/swagger/*", swagger) diff --git a/resources/embed.go b/resources/embed.go index 93e85ea06..0f8da53c8 100644 --- a/resources/embed.go +++ b/resources/embed.go @@ -4,12 +4,48 @@ package resources -import "embed" +import ( + "embed" + "fmt" + "strings" +) var ( //go:embed gitignore - Gitignore embed.FS + gitignore embed.FS //go:embed license - Licence embed.FS + licence embed.FS ) + +// Licenses returns map of licences in license folder. +func Licenses() ([]byte, error) { + return licence.ReadFile("license/index.json") +} + +// ReadLicense reads licence from license folder. +func ReadLicense(name string) ([]byte, error) { + content, err := licence.ReadFile(fmt.Sprintf("license/%s.txt", name)) + if err != nil { + return nil, err + } + return content, err +} + +// GitIgnores lists all files in gitignore folder and return file names. +func GitIgnores() ([]string, error) { + entries, err := gitignore.ReadDir("gitignore") + files := make([]string, len(entries)) + if err != nil { + return []string{}, err + } + for i, filename := range entries { + files[i] = strings.ReplaceAll(filename.Name(), ".gitignore", "") + } + return files, nil +} + +// ReadGitIgnore reads gitignore file from license folder. +func ReadGitIgnore(name string) ([]byte, error) { + return gitignore.ReadFile(fmt.Sprintf("gitignore/%s.gitignore", name)) +}