mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Go: Move getImportPath to shared util package
This commit is contained in:
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -56,63 +55,6 @@ Build behavior:
|
||||
fmt.Fprintf(os.Stderr, "Usage:\n\n %s\n", os.Args[0])
|
||||
}
|
||||
|
||||
// Returns the import path of the package being built, or "" if it cannot be determined.
|
||||
func getImportPath() (importpath string) {
|
||||
importpath = os.Getenv("LGTM_INDEX_IMPORT_PATH")
|
||||
if importpath == "" {
|
||||
repourl := os.Getenv("SEMMLE_REPO_URL")
|
||||
if repourl == "" {
|
||||
githubrepo := os.Getenv("GITHUB_REPOSITORY")
|
||||
if githubrepo == "" {
|
||||
log.Printf("Unable to determine import path, as neither LGTM_INDEX_IMPORT_PATH nor GITHUB_REPOSITORY is set\n")
|
||||
return ""
|
||||
} else {
|
||||
importpath = "github.com/" + githubrepo
|
||||
}
|
||||
} else {
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
// Returns the import path of the package being built from `repourl`, or "" if it cannot be
|
||||
// determined.
|
||||
func getImportPathFromRepoURL(repourl string) string {
|
||||
// check for scp-like URL as in "git@github.com:github/codeql-go.git"
|
||||
shorturl := regexp.MustCompile(`^([^@]+@)?([^:]+):([^/].*?)(\.git)?$`)
|
||||
m := shorturl.FindStringSubmatch(repourl)
|
||||
if m != nil {
|
||||
return m[2] + "/" + m[3]
|
||||
}
|
||||
|
||||
// otherwise parse as proper URL
|
||||
u, err := url.Parse(repourl)
|
||||
if err != nil {
|
||||
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
|
||||
path = regexp.MustCompile(`^/+|\.git$`).ReplaceAllString(path, "")
|
||||
return host + "/" + path
|
||||
}
|
||||
|
||||
func restoreRepoLayout(fromDir string, dirEntries []string, scratchDirName string, toDir string) {
|
||||
for _, dirEntry := range dirEntries {
|
||||
if dirEntry != scratchDirName {
|
||||
@@ -568,7 +510,7 @@ func installDependenciesAndBuild() {
|
||||
if len(workspaces) == 1 {
|
||||
workspace := workspaces[0]
|
||||
|
||||
importpath := getImportPath()
|
||||
importpath := util.GetImportPath()
|
||||
needGopath := getNeedGopath(workspace, importpath)
|
||||
|
||||
inLGTM := os.Getenv("LGTM_SRC") != "" || os.Getenv("LGTM_INDEX_NEED_GOPATH") != ""
|
||||
|
||||
@@ -6,9 +6,11 @@ import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -350,3 +352,60 @@ func GetParentDirs(paths []string) []string {
|
||||
}
|
||||
return dirs
|
||||
}
|
||||
|
||||
// Returns the import path of the package being built, or "" if it cannot be determined.
|
||||
func GetImportPath() (importpath string) {
|
||||
importpath = os.Getenv("LGTM_INDEX_IMPORT_PATH")
|
||||
if importpath == "" {
|
||||
repourl := os.Getenv("SEMMLE_REPO_URL")
|
||||
if repourl == "" {
|
||||
githubrepo := os.Getenv("GITHUB_REPOSITORY")
|
||||
if githubrepo == "" {
|
||||
log.Printf("Unable to determine import path, as neither LGTM_INDEX_IMPORT_PATH nor GITHUB_REPOSITORY is set\n")
|
||||
return ""
|
||||
} else {
|
||||
importpath = "github.com/" + githubrepo
|
||||
}
|
||||
} else {
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
// Returns the import path of the package being built from `repourl`, or "" if it cannot be
|
||||
// determined.
|
||||
func getImportPathFromRepoURL(repourl string) string {
|
||||
// check for scp-like URL as in "git@github.com:github/codeql-go.git"
|
||||
shorturl := regexp.MustCompile(`^([^@]+@)?([^:]+):([^/].*?)(\.git)?$`)
|
||||
m := shorturl.FindStringSubmatch(repourl)
|
||||
if m != nil {
|
||||
return m[2] + "/" + m[3]
|
||||
}
|
||||
|
||||
// otherwise parse as proper URL
|
||||
u, err := url.Parse(repourl)
|
||||
if err != nil {
|
||||
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
|
||||
path = regexp.MustCompile(`^/+|\.git$`).ReplaceAllString(path, "")
|
||||
return host + "/" + path
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package util
|
||||
|
||||
import "testing"
|
||||
|
||||
Reference in New Issue
Block a user