Skip to content

Commit

Permalink
cogito: add support for Github Enterprise API endpoint
Browse files Browse the repository at this point in the history
Initial implementation by @wanderanimrod and @dev-jin
Co-authored-by: Nimrod Wandera <wanderanimrod@gmail.com>
Co-authored-by: Jin-Chun <Jin.Chun@ibm.com>
  • Loading branch information
aliculPix4D committed Oct 27, 2023
1 parent 362de67 commit fa45e87
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.12.0] - Unreleased

### Added

- Support GitHub Enterprise (see `source.github_hostname` in the README). Initial implementation by @wanderanimrod and @dev-jin.

## [v0.11.0] - 2023-09-22

### Added
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ With reference to the [GitHub Commit status API], the `POST` parameters (`state`
The log level (one of `debug`, `info`, `warn`, `error`, `silent`).\
Default: `info`.

- `github_hostname`:\
Override the default GitHub hostname. This allows you to post commit statuses to repositories hosted by GitHub Enterprise (GHE) instances. e.g, `github.mycompany.org`, without protocol (only HTTPS is supported) and without API endpoint suffix (`/api/v3/`).
Default: `github.com`

- `log_url`. **DEPRECATED, no-op, will be removed**\
A Google Hangout Chat webhook. Useful to obtain logging for the `check` step for Concourse < v7.x

Expand Down
16 changes: 14 additions & 2 deletions cogito/ghcommitsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,21 @@ func ghMakeContext(request PutRequest) string {
}

func apiEndpoint(h string) string {
hostname := strings.ToLower(h)
hostname := normalizeHostname(h) // not really required; already called during protocol init
if hostname == github.GhDefaultHostname {
return github.API
}
if strings.Contains(hostname, "127.0.0.1") {
return fmt.Sprintf("http://%s", hostname)
}
return github.API
return fmt.Sprintf("https://%s/api/v3", hostname)
}

func normalizeHostname(h string) string {
hostname := strings.ToLower(h)
hostname = strings.TrimSuffix(hostname, "/")
hostname = strings.TrimSuffix(hostname, "/api/v3")
hostname = strings.TrimPrefix(hostname, "http://")
hostname = strings.TrimPrefix(hostname, "https://")
return hostname
}
4 changes: 3 additions & 1 deletion cogito/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ func (src *Source) Validate() error {
//
// Validate optional fields.
//
// In this case, nothing to validate.
if src.GhHostname != "" {
src.GhHostname = normalizeHostname(src.GhHostname)
}

//
// Apply defaults.
Expand Down

0 comments on commit fa45e87

Please sign in to comment.