Fix pseudo git HTTP urls broken since 59c82659 (#4453)

* Fix pseudo git HTTP urls broken since 59c82659

Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr>

* Add test for Git resources using HTTP URLs

Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr>
This commit is contained in:
Sylvain Rabot
2022-02-09 23:23:49 +01:00
committed by GitHub
parent 14cb815b5d
commit 2f17803c0a
2 changed files with 66 additions and 1 deletions

View File

@@ -4,6 +4,7 @@
package loader
import (
"errors"
"fmt"
"io/ioutil"
"log"
@@ -298,7 +299,7 @@ func (fl *fileLoader) errIfRepoCycle(newRepoSpec *git.RepoSpec) error {
}
// Load returns the content of file at the given path,
// else an error. Relative paths are taken relative
// else an error. Relative paths are taken relative
// to the root.
func (fl *fileLoader) Load(path string) ([]byte, error) {
if u, err := url.Parse(path); err == nil && (u.Scheme == "http" || u.Scheme == "https") {
@@ -314,6 +315,10 @@ func (fl *fileLoader) Load(path string) ([]byte, error) {
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
_, err := git.NewRepoSpecFromUrl(path)
if err == nil {
return nil, errors.New("URL is a git repository")
}
return nil, fmt.Errorf("%w: status code %d (%s)", ErrorHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
}
body, err := ioutil.ReadAll(resp.Body)