From 6ecf684b8541f0eb7e3bf2860aa029e67a4a28e5 Mon Sep 17 00:00:00 2001 From: aliculPix4D Date: Wed, 18 Oct 2023 15:20:26 +0200 Subject: [PATCH] cogito: don't hardcode Github domain name --- cmd/cogito/main_test.go | 9 +++++++-- cogito/putter.go | 14 ++++++++++---- cogito/putter_private_test.go | 16 +++++++++------- testhelp/testhelper.go | 12 ++++++------ 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/cmd/cogito/main_test.go b/cmd/cogito/main_test.go index 5a259560..15d309e8 100644 --- a/cmd/cogito/main_test.go +++ b/cmd/cogito/main_test.go @@ -3,6 +3,7 @@ package main import ( "bytes" "errors" + "fmt" "io" "net/http" "net/url" @@ -79,8 +80,11 @@ func TestRunPutSuccess(t *testing.T) { })) var out bytes.Buffer var logOut bytes.Buffer + + gitHubSpyDomain, _ := url.Parse(gitHubSpy.URL) + fmt.Println(gitHubSpyDomain) inputDir := testhelp.MakeGitRepoFromTestdata(t, "../../cogito/testdata/one-repo/a-repo", - testhelp.HttpsRemote("the-owner", "the-repo"), "dummySHA", wantGitRef) + testhelp.HttpsRemote(gitHubSpyDomain.Host, "the-owner", "the-repo"), "dummySHA", wantGitRef) t.Setenv("COGITO_GITHUB_API", gitHubSpy.URL) err := mainErr(in, &out, &logOut, []string{"out", inputDir}) @@ -114,8 +118,9 @@ func TestRunPutSuccessIntegration(t *testing.T) { })) var out bytes.Buffer var logOut bytes.Buffer + inputDir := testhelp.MakeGitRepoFromTestdata(t, "../../cogito/testdata/one-repo/a-repo", - testhelp.HttpsRemote(gitHubCfg.Owner, gitHubCfg.Repo), gitHubCfg.SHA, + testhelp.HttpsRemote(github.API, gitHubCfg.Owner, gitHubCfg.Repo), gitHubCfg.SHA, "ref: refs/heads/a-branch-FIXME") t.Setenv("BUILD_JOB_NAME", "TestRunPutSuccessIntegration") t.Setenv("ATC_EXTERNAL_URL", "https://cogito.invalid") diff --git a/cogito/putter.go b/cogito/putter.go index ad9ef048..fbc02eca 100644 --- a/cogito/putter.go +++ b/cogito/putter.go @@ -133,7 +133,7 @@ func (putter *ProdPutter) ProcessInputDir() error { case 1: repoDir := filepath.Join(putter.InputDir, inputDirs.OrderedList()[0]) putter.log.Debug("", "inputDirs", inputDirs, "repoDir", repoDir, "msgDir", msgDir) - if err := checkGitRepoDir(repoDir, source.Owner, source.Repo); err != nil { + if err := checkGitRepoDir(repoDir, putter.ghAPI, source.Owner, source.Repo); err != nil { return err } putter.gitRef, err = getGitCommit(repoDir) @@ -239,8 +239,8 @@ func collectInputDirs(dir string) ([]string, error) { // - DIR is indeed a git repository. // - The repo configuration contains a "remote origin" section. // - The remote origin url can be parsed following the GitHub conventions. -// - The result of the parse matches OWNER and REPO. -func checkGitRepoDir(dir, owner, repo string) error { +// - The result of the parse matches DOMAIN, OWNER and REPO. +func checkGitRepoDir(dir, domain, owner, repo string) error { cfg, err := mini.LoadConfiguration(filepath.Join(dir, ".git/config")) if err != nil { return fmt.Errorf("parsing .git/config: %w", err) @@ -262,10 +262,16 @@ func checkGitRepoDir(dir, owner, repo string) error { if err != nil { return fmt.Errorf(".git/config: remote: %w", err) } - left := []string{"github.com", owner, repo} + parsedUrl, err := safeUrlParse(domain) + if err != nil { + return err + } + fmt.Println(parsedUrl.Host) + left := []string{parsedUrl.Host, owner, repo} right := []string{gu.URL.Host, gu.Owner, gu.Repo} for i, l := range left { r := right[i] + fmt.Println(l, r) if !strings.EqualFold(l, r) { return fmt.Errorf(`the received git repository is incompatible with the Cogito configuration. diff --git a/cogito/putter_private_test.go b/cogito/putter_private_test.go index 36991f6b..4aac0fe2 100644 --- a/cogito/putter_private_test.go +++ b/cogito/putter_private_test.go @@ -10,6 +10,7 @@ import ( "github.com/google/go-cmp/cmp" "gotest.tools/v3/assert" + "github.com/Pix4D/cogito/github" "github.com/Pix4D/cogito/testhelp" ) @@ -65,6 +66,7 @@ func TestCheckGitRepoDirSuccess(t *testing.T) { repoURL string } + const wantDomain = "api.github.com" const wantOwner = "smiling" const wantRepo = "butterfly" @@ -73,7 +75,7 @@ func TestCheckGitRepoDirSuccess(t *testing.T) { "dummySHA", "dummyHead") err := checkGitRepoDir(filepath.Join(inputDir, filepath.Base(tc.dir)), - wantOwner, wantRepo) + github.API, wantOwner, wantRepo) assert.NilError(t, err) } @@ -82,17 +84,17 @@ func TestCheckGitRepoDirSuccess(t *testing.T) { { name: "repo with good SSH remote", dir: "testdata/one-repo/a-repo", - repoURL: testhelp.SshRemote(wantOwner, wantRepo), + repoURL: testhelp.SshRemote(wantDomain, wantOwner, wantRepo), }, { name: "repo with good HTTPS remote", dir: "testdata/one-repo/a-repo", - repoURL: testhelp.HttpsRemote(wantOwner, wantRepo), + repoURL: testhelp.HttpsRemote(wantDomain, wantOwner, wantRepo), }, { name: "repo with good HTTP remote", dir: "testdata/one-repo/a-repo", - repoURL: testhelp.HttpRemote(wantOwner, wantRepo), + repoURL: testhelp.HttpRemote(wantDomain, wantOwner, wantRepo), }, { name: "PR resource but with basic auth in URL (see PR #46)", @@ -122,7 +124,7 @@ func TestCheckGitRepoDirFailure(t *testing.T) { "dummySHA", "dummyHead") err := checkGitRepoDir(filepath.Join(inDir, filepath.Base(tc.dir)), - wantOwner, wantRepo) + github.API, wantOwner, wantRepo) assert.ErrorContains(t, err, tc.wantErrWild) } @@ -143,7 +145,7 @@ func TestCheckGitRepoDirFailure(t *testing.T) { { name: "repo with unrelated HTTPS remote", dir: "testdata/one-repo/a-repo", - repoURL: testhelp.HttpsRemote("owner-a", "repo-a"), + repoURL: testhelp.HttpsRemote("github.com", "owner-a", "repo-a"), wantErrWild: `the received git repository is incompatible with the Cogito configuration. Git repository configuration (received as 'inputs:' in this PUT step): @@ -158,7 +160,7 @@ Cogito SOURCE configuration: { name: "repo with unrelated SSH remote or wrong source config", dir: "testdata/one-repo/a-repo", - repoURL: testhelp.SshRemote("owner-a", "repo-a"), + repoURL: testhelp.SshRemote("github.com", "owner-a", "repo-a"), wantErrWild: `the received git repository is incompatible with the Cogito configuration. Git repository configuration (received as 'inputs:' in this PUT step): diff --git a/testhelp/testhelper.go b/testhelp/testhelper.go index 4849558a..a5cc70f8 100644 --- a/testhelp/testhelper.go +++ b/testhelp/testhelper.go @@ -247,18 +247,18 @@ func MakeGitRepoFromTestdata( } // SshRemote returns a GitHub SSH URL -func SshRemote(owner, repo string) string { - return fmt.Sprintf("git@github.com:%s/%s.git", owner, repo) +func SshRemote(domain, owner, repo string) string { + return fmt.Sprintf("git@%s:%s/%s.git", domain, owner, repo) } // HttpsRemote returns a GitHub HTTPS URL -func HttpsRemote(owner, repo string) string { - return fmt.Sprintf("https://github.com/%s/%s.git", owner, repo) +func HttpsRemote(domain, owner, repo string) string { + return fmt.Sprintf("https://%s/%s/%s.git", domain, owner, repo) } // HttpRemote returns a GitHub HTTP URL -func HttpRemote(owner, repo string) string { - return fmt.Sprintf("http://github.com/%s/%s.git", owner, repo) +func HttpRemote(domain, owner, repo string) string { + return fmt.Sprintf("http://%s/%s/%s.git", domain, owner, repo) } // ToJSON returns the JSON encoding of thing.