support github enterprise by allowing custom api and base urls#43
Conversation
brikis98
left a comment
There was a problem hiding this comment.
Thanks for the PR!
This definitely looks like a useful feature to have. Could you please:
- Run the existing automated tests to make sure they pass.
- Add a new automated test that sets the new base and API URL params. We don't have a GitHub enterprise instance to test with, so I suppose those could just be set to github.com and api.github.com to make sure those params are used correctly.
| Token string // The personal access token to access this repo (if it's a private repo) | ||
| Url string // The URL of the GitHub repo | ||
| BaseUrl string // The Base URL of the GitHub Instance | ||
| ApiUrl string // The API Url of the GitHub Instance |
There was a problem hiding this comment.
Are these 3 ever different? Or, more accurately, is it always the case where:
Url = <domain>/<asset_path>
BaseUrl = <domain>
ApiUrl = <domain>/api/<version>I'm mainly trying to figure out if we could deduce <domain> automatically from Url without a new parameter and have an optional ApiVersion param that defaults to something reasonable (v1)...
There was a problem hiding this comment.
On github.com, the ApiUrl is always api.github.com and the version can be chosen using an HTTP header. For enterprise, it's always baseurl.com/api/v3.
So we probably could use the repo url to do something like:
baseUrl := parseBaseFromRepoUrl(options.RepoUrl)
apiUrl := "api.github.com"
if baseUrl != "github.com" {
apiUrl := baseUrl+"/api/"+options.ApiVersion
}
There was a problem hiding this comment.
That seems reasonable to me and, if ApiVersion is set to a default of v3, it makes the user experience easier, as things will "just work" with no extra parameters. Might want to add a logging statement in that if-statement to make it explicit a non github.com URL was found. Also, does that if-statement also need to check against www.github.com?
There was a problem hiding this comment.
Also, does that if-statement also need to check against www.github.com?
Yes. Good catch. It will either need to be an || or I can use strings.Contains(baseUrl, "github.com")
I'm running the test suite without my changes and getting some failure right away:
% go test
# github.com/jniesen/fetch
./github_test.go:159: Fatalf format %s has arg tc.expected of wrong type main.GitHubReleaseApiResponse
./github_test.go:191: Fatalf format %s has arg tc.assetId of wrong type int
./github_test.go:197: Fatalf format %s has arg tc.assetId of wrong type int
FAIL github.com/jniesen/fetch [build failed]
I'm going to look into them, but I'm curious if there's something else in play that I'm missing.
% go version
go version go1.10.1 linux/amd64
There was a problem hiding this comment.
Could I get temporary read access to: https://github.com/gruntwork-io/fetch-test-private. So that some of the other acceptance tests pass?
There was a problem hiding this comment.
Ah, I always forget about that. We have to have a private repo for testing, but then, of course, contributors can't access it 😄
Just added you.
|
@brikis98 I made the changes you suggested and updated the test suite. This is ready for review. |
brikis98
left a comment
There was a problem hiding this comment.
This looks great, thank you. A few minor issues to fix and this is ready to merge.
| downloading from private GitHub repos. **NOTE:** fetch will also look for this token using the `GITHUB_OAUTH_TOKEN` | ||
| environment variable, which we recommend using instead of the command line option to ensure the token doesn't get | ||
| saved in bash history. | ||
| - `--github-api-version` (**Optional**): The used when fetching an artifact from a GitHub Enterprise instance. |
There was a problem hiding this comment.
Remove "the" at the beginning of the sentence. Also, please add "Defaults to v3.
| testInst := GitHubInstance{ | ||
| BaseUrl: "github.com", | ||
| ApiUrl: "api.github.com", | ||
| } |
| Token string // The personal access token to access this repo (if it's a private repo) | ||
| Url string // The URL of the GitHub repo | ||
| BaseUrl string // The Base URL of the GitHub Instance | ||
| ApiUrl string // The API Url of the GitHub Instance |
There was a problem hiding this comment.
Ah, I always forget about that. We have to have a private repo for testing, but then, of course, contributors can't access it 😄
Just added you.
| func ParseUrlIntoGithubInstance(url string, apiv string) (GitHubInstance, *FetchError) { | ||
| var instance GitHubInstance | ||
|
|
||
| regex, regexErr := regexp.Compile("https?://(?:www\\.)?(.+?\\.com).*") |
There was a problem hiding this comment.
Couldn't a user have GitHub Enterprise on a different TLD that isn't .com? Perhaps you should use url.Parse and extract hostname from that?
| testInst := GitHubInstance{ | ||
| BaseUrl: "github.com", | ||
| ApiUrl: "api.github.com", | ||
| } |
There was a problem hiding this comment.
Indentation? Please check your editor settings.
There was a problem hiding this comment.
Yep. I thought I had vim-go installed, but had vim-polyglot instead which wasn't cutting it on it's own.
| } | ||
| } | ||
|
|
||
| func TestParseUrlIntoGithubInstance(t *testing.T) { |
|
Updated to address the things brought up in the last round of review. Thank you for adding me to the repo. I was able to get the complete test suite passing. |
brikis98
left a comment
There was a problem hiding this comment.
Great, thank you! I'll merge this now and let the tests run. When they pass, I'll issue a new release and paste the link here.
Fetch seems like a much more enjoyable way to fetch release artifacts from GitHub on the command line. More enjoyable then writing a curl script and more reusable.
However, I'm working with repos in both GitHub Enterprise (GHE) and GitHub.com. These changes make it so that I can pass in the URLs (base and api) of the GHE instance that I'm working with and be able to fetch artifacts from there.
I did some basic smoke testing on this. I was able to fetch from GHE. I can add/fix tests if it's required for merge, it just won't be until tomorrow.