-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate package index.json #479
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
invalid package version |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
artifact not found |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
artifact not found |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
invalid package version |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package revision not found |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package revision not found |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,6 @@ func TestEndpoints(t *testing.T) { | |
{"/search?internal=bar", "/search", "search-package-internal-error.json", searchHandler(packagesBasePath, testCacheTime)}, | ||
{"/search?experimental=true", "/search", "search-package-experimental.json", searchHandler(packagesBasePath, testCacheTime)}, | ||
{"/search?experimental=foo", "/search", "search-package-experimental-error.json", searchHandler(packagesBasePath, testCacheTime)}, | ||
{"/package/example/1.0.0", "", "package.json", catchAll(http.Dir(publicPath), testCacheTime)}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we still have a test for this? The API endpoint should still work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is used in the |
||
{"/favicon.ico", "", "favicon.ico", faviconHandleFunc}, | ||
} | ||
|
||
|
@@ -87,9 +86,39 @@ func TestArtifacts(t *testing.T) { | |
handler func(w http.ResponseWriter, r *http.Request) | ||
}{ | ||
{"/epr/example/example-0.0.2.tar.gz", artifactsRouterPath, "example-0.0.2.tar.gz-preview.txt", artifactsHandler}, | ||
{"/epr/example/example-999.0.2.tar.gz", artifactsRouterPath, "package-version-not-found.txt", artifactsHandler}, | ||
{"/epr/example/missing-0.1.2.tar.gz", artifactsRouterPath, "package-missing.txt", artifactsHandler}, | ||
{"/epr/example/example-a.b.c.tar.gz", artifactsRouterPath, "package-invalid-version.txt", artifactsHandler}, | ||
{"/epr/example/example-999.0.2.tar.gz", artifactsRouterPath, "artifact-package-version-not-found.txt", artifactsHandler}, | ||
{"/epr/example/missing-0.1.2.tar.gz", artifactsRouterPath, "artifact-package-not-found.txt", artifactsHandler}, | ||
{"/epr/example/example-a.b.c.tar.gz", artifactsRouterPath, "artifact-package-invalid-version.txt", artifactsHandler}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.endpoint, func(t *testing.T) { | ||
runEndpoint(t, test.endpoint, test.path, test.file, test.handler) | ||
}) | ||
} | ||
} | ||
|
||
func TestPackageIndex(t *testing.T) { | ||
publicPath := "./testdata/public" | ||
packagesBasePath := publicPath + "/package" | ||
|
||
packageIndexHandler := packageIndexHandler(packagesBasePath, testCacheTime) | ||
|
||
tests := []struct { | ||
endpoint string | ||
path string | ||
file string | ||
handler func(w http.ResponseWriter, r *http.Request) | ||
}{ | ||
{"/package/example/1.0.0/index.json", packageIndexRouterPath1, "package.json", packageIndexHandler}, | ||
{"/package/missing/1.0.0/index.json", packageIndexRouterPath1, "index-package-not-found.txt", packageIndexHandler}, | ||
{"/package/example/999.0.0/index.json", packageIndexRouterPath1, "index-package-revision-not-found.txt", packageIndexHandler}, | ||
{"/package/example/a.b.c/index.json", packageIndexRouterPath1, "index-package-invalid-version.txt", packageIndexHandler}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a test that doesn't have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
|
||
{"/package/example/1.0.0/", packageIndexRouterPath2, "package.json", packageIndexHandler}, | ||
{"/package/missing/1.0.0/", packageIndexRouterPath2, "index-package-not-found.txt", packageIndexHandler}, | ||
{"/package/example/999.0.0/", packageIndexRouterPath2, "index-package-revision-not-found.txt", packageIndexHandler}, | ||
{"/package/example/a.b.c/", packageIndexRouterPath2, "index-package-invalid-version.txt", packageIndexHandler}, | ||
} | ||
|
||
for _, test := range tests { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/blang/semver" | ||
"github.com/gorilla/mux" | ||
|
||
"github.com/elastic/package-registry/util" | ||
) | ||
|
||
const ( | ||
packageIndexRouterPath1 = "/package/{packageName:[a-z_]+}/{packageVersion}/index.json" | ||
packageIndexRouterPath2 = "/package/{packageName:[a-z_]+}/{packageVersion}/" | ||
) | ||
|
||
var errPackageRevisionNotFound = errors.New("package revision not found") | ||
|
||
func packageIndexHandler(packagesBasePath string, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
packageName, ok := vars["packageName"] | ||
if !ok { | ||
badRequest(w, "missing package name") | ||
return | ||
} | ||
|
||
packageVersion, ok := vars["packageVersion"] | ||
if !ok { | ||
badRequest(w, "missing package version") | ||
return | ||
} | ||
|
||
_, err := semver.Parse(packageVersion) | ||
if err != nil { | ||
badRequest(w, "invalid package version") | ||
return | ||
} | ||
|
||
packagePath := filepath.Join(packagesBasePath, packageName, packageVersion) | ||
_, err = os.Stat(packagePath) | ||
if os.IsNotExist(err) { | ||
notFoundError(w, errPackageRevisionNotFound) | ||
return | ||
} | ||
if err != nil { | ||
log.Printf("stat package path '%s' failed: %v", packagePath, err) | ||
|
||
http.Error(w, "internal server error", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
cacheHeaders(w, cacheTime) | ||
|
||
aPackage, err := util.NewPackageWithResources(packagePath) | ||
if err != nil { | ||
log.Printf("loading package from path '%s' failed: %v", packagePath, err) | ||
|
||
http.Error(w, "internal server error", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
body, err := json.MarshalIndent(aPackage, "", " ") | ||
if err != nil { | ||
log.Printf("marshaling package index failed (path '%s'): %v", packagePath, err) | ||
|
||
http.Error(w, "internal server error", http.StatusInternalServerError) | ||
return | ||
} | ||
w.Write(body) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a note here that we have this in because Kibana still relies on it but it should potentially be removed in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a note