mirror of https://github.com/harness/drone.git
feat: [code-2461]: add go-import metatag in response for go get operations (#2775)
* requested changes * Merge branch 'eb/code-2461' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into eb/code-2461 * requested changes * requested changes * requested changes * add go-import metatag in response for go get operations * requested changes * requested changes * requested changes * add go-import metatag in response for go get operationspull/3576/head
parent
e9f6f1228a
commit
fdb41b5b38
|
@ -21,9 +21,11 @@ import (
|
|||
|
||||
"github.com/harness/gitness/app/api/render"
|
||||
"github.com/harness/gitness/app/request"
|
||||
"github.com/harness/gitness/errors"
|
||||
"github.com/harness/gitness/types"
|
||||
|
||||
"github.com/rs/zerolog/hlog"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -36,17 +38,66 @@ func GitPathBefore(next http.Handler) http.Handler {
|
|||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
_, err := pathTerminatedWithMarker(r, "", ".git", "")
|
||||
ok, err := pathTerminatedWithMarker(r, "", ".git", "")
|
||||
if err != nil {
|
||||
render.TranslatedUserError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
if _, err = processGitRequest(r); err != nil {
|
||||
render.TranslatedUserError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func processGitRequest(r *http.Request) (bool, error) {
|
||||
const infoRefsPath = "/info/refs"
|
||||
const uploadPack = "git-upload-pack"
|
||||
const uploadPackPath = "/" + uploadPack
|
||||
const receivePack = "git-receive-pack"
|
||||
const receivePackPath = "/" + receivePack
|
||||
const serviceParam = "service"
|
||||
|
||||
allowedServices := []string{
|
||||
uploadPack,
|
||||
receivePack,
|
||||
}
|
||||
|
||||
urlPath := r.URL.Path
|
||||
if r.URL.RawPath != "" {
|
||||
urlPath = r.URL.RawPath
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// check if request is coming from git client
|
||||
if strings.HasSuffix(urlPath, infoRefsPath) && r.URL.Query().Has(serviceParam) {
|
||||
service := r.URL.Query().Get(serviceParam)
|
||||
if !slices.Contains(allowedServices, service) {
|
||||
return false, errors.InvalidArgument("git request allows only %v service, got: %s",
|
||||
allowedServices, service)
|
||||
}
|
||||
return pathTerminatedWithMarkerAndURL(r, "", infoRefsPath, infoRefsPath, urlPath)
|
||||
}
|
||||
case http.MethodPost:
|
||||
if strings.HasSuffix(urlPath, uploadPackPath) {
|
||||
return pathTerminatedWithMarkerAndURL(r, "", uploadPackPath, uploadPackPath, urlPath)
|
||||
}
|
||||
|
||||
if strings.HasSuffix(urlPath, receivePackPath) {
|
||||
return pathTerminatedWithMarkerAndURL(r, "", receivePackPath, receivePackPath, urlPath)
|
||||
}
|
||||
}
|
||||
|
||||
// no other APIs are called by git - just treat it as a full repo path.
|
||||
return pathTerminatedWithMarkerAndURL(r, "", "", "", urlPath)
|
||||
}
|
||||
|
||||
// TerminatedPathBefore wraps an http.HandlerFunc in a layer that encodes a terminated path (e.g. "/space1/space2/+")
|
||||
// before executing the provided http.HandlerFunc. The first prefix that matches the URL.Path will
|
||||
// be used during encoding (prefix is ignored during encoding).
|
||||
|
|
|
@ -0,0 +1,112 @@
|
|||
// Copyright 2023 Harness, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package goget
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/harness/gitness/app/api/controller/repo"
|
||||
"github.com/harness/gitness/app/api/render"
|
||||
"github.com/harness/gitness/app/api/request"
|
||||
"github.com/harness/gitness/app/url"
|
||||
"github.com/harness/gitness/types"
|
||||
)
|
||||
|
||||
var goGetTmpl *template.Template
|
||||
var httpRegex = regexp.MustCompile("https?://")
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
goGetTmpl, err = template.New("goget").Parse(`<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="go-import" content="{{.GoImport}}">
|
||||
<meta name="go-source" content="{{.GoSource}}">
|
||||
</head>
|
||||
<body>
|
||||
{{.GoCLI}}
|
||||
</body>
|
||||
</html>`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Middleware writes to response with html meta tags go-import and go-source.
|
||||
func Middleware(
|
||||
config *types.Config,
|
||||
repoCtrl *repo.Controller,
|
||||
urlProvider url.Provider,
|
||||
) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet || r.URL.Query().Get("go-get") != "1" {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
ctx := r.Context()
|
||||
|
||||
session, _ := request.AuthSessionFrom(ctx)
|
||||
repoRef, err := request.GetRepoRefFromPath(r)
|
||||
if err != nil {
|
||||
render.TranslatedUserError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
repository, err := repoCtrl.Find(ctx, session, repoRef)
|
||||
if err != nil {
|
||||
render.TranslatedUserError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
defaultBranch := config.Git.DefaultBranch
|
||||
if repository.DefaultBranch != "" {
|
||||
defaultBranch = repository.DefaultBranch
|
||||
}
|
||||
|
||||
uiRepoURL := urlProvider.GenerateUIRepoURL(ctx, repoRef)
|
||||
goImportURL := httpRegex.ReplaceAllString(uiRepoURL, "")
|
||||
cloneURL := urlProvider.GenerateGITCloneURL(ctx, repoRef)
|
||||
prefix := fmt.Sprintf("%s/files/%s/~", uiRepoURL, defaultBranch)
|
||||
|
||||
insecure := ""
|
||||
if strings.HasPrefix(uiRepoURL, "http:") {
|
||||
insecure = "--insecure"
|
||||
}
|
||||
|
||||
goImportContent := fmt.Sprintf("%s git %s", goImportURL, cloneURL)
|
||||
goSourceContent := fmt.Sprintf("%s _ %s %s", goImportURL, prefix+"{/dir}", prefix+"{/dir}/{file}#L{line}")
|
||||
goGetCliContent := fmt.Sprintf("go get %s %s", insecure, goImportURL)
|
||||
err = goGetTmpl.Execute(w, struct {
|
||||
GoImport string
|
||||
GoSource string
|
||||
GoCLI string
|
||||
}{
|
||||
GoImport: goImportContent,
|
||||
GoSource: goSourceContent,
|
||||
GoCLI: goGetCliContent,
|
||||
})
|
||||
if err != nil {
|
||||
render.TranslatedUserError(ctx, w, err)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
|
@ -23,10 +23,12 @@ import (
|
|||
middlewareauthn "github.com/harness/gitness/app/api/middleware/authn"
|
||||
middlewareauthz "github.com/harness/gitness/app/api/middleware/authz"
|
||||
"github.com/harness/gitness/app/api/middleware/encode"
|
||||
"github.com/harness/gitness/app/api/middleware/goget"
|
||||
"github.com/harness/gitness/app/api/middleware/logging"
|
||||
"github.com/harness/gitness/app/api/request"
|
||||
"github.com/harness/gitness/app/auth/authn"
|
||||
"github.com/harness/gitness/app/url"
|
||||
"github.com/harness/gitness/types"
|
||||
"github.com/harness/gitness/types/enum"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
|
@ -36,6 +38,7 @@ import (
|
|||
|
||||
// NewGitHandler returns a new GitHandler.
|
||||
func NewGitHandler(
|
||||
config *types.Config,
|
||||
urlProvider url.Provider,
|
||||
authenticator authn.Authenticator,
|
||||
repoCtrl *repo.Controller,
|
||||
|
@ -57,6 +60,7 @@ func NewGitHandler(
|
|||
r.Use(middlewareauthn.Attempt(authenticator))
|
||||
|
||||
r.Route(fmt.Sprintf("/{%s}", request.PathParamRepoRef), func(r chi.Router) {
|
||||
r.Use(goget.Middleware(config, repoCtrl, urlProvider))
|
||||
// routes that aren't coming from git
|
||||
r.Group(func(r chi.Router) {
|
||||
// redirect to repo (meant for UI, in case user navigates to clone url in browser)
|
||||
|
|
|
@ -117,6 +117,7 @@ func ProvideRouter(
|
|||
|
||||
gitRoutingHost := GetGitRoutingHost(appCtx, urlProvider)
|
||||
gitHandler := NewGitHandler(
|
||||
config,
|
||||
urlProvider,
|
||||
authenticator,
|
||||
repoCtrl,
|
||||
|
|
Loading…
Reference in New Issue