Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support github enterprise by allowing custom api and base urls #43

Merged
merged 13 commits into from
Aug 11, 2018

Conversation

jniesen
Copy link

@jniesen jniesen commented Aug 9, 2018

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.

Copy link
Member

@brikis98 brikis98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

This definitely looks like a useful feature to have. Could you please:

  1. Run the existing automated tests to make sure they pass.
  2. 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)...

Copy link
Author

@jniesen jniesen Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Author

@jniesen jniesen Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could I get temporary read access to: https://github.com/gruntwork-io/fetch-test-private. So that some of the other acceptance tests pass?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jniesen
Copy link
Author

jniesen commented Aug 10, 2018

@brikis98 I made the changes you suggested and updated the test suite. This is ready for review.

Copy link
Member

@brikis98 brikis98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you. A few minor issues to fix and this is ready to merge.

README.md Outdated
@@ -75,6 +76,8 @@ The supported options are:
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "the" at the beginning of the sentence. Also, please add "Defaults to v3.

checksum_test.go Outdated
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is wrong?

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

github.go Outdated
func ParseUrlIntoGithubInstance(url string, apiv string) (GitHubInstance, *FetchError) {
var instance GitHubInstance

regex, regexErr := regexp.Compile("https?://(?:www\\.)?(.+?\\.com).*")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

github_test.go Outdated
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation? Please check your editor settings.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I thought I had vim-go installed, but had vim-polyglot instead which wasn't cutting it on it's own.

@@ -47,27 +52,92 @@ func TestGetListOfReleasesFromGitHubRepo(t *testing.T) {
}
}

func TestParseUrlIntoGithubInstance(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests 👍

@jniesen
Copy link
Author

jniesen commented Aug 11, 2018

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.

Copy link
Member

@brikis98 brikis98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@brikis98 brikis98 merged commit ad606a2 into gruntwork-io:master Aug 11, 2018
@brikis98
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants