From 3cef373ae6c0561ebd7007e9a12de3ed6dd477d7 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Thu, 20 Jun 2019 22:58:22 -0400 Subject: [PATCH 1/3] Removed github requirement --- repo.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/repo.go b/repo.go index eac3633..85a3d43 100644 --- a/repo.go +++ b/repo.go @@ -12,6 +12,7 @@ import ( "golang.org/x/xerrors" "go.coder.com/flog" + "go.coder.com/sail/internal/xexec" ) type repo struct { @@ -110,6 +111,24 @@ func parseRepo(defaultSchema, defaultHost, defaultOrganization, name string) (re return r, nil } +func (r repo) isGithubRemote() (ok bool) { + cmd := xexec.Fmt("git remote -v") + out, err := cmd.Output() + if err != nil { + flog.Error("Unable to check repo remotes") + return false + } + + o := strings.Split(string(out), "\n") + + for _, url := range o { + if strings.HasPrefix(url, "https://github.com") || strings.HasPrefix(url, "git@github.com") { + return true + } + } + return false +} + // language returns the language of a repository using github's detected language. // This is a best effort try and will return the empty string if something fails. func (r repo) language() string { @@ -118,6 +137,10 @@ func (r repo) language() string { return "" } + if ok := r.isGithubRemote(); !ok { + return "" + } + repo, resp, err := github.NewClient(nil).Repositories.Get(context.Background(), orgRepo[0], orgRepo[1]) if err != nil { flog.Error("unable to get repo language: %v", err) From c9f8fdf8a103b3b0e4137e92855ca70d5e3a4638 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Fri, 21 Jun 2019 13:14:10 -0400 Subject: [PATCH 2/3] Changes based on PR --- repo.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/repo.go b/repo.go index 85a3d43..a9dd70e 100644 --- a/repo.go +++ b/repo.go @@ -111,22 +111,25 @@ func parseRepo(defaultSchema, defaultHost, defaultOrganization, name string) (re return r, nil } -func (r repo) isGithubRemote() (ok bool) { - cmd := xexec.Fmt("git remote -v") - out, err := cmd.Output() +func (r repo) isGitHubRemote(orgRepo []string) (bool, error) { + // TODO: How do I get the path from here? + cmd := xexec.Fmt("git -C %s remote get-url origin") + out, err := cmd.CombinedOutput() if err != nil { - flog.Error("Unable to check repo remotes") - return false + flog.Error("unable to check Git remotes: %s", out) + return false, err } o := strings.Split(string(out), "\n") + org := fmt.Sprintf("%s/%s.git", orgRepo[0], orgRepo[1]) + for _, url := range o { - if strings.HasPrefix(url, "https://github.com") || strings.HasPrefix(url, "git@github.com") { - return true + if strings.Contains(url, "https://github.com/"+org) || strings.Contains(url, "ssh://git@github.com:"+org) { + return true, nil } } - return false + return false, nil } // language returns the language of a repository using github's detected language. @@ -137,7 +140,7 @@ func (r repo) language() string { return "" } - if ok := r.isGithubRemote(); !ok { + if ok, err := r.isGitHubRemote(orgRepo); !ok || err != nil { return "" } From 931da14971662bc6b1de86b6e4e0afa1d4609a42 Mon Sep 17 00:00:00 2001 From: Dean Sheather Date: Mon, 1 Jul 2019 13:25:29 +1000 Subject: [PATCH 3/3] use r.Host to determine repo host in r.language() - This means that only repositories on github.com will have their language autodetected through GitHub API --- repo.go | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/repo.go b/repo.go index a9dd70e..22c8264 100644 --- a/repo.go +++ b/repo.go @@ -12,7 +12,6 @@ import ( "golang.org/x/xerrors" "go.coder.com/flog" - "go.coder.com/sail/internal/xexec" ) type repo struct { @@ -111,42 +110,29 @@ func parseRepo(defaultSchema, defaultHost, defaultOrganization, name string) (re return r, nil } -func (r repo) isGitHubRemote(orgRepo []string) (bool, error) { - // TODO: How do I get the path from here? - cmd := xexec.Fmt("git -C %s remote get-url origin") - out, err := cmd.CombinedOutput() - if err != nil { - flog.Error("unable to check Git remotes: %s", out) - return false, err - } - - o := strings.Split(string(out), "\n") - - org := fmt.Sprintf("%s/%s.git", orgRepo[0], orgRepo[1]) - - for _, url := range o { - if strings.Contains(url, "https://github.com/"+org) || strings.Contains(url, "ssh://git@github.com:"+org) { - return true, nil - } +// language tries to determine the language of a repository. If any error is +// encountered, an empty string is returned. +func (r repo) language() string { + switch strings.ToLower(r.Host) { + case "github.com": + return r.getGitHubLanguage() + default: + return "" } - return false, nil } -// language returns the language of a repository using github's detected language. -// This is a best effort try and will return the empty string if something fails. -func (r repo) language() string { +// getGitHubLanguage returns the language of a repository using GitHub's +// detected language. This is a best effort try and will return the empty string +// if something fails. +func (r repo) getGitHubLanguage() string { orgRepo := strings.SplitN(r.trimPath(), "/", 2) if len(orgRepo) != 2 { return "" } - if ok, err := r.isGitHubRemote(orgRepo); !ok || err != nil { - return "" - } - repo, resp, err := github.NewClient(nil).Repositories.Get(context.Background(), orgRepo[0], orgRepo[1]) if err != nil { - flog.Error("unable to get repo language: %v", err) + flog.Error("unable to get repo language from GitHub: %v", err) return "" }