Skip to content

Commit

Permalink
cogito: configure 'source.github_hostname' to override the GitHub API…
Browse files Browse the repository at this point in the history
… in tests
  • Loading branch information
aliculPix4D committed Oct 27, 2023
1 parent b7515a8 commit 9394814
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 16 deletions.
9 changes: 7 additions & 2 deletions cmd/cogito/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func TestRunPutSuccess(t *testing.T) {
var ghReq github.AddRequest
var ghUrl *url.URL
gitHubSpy := testhelp.SpyHttpServer(&ghReq, nil, &ghUrl, http.StatusCreated)
gitHubSpyURL, err := url.Parse(gitHubSpy.URL)
if err != nil {
t.Fatalf("error parsing SpyHttpServer URL: %s", err)
}
var chatMsg googlechat.BasicMessage
chatReply := googlechat.MessageReply{}
var gchatUrl *url.URL
Expand All @@ -72,6 +76,7 @@ func TestRunPutSuccess(t *testing.T) {
Owner: "the-owner",
Repo: "the-repo",
AccessToken: "the-secret",
GhHostname: gitHubSpyURL.Host,
GChatWebHook: googleChatSpy.URL,
LogLevel: "debug",
},
Expand All @@ -80,9 +85,9 @@ func TestRunPutSuccess(t *testing.T) {
var out bytes.Buffer
var logOut bytes.Buffer
inputDir := testhelp.MakeGitRepoFromTestdata(t, "../../cogito/testdata/one-repo/a-repo",
testhelp.HttpsRemote("github.com", "the-owner", "the-repo"), "dummySHA", wantGitRef)
testhelp.HttpsRemote(gitHubSpyURL.Host, "the-owner", "the-repo"), "dummySHA", wantGitRef)

err := mainErr(in, &out, &logOut, []string{"out", inputDir})
err = mainErr(in, &out, &logOut, []string{"out", inputDir})

assert.NilError(t, err, "\nout: %s\nlogOut: %s", out.String(), logOut.String())
//
Expand Down
1 change: 1 addition & 0 deletions cogito/ghcommitsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (sink GitHubCommitStatusSink) Send() error {
context := ghMakeContext(sink.Request)

target := &github.Target{
Server: "http://" + sink.Request.Source.GhHostname,
Retry: retry.Retry{
FirstDelay: retryFirstDelay,
BackoffLimit: retryBackoffLimit,
Expand Down
14 changes: 12 additions & 2 deletions cogito/ghcommitsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ func TestSinkGitHubCommitStatusSendSuccess(t *testing.T) {
var ghReq github.AddRequest
var URL *url.URL
ts := testhelp.SpyHttpServer(&ghReq, nil, &URL, http.StatusCreated)
gitHubSpyURL, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("error parsing SpyHttpServer URL: %s", err)
}
sink := cogito.GitHubCommitStatusSink{
Log: hclog.NewNullLogger(),
GitRef: wantGitRef,
Request: cogito.PutRequest{
Source: cogito.Source{GhHostname: gitHubSpyURL.Host},
Params: cogito.PutParams{State: wantState},
Env: cogito.Environment{BuildJobName: jobName},
},
}

err := sink.Send()
err = sink.Send()

assert.NilError(t, err)
ts.Close() // Avoid races before the following asserts.
Expand All @@ -46,16 +51,21 @@ func TestSinkGitHubCommitStatusSendFailure(t *testing.T) {
http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusTeapot)
}))
gitHubSpyURL, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("error parsing SpyHttpServer URL: %s", err)
}
defer ts.Close()
sink := cogito.GitHubCommitStatusSink{
Log: hclog.NewNullLogger(),
GitRef: "deadbeefdeadbeef",
Request: cogito.PutRequest{
Source: cogito.Source{GhHostname: gitHubSpyURL.Host},
Params: cogito.PutParams{State: cogito.StatePending},
},
}

err := sink.Send()
err = sink.Send()

assert.ErrorContains(t, err,
`failed to add state "pending" for commit deadbee: 418 I'm a teapot`)
Expand Down
2 changes: 2 additions & 0 deletions cogito/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ type Source struct {
//
// Optional
//
GhHostname string `json:"github_hostname"`
GChatWebHook string `json:"gchat_webhook"` // SENSITIVE
LogLevel string `json:"log_level"`
LogUrl string `json:"log_url"` // DEPRECATED
Expand All @@ -179,6 +180,7 @@ func (src Source) String() string {

fmt.Fprintf(&bld, "owner: %s\n", src.Owner)
fmt.Fprintf(&bld, "repo: %s\n", src.Repo)
fmt.Fprintf(&bld, "github_hostname: %s\n", src.GhHostname)
fmt.Fprintf(&bld, "access_token: %s\n", redact(src.AccessToken))
fmt.Fprintf(&bld, "gchat_webhook: %s\n", redact(src.GChatWebHook))
fmt.Fprintf(&bld, "log_level: %s\n", src.LogLevel)
Expand Down
3 changes: 3 additions & 0 deletions cogito/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func TestSourcePrintLogRedaction(t *testing.T) {
source := cogito.Source{
Owner: "the-owner",
Repo: "the-repo",
GhHostname: "github.com",
AccessToken: "sensitive-the-access-token",
GChatWebHook: "sensitive-gchat-webhook",
LogLevel: "debug",
Expand All @@ -186,6 +187,7 @@ func TestSourcePrintLogRedaction(t *testing.T) {
t.Run("fmt.Print redacts fields", func(t *testing.T) {
want := `owner: the-owner
repo: the-repo
github_hostname: github.com
access_token: ***REDACTED***
gchat_webhook: ***REDACTED***
log_level: debug
Expand All @@ -205,6 +207,7 @@ sinks: []`
}
want := `owner: the-owner
repo:
github_hostname:
access_token:
gchat_webhook:
log_level:
Expand Down
4 changes: 2 additions & 2 deletions cogito/put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func TestPutterProcessInputDirSuccess(t *testing.T) {
"https://github.com/dummy-owner/dummy-repo", "dummySHA", "banana")
putter.InputDir = filepath.Join(tmpDir, filepath.Base(tc.inputDir))
putter.Request = cogito.PutRequest{
Source: cogito.Source{Owner: "dummy-owner", Repo: "dummy-repo", Sinks: tc.sink},
Source: cogito.Source{GhHostname: "github.com", Owner: "dummy-owner", Repo: "dummy-repo", Sinks: tc.sink},
Params: tc.params,
}

Expand Down Expand Up @@ -319,7 +319,7 @@ func TestPutterProcessInputDirFailure(t *testing.T) {
"https://github.com/dummy-owner/dummy-repo", "dummySHA", "banana mango")
putter := cogito.NewPutter(hclog.NewNullLogger())
putter.Request = cogito.PutRequest{
Source: cogito.Source{Owner: "dummy-owner", Repo: "dummy-repo"},
Source: cogito.Source{GhHostname: "github.com", Owner: "dummy-owner", Repo: "dummy-repo"},
Params: tc.params,
}
putter.InputDir = filepath.Join(tmpDir, filepath.Base(tc.inputDir))
Expand Down
9 changes: 5 additions & 4 deletions cogito/putter.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,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, source.GhHostname, source.Owner, source.Repo); err != nil {
return err
}
putter.gitRef, err = getGitCommit(repoDir)
Expand Down Expand Up @@ -237,7 +237,7 @@ func collectInputDirs(dir string) ([]string, error) {
// - 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 {
func checkGitRepoDir(dir, hostname, owner, repo string) error {
cfg, err := mini.LoadConfiguration(filepath.Join(dir, ".git/config"))
if err != nil {
return fmt.Errorf("parsing .git/config: %w", err)
Expand All @@ -259,7 +259,7 @@ func checkGitRepoDir(dir, owner, repo string) error {
if err != nil {
return fmt.Errorf(".git/config: remote: %w", err)
}
left := []string{"github.com", owner, repo}
left := []string{hostname, owner, repo}
right := []string{gu.URL.Host, gu.Owner, gu.Repo}
for i, l := range left {
r := right[i]
Expand All @@ -272,10 +272,11 @@ Git repository configuration (received as 'inputs:' in this PUT step):
repo: %s
Cogito SOURCE configuration:
hostname: %s
owner: %s
repo: %s`,
gitUrl, gu.Owner, gu.Repo,
owner, repo)
hostname, owner, repo)
}
}
return nil
Expand Down
17 changes: 11 additions & 6 deletions cogito/putter_private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cogito

import (
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -65,6 +66,7 @@ func TestCheckGitRepoDirSuccess(t *testing.T) {
repoURL string
}

const wantHostname = "github.com"
const wantOwner = "smiling"
const wantRepo = "butterfly"

Expand All @@ -73,7 +75,7 @@ func TestCheckGitRepoDirSuccess(t *testing.T) {
"dummySHA", "dummyHead")

err := checkGitRepoDir(filepath.Join(inputDir, filepath.Base(tc.dir)),
wantOwner, wantRepo)
wantHostname, wantOwner, wantRepo)

assert.NilError(t, err)
}
Expand All @@ -82,22 +84,22 @@ func TestCheckGitRepoDirSuccess(t *testing.T) {
{
name: "repo with good SSH remote",
dir: "testdata/one-repo/a-repo",
repoURL: testhelp.SshRemote("github.com", wantOwner, wantRepo),
repoURL: testhelp.SshRemote(wantHostname, wantOwner, wantRepo),
},
{
name: "repo with good HTTPS remote",
dir: "testdata/one-repo/a-repo",
repoURL: testhelp.HttpsRemote("github.com", wantOwner, wantRepo),
repoURL: testhelp.HttpsRemote(wantHostname, wantOwner, wantRepo),
},
{
name: "repo with good HTTP remote",
dir: "testdata/one-repo/a-repo",
repoURL: testhelp.HttpRemote("github.com", wantOwner, wantRepo),
repoURL: testhelp.HttpRemote(wantHostname, wantOwner, wantRepo),
},
{
name: "PR resource but with basic auth in URL (see PR #46)",
dir: "testdata/one-repo/a-repo",
repoURL: "https://x-oauth-basic:ghp_XXX@github.com/smiling/butterfly.git",
repoURL: fmt.Sprintf("https://x-oauth-basic:ghp_XXX@%s/%s/%s.git", wantHostname, wantOwner, wantRepo),
},
}

Expand All @@ -114,6 +116,7 @@ func TestCheckGitRepoDirFailure(t *testing.T) {
wantErrWild string // wildcard matching
}

const wantHostname = "github.com"
const wantOwner = "smiling"
const wantRepo = "butterfly"

Expand All @@ -122,7 +125,7 @@ func TestCheckGitRepoDirFailure(t *testing.T) {
"dummySHA", "dummyHead")

err := checkGitRepoDir(filepath.Join(inDir, filepath.Base(tc.dir)),
wantOwner, wantRepo)
wantHostname, wantOwner, wantRepo)

assert.ErrorContains(t, err, tc.wantErrWild)
}
Expand Down Expand Up @@ -152,6 +155,7 @@ Git repository configuration (received as 'inputs:' in this PUT step):
repo: repo-a
Cogito SOURCE configuration:
hostname: github.com
owner: smiling
repo: butterfly`,
},
Expand All @@ -167,6 +171,7 @@ Git repository configuration (received as 'inputs:' in this PUT step):
repo: repo-a
Cogito SOURCE configuration:
hostname: github.com
owner: smiling
repo: butterfly`,
},
Expand Down

0 comments on commit 9394814

Please sign in to comment.