diff --git a/extractor/cli/go-autobuilder/go-autobuilder.go b/extractor/cli/go-autobuilder/go-autobuilder.go index d3519eabc07..4df3d2c77e0 100644 --- a/extractor/cli/go-autobuilder/go-autobuilder.go +++ b/extractor/cli/go-autobuilder/go-autobuilder.go @@ -84,11 +84,16 @@ func getImportPath() (importpath string) { if importpath == "" { repourl := os.Getenv("SEMMLE_REPO_URL") if repourl == "" { + log.Printf("Unable to determine import path, as neither LGTM_INDEX_IMPORT_PATH nor SEMMLE_REPO_URL is set\n") return "" } importpath = getImportPathFromRepoURL(repourl) + if importpath == "" { + log.Printf("Failed to determine import path from SEMMLE_REPO_URL '%s'\n", repourl) + return + } } - log.Printf("Import path is %s\n", importpath) + log.Printf("Import path is '%s'\n", importpath) return } @@ -103,8 +108,18 @@ func getImportPathFromRepoURL(repourl string) string { // otherwise parse as proper URL u, err := url.Parse(repourl) if err != nil { - log.Fatalf("Malformed repository URL %s.\n", repourl) + log.Fatalf("Malformed repository URL '%s'\n", repourl) } + + if u.Scheme == "file" { + // we can't determine import paths from file paths + return "" + } + + if u.Hostname() == "" || u.Path == "" { + return "" + } + host := u.Hostname() path := u.Path // strip off leading slashes and trailing `.git` if present diff --git a/extractor/cli/go-autobuilder/go-autobuilder_test.go b/extractor/cli/go-autobuilder/go-autobuilder_test.go index f246f23555b..f4e8405fe36 100644 --- a/extractor/cli/go-autobuilder/go-autobuilder_test.go +++ b/extractor/cli/go-autobuilder/go-autobuilder_test.go @@ -9,6 +9,9 @@ func TestGetImportPathFromRepoURL(t *testing.T) { "https://github.com/github/codeql-go.git": "github.com/github/codeql-go", "https://github.com:12345/github/codeql-go": "github.com/github/codeql-go", "gitolite@some.url:some/repo": "some.url/some/repo", + "file:///C:/some/path": "", + "https:///no/hostname": "", + "https://hostnameonly": "", } for input, expected := range tests { actual := getImportPathFromRepoURL(input)