Skip to content

Commit

Permalink
cogito: add support for configuring Github API endpoint
Browse files Browse the repository at this point in the history
- adds support for Github Enterprise API endpoints
  • Loading branch information
aliculPix4D committed Oct 18, 2023
1 parent 11c31e7 commit 80bebc4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
11 changes: 6 additions & 5 deletions cmd/cogito/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ func TestRunPutSuccess(t *testing.T) {
googleChatSpy := testhelp.SpyHttpServer(&chatMsg, chatReply, &gchatUrl, http.StatusOK)
in := bytes.NewReader(testhelp.ToJSON(t, cogito.PutRequest{
Source: cogito.Source{
Owner: "the-owner",
Repo: "the-repo",
AccessToken: "the-secret",
GChatWebHook: googleChatSpy.URL,
LogLevel: "debug",
Owner: "the-owner",
Repo: "the-repo",
AccessToken: "the-secret",
GithubApiEndpoint: gitHubSpy.URL,
GChatWebHook: googleChatSpy.URL,
LogLevel: "debug",
},
Params: cogito.PutParams{State: wantState},
}))
Expand Down
3 changes: 1 addition & 2 deletions cogito/ghcommitsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const (
// GitHubCommitStatusSink is an implementation of [Sinker] for the Cogito resource.
type GitHubCommitStatusSink struct {
Log hclog.Logger
GhAPI string
GitRef string
Request PutRequest
}
Expand All @@ -42,7 +41,7 @@ func (sink GitHubCommitStatusSink) Send() error {
context := ghMakeContext(sink.Request)

target := &github.Target{
Server: sink.GhAPI,
Server: sink.Request.Source.GithubApiEndpoint,
Retry: retry.Retry{
FirstDelay: retryFirstDelay,
BackoffLimit: retryBackoffLimit,
Expand Down
4 changes: 2 additions & 2 deletions cogito/ghcommitsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func TestSinkGitHubCommitStatusSendSuccess(t *testing.T) {
ts := testhelp.SpyHttpServer(&ghReq, nil, &URL, http.StatusCreated)
sink := cogito.GitHubCommitStatusSink{
Log: hclog.NewNullLogger(),
GhAPI: ts.URL,
GitRef: wantGitRef,
Request: cogito.PutRequest{
Source: cogito.Source{GithubApiEndpoint: ts.URL},
Params: cogito.PutParams{State: wantState},
Env: cogito.Environment{BuildJobName: jobName},
},
Expand All @@ -50,9 +50,9 @@ func TestSinkGitHubCommitStatusSendFailure(t *testing.T) {
defer ts.Close()
sink := cogito.GitHubCommitStatusSink{
Log: hclog.NewNullLogger(),
GhAPI: ts.URL,
GitRef: "deadbeefdeadbeef",
Request: cogito.PutRequest{
Source: cogito.Source{GithubApiEndpoint: ts.URL},
Params: cogito.PutParams{State: cogito.StatePending},
},
}
Expand Down
14 changes: 13 additions & 1 deletion cogito/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"os"
"strings"

"github.com/Pix4D/cogito/github"
"github.com/Pix4D/cogito/sets"
)

Expand Down Expand Up @@ -164,6 +166,7 @@ type Source struct {
//
// Optional
//
GithubApiEndpoint string `json:"github_api_endpoint"`
GChatWebHook string `json:"gchat_webhook"` // SENSITIVE
LogLevel string `json:"log_level"`
LogUrl string `json:"log_url"` // DEPRECATED
Expand All @@ -179,6 +182,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_api_endpoint: %s\n", src.GithubApiEndpoint)
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 Expand Up @@ -257,7 +261,12 @@ func (src *Source) Validate() error {
//
// Validate optional fields.
//
// In this case, nothing to validate.
if src.GithubApiEndpoint != "" {
u, err := url.ParseRequestURI(src.GithubApiEndpoint)
if err != nil || u.Host == "" {
return fmt.Errorf("source: github_api_endpoint '%s' is an invalid api endpoint", src.GithubApiEndpoint)
}
}

//
// Apply defaults.
Expand All @@ -268,6 +277,9 @@ func (src *Source) Validate() error {
if len(src.ChatNotifyOnStates) == 0 {
src.ChatNotifyOnStates = defaultNotifyStates
}
if len(src.GithubApiEndpoint) == 0 {
src.GithubApiEndpoint = github.API
}

return nil
}
Expand Down
41 changes: 41 additions & 0 deletions cogito/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func TestSourceValidationSuccess(t *testing.T) {
return source
},
},
{
name: "optional git source github_api_endpoint",
mkSource: func() cogito.Source {
source := baseGithubSource
source.GithubApiEndpoint = "https://github.coffee.com/api/v3"
return source
},
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -102,6 +110,36 @@ func TestSourceValidationFailure(t *testing.T) {
},
wantErr: "source: invalid sink(s): [closed coffee shop]",
},
{
name: "no protocol prefix in git source github_api_endpoint",
source: cogito.Source{
Owner: "the-owner",
Repo: "the-repo",
AccessToken: "the-token",
GithubApiEndpoint: "github.coffee.com/api/v3",
},
wantErr: "source: github_api_endpoint 'github.coffee.com/api/v3' is an invalid api endpoint",
},
{
name: "invalid http protocol prefix in git source github_api_endpoint",
source: cogito.Source{
Owner: "the-owner",
Repo: "the-repo",
AccessToken: "the-token",
GithubApiEndpoint: "https:github.coffee.com/api/v3",
},
wantErr: "source: github_api_endpoint 'https:github.coffee.com/api/v3' is an invalid api endpoint",
},
{
name: "invalid http protocol prefix in git source github_api_endpoint",
source: cogito.Source{
Owner: "the-owner",
Repo: "the-repo",
AccessToken: "the-token",
GithubApiEndpoint: "john.smith.cim",
},
wantErr: "source: github_api_endpoint 'john.smith.cim' is an invalid api endpoint",
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -175,6 +213,7 @@ func TestSourcePrintLogRedaction(t *testing.T) {
source := cogito.Source{
Owner: "the-owner",
Repo: "the-repo",
GithubApiEndpoint: "dummy-api",
AccessToken: "sensitive-the-access-token",
GChatWebHook: "sensitive-gchat-webhook",
LogLevel: "debug",
Expand All @@ -186,6 +225,7 @@ func TestSourcePrintLogRedaction(t *testing.T) {
t.Run("fmt.Print redacts fields", func(t *testing.T) {
want := `owner: the-owner
repo: the-repo
github_api_endpoint: dummy-api
access_token: ***REDACTED***
gchat_webhook: ***REDACTED***
log_level: debug
Expand All @@ -205,6 +245,7 @@ sinks: []`
}
want := `owner: the-owner
repo:
github_api_endpoint:
access_token:
gchat_webhook:
log_level:
Expand Down

0 comments on commit 80bebc4

Please sign in to comment.