diff options
author | John Cai <jcai@gitlab.com> | 2020-05-15 21:17:55 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2020-05-18 19:35:49 +0300 |
commit | 4b43f421c2f0014b2310853c168fe932bcddb92a (patch) | |
tree | 7c092ebbe025ac4db337986b6515e992d3d18d08 | |
parent | 47b4f195006d0755f76767d2dfe4f89ef2cb9abe (diff) |
Do not relativize GitObjectDirectory and GitAlternateObjectDirectories
-rw-r--r-- | go.sum | 1 | ||||
-rw-r--r-- | internal/service/hooks/api/access.go | 34 | ||||
-rw-r--r-- | internal/service/hooks/api/access_test.go | 17 |
3 files changed, 17 insertions, 35 deletions
@@ -49,6 +49,7 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/client9/reopen v1.0.0 h1:8tpLVR74DLpLObrn2KvsyxJY++2iORGR17WLUdSzUws= github.com/client9/reopen v1.0.0/go.mod h1:caXVCEr+lUtoN1FlsRiOWdfQtdRHIYfcb0ai8qKWtkQ= +github.com/cloudflare/tableflip v0.0.0-20190329062924-8392f1641731/go.mod h1:erh4dYezoMVbIa52pi7i1Du7+cXOgqNuTamt10qvMoA= github.com/cloudflare/tableflip v1.2.1-0.20200514155827-4baec9811f2b h1:PF1TsqplD1wyV3nBSzRHP3teTQFtaWl3jaENrGqZ+lI= github.com/cloudflare/tableflip v1.2.1-0.20200514155827-4baec9811f2b/go.mod h1:vhhSlJqV8uUnxGkRSgyvGthfGlkAwJ4UuSV51fSrCQY= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd h1:qMd81Ts1T2OTKmB4acZcyKaMtRnY5Y44NuXGX2GFJ1w= diff --git a/internal/service/hooks/api/access.go b/internal/service/hooks/api/access.go index dbf2b7c58..f29a9e37a 100644 --- a/internal/service/hooks/api/access.go +++ b/internal/service/hooks/api/access.go @@ -7,7 +7,6 @@ import ( "io/ioutil" "mime" "net/http" - "path/filepath" "regexp" "strings" @@ -36,21 +35,11 @@ type AllowedRequest struct { UserID string `json:"user_id,omitempty"` } -// gitObjectDirs generates a json encoded string containing GIT_OBJECT_DIRECTORY_RELATIVE, and GIT_ALTERNATE_OBJECT_DIRECTORIES -func gitObjectDirs(repoPath, gitObjectDir string, gitAltObjDirs []string) (string, error) { - gitObjDirRel, err := filepath.Rel(repoPath, gitObjectDir) - if err != nil { - return "", err - } - - gitAltObjDirsRel, err := relativeAlternativeObjectPaths(repoPath, gitAltObjDirs) - if err != nil { - return "", err - } - +// marshallGitObjectDirs generates a json encoded string containing GIT_OBJECT_DIRECTORY_RELATIVE, and GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE +func marshallGitObjectDirs(gitObjectDirRel string, gitAltObjectDirsRel []string) (string, error) { envString, err := json.Marshal(map[string]interface{}{ - "GIT_OBJECT_DIRECTORY_RELATIVE": gitObjDirRel, - "GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE": gitAltObjDirsRel, + "GIT_OBJECT_DIRECTORY_RELATIVE": gitObjectDirRel, + "GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE": gitAltObjectDirsRel, }) if err != nil { @@ -60,19 +49,6 @@ func gitObjectDirs(repoPath, gitObjectDir string, gitAltObjDirs []string) (strin return string(envString), nil } -func relativeAlternativeObjectPaths(repoPath string, gitAltObjDirs []string) ([]string, error) { - relPaths := make([]string, 0, len(gitAltObjDirs)) - for _, objPath := range gitAltObjDirs { - relPath, err := filepath.Rel(repoPath, objPath) - if err != nil { - return relPaths, err - } - relPaths = append(relPaths, relPath) - } - - return relPaths, nil -} - // API is a wrapper around client.GitlabNetClient with api methods for gitlab git receive hooks type API struct { client *client.GitlabNetClient @@ -92,7 +68,7 @@ func (a *API) Allowed(repo *gitalypb.Repository, glRepository, glID, glProtocol, return false, fmt.Errorf("getting the repository path: %w", err) } - gitObjDirVars, err := gitObjectDirs(repoPath, repo.GetGitObjectDirectory(), repo.GetGitAlternateObjectDirectories()) + gitObjDirVars, err := marshallGitObjectDirs(repo.GetGitObjectDirectory(), repo.GetGitAlternateObjectDirectories()) if err != nil { return false, fmt.Errorf("when getting git object directories json encoded string: %w", err) } diff --git a/internal/service/hooks/api/access_test.go b/internal/service/hooks/api/access_test.go index 948ccfb90..198896898 100644 --- a/internal/service/hooks/api/access_test.go +++ b/internal/service/hooks/api/access_test.go @@ -28,11 +28,16 @@ func TestAllowedVerifyParams(t *testing.T) { defer cleanup() changes := "changes1\nchanges2\nchanges3" protocol := "protocol" - gitObjectDir := filepath.Join(testRepoPath, "object/dir") - gitAlternateObjectDirs := []string{filepath.Join(testRepoPath, "alt/object/dir1"), filepath.Join(testRepoPath, "alt/object/dir1")} - testRepo.GitObjectDirectory = gitObjectDir - testRepo.GitAlternateObjectDirectories = gitAlternateObjectDirs + testRepo.GitObjectDirectory = "object/dir" + testRepo.GitAlternateObjectDirectories = []string{"alt/object/dir1", "alt/object/dir2"} + + gitObjectDirFull := filepath.Join(testRepoPath, testRepo.GitObjectDirectory) + var gitAlternateObjectDirsFull []string + + for _, gitAlternateObjectDirRel := range testRepo.GitAlternateObjectDirectories { + gitAlternateObjectDirsFull = append(gitAlternateObjectDirsFull, filepath.Join(testRepoPath, gitAlternateObjectDirRel)) + } server := testhelper.NewGitlabTestServer(t, testhelper.GitlabTestServerOptions{ User: user, @@ -44,8 +49,8 @@ func TestAllowedVerifyParams(t *testing.T) { PostReceiveCounterDecreased: true, Protocol: protocol, GitPushOptions: nil, - GitObjectDir: gitObjectDir, - GitAlternateObjectDirs: gitAlternateObjectDirs, + GitObjectDir: gitObjectDirFull, + GitAlternateObjectDirs: gitAlternateObjectDirsFull, RepoPath: testRepoPath, }) |