mirror of
https://github.com/harness/drone.git
synced 2025-05-08 07:21:10 +08:00
clone url implemented (#63)
Clone URL (http) used for clone/pull/push git operations
This commit is contained in:
parent
a418e5e94f
commit
98436fb644
@ -7,6 +7,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/harness/gitness/gitrpc"
|
||||
server2 "github.com/harness/gitness/gitrpc/server"
|
||||
"github.com/harness/gitness/harness/auth/authn"
|
||||
|
@ -7,6 +7,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/harness/gitness/gitrpc"
|
||||
server2 "github.com/harness/gitness/gitrpc/server"
|
||||
"github.com/harness/gitness/internal/api/controller/repo"
|
||||
|
@ -6,6 +6,9 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
apiauth "github.com/harness/gitness/internal/api/auth"
|
||||
"github.com/harness/gitness/internal/auth"
|
||||
@ -13,10 +16,9 @@ import (
|
||||
"github.com/harness/gitness/types/enum"
|
||||
)
|
||||
|
||||
/*
|
||||
* Find finds a repo.
|
||||
*/
|
||||
func (c *Controller) Find(ctx context.Context, session *auth.Session, repoRef string) (*types.Repository, error) {
|
||||
// Find finds a repo.
|
||||
func (c *Controller) Find(ctx context.Context, session *auth.Session, repoRef string,
|
||||
cfg *types.Config) (*types.Repository, error) {
|
||||
repo, err := findRepoFromRef(ctx, c.repoStore, repoRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -25,6 +27,13 @@ func (c *Controller) Find(ctx context.Context, session *auth.Session, repoRef st
|
||||
if err = apiauth.CheckRepo(ctx, c.authorizer, session, repo, enum.PermissionRepoView, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
repoPath := path.Clean(repo.Path)
|
||||
if !strings.HasSuffix(repoPath, ".git") {
|
||||
repoPath += ".git"
|
||||
}
|
||||
repo.URL, err = url.JoinPath(cfg.Git.BaseURL, repoPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return repo, nil
|
||||
}
|
||||
|
@ -6,16 +6,18 @@ package repo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/harness/gitness/types"
|
||||
|
||||
"github.com/harness/gitness/internal/api/controller/repo"
|
||||
"github.com/harness/gitness/internal/api/render"
|
||||
"github.com/harness/gitness/internal/api/request"
|
||||
)
|
||||
|
||||
/*
|
||||
* Writes json-encoded repository information to the http response body.
|
||||
*/
|
||||
func HandleFind(repoCtrl *repo.Controller) http.HandlerFunc {
|
||||
// HandleFind writes json-encoded repository information to the http response body.
|
||||
func HandleFind(repoCtrl *repo.Controller, config *types.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
session, _ := request.AuthSessionFrom(ctx)
|
||||
@ -25,12 +27,31 @@ func HandleFind(repoCtrl *repo.Controller) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
repo, err := repoCtrl.Find(ctx, session, repoRef)
|
||||
repo, err := repoCtrl.Find(ctx, session, repoRef, config)
|
||||
if err != nil {
|
||||
render.TranslatedUserError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
parse, err := url.Parse(repo.URL)
|
||||
if err != nil {
|
||||
render.TranslatedUserError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if parse.Host == "" {
|
||||
parse.Host = r.Host
|
||||
}
|
||||
|
||||
if parse.Scheme == "" {
|
||||
parse.Scheme = "http"
|
||||
if !strings.Contains(parse.Host, "localhost") {
|
||||
parse.Scheme = "https"
|
||||
}
|
||||
}
|
||||
|
||||
repo.URL = parse.String()
|
||||
|
||||
render.JSON(w, http.StatusOK, repo)
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ func NewAPIHandler(
|
||||
r.Use(middlewareauthn.Attempt(authenticator))
|
||||
|
||||
r.Route("/v1", func(r chi.Router) {
|
||||
setupRoutesV1(r, repoCtrl, spaceCtrl, saCtrl, userCtrl)
|
||||
setupRoutesV1(r, repoCtrl, spaceCtrl, saCtrl, userCtrl, config)
|
||||
})
|
||||
|
||||
// wrap router in terminatedPath encoder.
|
||||
@ -99,9 +99,9 @@ func corsHandler(config *types.Config) func(http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
func setupRoutesV1(r chi.Router, repoCtrl *repo.Controller, spaceCtrl *space.Controller,
|
||||
saCtrl *serviceaccount.Controller, userCtrl *user.Controller) {
|
||||
saCtrl *serviceaccount.Controller, userCtrl *user.Controller, config *types.Config) {
|
||||
setupSpaces(r, spaceCtrl)
|
||||
setupRepos(r, repoCtrl)
|
||||
setupRepos(r, repoCtrl, config)
|
||||
setupUsers(r, userCtrl)
|
||||
setupServiceAccounts(r, saCtrl)
|
||||
setupAdmin(r, userCtrl)
|
||||
@ -140,13 +140,13 @@ func setupSpaces(r chi.Router, spaceCtrl *space.Controller) {
|
||||
})
|
||||
}
|
||||
|
||||
func setupRepos(r chi.Router, repoCtrl *repo.Controller) {
|
||||
func setupRepos(r chi.Router, repoCtrl *repo.Controller, config *types.Config) {
|
||||
r.Route("/repos", func(r chi.Router) {
|
||||
// Create takes path and parentId via body, not uri
|
||||
r.Post("/", handlerrepo.HandleCreate(repoCtrl))
|
||||
r.Route(fmt.Sprintf("/{%s}", request.PathParamRepoRef), func(r chi.Router) {
|
||||
// repo level operations
|
||||
r.Get("/", handlerrepo.HandleFind(repoCtrl))
|
||||
r.Get("/", handlerrepo.HandleFind(repoCtrl, config))
|
||||
r.Put("/", handlerrepo.HandleUpdate(repoCtrl))
|
||||
r.Delete("/", handlerrepo.HandleDelete(repoCtrl))
|
||||
|
||||
|
@ -13,6 +13,7 @@ type Config struct {
|
||||
|
||||
// Git defines the git configuration parameters
|
||||
Git struct {
|
||||
BaseURL string `envconfig:"GITNESS_GIT_BASE_URL"` // clone url
|
||||
Root string `envconfig:"GITNESS_GIT_ROOT"`
|
||||
DefaultBranch string `envconfig:"GITNESS_GIT_DEFAULTBRANCH" default:"main"`
|
||||
}
|
||||
|
@ -31,6 +31,9 @@ type Repository struct {
|
||||
NumPulls int `db:"repo_numPulls" json:"numPulls"`
|
||||
NumClosedPulls int `db:"repo_numClosedPulls" json:"numClosedPulls"`
|
||||
NumOpenPulls int `db:"repo_numOpenPulls" json:"numOpenPulls"`
|
||||
|
||||
// git urls
|
||||
URL string `db:"-" json:"url"`
|
||||
}
|
||||
|
||||
// RepoFilter stores repo query parameters.
|
||||
|
Loading…
Reference in New Issue
Block a user