diff --git a/cmd/cogito/main_test.go b/cmd/cogito/main_test.go index 5a259560..fdddc8cf 100644 --- a/cmd/cogito/main_test.go +++ b/cmd/cogito/main_test.go @@ -79,8 +79,10 @@ func TestRunPutSuccess(t *testing.T) { })) var out bytes.Buffer var logOut bytes.Buffer + + gitHubSpyDomain, _ := url.Parse(gitHubSpy.URL) 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}) @@ -115,7 +117,7 @@ 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.com", 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/put_test.go b/cogito/put_test.go index 23508b9d..325e7448 100644 --- a/cogito/put_test.go +++ b/cogito/put_test.go @@ -262,7 +262,7 @@ func TestPutterProcessInputDirSuccess(t *testing.T) { } test := func(t *testing.T, tc testCase) { - putter := cogito.NewPutter("dummy-API", hclog.NewNullLogger()) + putter := cogito.NewPutter("https://github.com", hclog.NewNullLogger()) tmpDir := testhelp.MakeGitRepoFromTestdata(t, tc.inputDir, "https://github.com/dummy-owner/dummy-repo", "dummySHA", "banana") putter.InputDir = filepath.Join(tmpDir, filepath.Base(tc.inputDir)) @@ -317,7 +317,7 @@ func TestPutterProcessInputDirFailure(t *testing.T) { test := func(t *testing.T, tc testCase) { tmpDir := testhelp.MakeGitRepoFromTestdata(t, tc.inputDir, "https://github.com/dummy-owner/dummy-repo", "dummySHA", "banana mango") - putter := cogito.NewPutter("dummy-api", hclog.NewNullLogger()) + putter := cogito.NewPutter("https://github.com", hclog.NewNullLogger()) putter.Request = cogito.PutRequest{ Source: cogito.Source{Owner: "dummy-owner", Repo: "dummy-repo"}, Params: tc.params, diff --git a/cogito/putter.go b/cogito/putter.go index ad9ef048..bc773106 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,7 +262,12 @@ 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, gu.URL.Host) + left := []string{parsedUrl.Host, owner, repo} right := []string{gu.URL.Host, gu.Owner, gu.Repo} for i, l := range left { r := right[i] diff --git a/cogito/putter_private_test.go b/cogito/putter_private_test.go index 36991f6b..d95ff447 100644 --- a/cogito/putter_private_test.go +++ b/cogito/putter_private_test.go @@ -65,6 +65,7 @@ func TestCheckGitRepoDirSuccess(t *testing.T) { repoURL string } + const wantDomain = "github.com" const wantOwner = "smiling" const wantRepo = "butterfly" @@ -73,7 +74,7 @@ func TestCheckGitRepoDirSuccess(t *testing.T) { "dummySHA", "dummyHead") err := checkGitRepoDir(filepath.Join(inputDir, filepath.Base(tc.dir)), - wantOwner, wantRepo) + "https://github.com", wantOwner, wantRepo) assert.NilError(t, err) } @@ -82,17 +83,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 +123,7 @@ func TestCheckGitRepoDirFailure(t *testing.T) { "dummySHA", "dummyHead") err := checkGitRepoDir(filepath.Join(inDir, filepath.Base(tc.dir)), - wantOwner, wantRepo) + "https://github.com", wantOwner, wantRepo) assert.ErrorContains(t, err, tc.wantErrWild) } @@ -143,7 +144,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 +159,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.